ServerPlaceRecipe .java

net.minecraft.recipebook.ServerPlaceRecipe (class): members: CraftingMenuAccess, ITEM_NOT_FOUND, inputGridSlots, slotsToClear, gridHeight, gridWidth, menu.

net.minecraft.recipebookclass251 linescommonextends Recipe<7.9 KB

API Surface (1)

<I extends RecipeInput, R extends Recipe<I>> RecipeBookMenu.PostPlaceAction placeRecipe(final ServerPlaceRecipe.CraftingMenuAccess<R> menu, final int gridWidth, final int gridHeight, final List<Slot> inputGridSlots, final List<Slot> slotsToClear, final Inventory inventory, final RecipeHolder<R> recipe, final boolean useMaxItems, final boolean allowDroppingItemsToClear)

Fields & Constants (10)

ITEM_NOT_FOUND
inventory
menu
useMaxItems
gridWidth
gridHeight
inputGridSlots
slotsToClear
RecipeInput
recipe

Full Source

ServerPlaceRecipe.java251 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.recipebook;
2
3
import com.google.common.collect.Lists;
4
import java.util.ArrayList;
5
import java.util.List;
6
import net.minecraft.core.Holder;
7
import net.minecraft.core.component.DataComponents;
8
import net.minecraft.util.Prediction;
9
import net.minecraft.world.entity.player.Inventory;
10
import net.minecraft.world.entity.player.StackedItemContents;
11
import net.minecraft.world.inventory.RecipeBookMenu;
12
import net.minecraft.world.inventory.Slot;
13
import net.minecraft.world.item.Item;
14
import net.minecraft.world.item.ItemStack;
15
import net.minecraft.world.item.crafting.Recipe;
16
import net.minecraft.world.item.crafting.RecipeHolder;
17
import net.minecraft.world.item.crafting.RecipeInput;
18
19
public class ServerPlaceRecipe<R extends Recipe<?>> {
20
	private static final int ITEM_NOT_FOUND = -1;
21
	private final Inventory inventory;
22
	private final ServerPlaceRecipe.CraftingMenuAccess<R> menu;
23
	private final boolean useMaxItems;
24
	private final int gridWidth;
25
	private final int gridHeight;
26
	private final List<Slot> inputGridSlots;
27
	private final List<Slot> slotsToClear;
28
29
	public static <I extends RecipeInput, R extends Recipe<I>> RecipeBookMenu.PostPlaceAction placeRecipe(
30
		final ServerPlaceRecipe.CraftingMenuAccess<R> menu,
31
		final int gridWidth,
32
		final int gridHeight,
33
		final List<Slot> inputGridSlots,
34
		final List<Slot> slotsToClear,
35
		final Inventory inventory,
36
		final RecipeHolder<R> recipe,
37
		final boolean useMaxItems,
38
		final boolean allowDroppingItemsToClear
39
	) {
40
		ServerPlaceRecipe<R> placer = new ServerPlaceRecipe<>(menu, inventory, useMaxItems, gridWidth, gridHeight, inputGridSlots, slotsToClear);
41
		if (!allowDroppingItemsToClear && !placer.testClearGrid()) {
42
			return RecipeBookMenu.PostPlaceAction.NOTHING;
43
		}
44
45
		StackedItemContents availableItems = new StackedItemContents();
46
		inventory.fillStackedContents(availableItems);
47
		menu.fillCraftSlotsStackedContents(availableItems);
48
		return placer.tryPlaceRecipe(recipe, availableItems);
49
	}
50
51
	private ServerPlaceRecipe(
52
		final ServerPlaceRecipe.CraftingMenuAccess<R> menu,
53
		final Inventory inventory,
54
		final boolean useMaxItems,
55
		final int gridWidth,
56
		final int gridHeight,
57
		final List<Slot> inputGridSlots,
58
		final List<Slot> slotsToClear
59
	) {
60
		this.menu = menu;
61
		this.inventory = inventory;
62
		this.useMaxItems = useMaxItems;
63
		this.gridWidth = gridWidth;
64
		this.gridHeight = gridHeight;
65
		this.inputGridSlots = inputGridSlots;
66
		this.slotsToClear = slotsToClear;
67
	}
68
69
	private RecipeBookMenu.PostPlaceAction tryPlaceRecipe(final RecipeHolder<R> recipe, final StackedItemContents availableItems) {
70
		if (availableItems.canCraft(recipe.value(), null)) {
71
			this.placeRecipe(recipe, availableItems);
72
			this.inventory.setChanged();
73
			return RecipeBookMenu.PostPlaceAction.NOTHING;
74
		} else {
75
			this.clearGrid();
76
			this.inventory.setChanged();
77
			return RecipeBookMenu.PostPlaceAction.PLACE_GHOST_RECIPE;
78
		}
79
	}
80
81
	private void clearGrid() {
82
		for (Slot slot : this.slotsToClear) {
83
			ItemStack itemStackCopy = slot.getItem().copy();
84
			this.inventory.placeItemBackInInventory(itemStackCopy, false, Prediction.SERVER_ONLY);
85
			slot.set(itemStackCopy);
86
		}
87
88
		this.menu.clearCraftingContent();
89
	}
90
91
	private void placeRecipe(final RecipeHolder<R> recipe, final StackedItemContents availableItems) {
92
		boolean recipeMatchesPlaced = this.menu.recipeMatches(recipe);
93
		int biggestCraftableStack = availableItems.getBiggestCraftableStack(recipe.value(), null);
94
		if (recipeMatchesPlaced) {
95
			for (Slot inputSlot : this.inputGridSlots) {
96
				ItemStack itemStack = inputSlot.getItem();
97
				if (!itemStack.isEmpty() && Math.min(biggestCraftableStack, itemStack.getMaxStackSize()) < itemStack.getCount() + 1) {
98
					return;
99
				}
100
			}
101
		}
102
103
		int amountToCraft = this.calculateAmountToCraft(biggestCraftableStack, recipeMatchesPlaced);
104
		List<Holder<Item>> itemsUsedPerIngredient = new ArrayList<>();
105
		if (availableItems.canCraft(recipe.value(), amountToCraft, itemsUsedPerIngredient::add)) {
106
			int adjustedAmountToCraft = clampToMaxStackSize(amountToCraft, itemsUsedPerIngredient);
107
			if (adjustedAmountToCraft != amountToCraft) {
108
				itemsUsedPerIngredient.clear();
109
				if (!availableItems.canCraft(recipe.value(), adjustedAmountToCraft, itemsUsedPerIngredient::add)) {
110
					return;
111
				}
112
			}
113
114
			this.clearGrid();
115
			PlaceRecipeHelper.placeRecipe(
116
				this.gridWidth,
117
				this.gridHeight,
118
				recipe.value(),
119
				recipe.value().placementInfo().slotsToIngredientIndex(),
120
				(ingredientIndex, gridIndex, gridXPos, gridYPos) -> {
121
					if (ingredientIndex != -1) {
122
						Slot targetGridSlot = this.inputGridSlots.get(gridIndex);
123
						Holder<Item> itemUsed = itemsUsedPerIngredient.get(ingredientIndex);
124
						int remainingCount = adjustedAmountToCraft;
125
126
						while (remainingCount > 0) {
127
							remainingCount = this.moveItemToGrid(targetGridSlot, itemUsed, remainingCount);
128
							if (remainingCount == -1) {
129
								return;
130
							}
131
						}
132
					}
133
				}
134
			);
135
		}
136
	}
137
138
	private static int clampToMaxStackSize(int value, final List<Holder<Item>> items) {
139
		for (Holder<Item> item : items) {
140
			value = Math.min(value, item.components().getOrDefault(DataComponents.MAX_STACK_SIZE, 1));
141
		}
142
143
		return value;
144
	}
145
146
	private int calculateAmountToCraft(final int biggestCraftableStack, final boolean recipeMatchesPlaced) {
147
		if (this.useMaxItems) {
148
			return biggestCraftableStack;
149
		}
150
151
		if (recipeMatchesPlaced) {
152
			int smallestStackSize = Integer.MAX_VALUE;
153
154
			for (Slot inputSlot : this.inputGridSlots) {
155
				ItemStack itemStack = inputSlot.getItem();
156
				if (!itemStack.isEmpty() && smallestStackSize > itemStack.getCount()) {
157
					smallestStackSize = itemStack.getCount();
158
				}
159
			}
160
161
			if (smallestStackSize != Integer.MAX_VALUE) {
162
				smallestStackSize++;
163
			}
164
165
			return smallestStackSize;
166
		} else {
167
			return 1;
168
		}
169
	}
170
171
	private int moveItemToGrid(final Slot targetSlot, final Holder<Item> itemInInventory, final int count) {
172
		ItemStack itemInTargetSlot = targetSlot.getItem();
173
		int inventorySlotId = this.inventory.findSlotMatchingCraftingIngredient(itemInInventory, itemInTargetSlot);
174
		if (inventorySlotId == -1) {
175
			return -1;
176
		}
177
178
		ItemStack inventoryItem = this.inventory.getItem(inventorySlotId);
179
		ItemStack takenStack;
180
		if (count < inventoryItem.getCount()) {
181
			takenStack = this.inventory.removeItem(inventorySlotId, count);
182
		} else {
183
			takenStack = this.inventory.removeItemNoUpdate(inventorySlotId);
184
		}
185
186
		int takenCount = takenStack.getCount();
187
		if (itemInTargetSlot.isEmpty()) {
188
			targetSlot.set(takenStack);
189
		} else {
190
			itemInTargetSlot.grow(takenCount);
191
		}
192
193
		return count - takenCount;
194
	}
195
196
	private boolean testClearGrid() {
197
		List<ItemStack> freeSlots = Lists.newArrayList();
198
		int freeSlotsInInventory = this.getAmountOfFreeSlotsInInventory();
199
200
		for (Slot inputSlot : this.inputGridSlots) {
201
			ItemStack itemStack = inputSlot.getItem().copy();
202
			if (!itemStack.isEmpty()) {
203
				int slotId = this.inventory.getSlotWithRemainingSpace(itemStack);
204
				if (slotId == -1 && freeSlots.size() <= freeSlotsInInventory) {
205
					for (ItemStack itemStackInList : freeSlots) {
206
						if (ItemStack.isSameItem(itemStackInList, itemStack)
207
							&& itemStackInList.getCount() != itemStackInList.getMaxStackSize()
208
							&& itemStackInList.getCount() + itemStack.getCount() <= itemStackInList.getMaxStackSize()) {
209
							itemStackInList.grow(itemStack.getCount());
210
							itemStack.setCount(0);
211
							break;
212
						}
213
					}
214
215
					if (!itemStack.isEmpty()) {
216
						if (freeSlots.size() >= freeSlotsInInventory) {
217
							return false;
218
						}
219
220
						freeSlots.add(itemStack);
221
					}
222
				} else if (slotId == -1) {
223
					return false;
224
				}
225
			}
226
		}
227
228
		return true;
229
	}
230
231
	private int getAmountOfFreeSlotsInInventory() {
232
		int freeSlots = 0;
233
234
		for (ItemStack item : this.inventory.getNonEquipmentItems()) {
235
			if (item.isEmpty()) {
236
				freeSlots++;
237
			}
238
		}
239
240
		return freeSlots;
241
	}
242
243
	public interface CraftingMenuAccess<T extends Recipe<?>> {
244
		void fillCraftSlotsStackedContents(StackedItemContents stackedContents);
245
246
		void clearCraftingContent();
247
248
		boolean recipeMatches(RecipeHolder<T> recipe);
249
	}
250
}
251

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