ItemStackTemplate .java

net.minecraft.world.item.ItemStackTemplate (record): members: item, count, components, fromNonEmptyStack, fromStack, withCount, typeHolder, validate, get.

net.minecraft.world.itemrecord120 linescommon4.3 KB

Implements

API Surface (8)

ItemStackTemplate fromNonEmptyStack(final ItemStack itemStack)
ItemStackTemplate fromStack(final ItemStack itemStack)
ItemStackTemplate fromNonEmptyStack(final ItemStack itemStack, final int newCount)
ItemStackTemplate withCount(final int count)
ItemStack create()
ItemStack apply(final DataComponentPatch additionalPatch)
ItemStack apply(final int count, final DataComponentPatch additionalPatch)
Holder<Item> typeHolder()

Fields & Constants (4)

LOGGER
MAP_CODEC
CODEC
STREAM_CODEC

String literals (4)

"count"
"components"
"Item must be non-empty"
"Stack must be non-empty"

Full Source

ItemStackTemplate.java120 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import com.mojang.logging.LogUtils;
4
import com.mojang.serialization.Codec;
5
import com.mojang.serialization.MapCodec;
6
import com.mojang.serialization.DataResult.Error;
7
import com.mojang.serialization.codecs.RecordCodecBuilder;
8
import java.util.Optional;
9
import net.minecraft.core.Holder;
10
import net.minecraft.core.component.DataComponentPatch;
11
import net.minecraft.core.component.DataComponentType;
12
import net.minecraft.network.RegistryFriendlyByteBuf;
13
import net.minecraft.network.codec.ByteBufCodecs;
14
import net.minecraft.network.codec.StreamCodec;
15
import net.minecraft.util.ExtraCodecs;
16
import org.jspecify.annotations.Nullable;
17
import org.slf4j.Logger;
18
19
public record ItemStackTemplate(Holder<Item> item, int count, DataComponentPatch components) implements ItemInstance {
20
	private static final Logger LOGGER = LogUtils.getLogger();
21
	public static final MapCodec<ItemStackTemplate> MAP_CODEC = RecordCodecBuilder.mapCodec(
22
		i -> i.group(
23
				Item.CODEC.fieldOf("id").forGetter(ItemStackTemplate::item),
24
				ExtraCodecs.intRange(1, 99).optionalFieldOf("count", 1).forGetter(ItemStackTemplate::count),
25
				DataComponentPatch.CODEC.optionalFieldOf("components", DataComponentPatch.EMPTY).forGetter(ItemStackTemplate::components)
26
			)
27
			.apply(i, ItemStackTemplate::new)
28
	);
29
	public static final Codec<ItemStackTemplate> CODEC = Codec.withAlternative(MAP_CODEC.codec(), Item.CODEC, item -> new ItemStackTemplate(item.value()));
30
	public static final StreamCodec<RegistryFriendlyByteBuf, ItemStackTemplate> STREAM_CODEC = StreamCodec.composite(
31
		Item.STREAM_CODEC,
32
		ItemStackTemplate::item,
33
		ByteBufCodecs.VAR_INT,
34
		ItemStackTemplate::count,
35
		DataComponentPatch.STREAM_CODEC,
36
		ItemStackTemplate::components,
37
		ItemStackTemplate::new
38
	);
39
40
	public ItemStackTemplate(final Item item) {
41
		this(item.builtInRegistryHolder(), 1, DataComponentPatch.EMPTY);
42
	}
43
44
	public ItemStackTemplate(final Item item, final int count) {
45
		this(item.builtInRegistryHolder(), count, DataComponentPatch.EMPTY);
46
	}
47
48
	public ItemStackTemplate(final Item item, final DataComponentPatch patch) {
49
		this(item.builtInRegistryHolder(), 1, patch);
50
	}
51
52
	public ItemStackTemplate(Holder<Item> item, int count, DataComponentPatch components) {
53
		if (count != 0 && !item.is(Items.AIR.builtInRegistryHolder())) {
54
			this.item = item;
55
			this.count = count;
56
			this.components = components;
57
		} else {
58
			throw new IllegalStateException("Item must be non-empty");
59
		}
60
	}
61
62
	public static ItemStackTemplate fromNonEmptyStack(final ItemStack itemStack) {
63
		if (itemStack.isEmpty()) {
64
			throw new IllegalStateException("Stack must be non-empty");
65
		} else {
66
			return fromStack(itemStack);
67
		}
68
	}
69
70
	public static ItemStackTemplate fromStack(final ItemStack itemStack) {
71
		return new ItemStackTemplate(itemStack.typeHolder(), itemStack.getCount(), itemStack.getComponentsPatch());
72
	}
73
74
	public static ItemStackTemplate fromNonEmptyStack(final ItemStack itemStack, final int newCount) {
75
		if (!itemStack.isEmpty() && newCount > 0) {
76
			return new ItemStackTemplate(itemStack.typeHolder(), newCount, itemStack.getComponentsPatch());
77
		} else {
78
			throw new IllegalStateException("Stack must be non-empty");
79
		}
80
	}
81
82
	public ItemStackTemplate withCount(final int count) {
83
		return this.count == count ? this : new ItemStackTemplate(this.item, count, this.components);
84
	}
85
86
	public ItemStack create() {
87
		return this.validate(new ItemStack(this.item, this.count, this.components));
88
	}
89
90
	private ItemStack validate(final ItemStack result) {
91
		Optional<Error<ItemStack>> error = ItemStack.validateStrict(result).error();
92
		if (error.isPresent()) {
93
			LOGGER.warn("Can't create item stack with properties {}, error: {}", this, error.get().message());
94
			return ItemStack.EMPTY;
95
		} else {
96
			return result;
97
		}
98
	}
99
100
	public ItemStack apply(final DataComponentPatch additionalPatch) {
101
		return this.apply(this.count, additionalPatch);
102
	}
103
104
	public ItemStack apply(final int count, final DataComponentPatch additionalPatch) {
105
		ItemStack result = new ItemStack(this.item, count, additionalPatch);
106
		result.applyComponents(this.components);
107
		return this.validate(result);
108
	}
109
110
	@Override
111
	public Holder<Item> typeHolder() {
112
		return this.item;
113
	}
114
115
	@Override
116
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
117
		return this.components.get(this.item.components(), type);
118
	}
119
}
120

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