CrafterMenu .java

net.minecraft.world.inventory.CrafterMenu (class, 128 lines): members: isSlotDisabled, containerData, setSlotState, resultContainer, dataChanged, player.

net.minecraft.world.inventoryclass128 linescommonextends AbstractContainerMenu4.0 KB

Implements

API Surface (8)

void setSlotState(final int slotId, final boolean isEnabled)
boolean isSlotDisabled(final int slotId)
boolean isPowered()
ItemStack quickMoveStack(final Player player, final int slotIndex)
boolean stillValid(final Player player)
Container getContainer()
void slotChanged(final AbstractContainerMenu container, final int slotIndex, final ItemStack itemStack)
void dataChanged(final AbstractContainerMenu container, final int id, final int value)

Fields & Constants (9)

SLOT_COUNT
INV_SLOT_START
INV_SLOT_END
USE_ROW_SLOT_START
USE_ROW_SLOT_END
resultContainer
containerData
player
container

Full Source

CrafterMenu.java128 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.inventory;
2
3
import net.minecraft.server.level.ServerLevel;
4
import net.minecraft.server.level.ServerPlayer;
5
import net.minecraft.world.Container;
6
import net.minecraft.world.entity.player.Inventory;
7
import net.minecraft.world.entity.player.Player;
8
import net.minecraft.world.item.ItemStack;
9
import net.minecraft.world.item.crafting.CraftingInput;
10
import net.minecraft.world.level.block.CrafterBlock;
11
12
public class CrafterMenu extends AbstractContainerMenu implements ContainerListener {
13
	protected static final int SLOT_COUNT = 9;
14
	private static final int INV_SLOT_START = 9;
15
	private static final int INV_SLOT_END = 36;
16
	private static final int USE_ROW_SLOT_START = 36;
17
	private static final int USE_ROW_SLOT_END = 45;
18
	private final ResultContainer resultContainer = new ResultContainer();
19
	private final ContainerData containerData;
20
	private final Player player;
21
	private final CraftingContainer container;
22
23
	public CrafterMenu(final int containerId, final Inventory inventory) {
24
		super(MenuType.CRAFTER_3x3, containerId);
25
		this.player = inventory.player;
26
		this.containerData = new SimpleContainerData(10);
27
		this.container = new TransientCraftingContainer(this, 3, 3);
28
		this.addSlots(inventory);
29
	}
30
31
	public CrafterMenu(final int containerId, final Inventory inventory, final CraftingContainer container, final ContainerData containerData) {
32
		super(MenuType.CRAFTER_3x3, containerId);
33
		this.player = inventory.player;
34
		this.containerData = containerData;
35
		this.container = container;
36
		checkContainerSize(container, 9);
37
		container.startOpen(inventory.player);
38
		this.addSlots(inventory);
39
		this.addSlotListener(this);
40
	}
41
42
	private void addSlots(final Inventory inventory) {
43
		for (int y = 0; y < 3; y++) {
44
			for (int x = 0; x < 3; x++) {
45
				int slot = x + y * 3;
46
				this.addSlot(new CrafterSlot(this.container, slot, 26 + x * 18, 17 + y * 18, this));
47
			}
48
		}
49
50
		this.addStandardInventorySlots(inventory, 8, 84);
51
		this.addSlot(new NonInteractiveResultSlot(this.resultContainer, 0, 134, 35));
52
		this.addDataSlots(this.containerData);
53
		this.refreshRecipeResult();
54
	}
55
56
	public void setSlotState(final int slotId, final boolean isEnabled) {
57
		CrafterSlot slot = (CrafterSlot)this.getSlot(slotId);
58
		this.containerData.set(slot.index, isEnabled ? 0 : 1);
59
		this.broadcastChanges();
60
	}
61
62
	public boolean isSlotDisabled(final int slotId) {
63
		return slotId > -1 && slotId < 9 ? this.containerData.get(slotId) == 1 : false;
64
	}
65
66
	public boolean isPowered() {
67
		return this.containerData.get(9) == 1;
68
	}
69
70
	@Override
71
	public ItemStack quickMoveStack(final Player player, final int slotIndex) {
72
		ItemStack clicked = ItemStack.EMPTY;
73
		Slot slot = this.slots.get(slotIndex);
74
		if (slot != null && slot.hasItem()) {
75
			ItemStack stack = slot.getItem();
76
			clicked = stack.copy();
77
			if (slotIndex < 9) {
78
				if (!this.moveItemStackTo(stack, 9, 45, true)) {
79
					return ItemStack.EMPTY;
80
				}
81
			} else if (!this.moveItemStackTo(stack, 0, 9, false)) {
82
				return ItemStack.EMPTY;
83
			}
84
85
			if (stack.isEmpty()) {
86
				slot.set(ItemStack.EMPTY);
87
			} else {
88
				slot.setChanged();
89
			}
90
91
			if (stack.getCount() == clicked.getCount()) {
92
				return ItemStack.EMPTY;
93
			}
94
95
			slot.onTake(player, stack);
96
		}
97
98
		return clicked;
99
	}
100
101
	@Override
102
	public boolean stillValid(final Player player) {
103
		return this.container.stillValid(player);
104
	}
105
106
	private void refreshRecipeResult() {
107
		if (this.player instanceof ServerPlayer serverPlayer) {
108
			ServerLevel level = serverPlayer.level();
109
			CraftingInput craftInput = this.container.asCraftInput();
110
			ItemStack result = CrafterBlock.getPotentialResults(level, craftInput).map(recipe -> recipe.value().assemble(craftInput)).orElse(ItemStack.EMPTY);
111
			this.resultContainer.setItem(0, result);
112
		}
113
	}
114
115
	public Container getContainer() {
116
		return this.container;
117
	}
118
119
	@Override
120
	public void slotChanged(final AbstractContainerMenu container, final int slotIndex, final ItemStack itemStack) {
121
		this.refreshRecipeResult();
122
	}
123
124
	@Override
125
	public void dataChanged(final AbstractContainerMenu container, final int id, final int value) {
126
	}
127
}
128

Decompiled Minecraft 26.3 source of CrafterMenu (net/minecraft/world/inventory/CrafterMenu.java) with Mojang official mappings, stored verbatim on the site.