| 1 | package net.minecraft.world.level.storage.loot.predicates;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.HolderGetter;
|
| 9 | import net.minecraft.util.context.ContextKey;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.LivingEntity;
|
| 12 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 13 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 14 | import net.minecraft.world.item.enchantment.Enchantments;
|
| 15 | import net.minecraft.world.item.enchantment.LevelBasedValue;
|
| 16 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 17 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 18 |
|
| 19 | public record LootItemRandomChanceWithEnchantedBonusCondition(float unenchantedChance, LevelBasedValue enchantedChance, Holder<Enchantment> enchantment)
|
| 20 | implements LootItemCondition {
|
| 21 | public static final MapCodec<LootItemRandomChanceWithEnchantedBonusCondition> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 22 | i -> i.group(
|
| 23 | Codec.floatRange(0.0F, 1.0F).fieldOf("unenchanted_chance").forGetter(LootItemRandomChanceWithEnchantedBonusCondition::unenchantedChance),
|
| 24 | LevelBasedValue.CODEC.fieldOf("enchanted_chance").forGetter(LootItemRandomChanceWithEnchantedBonusCondition::enchantedChance),
|
| 25 | Enchantment.CODEC.fieldOf("enchantment").forGetter(LootItemRandomChanceWithEnchantedBonusCondition::enchantment)
|
| 26 | )
|
| 27 | .apply(i, LootItemRandomChanceWithEnchantedBonusCondition::new)
|
| 28 | );
|
| 29 |
|
| 30 | @Override
|
| 31 | public MapCodec<LootItemRandomChanceWithEnchantedBonusCondition> codec() {
|
| 32 | return MAP_CODEC;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 37 | return Set.of(LootContextParams.ATTACKING_ENTITY);
|
| 38 | }
|
| 39 |
|
| 40 | public boolean test(final LootContext context) {
|
| 41 | Entity killerEntity = context.getOptional(LootContextParams.ATTACKING_ENTITY);
|
| 42 | int enchantmentLevel = killerEntity instanceof LivingEntity livingKiller ? EnchantmentHelper.getEnchantmentLevel(this.enchantment, livingKiller) : 0;
|
| 43 | float chance = enchantmentLevel > 0 ? this.enchantedChance.calculate(enchantmentLevel) : this.unenchantedChance;
|
| 44 | return context.getRandom().nextFloat() < chance;
|
| 45 | }
|
| 46 |
|
| 47 | public static LootItemCondition.Builder randomChanceAndLootingBoost(
|
| 48 | final HolderGetter<Enchantment> enchantments, final float chance, final float perEnchantmentLevel
|
| 49 | ) {
|
| 50 | return () -> new LootItemRandomChanceWithEnchantedBonusCondition(
|
| 51 | chance, new LevelBasedValue.Linear(chance + perEnchantmentLevel, perEnchantmentLevel), enchantments.getOrThrow(Enchantments.LOOTING)
|
| 52 | );
|
| 53 | }
|
| 54 | }
|
| 55 |
|