| 1 | package net.minecraft.world.item.component;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 7 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 | import net.minecraft.world.effect.MobEffectInstance;
|
| 10 | import net.minecraft.world.effect.MobEffects;
|
| 11 | import net.minecraft.world.entity.LivingEntity;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
|
| 14 | import net.minecraft.world.item.consume_effects.ClearAllStatusEffectsConsumeEffect;
|
| 15 | import net.minecraft.world.item.consume_effects.ConsumeEffect;
|
| 16 |
|
| 17 | public record DeathProtection(List<ConsumeEffect> deathEffects) {
|
| 18 | public static final Codec<DeathProtection> CODEC = RecordCodecBuilder.create(
|
| 19 | i -> i.group(ConsumeEffect.CODEC.listOf().optionalFieldOf("death_effects", List.of()).forGetter(DeathProtection::deathEffects))
|
| 20 | .apply(i, DeathProtection::new)
|
| 21 | );
|
| 22 | public static final StreamCodec<RegistryFriendlyByteBuf, DeathProtection> STREAM_CODEC = StreamCodec.composite(
|
| 23 | ConsumeEffect.STREAM_CODEC.apply(ByteBufCodecs.list()), DeathProtection::deathEffects, DeathProtection::new
|
| 24 | );
|
| 25 | public static final DeathProtection TOTEM_OF_UNDYING = new DeathProtection(
|
| 26 | List.of(
|
| 27 | new ClearAllStatusEffectsConsumeEffect(),
|
| 28 | new ApplyStatusEffectsConsumeEffect(
|
| 29 | List.of(
|
| 30 | new MobEffectInstance(MobEffects.REGENERATION, 900, 1),
|
| 31 | new MobEffectInstance(MobEffects.ABSORPTION, 100, 1),
|
| 32 | new MobEffectInstance(MobEffects.FIRE_RESISTANCE, 800, 0)
|
| 33 | )
|
| 34 | )
|
| 35 | )
|
| 36 | );
|
| 37 |
|
| 38 | public void applyEffects(final ItemStack itemStack, final LivingEntity entity) {
|
| 39 | for (ConsumeEffect effect : this.deathEffects) {
|
| 40 | effect.apply(entity.level(), itemStack, entity);
|
| 41 | }
|
| 42 | }
|
| 43 | }
|
| 44 |
|