package net.minecraft.world.inventory; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.Container; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.CraftingInput; import net.minecraft.world.item.crafting.CraftingRecipe; import net.minecraft.world.level.block.CrafterBlock; public class CrafterMenu extends AbstractContainerMenu implements ContainerListener { protected static final int SLOT_COUNT = 9; private static final int INV_SLOT_START = 9; private static final int INV_SLOT_END = 36; private static final int USE_ROW_SLOT_START = 36; private static final int USE_ROW_SLOT_END = 45; private final ResultContainer resultContainer = new ResultContainer(); private final ContainerData containerData; private final Player player; private final CraftingContainer container; public CrafterMenu(int containerId, Inventory playerInventory) { super(MenuType.CRAFTER_3x3, containerId); this.player = playerInventory.player; this.containerData = new SimpleContainerData(10); this.container = new TransientCraftingContainer(this, 3, 3); this.addSlots(playerInventory); } public CrafterMenu(int containerId, Inventory playerInventory, CraftingContainer container, ContainerData containerData) { super(MenuType.CRAFTER_3x3, containerId); this.player = playerInventory.player; this.containerData = containerData; this.container = container; checkContainerSize(container, 9); container.startOpen(playerInventory.player); this.addSlots(playerInventory); this.addSlotListener(this); } private void addSlots(Inventory playerInventory) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int k = j + i * 3; this.addSlot(new CrafterSlot(this.container, k, 26 + j * 18, 17 + i * 18, this)); } } this.addStandardInventorySlots(playerInventory, 8, 84); this.addSlot(new NonInteractiveResultSlot(this.resultContainer, 0, 134, 35)); this.addDataSlots(this.containerData); this.refreshRecipeResult(); } public void setSlotState(int slot, boolean enabled) { CrafterSlot crafterSlot = (CrafterSlot)this.getSlot(slot); this.containerData.set(crafterSlot.index, enabled ? 0 : 1); this.broadcastChanges(); } public boolean isSlotDisabled(int slot) { return slot > -1 && slot < 9 ? this.containerData.get(slot) == 1 : false; } public boolean isPowered() { return this.containerData.get(9) == 1; } @Override public ItemStack quickMoveStack(Player player, int index) { ItemStack itemStack = ItemStack.EMPTY; Slot slot = this.slots.get(index); if (slot != null && slot.hasItem()) { ItemStack itemStack2 = slot.getItem(); itemStack = itemStack2.copy(); if (index < 9) { if (!this.moveItemStackTo(itemStack2, 9, 45, true)) { return ItemStack.EMPTY; } } else if (!this.moveItemStackTo(itemStack2, 0, 9, false)) { return ItemStack.EMPTY; } if (itemStack2.isEmpty()) { slot.set(ItemStack.EMPTY); } else { slot.setChanged(); } if (itemStack2.getCount() == itemStack.getCount()) { return ItemStack.EMPTY; } slot.onTake(player, itemStack2); } return itemStack; } @Override public boolean stillValid(Player player) { return this.container.stillValid(player); } private void refreshRecipeResult() { if (this.player instanceof ServerPlayer serverPlayer) { ServerLevel serverLevel = serverPlayer.serverLevel(); CraftingInput craftingInput = this.container.asCraftInput(); ItemStack itemStack = (ItemStack)CrafterBlock.getPotentialResults(serverLevel, craftingInput) .map(recipeHolder -> ((CraftingRecipe)recipeHolder.value()).assemble(craftingInput, serverLevel.registryAccess())) .orElse(ItemStack.EMPTY); this.resultContainer.setItem(0, itemStack); } } public Container getContainer() { return this.container; } @Override public void slotChanged(AbstractContainerMenu containerToSend, int dataSlotIndex, ItemStack stack) { this.refreshRecipeResult(); } @Override public void dataChanged(AbstractContainerMenu containerMenu, int dataSlotIndex, int value) { } }