Slot .java

net.minecraft.world.inventory.Slot (class): members: getContainerSlot, hasItem, allowModification, isHighlightable, getNoItemIcon, onSwapCraft, safeInsert.

net.minecraft.world.inventoryclass172 linescommon4.2 KB

API Surface (26)

void onQuickCraft(final ItemStack picked, final ItemStack original)
void onQuickCraft(final ItemStack picked, final int count)
void onSwapCraft(final int count)
void checkTakeAchievements(final ItemStack carried)
void onTake(final Player player, final ItemStack carried)
boolean mayPlace(final ItemStack itemStack)
ItemStack getItem()
boolean hasItem()
void setByPlayer(final ItemStack itemStack)
void setByPlayer(final ItemStack itemStack, final ItemStack previous)
void set(final ItemStack itemStack)
void setChanged()
int getMaxStackSize()
int getMaxStackSize(final ItemStack itemStack)
ItemStack remove(final int amount)
boolean mayPickup(final Player player)
boolean isActive()
Optional<ItemStack> tryRemove(int amount, final int maxAmount, final Player player)
ItemStack safeTake(final int amount, final int maxAmount, final Player player)
ItemStack safeClone(final Player player)
ItemStack safeInsert(final ItemStack stack)
ItemStack safeInsert(final ItemStack inputStack, final int inputAmount)
boolean allowModification(final Player player)
int getContainerSlot()
boolean isHighlightable()
boolean isFake()

Fields & Constants (5)

slot
container
index
x
y

Code Graph: Dependencies (4)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Slot.java172 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.inventory;
2
3
import java.util.Optional;
4
import net.minecraft.resources.Identifier;
5
import net.minecraft.world.Container;
6
import net.minecraft.world.entity.player.Player;
7
import net.minecraft.world.item.ItemStack;
8
import org.jspecify.annotations.Nullable;
9
10
public class Slot {
11
	private final int slot;
12
	public final Container container;
13
	public int index;
14
	public final int x;
15
	public final int y;
16
17
	public Slot(final Container container, final int slot, final int x, final int y) {
18
		this.container = container;
19
		this.slot = slot;
20
		this.x = x;
21
		this.y = y;
22
	}
23
24
	public void onQuickCraft(final ItemStack picked, final ItemStack original) {
25
		int count = original.getCount() - picked.getCount();
26
		if (count > 0) {
27
			this.onQuickCraft(original, count);
28
		}
29
	}
30
31
	protected void onQuickCraft(final ItemStack picked, final int count) {
32
	}
33
34
	protected void onSwapCraft(final int count) {
35
	}
36
37
	protected void checkTakeAchievements(final ItemStack carried) {
38
	}
39
40
	public void onTake(final Player player, final ItemStack carried) {
41
		this.setChanged();
42
	}
43
44
	public boolean mayPlace(final ItemStack itemStack) {
45
		return true;
46
	}
47
48
	public ItemStack getItem() {
49
		return this.container.getItem(this.slot);
50
	}
51
52
	public boolean hasItem() {
53
		return !this.getItem().isEmpty();
54
	}
55
56
	public void setByPlayer(final ItemStack itemStack) {
57
		this.setByPlayer(itemStack, this.getItem());
58
	}
59
60
	public void setByPlayer(final ItemStack itemStack, final ItemStack previous) {
61
		this.set(itemStack);
62
	}
63
64
	public void set(final ItemStack itemStack) {
65
		this.container.setItem(this.slot, itemStack);
66
		this.setChanged();
67
	}
68
69
	public void setChanged() {
70
		this.container.setChanged();
71
	}
72
73
	public int getMaxStackSize() {
74
		return this.container.getMaxStackSize();
75
	}
76
77
	public int getMaxStackSize(final ItemStack itemStack) {
78
		return Math.min(this.getMaxStackSize(), itemStack.getMaxStackSize());
79
	}
80
81
	public @Nullable Identifier getNoItemIcon() {
82
		return null;
83
	}
84
85
	public ItemStack remove(final int amount) {
86
		return this.container.removeItem(this.slot, amount);
87
	}
88
89
	public boolean mayPickup(final Player player) {
90
		return true;
91
	}
92
93
	public boolean isActive() {
94
		return true;
95
	}
96
97
	public Optional<ItemStack> tryRemove(int amount, final int maxAmount, final Player player) {
98
		if (!this.mayPickup(player)) {
99
			return Optional.empty();
100
		}
101
102
		if (!this.allowModification(player) && maxAmount < this.getItem().getCount()) {
103
			return Optional.empty();
104
		}
105
106
		amount = Math.min(amount, maxAmount);
107
		ItemStack result = this.remove(amount);
108
		if (result.isEmpty()) {
109
			return Optional.empty();
110
		}
111
112
		if (this.getItem().isEmpty()) {
113
			this.setByPlayer(ItemStack.EMPTY, result);
114
		}
115
116
		return Optional.of(result);
117
	}
118
119
	public ItemStack safeTake(final int amount, final int maxAmount, final Player player) {
120
		Optional<ItemStack> result = this.tryRemove(amount, maxAmount, player);
121
		result.ifPresent(item -> this.onTake(player, item));
122
		return result.orElse(ItemStack.EMPTY);
123
	}
124
125
	public ItemStack safeClone(final Player player) {
126
		ItemStack item = this.getItem();
127
		return item.copyWithCount(item.getMaxStackSize());
128
	}
129
130
	public ItemStack safeInsert(final ItemStack stack) {
131
		return this.safeInsert(stack, stack.getCount());
132
	}
133
134
	public ItemStack safeInsert(final ItemStack inputStack, final int inputAmount) {
135
		if (!inputStack.isEmpty() && this.mayPlace(inputStack)) {
136
			ItemStack slotStack = this.getItem();
137
			int transferableItemCount = Math.min(Math.min(inputAmount, inputStack.getCount()), this.getMaxStackSize(inputStack) - slotStack.getCount());
138
			if (transferableItemCount <= 0) {
139
				return inputStack;
140
			}
141
142
			if (slotStack.isEmpty()) {
143
				this.setByPlayer(inputStack.split(transferableItemCount));
144
			} else if (ItemStack.isSameItemSameComponents(slotStack, inputStack)) {
145
				inputStack.shrink(transferableItemCount);
146
				slotStack.grow(transferableItemCount);
147
				this.setByPlayer(slotStack);
148
			}
149
150
			return inputStack;
151
		} else {
152
			return inputStack;
153
		}
154
	}
155
156
	public boolean allowModification(final Player player) {
157
		return this.mayPickup(player) && this.mayPlace(this.getItem());
158
	}
159
160
	public int getContainerSlot() {
161
		return this.slot;
162
	}
163
164
	public boolean isHighlightable() {
165
		return true;
166
	}
167
168
	public boolean isFake() {
169
		return false;
170
	}
171
}
172

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