EnchantmentsByCostWithDifficulty .java

net.minecraft.world.item.enchantment.providers.EnchantmentsByCostWithDifficulty (record): members: enchantments, minCost, maxCostSpan, enchant, codec.

net.minecraft.world.item.enchantment.providersrecord44 linescommon2.1 KB

Implements

API Surface (2)

void enchant(final ItemStack item, final ItemEnchantments.Mutable itemEnchantments, final RandomSource random, final DifficultyInstance difficulty)
MapCodec<EnchantmentsByCostWithDifficulty> codec()

Fields & Constants (2)

MAX_ALLOWED_VALUE_PART
CODEC

String literals (3)

"enchantments"
"min_cost"
"max_cost_span"

Full Source

EnchantmentsByCostWithDifficulty.java44 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.enchantment.providers;
2
3
import com.mojang.serialization.MapCodec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import net.minecraft.core.HolderSet;
6
import net.minecraft.core.registries.Registries;
7
import net.minecraft.core.registries.codec.RegistryCodecs;
8
import net.minecraft.util.ExtraCodecs;
9
import net.minecraft.util.Mth;
10
import net.minecraft.util.RandomSource;
11
import net.minecraft.world.DifficultyInstance;
12
import net.minecraft.world.item.ItemStack;
13
import net.minecraft.world.item.enchantment.Enchantment;
14
import net.minecraft.world.item.enchantment.EnchantmentHelper;
15
import net.minecraft.world.item.enchantment.EnchantmentInstance;
16
import net.minecraft.world.item.enchantment.ItemEnchantments;
17
18
public record EnchantmentsByCostWithDifficulty(HolderSet<Enchantment> enchantments, int minCost, int maxCostSpan) implements EnchantmentProvider {
19
	public static final int MAX_ALLOWED_VALUE_PART = 10000;
20
	public static final MapCodec<EnchantmentsByCostWithDifficulty> CODEC = RecordCodecBuilder.mapCodec(
21
		i -> i.group(
22
				RegistryCodecs.holderSet(Registries.ENCHANTMENT).fieldOf("enchantments").forGetter(EnchantmentsByCostWithDifficulty::enchantments),
23
				ExtraCodecs.intRange(1, 10000).fieldOf("min_cost").forGetter(EnchantmentsByCostWithDifficulty::minCost),
24
				ExtraCodecs.intRange(0, 10000).fieldOf("max_cost_span").forGetter(EnchantmentsByCostWithDifficulty::maxCostSpan)
25
			)
26
			.apply(i, EnchantmentsByCostWithDifficulty::new)
27
	);
28
29
	@Override
30
	public void enchant(final ItemStack item, final ItemEnchantments.Mutable itemEnchantments, final RandomSource random, final DifficultyInstance difficulty) {
31
		float difficultyModifier = difficulty.getSpecialMultiplier();
32
		int cost = Mth.randomBetweenInclusive(random, this.minCost, this.minCost + (int)(difficultyModifier * this.maxCostSpan));
33
34
		for (EnchantmentInstance instance : EnchantmentHelper.selectEnchantment(random, item, cost, this.enchantments.stream())) {
35
			itemEnchantments.upgrade(instance.enchantment(), instance.level());
36
		}
37
	}
38
39
	@Override
40
	public MapCodec<EnchantmentsByCostWithDifficulty> codec() {
41
		return CODEC;
42
	}
43
}
44

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