ContainerComponentManipulator .java

net.minecraft.world.level.storage.loot.ContainerComponentManipulator (record): members: empty, ComponentSlotCollection, modifyItems, setContents, getSlots.

net.minecraft.world.level.storage.lootrecord77 linescommonextends ContainerComponent<T>>3.0 KB

API Surface (8)

void setContents(final ItemStack itemStack, final T defaultValue, final Stream<ItemStack> newContents)
void setContents(final ItemStack itemStack, final Stream<ItemStack> newContents)
void modifyItems(final ItemStack itemStack, final UnaryOperator<ItemStack> modifier)
SlotCollection getSlots(final ItemStack itemStack)
Stream<ItemStack> itemCopies()
int size()
int replaceSlotItems(final ItemProvider items, final SlotSelector slotSelector)
void modifySlots(final Consumer<? super SlotAccess> consumer, final SlotSelector slotSelector)

Full Source

ContainerComponentManipulator.java77 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.storage.loot;
2
3
import java.util.function.Consumer;
4
import java.util.function.UnaryOperator;
5
import java.util.stream.Stream;
6
import net.minecraft.core.component.DataComponentType;
7
import net.minecraft.world.entity.SlotAccess;
8
import net.minecraft.world.item.ItemProvider;
9
import net.minecraft.world.item.ItemStack;
10
import net.minecraft.world.item.component.ContainerComponent;
11
import net.minecraft.world.item.slot.SlotCollection;
12
import net.minecraft.world.item.slot.SlotSelector;
13
14
public record ContainerComponentManipulator<T extends ContainerComponent<T>>(DataComponentType<T> type, T empty) {
15
	public void setContents(final ItemStack itemStack, final T defaultValue, final Stream<ItemStack> newContents) {
16
		T currentValue = itemStack.getOrDefault(this.type(), defaultValue);
17
		T newValue = currentValue.copyWithContents(newContents);
18
		itemStack.set(this.type(), newValue);
19
	}
20
21
	public void setContents(final ItemStack itemStack, final Stream<ItemStack> newContents) {
22
		this.setContents(itemStack, this.empty(), newContents);
23
	}
24
25
	public void modifyItems(final ItemStack itemStack, final UnaryOperator<ItemStack> modifier) {
26
		T contents = itemStack.get(this.type());
27
		if (contents != null) {
28
			UnaryOperator<ItemStack> nonEmptyModifier = currentItemStack -> {
29
				if (currentItemStack.isEmpty()) {
30
					return currentItemStack;
31
				}
32
33
				ItemStack newItemStack = modifier.apply(currentItemStack);
34
				newItemStack.limitSize(newItemStack.getMaxStackSize());
35
				return newItemStack;
36
			};
37
			this.setContents(itemStack, contents.itemCopies().map(nonEmptyModifier));
38
		}
39
	}
40
41
	public SlotCollection getSlots(final ItemStack itemStack) {
42
		return new ContainerComponentManipulator.ComponentSlotCollection<>(itemStack, this);
43
	}
44
45
	private record ComponentSlotCollection<T extends ContainerComponent<T>>(ItemStack itemStack, ContainerComponentManipulator<T> component)
46
		implements SlotCollection {
47
		@Override
48
		public Stream<ItemStack> itemCopies() {
49
			T contents = this.itemStack.get(this.component.type());
50
			return contents != null ? contents.itemCopies() : Stream.empty();
51
		}
52
53
		@Override
54
		public int size() {
55
			T contents = this.itemStack.get(this.component.type());
56
			return contents != null ? contents.size() : 0;
57
		}
58
59
		@Override
60
		public int replaceSlotItems(final ItemProvider items, final SlotSelector slotSelector) {
61
			T currentValue = this.itemStack.getOrDefault(this.component.type(), this.component.empty());
62
			ContainerComponent.Mutable<T> mutable = currentValue.asMutable();
63
			int slotsReplaced = mutable.replaceSlotItems(items, slotSelector);
64
			this.itemStack.set(this.component.type(), mutable.toImmutable());
65
			return slotsReplaced;
66
		}
67
68
		@Override
69
		public void modifySlots(final Consumer<? super SlotAccess> consumer, final SlotSelector slotSelector) {
70
			T currentValue = this.itemStack.getOrDefault(this.component.type(), this.component.empty());
71
			ContainerComponent.Mutable<T> mutable = currentValue.asMutable();
72
			mutable.modifySlots(consumer, slotSelector);
73
			this.itemStack.set(this.component.type(), mutable.toImmutable());
74
		}
75
	}
76
}
77

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