From 3cc35181a776454e0dd6fee161bdb18b72e6272c Mon Sep 17 00:00:00 2001 From: Elenterius Date: Thu, 14 Sep 2023 20:11:46 +0200 Subject: [PATCH] fix: fix machine menus displaying wrong crafting cost --- .../block/biolab/BioLabStateData.java | 36 ++-------------- .../block/decomposer/DecomposerStateData.java | 42 ++----------------- .../block/digester/DigesterStateData.java | 42 ++----------------- .../FuelConsumingRecipeCraftingStateData.java | 40 ++++++++++++++++++ .../block/state/RecipeCraftingStateData.java | 31 +++++++++----- .../biomancy/client/gui/DigesterScreen.java | 2 +- .../biomancy/menu/DigesterMenu.java | 2 +- 7 files changed, 71 insertions(+), 124 deletions(-) create mode 100644 src/main/java/com/github/elenterius/biomancy/block/state/FuelConsumingRecipeCraftingStateData.java diff --git a/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabStateData.java b/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabStateData.java index 77bd80490..42d557606 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabStateData.java +++ b/src/main/java/com/github/elenterius/biomancy/block/biolab/BioLabStateData.java @@ -1,16 +1,13 @@ package com.github.elenterius.biomancy.block.biolab; -import com.github.elenterius.biomancy.block.state.RecipeCraftingStateData; +import com.github.elenterius.biomancy.block.state.FuelConsumingRecipeCraftingStateData; import com.github.elenterius.biomancy.recipe.BioLabRecipe; import com.github.elenterius.biomancy.util.fuel.IFuelHandler; -public class BioLabStateData extends RecipeCraftingStateData { - - public static final int FUEL_INDEX = 2; - public final IFuelHandler fuelHandler; +public class BioLabStateData extends FuelConsumingRecipeCraftingStateData { public BioLabStateData(IFuelHandler fuelHandler) { - this.fuelHandler = fuelHandler; + super(fuelHandler); } @Override @@ -18,31 +15,4 @@ protected Class getRecipeType() { return BioLabRecipe.class; } - @Override - public int getFuelCost() { - return fuelHandler.getFuelCost(timeForCompletion); - } - - @Override - public int get(int index) { - validateIndex(index); - if (index == TIME_INDEX) return timeElapsed; - else if (index == TIME_FOR_COMPLETION_INDEX) return timeForCompletion; - else if (index == FUEL_INDEX) return fuelHandler.getFuelAmount(); - return 0; - } - - @Override - public void set(int index, int value) { - validateIndex(index); - if (index == TIME_INDEX) timeElapsed = value; - else if (index == TIME_FOR_COMPLETION_INDEX) timeForCompletion = value; - else if (index == FUEL_INDEX) fuelHandler.setFuelAmount(value); - } - - @Override - public int getCount() { - return 3; - } - } diff --git a/src/main/java/com/github/elenterius/biomancy/block/decomposer/DecomposerStateData.java b/src/main/java/com/github/elenterius/biomancy/block/decomposer/DecomposerStateData.java index cc77b33b5..dad48c5b2 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/decomposer/DecomposerStateData.java +++ b/src/main/java/com/github/elenterius/biomancy/block/decomposer/DecomposerStateData.java @@ -1,17 +1,13 @@ package com.github.elenterius.biomancy.block.decomposer; -import com.github.elenterius.biomancy.block.state.RecipeCraftingStateData; +import com.github.elenterius.biomancy.block.state.FuelConsumingRecipeCraftingStateData; import com.github.elenterius.biomancy.recipe.DecomposerRecipe; import com.github.elenterius.biomancy.util.fuel.IFuelHandler; -public class DecomposerStateData extends RecipeCraftingStateData { - - public static final int FUEL_INDEX = 2; - - public final IFuelHandler fuelHandler; +public class DecomposerStateData extends FuelConsumingRecipeCraftingStateData { public DecomposerStateData(IFuelHandler fuelHandler) { - this.fuelHandler = fuelHandler; + super(fuelHandler); } @Override @@ -19,36 +15,4 @@ protected Class getRecipeType() { return DecomposerRecipe.class; } - @Override - public int getFuelCost() { - return fuelHandler.getFuelCost(timeForCompletion); - } - - @Override - public int get(int index) { - validateIndex(index); - return switch (index) { - case TIME_INDEX -> timeElapsed; - case TIME_FOR_COMPLETION_INDEX -> timeForCompletion; - case FUEL_INDEX -> fuelHandler.getFuelAmount(); - default -> 0; - }; - } - - @Override - public void set(int index, int value) { - validateIndex(index); - switch (index) { - case TIME_INDEX -> timeElapsed = value; - case TIME_FOR_COMPLETION_INDEX -> timeForCompletion = value; - case FUEL_INDEX -> fuelHandler.setFuelAmount(value); - default -> throw new IllegalStateException("Unexpected value: " + index); - } - } - - @Override - public int getCount() { - return 3; - } - } diff --git a/src/main/java/com/github/elenterius/biomancy/block/digester/DigesterStateData.java b/src/main/java/com/github/elenterius/biomancy/block/digester/DigesterStateData.java index 1d9bbdf8d..6337ea435 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/digester/DigesterStateData.java +++ b/src/main/java/com/github/elenterius/biomancy/block/digester/DigesterStateData.java @@ -1,17 +1,13 @@ package com.github.elenterius.biomancy.block.digester; -import com.github.elenterius.biomancy.block.state.RecipeCraftingStateData; +import com.github.elenterius.biomancy.block.state.FuelConsumingRecipeCraftingStateData; import com.github.elenterius.biomancy.recipe.DigesterRecipe; import com.github.elenterius.biomancy.util.fuel.IFuelHandler; -public class DigesterStateData extends RecipeCraftingStateData { - - public static final int FUEL_AMOUNT_INDEX = 2; - - public final IFuelHandler fuelHandler; +public class DigesterStateData extends FuelConsumingRecipeCraftingStateData { public DigesterStateData(IFuelHandler fuelHandler) { - this.fuelHandler = fuelHandler; + super(fuelHandler); } @Override @@ -19,36 +15,4 @@ protected Class getRecipeType() { return DigesterRecipe.class; } - @Override - public int getFuelCost() { - return fuelHandler.getFuelCost(timeForCompletion); - } - - @Override - public int get(int index) { - validateIndex(index); - return switch (index) { - case TIME_INDEX -> timeElapsed; - case TIME_FOR_COMPLETION_INDEX -> timeForCompletion; - case FUEL_AMOUNT_INDEX -> fuelHandler.getFuelAmount(); - default -> 0; - }; - } - - @Override - public void set(int index, int value) { - validateIndex(index); - switch (index) { - case TIME_INDEX -> timeElapsed = value; - case TIME_FOR_COMPLETION_INDEX -> timeForCompletion = value; - case FUEL_AMOUNT_INDEX -> fuelHandler.setFuelAmount(value); - default -> throw new IllegalStateException("Unexpected value: " + index); - } - } - - @Override - public int getCount() { - return 3; - } - } diff --git a/src/main/java/com/github/elenterius/biomancy/block/state/FuelConsumingRecipeCraftingStateData.java b/src/main/java/com/github/elenterius/biomancy/block/state/FuelConsumingRecipeCraftingStateData.java new file mode 100644 index 000000000..f4d9d787e --- /dev/null +++ b/src/main/java/com/github/elenterius/biomancy/block/state/FuelConsumingRecipeCraftingStateData.java @@ -0,0 +1,40 @@ +package com.github.elenterius.biomancy.block.state; + +import com.github.elenterius.biomancy.recipe.ProcessingRecipe; +import com.github.elenterius.biomancy.util.fuel.IFuelHandler; + +public abstract class FuelConsumingRecipeCraftingStateData extends RecipeCraftingStateData { + + public static final int FUEL_INDEX = 3; + + public final IFuelHandler fuelHandler; + + protected FuelConsumingRecipeCraftingStateData(IFuelHandler fuelHandler) { + this.fuelHandler = fuelHandler; + } + + @Override + public int getFuelCost() { + return fuelHandler.getFuelCost(nutrientsCost); + } + + @Override + public int get(int index) { + if (index == FUEL_INDEX) return fuelHandler.getFuelAmount(); + return super.get(index); + } + + @Override + public void set(int index, int value) { + if (index == FUEL_INDEX) { + fuelHandler.setFuelAmount(value); + return; + } + super.set(index, value); + } + + @Override + public int getCount() { + return 4; + } +} diff --git a/src/main/java/com/github/elenterius/biomancy/block/state/RecipeCraftingStateData.java b/src/main/java/com/github/elenterius/biomancy/block/state/RecipeCraftingStateData.java index 8fa0305bd..ecf8e13bc 100644 --- a/src/main/java/com/github/elenterius/biomancy/block/state/RecipeCraftingStateData.java +++ b/src/main/java/com/github/elenterius/biomancy/block/state/RecipeCraftingStateData.java @@ -12,15 +12,18 @@ public abstract class RecipeCraftingStateData implements ContainerData { - public static final String NBT_KEY_RECIPE_ID = "RecipeId"; - public static final String NBT_KEY_TIME_ELAPSED = "TimeElapsed"; - public static final String NBT_KEY_TIME_FOR_COMPLETION = "TimeForCompletion"; + public static final String RECIPE_ID_KEY = "RecipeId"; + public static final String TIME_ELAPSED_KEY = "TimeElapsed"; + public static final String TIME_FOR_COMPLETION_KEY = "TimeForCompletion"; + public static final String NUTRIENTS_COST_KEY = "NutrientsCost"; public static final int TIME_INDEX = 0; public static final int TIME_FOR_COMPLETION_INDEX = 1; + public static final int NUTRIENTS_COST_INDEX = 2; public int timeElapsed; public int timeForCompletion; + public int nutrientsCost; private CraftingState craftingState = CraftingState.NONE; private ResourceLocation recipeId; @@ -63,6 +66,7 @@ public Optional getCraftingGoalRecipe(Level world) { public void setCraftingGoalRecipe(T recipe) { recipeId = recipe.getId(); timeForCompletion = recipe.getCraftingTimeTicks(); + nutrientsCost = recipe.getCraftingCostNutrients(); } public abstract int getFuelCost(); @@ -71,26 +75,29 @@ public void clear() { recipeId = null; timeElapsed = 0; timeForCompletion = 0; + nutrientsCost = 0; } public void serialize(CompoundTag tag) { CraftingState.toNBT(tag, craftingState); if (recipeId != null) { - tag.putString(NBT_KEY_RECIPE_ID, recipeId.toString()); + tag.putString(RECIPE_ID_KEY, recipeId.toString()); } - tag.putInt(NBT_KEY_TIME_ELAPSED, timeElapsed); - tag.putInt(NBT_KEY_TIME_FOR_COMPLETION, timeForCompletion); + tag.putInt(TIME_ELAPSED_KEY, timeElapsed); + tag.putInt(TIME_FOR_COMPLETION_KEY, timeForCompletion); + tag.putInt(NUTRIENTS_COST_KEY, nutrientsCost); } public void deserialize(CompoundTag tag) { craftingState = CraftingState.fromNBT(tag); - if (tag.contains(NBT_KEY_RECIPE_ID)) { - String id = tag.getString(NBT_KEY_RECIPE_ID); + if (tag.contains(RECIPE_ID_KEY)) { + String id = tag.getString(RECIPE_ID_KEY); recipeId = ResourceLocation.tryParse(id); } else recipeId = null; - timeElapsed = tag.getInt(NBT_KEY_TIME_ELAPSED); - timeForCompletion = tag.getInt(NBT_KEY_TIME_FOR_COMPLETION); + timeElapsed = tag.getInt(TIME_ELAPSED_KEY); + timeForCompletion = tag.getInt(TIME_FOR_COMPLETION_KEY); + nutrientsCost = tag.getInt(NUTRIENTS_COST_KEY); } protected void validateIndex(int index) { @@ -102,6 +109,7 @@ public int get(int index) { validateIndex(index); if (index == TIME_INDEX) return timeElapsed; else if (index == TIME_FOR_COMPLETION_INDEX) return timeForCompletion; + else if (index == NUTRIENTS_COST_INDEX) return nutrientsCost; return 0; } @@ -110,11 +118,12 @@ public void set(int index, int value) { validateIndex(index); if (index == TIME_INDEX) timeElapsed = value; else if (index == TIME_FOR_COMPLETION_INDEX) timeForCompletion = value; + else if (index == NUTRIENTS_COST_INDEX) nutrientsCost = value; } @Override public int getCount() { - return 2; + return 3; } } diff --git a/src/main/java/com/github/elenterius/biomancy/client/gui/DigesterScreen.java b/src/main/java/com/github/elenterius/biomancy/client/gui/DigesterScreen.java index 5f4c19d98..9e81009d6 100644 --- a/src/main/java/com/github/elenterius/biomancy/client/gui/DigesterScreen.java +++ b/src/main/java/com/github/elenterius/biomancy/client/gui/DigesterScreen.java @@ -72,7 +72,7 @@ protected void renderTooltip(PoseStack poseStack, int mouseX, int mouseY) { } private void drawFuelTooltip(PoseStack poseStack, int mouseX, int mouseY) { - int maxFuel = menu.getMAxFuelAmount(); + int maxFuel = menu.getMaxFuelAmount(); int fuelAmount = menu.getFuelAmount(); int totalFuelCost = menu.getFuelCost(); GuiRenderUtil.drawFuelTooltip(this, poseStack, mouseX, mouseY, maxFuel, fuelAmount, totalFuelCost); diff --git a/src/main/java/com/github/elenterius/biomancy/menu/DigesterMenu.java b/src/main/java/com/github/elenterius/biomancy/menu/DigesterMenu.java index 5bad7c3d7..7aa3f07dd 100644 --- a/src/main/java/com/github/elenterius/biomancy/menu/DigesterMenu.java +++ b/src/main/java/com/github/elenterius/biomancy/menu/DigesterMenu.java @@ -74,7 +74,7 @@ public int getFuelAmount() { return stateData.fuelHandler.getFuelAmount(); } - public int getMAxFuelAmount() { + public int getMaxFuelAmount() { return stateData.fuelHandler.getMaxFuelAmount(); }