Skip to content

Commit

Permalink
Fixes for problems caused by downgrading MC Version
Browse files Browse the repository at this point in the history
  • Loading branch information
CitrusOSLinux committed Dec 18, 2023
1 parent 99f279f commit 1e62b9e
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {

@Nullable
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return world.isClient ? null : validateTicker(type, ModBlocks.COFFEE_BREWER_BLOCK_ENTITY_TYPE, CoffeeBrewerBlockEntity::tick);
return world.isClient ? null : checkType(type, ModBlocks.COFFEE_BREWER_BLOCK_ENTITY_TYPE, CoffeeBrewerBlockEntity::tick);
}

public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected void init() {

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
this.renderBackground(context, mouseX, mouseY, delta);
this.renderBackground(context);
super.render(context, mouseX, mouseY, delta);
this.drawMouseoverTooltip(context, mouseX, mouseY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeEntry;
import net.minecraft.recipe.SmithingRecipe;
import net.minecraft.screen.ForgingScreenHandler;
import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.screen.slot.ForgingSlotsManager;
import net.minecraft.screen.slot.Slot;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

public class CoffeeWorkstationScreenHandler extends ForgingScreenHandler {

private final World world;
private RecipeEntry<CoffeeWorkstationRecipe> currentRecipe;
private final List<RecipeEntry<CoffeeWorkstationRecipe>> recipes;
private CoffeeWorkstationRecipe currentRecipe;
private final List<CoffeeWorkstationRecipe> recipes;

public CoffeeWorkstationScreenHandler(int syncId, PlayerInventory playerInventory) {
this(syncId, playerInventory, ScreenHandlerContext.EMPTY);
Expand All @@ -36,7 +38,7 @@ public CoffeeWorkstationScreenHandler(int syncId, PlayerInventory playerInventor

@Override
protected boolean canTakeOutput(PlayerEntity player, boolean present) {
return currentRecipe != null && currentRecipe.value().matches(input, world);
return currentRecipe != null && currentRecipe.matches(input, world);
}

@Override
Expand All @@ -45,10 +47,11 @@ protected void onTakeOutput(PlayerEntity player, ItemStack stack) {
this.output.unlockLastRecipe(player, List.of(getSlot(0).getStack(), getSlot(1).getStack()));
decrementStack(0, player);
decrementStack(1, player);
context.run((world, pos) -> world.syncWorldEvent(10000, pos, 0));
context.run((world, pos) -> {
world.syncWorldEvent(10000, pos, 0);
});
}

@SuppressWarnings("DataFlowIssue")
private void decrementStack(int slot, PlayerEntity player) {
ItemStack itemStack = input.getStack(slot);
if (itemStack.getCount() == 1) {
Expand All @@ -73,25 +76,28 @@ protected boolean canUse(BlockState state) {

@Override
public void updateResult() {
List<RecipeEntry<CoffeeWorkstationRecipe>> list = world.getRecipeManager().getAllMatches(ModMisc.COFFEE_WORK_RECIPE_TYPE, input, world);
List<CoffeeWorkstationRecipe> list = world.getRecipeManager().getAllMatches(ModMisc.COFFEE_WORK_RECIPE_TYPE, input, world);
if (list.isEmpty()) {
output.setStack(0, ItemStack.EMPTY);
} else {
currentRecipe = list.get(0);
ItemStack stack = currentRecipe.value().craft(input);
ItemStack stack = currentRecipe.craft(input);
output.setLastRecipe(currentRecipe);
output.setStack(0, stack);
}
}

@Override
protected ForgingSlotsManager getForgingSlotsManager() {
return ForgingSlotsManager.create()
.input(0, 27, 47,
stack -> this.recipes.stream().anyMatch(recipe -> recipe.value().testBase(stack)))
.input(1, 76, 47,
stack -> this.recipes.stream().anyMatch(recipe -> recipe.value().testAddition(stack)))
.output(2, 134, 47).build();
return ForgingSlotsManager.create().input(0, 27, 47, stack -> {
return this.recipes.stream().anyMatch(recipe -> {
return recipe.testBase(stack);
});
}).input(1, 76, 47, stack -> {
return this.recipes.stream().anyMatch(recipe -> {
return recipe.testAddition(stack);
});
}).output(2, 134, 47).build();
}

private static Optional<Integer> getQuickMoveSlot(CoffeeWorkstationRecipe recipe, ItemStack stack) {
Expand All @@ -104,7 +110,9 @@ private static Optional<Integer> getQuickMoveSlot(CoffeeWorkstationRecipe recipe

@Override
protected boolean isValidIngredient(ItemStack stack) {
return this.recipes.stream().map((recipe) -> getQuickMoveSlot(recipe.value(), stack)).anyMatch(Optional::isPresent);
return this.recipes.stream().map((recipe) -> {
return getQuickMoveSlot(recipe, stack);
}).anyMatch(Optional::isPresent);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
@Mixin(InGameHud.class)
public abstract class InGameHudMixin {

@Unique private static final Identifier CAFFEINE_OUTLINE_TEXTURE = new Identifier(PlutosCoffee.MOD_ID, "hud/caffeine_content_outline");
@Unique private static final Identifier CAFFEINE_FILL_TEXTURE = new Identifier(PlutosCoffee.MOD_ID, "hud/caffeine_content_fill");
@Unique private static final Identifier CAFFEINE_OUTLINE_TEXTURE = new Identifier(PlutosCoffee.MOD_ID, "textures/gui/caffeine_content/outline.png");
@Unique private static final Identifier CAFFEINE_FILL_TEXTURE = new Identifier(PlutosCoffee.MOD_ID, "textures/gui/caffeine_content/fill.png");
@Unique private static final Identifier ICONS = new Identifier(PlutosCoffee.MOD_ID, "textures/gui/pc_icons.png");

@Shadow @Final private MinecraftClient client;

Expand All @@ -46,9 +47,8 @@ public void plutoscoffee_renderCaffeineContentDisplay(DrawContext context, Callb

float currentCaffeine = Math.min(3000f, Utils.getPlayerCaffeine(playerEntity));
int scaledCaffeineOutput = Math.round(currentCaffeine * (71f/3000f));

context.drawGuiTexture(CAFFEINE_OUTLINE_TEXTURE, centerX + 10, baseYValue, 80, 8);
context.drawGuiTexture(CAFFEINE_FILL_TEXTURE, 80, 8, 0, 0, centerX + 10, baseYValue, scaledCaffeineOutput, 8);
context.drawTexture(ICONS, centerX + 10, baseYValue, 0, 0, 80, 8, 80, 16);
context.drawTexture(ICONS, centerX + 10, baseYValue, 0, 9, scaledCaffeineOutput, 8, 80, 16);

this.client.getProfiler().pop();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package ml.pluto7073.plutoscoffee.recipes;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.google.gson.JsonObject;
import ml.pluto7073.plutoscoffee.Utils;
import ml.pluto7073.plutoscoffee.registry.ModBlocks;
import ml.pluto7073.plutoscoffee.registry.ModItems;
import ml.pluto7073.plutoscoffee.registry.ModMisc;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.nbt.NbtList;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.*;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.world.World;

import java.util.stream.Stream;
Expand All @@ -25,13 +24,14 @@ public class CoffeeWorkstationRecipe implements Recipe<Inventory> {

final Ingredient base;
final Ingredient addition;
final CoffeeStackBuilder result;
//public final Identifier id;
final ItemStack result;
private final Identifier id;

public CoffeeWorkstationRecipe(Ingredient base, Ingredient addition, CoffeeStackBuilder result) {
public CoffeeWorkstationRecipe(Identifier id, Ingredient base, Ingredient addition, ItemStack result) {
this.base = base;
this.addition = addition;
this.result = result;
this.id = id;
}

@Override
Expand All @@ -45,11 +45,10 @@ public ItemStack craft(Inventory inventory, DynamicRegistryManager registryManag
}

public ItemStack craft(Inventory inventory) {
ItemStack stack = new ItemStack(ModItems.BREWED_COFFEE, 1);
ItemStack stack = result.copy();
NbtList sourceAdds = inventory.getStack(0).getOrCreateSubNbt("Coffee").getList("Additions", NbtElement.STRING_TYPE);
NbtList resAdds = new NbtList();
NbtList resAdds = stack.getOrCreateSubNbt("Coffee").getList("Additions", NbtElement.STRING_TYPE);
resAdds.addAll(sourceAdds);
resAdds.add(Utils.stringAsNbt(result.addition()));
String coffeeType = inventory.getStack(0).getOrCreateSubNbt("Coffee").getString("CoffeeType");
stack.getOrCreateSubNbt("Coffee").put("Additions", resAdds);
stack.getOrCreateSubNbt("Coffee").putString("CoffeeType", coffeeType);
Expand All @@ -63,8 +62,13 @@ public boolean fits(int width, int height) {
}

@Override
public ItemStack getResult(DynamicRegistryManager registryManager) {
return this.result.example();
public ItemStack getOutput(DynamicRegistryManager registryManager) {
return this.result;
}

@Override
public Identifier getId() {
return this.id;
}

public boolean testAddition(ItemStack stack) {
Expand Down Expand Up @@ -95,42 +99,35 @@ public boolean isEmpty() {

public static class Serializer implements RecipeSerializer<CoffeeWorkstationRecipe> {

private static final MapCodec<CoffeeStackBuilder> RESULT_STACK_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(Codec.STRING.fieldOf("result").forGetter(CoffeeStackBuilder::addition)).apply(instance, CoffeeStackBuilder::new));

private static final Codec<CoffeeWorkstationRecipe> CODEC = RecordCodecBuilder.create(instance -> instance.group(Ingredient.ALLOW_EMPTY_CODEC.fieldOf("base").forGetter((recipe) -> recipe.base), Ingredient.ALLOW_EMPTY_CODEC.fieldOf("addition").forGetter((recipe) -> recipe.addition), RESULT_STACK_CODEC.forGetter(recipe -> recipe.result)).apply(instance, CoffeeWorkstationRecipe::new));

public Serializer() {}

@Override
public Codec<CoffeeWorkstationRecipe> codec() {
return CODEC;
public CoffeeWorkstationRecipe read(Identifier id, JsonObject jsonObject) {
Ingredient ingredient = Ingredient.fromJson(JsonHelper.getObject(jsonObject, "base"));
Ingredient ingredient2 = Ingredient.fromJson(JsonHelper.getObject(jsonObject, "addition"));
JsonObject resultObject = JsonHelper.getObject(jsonObject, "result");
Item item = ShapedRecipe.getItem(resultObject);
int count = JsonHelper.getInt(resultObject, "count", 1);
ItemStack itemStack = new ItemStack(item, count);
NbtList list = new NbtList();
list.add(Utils.stringAsNbt(JsonHelper.getString(resultObject, "addition")));
itemStack.getOrCreateSubNbt("Coffee").put("Additions", list);
return new CoffeeWorkstationRecipe(id, ingredient, ingredient2, itemStack);
}

@Override
public CoffeeWorkstationRecipe read(PacketByteBuf buf) {
public CoffeeWorkstationRecipe read(Identifier id, PacketByteBuf buf) {
Ingredient ingredient = Ingredient.fromPacket(buf);
Ingredient ingredient2 = Ingredient.fromPacket(buf);
String result = buf.readString();
return new CoffeeWorkstationRecipe(ingredient, ingredient2, new CoffeeStackBuilder(result));
ItemStack itemStack = buf.readItemStack();
return new CoffeeWorkstationRecipe(id, ingredient, ingredient2, itemStack);
}

@Override
public void write(PacketByteBuf buf, CoffeeWorkstationRecipe recipe) {
recipe.base.write(buf);
recipe.addition.write(buf);
buf.writeString(recipe.result.addition());
}

}

public record CoffeeStackBuilder(String addition) {

ItemStack example() {
ItemStack example = new ItemStack(ModItems.BREWED_COFFEE, 1);
NbtList list = new NbtList();
list.add(Utils.stringAsNbt(addition()));
example.getOrCreateSubNbt("Coffee").put("Additions", list);
return example;
buf.writeItemStack(recipe.result);
}

}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"addition": {
"item": "plutoscoffee:caramel"
},
"result": "plutoscoffee:caramel"
"result": {
"item": "plutoscoffee:brewed_coffee",
"count": 1,
"addition": "plutoscoffee:caramel"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"addition": {
"item": "plutoscoffee:milk_bottle"
},
"result": "plutoscoffee:milk"
"result": {
"item": "plutoscoffee:brewed_coffee",
"count": 1,
"addition": "plutoscoffee:milk"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"addition": {
"item": "plutoscoffee:mocha_syrup"
},
"result": "plutoscoffee:mocha_syrup"
"result": {
"item": "plutoscoffee:brewed_coffee",
"count": 1,
"addition": "plutoscoffee:mocha_syrup"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"addition": {
"item": "minecraft:sugar"
},
"result": "plutoscoffee:sugar"
"result": {
"item": "plutoscoffee:brewed_coffee",
"count": 1,
"addition": "plutoscoffee:sugar"
}
}

0 comments on commit 1e62b9e

Please sign in to comment.