EnchantmentMenu .java

net.minecraft.world.inventory.EnchantmentMenu (class): members: enchantSlots, getGoldCount, enchantClue, levelClue, costs, EMPTY_SLOT_LAPIS_LAZULI, access.

net.minecraft.world.inventoryclass267 linescommonextends AbstractContainerMenu9.1 KB

API Surface (11)

void setChanged()
int getMaxStackSize()
boolean mayPlace(final ItemStack itemStack)
Identifier getNoItemIcon()
void slotsChanged(final Container container)
boolean clickMenuButton(final Player player, final int buttonId)
int getGoldCount()
int getEnchantmentSeed()
void removed(final Player player)
boolean stillValid(final Player player)
ItemStack quickMoveStack(final Player player, final int slotIndex)

Fields & Constants (8)

EMPTY_SLOT_LAPIS_LAZULI
enchantSlots
access
random
enchantmentSeed
costs
enchantClue
levelClue

String literals (2)

"container/slot/lapis_lazuli"
" pressed invalid button id: "

Full Source

EnchantmentMenu.java267 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.advancements.triggers.CriteriaTriggers;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Holder;
8
import net.minecraft.core.HolderSet;
9
import net.minecraft.core.IdMap;
10
import net.minecraft.core.RegistryAccess;
11
import net.minecraft.core.registries.Registries;
12
import net.minecraft.resources.Identifier;
13
import net.minecraft.server.level.ServerPlayer;
14
import net.minecraft.sounds.SoundEvents;
15
import net.minecraft.sounds.SoundSource;
16
import net.minecraft.stats.Stats;
17
import net.minecraft.tags.EnchantmentTags;
18
import net.minecraft.util.RandomSource;
19
import net.minecraft.util.Util;
20
import net.minecraft.world.Container;
21
import net.minecraft.world.SimpleContainer;
22
import net.minecraft.world.entity.player.Inventory;
23
import net.minecraft.world.entity.player.Player;
24
import net.minecraft.world.item.ItemStack;
25
import net.minecraft.world.item.Items;
26
import net.minecraft.world.item.enchantment.Enchantment;
27
import net.minecraft.world.item.enchantment.EnchantmentHelper;
28
import net.minecraft.world.item.enchantment.EnchantmentInstance;
29
import net.minecraft.world.level.block.Blocks;
30
import net.minecraft.world.level.block.EnchantingTableBlock;
31
32
public class EnchantmentMenu extends AbstractContainerMenu {
33
	private static final Identifier EMPTY_SLOT_LAPIS_LAZULI = Identifier.withDefaultNamespace("container/slot/lapis_lazuli");
34
	private final Container enchantSlots = new SimpleContainer(2) {
35
		@Override
36
		public void setChanged() {
37
			super.setChanged();
38
			EnchantmentMenu.this.slotsChanged(this);
39
		}
40
	};
41
	private final ContainerLevelAccess access;
42
	private final RandomSource random = RandomSource.create();
43
	private final DataSlot enchantmentSeed = DataSlot.standalone();
44
	public final int[] costs = new int[3];
45
	public final int[] enchantClue = new int[]{-1, -1, -1};
46
	public final int[] levelClue = new int[]{-1, -1, -1};
47
48
	public EnchantmentMenu(final int containerId, final Inventory inventory) {
49
		this(containerId, inventory, ContainerLevelAccess.NULL);
50
	}
51
52
	public EnchantmentMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access) {
53
		super(MenuType.ENCHANTMENT, containerId);
54
		this.access = access;
55
		this.addSlot(new Slot(this.enchantSlots, 0, 15, 47) {
56
			@Override
57
			public int getMaxStackSize() {
58
				return 1;
59
			}
60
		});
61
		this.addSlot(new Slot(this.enchantSlots, 1, 35, 47) {
62
			@Override
63
			public boolean mayPlace(final ItemStack itemStack) {
64
				return itemStack.is(Items.LAPIS_LAZULI);
65
			}
66
67
			@Override
68
			public Identifier getNoItemIcon() {
69
				return EnchantmentMenu.EMPTY_SLOT_LAPIS_LAZULI;
70
			}
71
		});
72
		this.addStandardInventorySlots(inventory, 8, 84);
73
		this.addDataSlot(DataSlot.shared(this.costs, 0));
74
		this.addDataSlot(DataSlot.shared(this.costs, 1));
75
		this.addDataSlot(DataSlot.shared(this.costs, 2));
76
		this.addDataSlot(this.enchantmentSeed).set(inventory.player.getEnchantmentSeed());
77
		this.addDataSlot(DataSlot.shared(this.enchantClue, 0));
78
		this.addDataSlot(DataSlot.shared(this.enchantClue, 1));
79
		this.addDataSlot(DataSlot.shared(this.enchantClue, 2));
80
		this.addDataSlot(DataSlot.shared(this.levelClue, 0));
81
		this.addDataSlot(DataSlot.shared(this.levelClue, 1));
82
		this.addDataSlot(DataSlot.shared(this.levelClue, 2));
83
	}
84
85
	@Override
86
	public void slotsChanged(final Container container) {
87
		if (container == this.enchantSlots) {
88
			ItemStack itemStack = container.getItem(0);
89
			if (!itemStack.isEmpty() && itemStack.isEnchantable()) {
90
				this.access.execute((level, pos) -> {
91
					IdMap<Holder<Enchantment>> holders = level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).asHolderIdMap();
92
					int bookcases = 0;
93
94
					for (BlockPos offset : EnchantingTableBlock.BOOKSHELF_OFFSETS) {
95
						if (EnchantingTableBlock.isValidBookShelf(level, pos, offset)) {
96
							bookcases++;
97
						}
98
					}
99
100
					this.random.setSeed(this.enchantmentSeed.get());
101
102
					for (int ixx = 0; ixx < 3; ixx++) {
103
						this.costs[ixx] = EnchantmentHelper.getEnchantmentCost(this.random, ixx, bookcases, itemStack);
104
						this.enchantClue[ixx] = -1;
105
						this.levelClue[ixx] = -1;
106
						if (this.costs[ixx] < ixx + 1) {
107
							this.costs[ixx] = 0;
108
						}
109
					}
110
111
					for (int ix = 0; ix < 3; ix++) {
112
						if (this.costs[ix] > 0) {
113
							List<EnchantmentInstance> list = this.getEnchantmentList(level.registryAccess(), itemStack, ix, this.costs[ix]);
114
							if (!list.isEmpty()) {
115
								EnchantmentInstance ench = list.get(this.random.nextInt(list.size()));
116
								this.enchantClue[ix] = holders.getId(ench.enchantment());
117
								this.levelClue[ix] = ench.level();
118
							}
119
						}
120
					}
121
122
					this.broadcastChanges();
123
				});
124
			} else {
125
				for (int i = 0; i < 3; i++) {
126
					this.costs[i] = 0;
127
					this.enchantClue[i] = -1;
128
					this.levelClue[i] = -1;
129
				}
130
			}
131
		}
132
	}
133
134
	@Override
135
	public boolean clickMenuButton(final Player player, final int buttonId) {
136
		if (buttonId >= 0 && buttonId < this.costs.length) {
137
			ItemStack itemStack = this.enchantSlots.getItem(0);
138
			ItemStack currency = this.enchantSlots.getItem(1);
139
			int enchantmentCost = buttonId + 1;
140
			if ((currency.isEmpty() || currency.getCount() < enchantmentCost) && !player.hasInfiniteMaterials()) {
141
				return false;
142
			}
143
144
			if (this.costs[buttonId] <= 0
145
				|| itemStack.isEmpty()
146
				|| (player.experienceLevel < enchantmentCost || player.experienceLevel < this.costs[buttonId]) && !player.hasInfiniteMaterials()) {
147
				return false;
148
			}
149
150
			this.access.execute((level, pos) -> {
151
				ItemStack enchantmentItem = itemStack;
152
				List<EnchantmentInstance> newEnchantment = this.getEnchantmentList(level.registryAccess(), enchantmentItem, buttonId, this.costs[buttonId]);
153
				if (!newEnchantment.isEmpty()) {
154
					player.onEnchantmentPerformed(enchantmentItem, enchantmentCost);
155
					if (enchantmentItem.is(Items.BOOK)) {
156
						enchantmentItem = itemStack.transmuteCopy(Items.ENCHANTED_BOOK);
157
						this.enchantSlots.setItem(0, enchantmentItem);
158
					}
159
160
					for (EnchantmentInstance enchantment : newEnchantment) {
161
						enchantmentItem.enchant(enchantment.enchantment(), enchantment.level());
162
					}
163
164
					currency.consume(enchantmentCost, player);
165
					if (currency.isEmpty()) {
166
						this.enchantSlots.setItem(1, ItemStack.EMPTY);
167
					}
168
169
					player.awardStat(Stats.ENCHANT_ITEM);
170
					if (player instanceof ServerPlayer serverPlayer) {
171
						CriteriaTriggers.ENCHANTED_ITEM.trigger(serverPlayer, enchantmentItem, enchantmentCost);
172
					}
173
174
					this.enchantSlots.setChanged();
175
					this.enchantmentSeed.set(player.getEnchantmentSeed());
176
					this.slotsChanged(this.enchantSlots);
177
					level.playSound(null, pos, SoundEvents.ENCHANTMENT_TABLE_USE, SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F);
178
				}
179
			});
180
			return true;
181
		} else {
182
			Util.logAndPauseIfInIde(player.getPlainTextName() + " pressed invalid button id: " + buttonId);
183
			return false;
184
		}
185
	}
186
187
	private List<EnchantmentInstance> getEnchantmentList(final RegistryAccess access, final ItemStack itemStack, final int slot, final int enchantmentCost) {
188
		this.random.setSeed(this.enchantmentSeed.get() + slot);
189
		Optional<HolderSet.Named<Enchantment>> tag = access.lookupOrThrow(Registries.ENCHANTMENT).get(EnchantmentTags.IN_ENCHANTING_TABLE);
190
		if (tag.isEmpty()) {
191
			return List.of();
192
		}
193
194
		List<EnchantmentInstance> list = EnchantmentHelper.selectEnchantment(this.random, itemStack, enchantmentCost, tag.get().stream());
195
		if (itemStack.is(Items.BOOK) && list.size() > 1) {
196
			list.remove(this.random.nextInt(list.size()));
197
		}
198
199
		return list;
200
	}
201
202
	public int getGoldCount() {
203
		ItemStack goldStack = this.enchantSlots.getItem(1);
204
		return goldStack.isEmpty() ? 0 : goldStack.getCount();
205
	}
206
207
	public int getEnchantmentSeed() {
208
		return this.enchantmentSeed.get();
209
	}
210
211
	@Override
212
	public void removed(final Player player) {
213
		super.removed(player);
214
		this.access.execute((level, pos) -> this.clearContainer(player, this.enchantSlots));
215
	}
216
217
	@Override
218
	public boolean stillValid(final Player player) {
219
		return stillValid(this.access, player, Blocks.ENCHANTING_TABLE);
220
	}
221
222
	@Override
223
	public ItemStack quickMoveStack(final Player player, final int slotIndex) {
224
		ItemStack clicked = ItemStack.EMPTY;
225
		Slot slot = this.slots.get(slotIndex);
226
		if (slot != null && slot.hasItem()) {
227
			ItemStack stack = slot.getItem();
228
			clicked = stack.copy();
229
			if (slotIndex == 0) {
230
				if (!this.moveItemStackTo(stack, 2, 38, true)) {
231
					return ItemStack.EMPTY;
232
				}
233
			} else if (slotIndex == 1) {
234
				if (!this.moveItemStackTo(stack, 2, 38, true)) {
235
					return ItemStack.EMPTY;
236
				}
237
			} else if (stack.is(Items.LAPIS_LAZULI)) {
238
				if (!this.moveItemStackTo(stack, 1, 2, true)) {
239
					return ItemStack.EMPTY;
240
				}
241
			} else {
242
				if (this.slots.get(0).hasItem() || !this.slots.get(0).mayPlace(stack)) {
243
					return ItemStack.EMPTY;
244
				}
245
246
				ItemStack singleItem = stack.copyWithCount(1);
247
				stack.shrink(1);
248
				this.slots.get(0).setByPlayer(singleItem);
249
			}
250
251
			if (stack.isEmpty()) {
252
				slot.setByPlayer(ItemStack.EMPTY);
253
			} else {
254
				slot.setChanged();
255
			}
256
257
			if (stack.getCount() == clicked.getCount()) {
258
				return ItemStack.EMPTY;
259
			}
260
261
			slot.onTake(player, stack);
262
		}
263
264
		return clicked;
265
	}
266
}
267

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