CraftingMenu .java

net.minecraft.world.inventory.CraftingMenu (class): members: slotChangedCraftingGrid, placingRecipe, CRAFTING_GRID_HEIGHT, CRAFTING_GRID_WIDTH, menu.

net.minecraft.world.inventoryclass181 linescommonextends AbstractCraftingMenu5.6 KB

API Surface (12)

void slotChangedCraftingGrid(final AbstractContainerMenu menu, final ServerLevel level, final Player player, final CraftingContainer container, final ResultContainer resultSlots, final @Nullable RecipeHolder<CraftingRecipe> recipeHint)
void slotsChanged(final Container container)
void beginPlacingRecipe()
void finishPlacingRecipe(final ServerLevel level, final RecipeHolder<CraftingRecipe> recipe)
void removed(final Player player)
boolean stillValid(final Player player)
ItemStack quickMoveStack(final Player player, final int slotIndex)
boolean canTakeItemForPickAll(final ItemStack carried, final Slot target)
Slot getResultSlot()
List<Slot> getInputGridSlots()
RecipeBookType getRecipeBookType()
Player owner()

Fields & Constants (17)

CRAFTING_GRID_WIDTH
CRAFTING_GRID_HEIGHT
RESULT_SLOT
CRAFT_SLOT_START
CRAFT_SLOT_COUNT
CRAFT_SLOT_END
INV_SLOT_START
INV_SLOT_END
USE_ROW_SLOT_START
USE_ROW_SLOT_END
access
player
placingRecipe
menu
level
container
resultSlots

Full Source

CraftingMenu.java181 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.inventory;
2
3
import java.util.List;
4
import java.util.Optional;
5
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
6
import net.minecraft.server.level.ServerLevel;
7
import net.minecraft.server.level.ServerPlayer;
8
import net.minecraft.util.Prediction;
9
import net.minecraft.world.Container;
10
import net.minecraft.world.entity.player.Inventory;
11
import net.minecraft.world.entity.player.Player;
12
import net.minecraft.world.item.ItemStack;
13
import net.minecraft.world.item.crafting.CraftingInput;
14
import net.minecraft.world.item.crafting.CraftingRecipe;
15
import net.minecraft.world.item.crafting.RecipeHolder;
16
import net.minecraft.world.item.crafting.RecipeType;
17
import net.minecraft.world.level.block.Blocks;
18
import org.jspecify.annotations.Nullable;
19
20
public class CraftingMenu extends AbstractCraftingMenu {
21
	private static final int CRAFTING_GRID_WIDTH = 3;
22
	private static final int CRAFTING_GRID_HEIGHT = 3;
23
	public static final int RESULT_SLOT = 0;
24
	private static final int CRAFT_SLOT_START = 1;
25
	private static final int CRAFT_SLOT_COUNT = 9;
26
	private static final int CRAFT_SLOT_END = 10;
27
	private static final int INV_SLOT_START = 10;
28
	private static final int INV_SLOT_END = 37;
29
	private static final int USE_ROW_SLOT_START = 37;
30
	private static final int USE_ROW_SLOT_END = 46;
31
	private final ContainerLevelAccess access;
32
	private final Player player;
33
	private boolean placingRecipe;
34
35
	public CraftingMenu(final int containerId, final Inventory inventory) {
36
		this(containerId, inventory, ContainerLevelAccess.NULL);
37
	}
38
39
	public CraftingMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access) {
40
		super(MenuType.CRAFTING, containerId, 3, 3);
41
		this.access = access;
42
		this.player = inventory.player;
43
		this.addResultSlot(this.player, 124, 35);
44
		this.addCraftingGridSlots(30, 17);
45
		this.addStandardInventorySlots(inventory, 8, 84);
46
	}
47
48
	protected static void slotChangedCraftingGrid(
49
		final AbstractContainerMenu menu,
50
		final ServerLevel level,
51
		final Player player,
52
		final CraftingContainer container,
53
		final ResultContainer resultSlots,
54
		final @Nullable RecipeHolder<CraftingRecipe> recipeHint
55
	) {
56
		CraftingInput input = container.asCraftInput();
57
		ServerPlayer serverPlayer = (ServerPlayer)player;
58
		ItemStack result = ItemStack.EMPTY;
59
		Optional<RecipeHolder<CraftingRecipe>> maybeRecipe = level.getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, input, level, recipeHint);
60
		if (maybeRecipe.isPresent()) {
61
			RecipeHolder<CraftingRecipe> recipeHolder = maybeRecipe.get();
62
			CraftingRecipe craftingRecipe = recipeHolder.value();
63
			if (resultSlots.setRecipeUsed(serverPlayer, recipeHolder)) {
64
				ItemStack recipeResult = craftingRecipe.assemble(input);
65
				if (recipeResult.isItemEnabled(level.enabledFeatures())) {
66
					result = recipeResult;
67
				}
68
			}
69
		}
70
71
		resultSlots.setItem(0, result);
72
		menu.setRemoteSlot(0, result);
73
		serverPlayer.connection.send(new ClientboundContainerSetSlotPacket(menu.containerId, menu.incrementStateId(), 0, result));
74
	}
75
76
	@Override
77
	public void slotsChanged(final Container container) {
78
		if (!this.placingRecipe) {
79
			this.access.execute((level, pos) -> {
80
				if (level instanceof ServerLevel serverLevel) {
81
					slotChangedCraftingGrid(this, serverLevel, this.player, this.craftSlots, this.resultSlots, null);
82
				}
83
			});
84
		}
85
	}
86
87
	@Override
88
	public void beginPlacingRecipe() {
89
		this.placingRecipe = true;
90
	}
91
92
	@Override
93
	public void finishPlacingRecipe(final ServerLevel level, final RecipeHolder<CraftingRecipe> recipe) {
94
		this.placingRecipe = false;
95
		slotChangedCraftingGrid(this, level, this.player, this.craftSlots, this.resultSlots, recipe);
96
	}
97
98
	@Override
99
	public void removed(final Player player) {
100
		super.removed(player);
101
		this.access.execute((level, pos) -> this.clearContainer(player, this.craftSlots));
102
	}
103
104
	@Override
105
	public boolean stillValid(final Player player) {
106
		return stillValid(this.access, player, Blocks.CRAFTING_TABLE);
107
	}
108
109
	@Override
110
	public ItemStack quickMoveStack(final Player player, final int slotIndex) {
111
		ItemStack clicked = ItemStack.EMPTY;
112
		Slot slot = this.slots.get(slotIndex);
113
		if (slot != null && slot.hasItem()) {
114
			ItemStack stack = slot.getItem();
115
			clicked = stack.copy();
116
			if (slotIndex == 0) {
117
				stack.getItem().onCraftedBy(stack, player);
118
				if (!this.moveItemStackTo(stack, 10, 46, true)) {
119
					return ItemStack.EMPTY;
120
				}
121
122
				slot.onQuickCraft(stack, clicked);
123
			} else if (slotIndex >= 10 && slotIndex < 46) {
124
				if (!this.moveItemStackTo(stack, 1, 10, false)) {
125
					if (slotIndex < 37) {
126
						if (!this.moveItemStackTo(stack, 37, 46, false)) {
127
							return ItemStack.EMPTY;
128
						}
129
					} else if (!this.moveItemStackTo(stack, 10, 37, false)) {
130
						return ItemStack.EMPTY;
131
					}
132
				}
133
			} else if (!this.moveItemStackTo(stack, 10, 46, false)) {
134
				return ItemStack.EMPTY;
135
			}
136
137
			if (stack.isEmpty()) {
138
				slot.setByPlayer(ItemStack.EMPTY);
139
			} else {
140
				slot.setChanged();
141
			}
142
143
			if (stack.getCount() == clicked.getCount()) {
144
				return ItemStack.EMPTY;
145
			}
146
147
			slot.onTake(player, stack);
148
			if (slotIndex == 0) {
149
				player.drop(stack, false, Prediction.PREDICTED);
150
			}
151
		}
152
153
		return clicked;
154
	}
155
156
	@Override
157
	public boolean canTakeItemForPickAll(final ItemStack carried, final Slot target) {
158
		return target.container != this.resultSlots && super.canTakeItemForPickAll(carried, target);
159
	}
160
161
	@Override
162
	public Slot getResultSlot() {
163
		return this.slots.get(0);
164
	}
165
166
	@Override
167
	public List<Slot> getInputGridSlots() {
168
		return this.slots.subList(1, 10);
169
	}
170
171
	@Override
172
	public RecipeBookType getRecipeBookType() {
173
		return RecipeBookType.CRAFTING;
174
	}
175
176
	@Override
177
	protected Player owner() {
178
		return this.player;
179
	}
180
}
181

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