SmithingMenu .java

net.minecraft.world.inventory.SmithingMenu (class): members: TEMPLATE_SLOT_X_PLACEMENT, BASE_SLOT_X_PLACEMENT, additionItemTest, templateItemTest, onTake.

net.minecraft.world.inventoryclass146 linescommonextends ItemCombinerMenu5.6 KB

API Surface (7)

boolean isValidBlock(final BlockState state)
void onTake(final Player player, final ItemStack carried)
void slotsChanged(final Container container)
void createResult()
boolean canTakeItemForPickAll(final ItemStack carried, final Slot target)
boolean canMoveIntoInputSlots(final ItemStack stack)
boolean hasRecipeError()

Fields & Constants (14)

TEMPLATE_SLOT
BASE_SLOT
ADDITIONAL_SLOT
RESULT_SLOT
TEMPLATE_SLOT_X_PLACEMENT
BASE_SLOT_X_PLACEMENT
ADDITIONAL_SLOT_X_PLACEMENT
RESULT_SLOT_X_PLACEMENT
SLOT_Y_PLACEMENT
level
baseItemTest
templateItemTest
additionItemTest
hasRecipeError

Full Source

SmithingMenu.java146 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.server.level.ServerLevel;
6
import net.minecraft.world.Container;
7
import net.minecraft.world.entity.player.Inventory;
8
import net.minecraft.world.entity.player.Player;
9
import net.minecraft.world.item.ItemStack;
10
import net.minecraft.world.item.crafting.RecipeAccess;
11
import net.minecraft.world.item.crafting.RecipeHolder;
12
import net.minecraft.world.item.crafting.RecipePropertySet;
13
import net.minecraft.world.item.crafting.RecipeType;
14
import net.minecraft.world.item.crafting.SmithingRecipe;
15
import net.minecraft.world.item.crafting.SmithingRecipeInput;
16
import net.minecraft.world.level.Level;
17
import net.minecraft.world.level.block.Blocks;
18
import net.minecraft.world.level.block.state.BlockState;
19
20
public class SmithingMenu extends ItemCombinerMenu {
21
	public static final int TEMPLATE_SLOT = 0;
22
	public static final int BASE_SLOT = 1;
23
	public static final int ADDITIONAL_SLOT = 2;
24
	public static final int RESULT_SLOT = 3;
25
	public static final int TEMPLATE_SLOT_X_PLACEMENT = 8;
26
	public static final int BASE_SLOT_X_PLACEMENT = 26;
27
	public static final int ADDITIONAL_SLOT_X_PLACEMENT = 44;
28
	private static final int RESULT_SLOT_X_PLACEMENT = 98;
29
	public static final int SLOT_Y_PLACEMENT = 48;
30
	private final Level level;
31
	private final RecipePropertySet baseItemTest;
32
	private final RecipePropertySet templateItemTest;
33
	private final RecipePropertySet additionItemTest;
34
	private final DataSlot hasRecipeError = DataSlot.standalone();
35
36
	public SmithingMenu(final int containerId, final Inventory inventory) {
37
		this(containerId, inventory, ContainerLevelAccess.NULL);
38
	}
39
40
	public SmithingMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access) {
41
		this(containerId, inventory, access, inventory.player.level());
42
	}
43
44
	private SmithingMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access, final Level level) {
45
		super(MenuType.SMITHING, containerId, inventory, access, createInputSlotDefinitions(level.recipeAccess()));
46
		this.level = level;
47
		this.baseItemTest = level.recipeAccess().propertySet(RecipePropertySet.SMITHING_BASE);
48
		this.templateItemTest = level.recipeAccess().propertySet(RecipePropertySet.SMITHING_TEMPLATE);
49
		this.additionItemTest = level.recipeAccess().propertySet(RecipePropertySet.SMITHING_ADDITION);
50
		this.addDataSlot(this.hasRecipeError).set(0);
51
	}
52
53
	private static ItemCombinerMenuSlotDefinition createInputSlotDefinitions(final RecipeAccess recipes) {
54
		RecipePropertySet baseItemTest = recipes.propertySet(RecipePropertySet.SMITHING_BASE);
55
		RecipePropertySet templateItemTest = recipes.propertySet(RecipePropertySet.SMITHING_TEMPLATE);
56
		RecipePropertySet additionItemTest = recipes.propertySet(RecipePropertySet.SMITHING_ADDITION);
57
		return ItemCombinerMenuSlotDefinition.create()
58
			.withSlot(0, 8, 48, templateItemTest::test)
59
			.withSlot(1, 26, 48, baseItemTest::test)
60
			.withSlot(2, 44, 48, additionItemTest::test)
61
			.withResultSlot(3, 98, 48)
62
			.build();
63
	}
64
65
	@Override
66
	protected boolean isValidBlock(final BlockState state) {
67
		return state.is(Blocks.SMITHING_TABLE);
68
	}
69
70
	@Override
71
	protected void onTake(final Player player, final ItemStack carried) {
72
		carried.onCraftedBy(player, carried.getCount());
73
		this.resultSlots.awardUsedRecipes(player, this.getRelevantItems());
74
		this.shrinkStackInSlot(0);
75
		this.shrinkStackInSlot(1);
76
		this.shrinkStackInSlot(2);
77
		this.access.execute((level, pos) -> level.levelEvent(1044, pos, 0));
78
	}
79
80
	private List<ItemStack> getRelevantItems() {
81
		return List.of(this.inputSlots.getItem(0), this.inputSlots.getItem(1), this.inputSlots.getItem(2));
82
	}
83
84
	private SmithingRecipeInput createRecipeInput() {
85
		return new SmithingRecipeInput(this.inputSlots.getItem(0), this.inputSlots.getItem(1), this.inputSlots.getItem(2));
86
	}
87
88
	private void shrinkStackInSlot(final int slot) {
89
		ItemStack stack = this.inputSlots.getItem(slot);
90
		if (!stack.isEmpty()) {
91
			stack.shrink(1);
92
			this.inputSlots.setItem(slot, stack);
93
		}
94
	}
95
96
	@Override
97
	public void slotsChanged(final Container container) {
98
		super.slotsChanged(container);
99
		if (this.level instanceof ServerLevel) {
100
			boolean hasRecipeError = this.getSlot(0).hasItem()
101
				&& this.getSlot(1).hasItem()
102
				&& this.getSlot(2).hasItem()
103
				&& !this.getSlot(this.getResultSlot()).hasItem();
104
			this.hasRecipeError.set(hasRecipeError ? 1 : 0);
105
		}
106
	}
107
108
	@Override
109
	public void createResult() {
110
		SmithingRecipeInput input = this.createRecipeInput();
111
		Optional<RecipeHolder<SmithingRecipe>> foundRecipe;
112
		if (this.level instanceof ServerLevel serverLevel) {
113
			foundRecipe = serverLevel.recipeAccess().getRecipeFor(RecipeType.SMITHING, input, serverLevel);
114
		} else {
115
			foundRecipe = Optional.empty();
116
		}
117
118
		foundRecipe.ifPresentOrElse(recipe -> {
119
			ItemStack result = recipe.value().assemble(input);
120
			this.resultSlots.setRecipeUsed((RecipeHolder<?>)recipe);
121
			this.resultSlots.setItem(0, result);
122
		}, () -> {
123
			this.resultSlots.setRecipeUsed(null);
124
			this.resultSlots.setItem(0, ItemStack.EMPTY);
125
		});
126
	}
127
128
	@Override
129
	public boolean canTakeItemForPickAll(final ItemStack carried, final Slot target) {
130
		return target.container != this.resultSlots && super.canTakeItemForPickAll(carried, target);
131
	}
132
133
	@Override
134
	public boolean canMoveIntoInputSlots(final ItemStack stack) {
135
		if (this.templateItemTest.test(stack) && !this.getSlot(0).hasItem()) {
136
			return true;
137
		} else {
138
			return this.baseItemTest.test(stack) && !this.getSlot(1).hasItem() ? true : this.additionItemTest.test(stack) && !this.getSlot(2).hasItem();
139
		}
140
	}
141
142
	public boolean hasRecipeError() {
143
		return this.hasRecipeError.get() > 0;
144
	}
145
}
146

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