From 0f67242b2fbd12467d8eb004329e970132470596 Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Fri, 4 Aug 2023 01:16:19 +0200 Subject: [PATCH 1/2] store recipe --- .../common/blockentity/EnchanterBlockEntity.java | 15 +++++++++------ .../machines/common/menu/EnchanterMenu.java | 7 +++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/machines/java/com/enderio/machines/common/blockentity/EnchanterBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/EnchanterBlockEntity.java index c0f42285a3..2816982d22 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/EnchanterBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/EnchanterBlockEntity.java @@ -26,7 +26,7 @@ public class EnchanterBlockEntity extends MachineBlockEntity { private final RecipeWrapper container; - + private Optional currentRecipe; public static final SingleSlotAccess BOOK = new SingleSlotAccess(); public static final SingleSlotAccess CATALYST = new SingleSlotAccess(); public static final SingleSlotAccess LAPIS = new SingleSlotAccess(); @@ -79,15 +79,15 @@ public AbstractContainerMenu createMenu(int pContainerId, Inventory pInventory, protected MachineInventory createMachineInventory(MachineInventoryLayout layout) { // Custom behaviour as this works more like a crafting table than a machine. return new MachineInventory(getIOConfig(), layout) { + protected void onContentsChanged(int slot) { if (level == null) { return; } - + currentRecipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), container, level); if (!OUTPUT.isSlot(slot)) { - Optional recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), container, level); - if (recipe.isPresent()) { - OUTPUT.setStackInSlot(this, recipe.get().assemble(container, level.registryAccess())); + if (currentRecipe.isPresent()) { + OUTPUT.setStackInSlot(this, currentRecipe.get().assemble(container, level.registryAccess())); } else { OUTPUT.setStackInSlot(this, ItemStack.EMPTY); } @@ -109,5 +109,8 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) { } }; } - + + public Optional getCurrentRecipe() { + return currentRecipe; + } } diff --git a/src/machines/java/com/enderio/machines/common/menu/EnchanterMenu.java b/src/machines/java/com/enderio/machines/common/menu/EnchanterMenu.java index f69c1dbee2..f254d54368 100644 --- a/src/machines/java/com/enderio/machines/common/menu/EnchanterMenu.java +++ b/src/machines/java/com/enderio/machines/common/menu/EnchanterMenu.java @@ -2,7 +2,6 @@ import com.enderio.machines.common.blockentity.EnchanterBlockEntity; import com.enderio.machines.common.init.MachineMenus; -import com.enderio.machines.common.init.MachineRecipes; import com.enderio.machines.common.recipe.EnchanterRecipe; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.entity.player.Inventory; @@ -34,7 +33,7 @@ public EnchanterMenu(@Nullable EnchanterBlockEntity blockEntity, Inventory inven addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.OUTPUT, 144, 35) { @Override public void onTake(Player pPlayer, ItemStack pStack) { - Optional recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), blockEntity.getContainer(), level); + Optional recipe = blockEntity.getCurrentRecipe(); if (recipe.isPresent() && (pPlayer.experienceLevel >= recipe.get().getXPCost(blockEntity.getContainer()) || pPlayer.isCreative())) { int amount = recipe.get().getInputAmountConsumed(blockEntity.getContainer()); int lapizForLevel = recipe.get().getLapisForLevel(recipe.get().getEnchantmentLevel(EnchanterBlockEntity.CATALYST.getItemStack(blockEntity).getCount())); @@ -48,7 +47,7 @@ public void onTake(Player pPlayer, ItemStack pStack) { @Override public boolean mayPickup(Player playerIn) { - Optional recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), blockEntity.getContainer(), level); + Optional recipe = blockEntity.getCurrentRecipe(); if (recipe.isPresent() && (playerIn.experienceLevel >= recipe.get().getXPCost(blockEntity.getContainer()) || playerIn.isCreative()) && blockEntity.canAct()) { return super.mayPickup(playerIn); } @@ -69,7 +68,7 @@ public static EnchanterMenu factory(@Nullable MenuType pMenuType, public int getCurrentCost() { if (level != null) { - Optional recipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), getBlockEntity().getContainer(), level); + Optional recipe = this.getBlockEntity().getCurrentRecipe(); if (recipe.isPresent()) { return recipe.get().getXPCost(new RecipeWrapper(this.getBlockEntity().getInventory())); } From 2e5265ae0ff7498187bfdf166496c636c1869a22 Mon Sep 17 00:00:00 2001 From: ferriarnus <61201275+ferriarnus@users.noreply.github.com> Date: Tue, 15 Aug 2023 21:11:08 +0200 Subject: [PATCH 2/2] make field nullable --- .../blockentity/EnchanterBlockEntity.java | 15 +++++----- .../machines/common/menu/EnchanterMenu.java | 28 +++++++------------ 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/machines/java/com/enderio/machines/common/blockentity/EnchanterBlockEntity.java b/src/machines/java/com/enderio/machines/common/blockentity/EnchanterBlockEntity.java index 2816982d22..1836881612 100644 --- a/src/machines/java/com/enderio/machines/common/blockentity/EnchanterBlockEntity.java +++ b/src/machines/java/com/enderio/machines/common/blockentity/EnchanterBlockEntity.java @@ -20,13 +20,13 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.common.Tags; import net.minecraftforge.items.wrapper.RecipeWrapper; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; public class EnchanterBlockEntity extends MachineBlockEntity { private final RecipeWrapper container; - private Optional currentRecipe; + @Nullable + private EnchanterRecipe currentRecipe; public static final SingleSlotAccess BOOK = new SingleSlotAccess(); public static final SingleSlotAccess CATALYST = new SingleSlotAccess(); public static final SingleSlotAccess LAPIS = new SingleSlotAccess(); @@ -84,10 +84,10 @@ protected void onContentsChanged(int slot) { if (level == null) { return; } - currentRecipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), container, level); + currentRecipe = level.getRecipeManager().getRecipeFor(MachineRecipes.ENCHANTING.type().get(), container, level).orElse(null); if (!OUTPUT.isSlot(slot)) { - if (currentRecipe.isPresent()) { - OUTPUT.setStackInSlot(this, currentRecipe.get().assemble(container, level.registryAccess())); + if (currentRecipe != null) { + OUTPUT.setStackInSlot(this, currentRecipe.assemble(container, level.registryAccess())); } else { OUTPUT.setStackInSlot(this, ItemStack.EMPTY); } @@ -110,7 +110,8 @@ public ItemStack extractItem(int slot, int amount, boolean simulate) { }; } - public Optional getCurrentRecipe() { + @Nullable + public EnchanterRecipe getCurrentRecipe() { return currentRecipe; } } diff --git a/src/machines/java/com/enderio/machines/common/menu/EnchanterMenu.java b/src/machines/java/com/enderio/machines/common/menu/EnchanterMenu.java index f254d54368..021dad1a8f 100644 --- a/src/machines/java/com/enderio/machines/common/menu/EnchanterMenu.java +++ b/src/machines/java/com/enderio/machines/common/menu/EnchanterMenu.java @@ -8,36 +8,30 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.MenuType; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.items.wrapper.RecipeWrapper; import org.apache.logging.log4j.LogManager; import org.jetbrains.annotations.Nullable; -import java.util.Optional; - public class EnchanterMenu extends MachineMenu { public static int INPUTS_INDEX = 0; public static int INPUT_COUNT = 3; public static int LAST_INDEX = 3; - private Level level; - public EnchanterMenu(@Nullable EnchanterBlockEntity blockEntity, Inventory inventory, int pContainerId) { super(blockEntity, inventory, MachineMenus.ENCHANTER.get(), pContainerId); if (blockEntity != null) { - this.level = blockEntity.getLevel(); addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.BOOK, 16, 35)); addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.CATALYST, 65, 35)); addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.LAPIS, 85, 35)); addSlot(new MachineSlot(blockEntity.getInventory(), EnchanterBlockEntity.OUTPUT, 144, 35) { @Override public void onTake(Player pPlayer, ItemStack pStack) { - Optional recipe = blockEntity.getCurrentRecipe(); - if (recipe.isPresent() && (pPlayer.experienceLevel >= recipe.get().getXPCost(blockEntity.getContainer()) || pPlayer.isCreative())) { - int amount = recipe.get().getInputAmountConsumed(blockEntity.getContainer()); - int lapizForLevel = recipe.get().getLapisForLevel(recipe.get().getEnchantmentLevel(EnchanterBlockEntity.CATALYST.getItemStack(blockEntity).getCount())); - pPlayer.giveExperienceLevels(-recipe.get().getXPCost(blockEntity.getContainer())); + EnchanterRecipe recipe = blockEntity.getCurrentRecipe(); + if (recipe != null && (pPlayer.experienceLevel >= recipe.getXPCost(blockEntity.getContainer()) || pPlayer.isCreative())) { + int amount = recipe.getInputAmountConsumed(blockEntity.getContainer()); + int lapizForLevel = recipe.getLapisForLevel(recipe.getEnchantmentLevel(EnchanterBlockEntity.CATALYST.getItemStack(blockEntity).getCount())); + pPlayer.giveExperienceLevels(-recipe.getXPCost(blockEntity.getContainer())); EnchanterBlockEntity.BOOK.getItemStack(blockEntity).shrink(1); EnchanterBlockEntity.CATALYST.getItemStack(blockEntity).shrink(amount); EnchanterBlockEntity.LAPIS.getItemStack(blockEntity).shrink(lapizForLevel); @@ -47,8 +41,8 @@ public void onTake(Player pPlayer, ItemStack pStack) { @Override public boolean mayPickup(Player playerIn) { - Optional recipe = blockEntity.getCurrentRecipe(); - if (recipe.isPresent() && (playerIn.experienceLevel >= recipe.get().getXPCost(blockEntity.getContainer()) || playerIn.isCreative()) && blockEntity.canAct()) { + EnchanterRecipe recipe = blockEntity.getCurrentRecipe(); + if (recipe != null && (playerIn.experienceLevel >= recipe.getXPCost(blockEntity.getContainer()) || playerIn.isCreative()) && blockEntity.canAct()) { return super.mayPickup(playerIn); } return false; @@ -67,11 +61,9 @@ public static EnchanterMenu factory(@Nullable MenuType pMenuType, } public int getCurrentCost() { - if (level != null) { - Optional recipe = this.getBlockEntity().getCurrentRecipe(); - if (recipe.isPresent()) { - return recipe.get().getXPCost(new RecipeWrapper(this.getBlockEntity().getInventory())); - } + EnchanterRecipe recipe = this.getBlockEntity().getCurrentRecipe(); + if (recipe != null) { + return recipe.getXPCost(new RecipeWrapper(this.getBlockEntity().getInventory())); } return -1; }