SimpleContainer .java

net.minecraft.world.SimpleContainer (class, 219 lines): members: storeAsItemList, removeAllItems, removeItemType, fromItemList, canAddItem, addItem, items.

net.minecraft.worldclass219 linescommon5.5 KB

Implements

API Surface (18)

ItemStack getItem(final int slot)
List<ItemStack> removeAllItems()
ItemStack removeItem(final int slot, final int count)
ItemStack removeItemType(final Item itemType, final int count)
ItemStack addItem(final ItemStack itemStack)
boolean canAddItem(final ItemStack itemStack)
ItemStack removeItemNoUpdate(final int slot)
void setItem(final int slot, final ItemStack itemStack)
void setChanged()
int getContainerSize()
boolean isEmpty()
boolean stillValid(final Player player)
void clearContent()
void fillStackedContents(final StackedItemContents contents)
String toString()
void fromItemList(final ValueInput.TypedInputList<ItemStack> items)
void storeAsItemList(final ValueOutput.TypedOutputList<ItemStack> output)
NonNullList<ItemStack> getItems()

Fields & Constants (2)

size
items

Full Source

SimpleContainer.java219 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world;
2
3
import java.util.List;
4
import java.util.stream.Collectors;
5
import net.minecraft.core.NonNullList;
6
import net.minecraft.world.entity.player.Player;
7
import net.minecraft.world.entity.player.StackedItemContents;
8
import net.minecraft.world.inventory.StackedContentsCompatible;
9
import net.minecraft.world.item.Item;
10
import net.minecraft.world.item.ItemStack;
11
import net.minecraft.world.level.storage.ValueInput;
12
import net.minecraft.world.level.storage.ValueOutput;
13
14
/**
15
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
16
 */
17
public class SimpleContainer implements Container, StackedContentsCompatible {
18
	private final int size;
19
	/**
20
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
21
	 */
22
	public final NonNullList<ItemStack> items;
23
24
	public SimpleContainer(final int size) {
25
		this.size = size;
26
		this.items = NonNullList.withSize(size, ItemStack.EMPTY);
27
	}
28
29
	public SimpleContainer(final ItemStack... itemstacks) {
30
		this.size = itemstacks.length;
31
		this.items = NonNullList.of(ItemStack.EMPTY, itemstacks);
32
	}
33
34
	@Override
35
	public ItemStack getItem(final int slot) {
36
		return slot >= 0 && slot < this.items.size() ? this.items.get(slot) : ItemStack.EMPTY;
37
	}
38
39
	public List<ItemStack> removeAllItems() {
40
		List<ItemStack> itemsRemoved = this.items.stream().filter(item -> !item.isEmpty()).collect(Collectors.toList());
41
		this.clearContent();
42
		return itemsRemoved;
43
	}
44
45
	@Override
46
	public ItemStack removeItem(final int slot, final int count) {
47
		ItemStack result = ContainerHelper.removeItem(this.items, slot, count);
48
		if (!result.isEmpty()) {
49
			this.setChanged();
50
		}
51
52
		return result;
53
	}
54
55
	public ItemStack removeItemType(final Item itemType, final int count) {
56
		ItemStack removed = new ItemStack(itemType, 0);
57
58
		for (int slot = this.size - 1; slot >= 0; slot--) {
59
			ItemStack current = this.getItem(slot);
60
			if (current.getItem().equals(itemType)) {
61
				int stillNeeded = count - removed.getCount();
62
				ItemStack removedFromThisSlot = current.split(stillNeeded);
63
				removed.grow(removedFromThisSlot.getCount());
64
				if (removed.getCount() == count) {
65
					break;
66
				}
67
			}
68
		}
69
70
		if (!removed.isEmpty()) {
71
			this.setChanged();
72
		}
73
74
		return removed;
75
	}
76
77
	public ItemStack addItem(final ItemStack itemStack) {
78
		if (itemStack.isEmpty()) {
79
			return ItemStack.EMPTY;
80
		}
81
82
		ItemStack remainingItems = itemStack.copy();
83
		this.moveItemToOccupiedSlotsWithSameType(remainingItems);
84
		if (remainingItems.isEmpty()) {
85
			return ItemStack.EMPTY;
86
		}
87
88
		this.moveItemToEmptySlots(remainingItems);
89
		return remainingItems.isEmpty() ? ItemStack.EMPTY : remainingItems;
90
	}
91
92
	public boolean canAddItem(final ItemStack itemStack) {
93
		boolean hasSpace = false;
94
95
		for (ItemStack targetStack : this.items) {
96
			if (targetStack.isEmpty() || ItemStack.isSameItemSameComponents(targetStack, itemStack) && targetStack.getCount() < targetStack.getMaxStackSize()) {
97
				hasSpace = true;
98
				break;
99
			}
100
		}
101
102
		return hasSpace;
103
	}
104
105
	@Override
106
	public ItemStack removeItemNoUpdate(final int slot) {
107
		ItemStack itemStack = this.items.get(slot);
108
		if (itemStack.isEmpty()) {
109
			return ItemStack.EMPTY;
110
		}
111
112
		this.items.set(slot, ItemStack.EMPTY);
113
		return itemStack;
114
	}
115
116
	@Override
117
	public void setItem(final int slot, final ItemStack itemStack) {
118
		this.items.set(slot, itemStack);
119
		itemStack.limitSize(this.getMaxStackSize(itemStack));
120
		this.setChanged();
121
	}
122
123
	@Override
124
	public void setChanged() {
125
	}
126
127
	@Override
128
	public int getContainerSize() {
129
		return this.size;
130
	}
131
132
	@Override
133
	public boolean isEmpty() {
134
		for (ItemStack itemStack : this.items) {
135
			if (!itemStack.isEmpty()) {
136
				return false;
137
			}
138
		}
139
140
		return true;
141
	}
142
143
	@Override
144
	public boolean stillValid(final Player player) {
145
		return true;
146
	}
147
148
	@Override
149
	public void clearContent() {
150
		this.items.clear();
151
		this.setChanged();
152
	}
153
154
	@Override
155
	public void fillStackedContents(final StackedItemContents contents) {
156
		for (ItemStack itemStack : this.items) {
157
			contents.accountStack(itemStack);
158
		}
159
	}
160
161
	@Override
162
	public String toString() {
163
		return this.items.stream().filter(item -> !item.isEmpty()).toList().toString();
164
	}
165
166
	private void moveItemToEmptySlots(final ItemStack sourceStack) {
167
		for (int slot = 0; slot < this.size; slot++) {
168
			ItemStack targetStack = this.getItem(slot);
169
			if (targetStack.isEmpty()) {
170
				this.setItem(slot, sourceStack.copyAndClear());
171
				return;
172
			}
173
		}
174
	}
175
176
	private void moveItemToOccupiedSlotsWithSameType(final ItemStack sourceStack) {
177
		for (int slot = 0; slot < this.size; slot++) {
178
			ItemStack targetStack = this.getItem(slot);
179
			if (ItemStack.isSameItemSameComponents(targetStack, sourceStack)) {
180
				this.moveItemsBetweenStacks(sourceStack, targetStack);
181
				if (sourceStack.isEmpty()) {
182
					return;
183
				}
184
			}
185
		}
186
	}
187
188
	private void moveItemsBetweenStacks(final ItemStack sourceStack, final ItemStack targetStack) {
189
		int maxCount = this.getMaxStackSize(targetStack);
190
		int diff = Math.min(sourceStack.getCount(), maxCount - targetStack.getCount());
191
		if (diff > 0) {
192
			targetStack.grow(diff);
193
			sourceStack.shrink(diff);
194
			this.setChanged();
195
		}
196
	}
197
198
	public void fromItemList(final ValueInput.TypedInputList<ItemStack> items) {
199
		this.clearContent();
200
201
		for (ItemStack stack : items) {
202
			this.addItem(stack);
203
		}
204
	}
205
206
	public void storeAsItemList(final ValueOutput.TypedOutputList<ItemStack> output) {
207
		for (int i = 0; i < this.getContainerSize(); i++) {
208
			ItemStack itemStack = this.getItem(i);
209
			if (!itemStack.isEmpty()) {
210
				output.add(itemStack);
211
			}
212
		}
213
	}
214
215
	public NonNullList<ItemStack> getItems() {
216
		return this.items;
217
	}
218
}
219

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