EnchantedCountIncreaseFunction .java

net.minecraft.world.level.storage.loot.functions.EnchantedCountIncreaseFunction (class): members: lootingMultiplier, NO_LIMIT, enchantment, limit, count.

net.minecraft.world.level.storage.loot.functionsclass119 linescommonextends LootItemConditionalFunction4.1 KB

API Surface (8)

MapCodec<EnchantedCountIncreaseFunction> codec()
Set<ContextKey<?>> getReferencedContextParams()
void validate(final ValidationContext context)
ItemStack run(final ItemStack itemStack, final LootContext context)
EnchantedCountIncreaseFunction.Builder lootingMultiplier(final HolderGetter<Enchantment> enchantments, final Holder<ContextFloatProvider> count)
EnchantedCountIncreaseFunction.Builder getThis()
EnchantedCountIncreaseFunction.Builder setLimit(final int limit)
LootItemFunction build()

Fields & Constants (5)

NO_LIMIT
MAP_CODEC
enchantment
count
limit

String literals (3)

"enchantment"
"count"
"limit"

Full Source

EnchantedCountIncreaseFunction.java119 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.storage.loot.functions;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.MapCodec;
5
import com.mojang.serialization.codecs.RecordCodecBuilder;
6
import java.util.Optional;
7
import java.util.Set;
8
import net.minecraft.core.Holder;
9
import net.minecraft.core.HolderGetter;
10
import net.minecraft.util.context.ContextKey;
11
import net.minecraft.world.entity.Entity;
12
import net.minecraft.world.entity.LivingEntity;
13
import net.minecraft.world.item.ItemStack;
14
import net.minecraft.world.item.enchantment.Enchantment;
15
import net.minecraft.world.item.enchantment.EnchantmentHelper;
16
import net.minecraft.world.item.enchantment.Enchantments;
17
import net.minecraft.world.level.storage.loot.LootContext;
18
import net.minecraft.world.level.storage.loot.Validatable;
19
import net.minecraft.world.level.storage.loot.ValidationContext;
20
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
21
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
22
import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProvider;
23
import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProviders;
24
25
public class EnchantedCountIncreaseFunction extends LootItemConditionalFunction {
26
	public static final int NO_LIMIT = 0;
27
	public static final MapCodec<EnchantedCountIncreaseFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
28
		i -> commonFields(i)
29
			.and(
30
				i.group(
31
					Enchantment.CODEC.fieldOf("enchantment").forGetter(f -> f.enchantment),
32
					ContextFloatProviders.CODEC.fieldOf("count").forGetter(f -> f.count),
33
					Codec.INT.optionalFieldOf("limit", 0).forGetter(f -> f.limit)
34
				)
35
			)
36
			.apply(i, EnchantedCountIncreaseFunction::new)
37
	);
38
	private final Holder<Enchantment> enchantment;
39
	private final Holder<ContextFloatProvider> count;
40
	private final int limit;
41
42
	private EnchantedCountIncreaseFunction(
43
		final Optional<Holder<LootItemCondition>> condition, final Holder<Enchantment> enchantment, final Holder<ContextFloatProvider> count, final int limit
44
	) {
45
		super(condition);
46
		this.enchantment = enchantment;
47
		this.count = count;
48
		this.limit = limit;
49
	}
50
51
	@Override
52
	public MapCodec<EnchantedCountIncreaseFunction> codec() {
53
		return MAP_CODEC;
54
	}
55
56
	@Override
57
	public Set<ContextKey<?>> getReferencedContextParams() {
58
		return Set.of(LootContextParams.ATTACKING_ENTITY);
59
	}
60
61
	@Override
62
	public void validate(final ValidationContext context) {
63
		super.validate(context);
64
		Validatable.validateHolder(context, "count", this.count);
65
	}
66
67
	private boolean hasLimit() {
68
		return this.limit > 0;
69
	}
70
71
	@Override
72
	public ItemStack run(final ItemStack itemStack, final LootContext context) {
73
		Entity killer = context.getOptional(LootContextParams.ATTACKING_ENTITY);
74
		if (killer instanceof LivingEntity entity) {
75
			int level = EnchantmentHelper.getEnchantmentLevel(this.enchantment, entity);
76
			if (level == 0) {
77
				return itemStack;
78
			}
79
80
			float addition = level * this.count.value().getFloat(context);
81
			itemStack.grow(Math.round(addition));
82
			if (this.hasLimit()) {
83
				itemStack.limitSize(this.limit);
84
			}
85
		}
86
87
		return itemStack;
88
	}
89
90
	public static EnchantedCountIncreaseFunction.Builder lootingMultiplier(final HolderGetter<Enchantment> enchantments, final Holder<ContextFloatProvider> count) {
91
		return new EnchantedCountIncreaseFunction.Builder(enchantments.getOrThrow(Enchantments.LOOTING), count);
92
	}
93
94
	public static class Builder extends LootItemConditionalFunction.Builder<EnchantedCountIncreaseFunction.Builder> {
95
		private final Holder<Enchantment> enchantment;
96
		private final Holder<ContextFloatProvider> count;
97
		private int limit = 0;
98
99
		public Builder(final Holder<Enchantment> enchantment, final Holder<ContextFloatProvider> count) {
100
			this.enchantment = enchantment;
101
			this.count = count;
102
		}
103
104
		protected EnchantedCountIncreaseFunction.Builder getThis() {
105
			return this;
106
		}
107
108
		public EnchantedCountIncreaseFunction.Builder setLimit(final int limit) {
109
			this.limit = limit;
110
			return this;
111
		}
112
113
		@Override
114
		public LootItemFunction build() {
115
			return new EnchantedCountIncreaseFunction(this.getCondition(), this.enchantment, this.count, this.limit);
116
		}
117
	}
118
}
119

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