ItemCombinerMenu .java

net.minecraft.world.inventory.ItemCombinerMenu (class, 185 lines): members: INVENTORY_SLOTS_PER_ROW, INPUT_SLOT_START, resultSlotIndex, inputSlots, onTake.

net.minecraft.world.inventoryclass185 linescommonextends AbstractContainerMenu5.6 KB

API Surface (13)

void setChanged()
boolean mayPickup(final Player player, final boolean hasItem)
boolean mayPlace(final ItemStack itemStack)
boolean mayPlace(final ItemStack itemStack)
boolean mayPickup(final Player player)
void onTake(final Player player, final ItemStack carried)
void setChanged()
void slotsChanged(final Container container)
void removed(final Player player)
boolean stillValid(final Player player)
ItemStack quickMoveStack(final Player player, final int slotIndex)
boolean canMoveIntoInputSlots(final ItemStack stack)
int getResultSlot()

Fields & Constants (11)

INVENTORY_SLOTS_PER_ROW
INVENTORY_ROWS
INPUT_SLOT_START
access
player
inputSlots
resultSlots
resultSlotIndex
menuType
containerId
inventory

Full Source

ItemCombinerMenu.java185 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.inventory;
2
3
import net.minecraft.world.Container;
4
import net.minecraft.world.SimpleContainer;
5
import net.minecraft.world.entity.player.Inventory;
6
import net.minecraft.world.entity.player.Player;
7
import net.minecraft.world.item.ItemStack;
8
import net.minecraft.world.level.block.state.BlockState;
9
import org.jspecify.annotations.Nullable;
10
11
public abstract class ItemCombinerMenu extends AbstractContainerMenu {
12
	private static final int INVENTORY_SLOTS_PER_ROW = 9;
13
	private static final int INVENTORY_ROWS = 3;
14
	private static final int INPUT_SLOT_START = 0;
15
	protected final ContainerLevelAccess access;
16
	protected final Player player;
17
	protected final Container inputSlots;
18
	protected final ResultContainer resultSlots = new ResultContainer() {
19
		@Override
20
		public void setChanged() {
21
			ItemCombinerMenu.this.slotsChanged(this);
22
		}
23
	};
24
	private final int resultSlotIndex;
25
26
	protected boolean mayPickup(final Player player, final boolean hasItem) {
27
		return true;
28
	}
29
30
	protected abstract void onTake(Player player, ItemStack carried);
31
32
	protected abstract boolean isValidBlock(BlockState state);
33
34
	public ItemCombinerMenu(
35
		final @Nullable MenuType<?> menuType,
36
		final int containerId,
37
		final Inventory inventory,
38
		final ContainerLevelAccess access,
39
		final ItemCombinerMenuSlotDefinition itemInputSlots
40
	) {
41
		super(menuType, containerId);
42
		this.access = access;
43
		this.player = inventory.player;
44
		this.inputSlots = this.createContainer(itemInputSlots.getNumOfInputSlots());
45
		this.resultSlotIndex = itemInputSlots.getResultSlotIndex();
46
		this.createInputSlots(itemInputSlots);
47
		this.createResultSlot(itemInputSlots);
48
		this.addStandardInventorySlots(inventory, 8, 84);
49
	}
50
51
	private void createInputSlots(final ItemCombinerMenuSlotDefinition itemInputSlots) {
52
		for (final ItemCombinerMenuSlotDefinition.SlotDefinition slot : itemInputSlots.getSlots()) {
53
			this.addSlot(new Slot(this.inputSlots, slot.slotIndex(), slot.x(), slot.y()) {
54
				@Override
55
				public boolean mayPlace(final ItemStack itemStack) {
56
					return slot.mayPlace().test(itemStack);
57
				}
58
			});
59
		}
60
	}
61
62
	private void createResultSlot(final ItemCombinerMenuSlotDefinition itemInputSlots) {
63
		this.addSlot(new Slot(this.resultSlots, itemInputSlots.getResultSlot().slotIndex(), itemInputSlots.getResultSlot().x(), itemInputSlots.getResultSlot().y()) {
64
			@Override
65
			public boolean mayPlace(final ItemStack itemStack) {
66
				return false;
67
			}
68
69
			@Override
70
			public boolean mayPickup(final Player player) {
71
				return ItemCombinerMenu.this.mayPickup(player, this.hasItem());
72
			}
73
74
			@Override
75
			public void onTake(final Player player, final ItemStack carried) {
76
				ItemCombinerMenu.this.onTake(player, carried);
77
			}
78
		});
79
	}
80
81
	public abstract void createResult();
82
83
	private SimpleContainer createContainer(final int size) {
84
		return new SimpleContainer(size) {
85
			@Override
86
			public void setChanged() {
87
				super.setChanged();
88
				ItemCombinerMenu.this.slotsChanged(this);
89
			}
90
		};
91
	}
92
93
	@Override
94
	public void slotsChanged(final Container container) {
95
		super.slotsChanged(container);
96
		if (container == this.inputSlots) {
97
			this.createResult();
98
		}
99
	}
100
101
	@Override
102
	public void removed(final Player player) {
103
		super.removed(player);
104
		this.access.execute((level, pos) -> this.clearContainer(player, this.inputSlots));
105
	}
106
107
	@Override
108
	public boolean stillValid(final Player player) {
109
		return this.access.evaluate((level, pos) -> !this.isValidBlock(level.getBlockState(pos)) ? false : player.isWithinBlockInteractionRange(pos, 4.0), true);
110
	}
111
112
	@Override
113
	public ItemStack quickMoveStack(final Player player, final int slotIndex) {
114
		ItemStack clicked = ItemStack.EMPTY;
115
		Slot slot = this.slots.get(slotIndex);
116
		if (slot != null && slot.hasItem()) {
117
			ItemStack stack = slot.getItem();
118
			clicked = stack.copy();
119
			int inventorySlotStart = this.getInventorySlotStart();
120
			int useRowSlotEnd = this.getUseRowEnd();
121
			if (slotIndex == this.getResultSlot()) {
122
				if (!this.moveItemStackTo(stack, inventorySlotStart, useRowSlotEnd, true)) {
123
					return ItemStack.EMPTY;
124
				}
125
126
				slot.onQuickCraft(stack, clicked);
127
			} else if (slotIndex >= 0 && slotIndex < this.getResultSlot()) {
128
				if (!this.moveItemStackTo(stack, inventorySlotStart, useRowSlotEnd, false)) {
129
					return ItemStack.EMPTY;
130
				}
131
			} else if (this.canMoveIntoInputSlots(stack) && slotIndex >= this.getInventorySlotStart() && slotIndex < this.getUseRowEnd()) {
132
				if (!this.moveItemStackTo(stack, 0, this.getResultSlot(), false)) {
133
					return ItemStack.EMPTY;
134
				}
135
			} else if (slotIndex >= this.getInventorySlotStart() && slotIndex < this.getInventorySlotEnd()) {
136
				if (!this.moveItemStackTo(stack, this.getUseRowStart(), this.getUseRowEnd(), false)) {
137
					return ItemStack.EMPTY;
138
				}
139
			} else if (slotIndex >= this.getUseRowStart()
140
				&& slotIndex < this.getUseRowEnd()
141
				&& !this.moveItemStackTo(stack, this.getInventorySlotStart(), this.getInventorySlotEnd(), false)) {
142
				return ItemStack.EMPTY;
143
			}
144
145
			if (stack.isEmpty()) {
146
				slot.setByPlayer(ItemStack.EMPTY);
147
			} else {
148
				slot.setChanged();
149
			}
150
151
			if (stack.getCount() == clicked.getCount()) {
152
				return ItemStack.EMPTY;
153
			}
154
155
			slot.onTake(player, stack);
156
		}
157
158
		return clicked;
159
	}
160
161
	protected boolean canMoveIntoInputSlots(final ItemStack stack) {
162
		return true;
163
	}
164
165
	public int getResultSlot() {
166
		return this.resultSlotIndex;
167
	}
168
169
	private int getInventorySlotStart() {
170
		return this.getResultSlot() + 1;
171
	}
172
173
	private int getInventorySlotEnd() {
174
		return this.getInventorySlotStart() + 27;
175
	}
176
177
	private int getUseRowStart() {
178
		return this.getInventorySlotEnd();
179
	}
180
181
	private int getUseRowEnd() {
182
		return this.getUseRowStart() + 9;
183
	}
184
}
185

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