StackedContents .java

net.minecraft.world.entity.player.StackedContents (class): members: getResultUpperBound, IngredientInfo, RecipePicker, tryPickAll, account, amounts, clear.

net.minecraft.world.entity.playerclass405 linescommon11.9 KB

API Surface (7)

boolean tryPick(final List<? extends StackedContents.IngredientInfo<T>> ingredients, final int amount, final StackedContents.@Nullable Output<T> output)
int tryPickAll(final List<? extends StackedContents.IngredientInfo<T>> ingredients, final int maxSize, final StackedContents.@Nullable Output<T> output)
void clear()
void account(final T item, final int count)
int getResultUpperBound(final List<? extends StackedContents.IngredientInfo<T>> ingredients)
boolean tryPick(final int capacity, final StackedContents.@Nullable Output<T> output)
int tryPickAll(final int maxSize, final StackedContents.@Nullable Output<T> output)

Fields & Constants (1)

amounts

String literals (2)

"Took "
" items, but only had "

Full Source

StackedContents.java405 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.player;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import it.unimi.dsi.fastutil.ints.IntArrayList;
5
import it.unimi.dsi.fastutil.ints.IntList;
6
import it.unimi.dsi.fastutil.objects.ObjectIterable;
7
import it.unimi.dsi.fastutil.objects.Reference2IntMaps;
8
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
9
import it.unimi.dsi.fastutil.objects.Reference2IntMap.Entry;
10
import java.util.ArrayList;
11
import java.util.BitSet;
12
import java.util.List;
13
import org.jspecify.annotations.Nullable;
14
15
public class StackedContents<T> {
16
	public final Reference2IntOpenHashMap<T> amounts = new Reference2IntOpenHashMap<>();
17
18
	private boolean hasAtLeast(final T item, final int count) {
19
		return this.amounts.getInt(item) >= count;
20
	}
21
22
	private void take(final T item, final int amount) {
23
		int previous = this.amounts.addTo(item, -amount);
24
		if (previous < amount) {
25
			throw new IllegalStateException("Took " + amount + " items, but only had " + previous);
26
		}
27
	}
28
29
	private void put(final T item, final int count) {
30
		this.amounts.addTo(item, count);
31
	}
32
33
	public boolean tryPick(final List<? extends StackedContents.IngredientInfo<T>> ingredients, final int amount, final StackedContents.@Nullable Output<T> output) {
34
		return new StackedContents.RecipePicker(ingredients).tryPick(amount, output);
35
	}
36
37
	public int tryPickAll(final List<? extends StackedContents.IngredientInfo<T>> ingredients, final int maxSize, final StackedContents.@Nullable Output<T> output) {
38
		return new StackedContents.RecipePicker(ingredients).tryPickAll(maxSize, output);
39
	}
40
41
	public void clear() {
42
		this.amounts.clear();
43
	}
44
45
	public void account(final T item, final int count) {
46
		this.put(item, count);
47
	}
48
49
	private List<T> getUniqueAvailableIngredientItems(final Iterable<? extends StackedContents.IngredientInfo<T>> ingredients) {
50
		List<T> result = new ArrayList<>();
51
52
		for (Entry<T> availableItem : Reference2IntMaps.fastIterable(this.amounts)) {
53
			if (availableItem.getIntValue() > 0 && anyIngredientMatches(ingredients, availableItem.getKey())) {
54
				result.add(availableItem.getKey());
55
			}
56
		}
57
58
		return result;
59
	}
60
61
	private static <T> boolean anyIngredientMatches(final Iterable<? extends StackedContents.IngredientInfo<T>> ingredients, final T item) {
62
		for (StackedContents.IngredientInfo<T> ingredient : ingredients) {
63
			if (ingredient.acceptsItem(item)) {
64
				return true;
65
			}
66
		}
67
68
		return false;
69
	}
70
71
	@VisibleForTesting
72
	public int getResultUpperBound(final List<? extends StackedContents.IngredientInfo<T>> ingredients) {
73
		int min = Integer.MAX_VALUE;
74
		ObjectIterable<Entry<T>> availableItems = Reference2IntMaps.fastIterable(this.amounts);
75
76
		label31:
77
		for (StackedContents.IngredientInfo<T> ingredient : ingredients) {
78
			int max = 0;
79
80
			for (Entry<T> entry : availableItems) {
81
				int itemCount = entry.getIntValue();
82
				if (itemCount > max) {
83
					if (ingredient.acceptsItem(entry.getKey())) {
84
						max = itemCount;
85
					}
86
87
					if (max >= min) {
88
						continue label31;
89
					}
90
				}
91
			}
92
93
			min = max;
94
			if (min == 0) {
95
				break;
96
			}
97
		}
98
99
		return min;
100
	}
101
102
	@FunctionalInterface
103
	public interface IngredientInfo<T> {
104
		boolean acceptsItem(T item);
105
	}
106
107
	@FunctionalInterface
108
	public interface Output<T> {
109
		void accept(T item);
110
	}
111
112
	private class RecipePicker {
113
		private final List<? extends StackedContents.IngredientInfo<T>> ingredients;
114
		private final int ingredientCount;
115
		private final List<T> items;
116
		private final int itemCount;
117
		private final BitSet data;
118
		private final IntList path = new IntArrayList();
119
120
		public RecipePicker(final List<? extends StackedContents.IngredientInfo<T>> ingredients) {
121
			this.ingredients = ingredients;
122
			this.ingredientCount = ingredients.size();
123
			this.items = StackedContents.this.getUniqueAvailableIngredientItems(ingredients);
124
			this.itemCount = this.items.size();
125
			this.data = new BitSet(this.visitedIngredientCount() + this.visitedItemCount() + this.satisfiedCount() + this.connectionCount() + this.residualCount());
126
			this.setInitialConnections();
127
		}
128
129
		private void setInitialConnections() {
130
			for (int ingredient = 0; ingredient < this.ingredientCount; ingredient++) {
131
				StackedContents.IngredientInfo<T> ingredientInfo = (StackedContents.IngredientInfo<T>)this.ingredients.get(ingredient);
132
133
				for (int item = 0; item < this.itemCount; item++) {
134
					if (ingredientInfo.acceptsItem(this.items.get(item))) {
135
						this.setConnection(item, ingredient);
136
					}
137
				}
138
			}
139
		}
140
141
		public boolean tryPick(final int capacity, final StackedContents.@Nullable Output<T> output) {
142
			if (capacity <= 0) {
143
				return true;
144
			}
145
146
			int satisfiedIngredientCount = 0;
147
148
			while (true) {
149
				IntList path = this.tryAssigningNewItem(capacity);
150
				if (path == null) {
151
					boolean isValidAssignment = satisfiedIngredientCount == this.ingredientCount;
152
					boolean hasOutput = isValidAssignment && output != null;
153
					this.clearAllVisited();
154
					this.clearSatisfied();
155
156
					for (int ingredient = 0; ingredient < this.ingredientCount; ingredient++) {
157
						for (int item = 0; item < this.itemCount; item++) {
158
							if (this.isAssigned(item, ingredient)) {
159
								this.unassign(item, ingredient);
160
								StackedContents.this.put(this.items.get(item), capacity);
161
								if (hasOutput) {
162
									output.accept(this.items.get(item));
163
								}
164
								break;
165
							}
166
						}
167
					}
168
169
					assert this.data.get(this.residualOffset(), this.residualOffset() + this.residualCount()).isEmpty();
170
					return isValidAssignment;
171
				}
172
173
				int assignedItem = path.getInt(0);
174
				StackedContents.this.take(this.items.get(assignedItem), capacity);
175
				int satisfiedIngredient = path.size() - 1;
176
				this.setSatisfied(path.getInt(satisfiedIngredient));
177
				satisfiedIngredientCount++;
178
179
				for (int i = 0; i < path.size() - 1; i++) {
180
					if (isPathIndexItem(i)) {
181
						int item = path.getInt(i);
182
						int ingredient = path.getInt(i + 1);
183
						this.assign(item, ingredient);
184
					} else {
185
						int item = path.getInt(i + 1);
186
						int ingredient = path.getInt(i);
187
						this.unassign(item, ingredient);
188
					}
189
				}
190
			}
191
		}
192
193
		private static boolean isPathIndexItem(final int index) {
194
			return (index & 1) == 0;
195
		}
196
197
		private @Nullable IntList tryAssigningNewItem(final int capacity) {
198
			this.clearAllVisited();
199
200
			for (int item = 0; item < this.itemCount; item++) {
201
				if (StackedContents.this.hasAtLeast(this.items.get(item), capacity)) {
202
					IntList path = this.findNewItemAssignmentPath(item);
203
					if (path != null) {
204
						return path;
205
					}
206
				}
207
			}
208
209
			return null;
210
		}
211
212
		private @Nullable IntList findNewItemAssignmentPath(final int startingItem) {
213
			this.path.clear();
214
			this.visitItem(startingItem);
215
			this.path.add(startingItem);
216
217
			while (!this.path.isEmpty()) {
218
				int pathLength = this.path.size();
219
				if (isPathIndexItem(pathLength - 1)) {
220
					int itemToAssign = this.path.getInt(pathLength - 1);
221
222
					for (int ingredient = 0; ingredient < this.ingredientCount; ingredient++) {
223
						if (!this.hasVisitedIngredient(ingredient) && this.hasConnection(itemToAssign, ingredient) && !this.isAssigned(itemToAssign, ingredient)) {
224
							this.visitIngredient(ingredient);
225
							this.path.add(ingredient);
226
							break;
227
						}
228
					}
229
				} else {
230
					int lastAssignedIngredient = this.path.getInt(pathLength - 1);
231
					if (!this.isSatisfied(lastAssignedIngredient)) {
232
						return this.path;
233
					}
234
235
					for (int item = 0; item < this.itemCount; item++) {
236
						if (!this.hasVisitedItem(item) && this.isAssigned(item, lastAssignedIngredient)) {
237
							assert this.hasConnection(item, lastAssignedIngredient);
238
							this.visitItem(item);
239
							this.path.add(item);
240
							break;
241
						}
242
					}
243
				}
244
245
				int newLength = this.path.size();
246
				if (newLength == pathLength) {
247
					this.path.removeInt(newLength - 1);
248
				}
249
			}
250
251
			return null;
252
		}
253
254
		private int visitedIngredientOffset() {
255
			return 0;
256
		}
257
258
		private int visitedIngredientCount() {
259
			return this.ingredientCount;
260
		}
261
262
		private int visitedItemOffset() {
263
			return this.visitedIngredientOffset() + this.visitedIngredientCount();
264
		}
265
266
		private int visitedItemCount() {
267
			return this.itemCount;
268
		}
269
270
		private int satisfiedOffset() {
271
			return this.visitedItemOffset() + this.visitedItemCount();
272
		}
273
274
		private int satisfiedCount() {
275
			return this.ingredientCount;
276
		}
277
278
		private int connectionOffset() {
279
			return this.satisfiedOffset() + this.satisfiedCount();
280
		}
281
282
		private int connectionCount() {
283
			return this.ingredientCount * this.itemCount;
284
		}
285
286
		private int residualOffset() {
287
			return this.connectionOffset() + this.connectionCount();
288
		}
289
290
		private int residualCount() {
291
			return this.ingredientCount * this.itemCount;
292
		}
293
294
		private boolean isSatisfied(final int ingredient) {
295
			return this.data.get(this.getSatisfiedIndex(ingredient));
296
		}
297
298
		private void setSatisfied(final int ingredient) {
299
			this.data.set(this.getSatisfiedIndex(ingredient));
300
		}
301
302
		private int getSatisfiedIndex(final int ingredient) {
303
			assert ingredient >= 0 && ingredient < this.ingredientCount;
304
			return this.satisfiedOffset() + ingredient;
305
		}
306
307
		private void clearSatisfied() {
308
			this.clearRange(this.satisfiedOffset(), this.satisfiedCount());
309
		}
310
311
		private void setConnection(final int item, final int ingredient) {
312
			this.data.set(this.getConnectionIndex(item, ingredient));
313
		}
314
315
		private boolean hasConnection(final int item, final int ingredient) {
316
			return this.data.get(this.getConnectionIndex(item, ingredient));
317
		}
318
319
		private int getConnectionIndex(final int item, final int ingredient) {
320
			assert item >= 0 && item < this.itemCount;
321
			assert ingredient >= 0 && ingredient < this.ingredientCount;
322
			return this.connectionOffset() + item * this.ingredientCount + ingredient;
323
		}
324
325
		private boolean isAssigned(final int item, final int ingredient) {
326
			return this.data.get(this.getResidualIndex(item, ingredient));
327
		}
328
329
		private void assign(final int item, final int ingredient) {
330
			int residualIndex = this.getResidualIndex(item, ingredient);
331
			assert !this.data.get(residualIndex);
332
			this.data.set(residualIndex);
333
		}
334
335
		private void unassign(final int item, final int ingredient) {
336
			int residualIndex = this.getResidualIndex(item, ingredient);
337
			assert this.data.get(residualIndex);
338
			this.data.clear(residualIndex);
339
		}
340
341
		private int getResidualIndex(final int item, final int ingredient) {
342
			assert item >= 0 && item < this.itemCount;
343
			assert ingredient >= 0 && ingredient < this.ingredientCount;
344
			return this.residualOffset() + item * this.ingredientCount + ingredient;
345
		}
346
347
		private void visitIngredient(final int item) {
348
			this.data.set(this.getVisitedIngredientIndex(item));
349
		}
350
351
		private boolean hasVisitedIngredient(final int ingredient) {
352
			return this.data.get(this.getVisitedIngredientIndex(ingredient));
353
		}
354
355
		private int getVisitedIngredientIndex(final int ingredient) {
356
			assert ingredient >= 0 && ingredient < this.ingredientCount;
357
			return this.visitedIngredientOffset() + ingredient;
358
		}
359
360
		private void visitItem(final int item) {
361
			this.data.set(this.getVisitiedItemIndex(item));
362
		}
363
364
		private boolean hasVisitedItem(final int item) {
365
			return this.data.get(this.getVisitiedItemIndex(item));
366
		}
367
368
		private int getVisitiedItemIndex(final int item) {
369
			assert item >= 0 && item < this.itemCount;
370
			return this.visitedItemOffset() + item;
371
		}
372
373
		private void clearAllVisited() {
374
			this.clearRange(this.visitedIngredientOffset(), this.visitedIngredientCount());
375
			this.clearRange(this.visitedItemOffset(), this.visitedItemCount());
376
		}
377
378
		private void clearRange(final int offset, final int count) {
379
			this.data.clear(offset, offset + count);
380
		}
381
382
		public int tryPickAll(final int maxSize, final StackedContents.@Nullable Output<T> output) {
383
			int min = 0;
384
			int max = Math.min(maxSize, StackedContents.this.getResultUpperBound(this.ingredients)) + 1;
385
386
			while (true) {
387
				int mid = (min + max) / 2;
388
				if (this.tryPick(mid, null)) {
389
					if (max - min <= 1) {
390
						if (mid > 0) {
391
							this.tryPick(mid, output);
392
						}
393
394
						return mid;
395
					}
396
397
					min = mid;
398
				} else {
399
					max = mid;
400
				}
401
			}
402
		}
403
	}
404
}
405

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