| 1 | package net.minecraft.world.item.enchantment.effects;
|
| 2 |
|
| 3 | import com.google.common.collect.HashMultimap;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.util.StringRepresentable;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.EquipmentSlot;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.ai.attributes.Attribute;
|
| 14 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 15 | import net.minecraft.world.item.enchantment.EnchantedItemInUse;
|
| 16 | import net.minecraft.world.item.enchantment.LevelBasedValue;
|
| 17 | import net.minecraft.world.phys.Vec3;
|
| 18 |
|
| 19 | public record EnchantmentAttributeEffect(Identifier id, Holder<Attribute> attribute, LevelBasedValue amount, AttributeModifier.Operation operation)
|
| 20 | implements EnchantmentLocationBasedEffect {
|
| 21 | public static final MapCodec<EnchantmentAttributeEffect> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 22 | i -> i.group(
|
| 23 | Identifier.CODEC.fieldOf("id").forGetter(EnchantmentAttributeEffect::id),
|
| 24 | Attribute.CODEC.fieldOf("attribute").forGetter(EnchantmentAttributeEffect::attribute),
|
| 25 | LevelBasedValue.CODEC.fieldOf("amount").forGetter(EnchantmentAttributeEffect::amount),
|
| 26 | AttributeModifier.Operation.CODEC.fieldOf("operation").forGetter(EnchantmentAttributeEffect::operation)
|
| 27 | )
|
| 28 | .apply(i, EnchantmentAttributeEffect::new)
|
| 29 | );
|
| 30 |
|
| 31 | private Identifier idForSlot(final StringRepresentable slot) {
|
| 32 | return this.id.withSuffix("/" + slot.getSerializedName());
|
| 33 | }
|
| 34 |
|
| 35 | public AttributeModifier getModifier(final int level, final StringRepresentable slot) {
|
| 36 | return new AttributeModifier(this.idForSlot(slot), this.amount().calculate(level), this.operation());
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public void onChangedBlock(
|
| 41 | final ServerLevel serverLevel,
|
| 42 | final int enchantmentLevel,
|
| 43 | final EnchantedItemInUse item,
|
| 44 | final Entity entity,
|
| 45 | final Vec3 position,
|
| 46 | final boolean becameActive
|
| 47 | ) {
|
| 48 | if (becameActive && entity instanceof LivingEntity living) {
|
| 49 | living.getAttributes().addTransientAttributeModifiers(this.makeAttributeMap(enchantmentLevel, item.inSlot()));
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public void onDeactivated(final EnchantedItemInUse item, final Entity entity, final Vec3 position, final int level) {
|
| 55 | if (entity instanceof LivingEntity living) {
|
| 56 | living.getAttributes().removeAttributeModifiers(this.makeAttributeMap(level, item.inSlot()));
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | private HashMultimap<Holder<Attribute>, AttributeModifier> makeAttributeMap(final int enchantmentLevel, final EquipmentSlot slot) {
|
| 61 | HashMultimap<Holder<Attribute>, AttributeModifier> map = HashMultimap.create();
|
| 62 | map.put(this.attribute, this.getModifier(enchantmentLevel, slot));
|
| 63 | return map;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public MapCodec<EnchantmentAttributeEffect> codec() {
|
| 68 | return MAP_CODEC;
|
| 69 | }
|
| 70 | }
|
| 71 |
|