package com.thebrokenrail.energonrelics.mixin; import com.thebrokenrail.energonrelics.potion.CustomPotions; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.attribute.EntityAttribute; import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.entity.effect.StatusEffect; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(LivingEntity.class) public abstract class MixinLivingEntity { @Unique private static final TrackedData HAS_VERIDIUM_POISON = DataTracker.registerData(LivingEntity.class, TrackedDataHandlerRegistry.BOOLEAN); @Shadow public abstract boolean hasStatusEffect(StatusEffect effect); @Shadow public abstract void setHealth(float health); @Shadow public abstract float getHealth(); @Inject(at = @At("RETURN"), method = "initDataTracker") public void initDataTracker(CallbackInfo info) { ((LivingEntity) (Object) this).getDataTracker().startTracking(HAS_VERIDIUM_POISON, false); } @Inject(at = @At("RETURN"), method = "onTrackedDataSet") public void onTrackedDataSet(TrackedData data, CallbackInfo info) { if (HAS_VERIDIUM_POISON.equals(data)) { setHealth(getHealth()); } } @Inject(at = @At("HEAD"), method = "tick") public void tick(CallbackInfo info) { if (!((LivingEntity) (Object) this).getEntityWorld().isClient()) { boolean hasVeridiumPoison = hasStatusEffect(CustomPotions.VERIDIUM_POISON_EFFECT.statusEffect); if (((LivingEntity) (Object) this).getDataTracker().get(HAS_VERIDIUM_POISON) != hasVeridiumPoison) { ((LivingEntity) (Object) this).getDataTracker().set(HAS_VERIDIUM_POISON, hasVeridiumPoison); } } } @Inject(at = @At("HEAD"), method = "getAttributeValue", cancellable = true) public void getAttributeValue(EntityAttribute attribute, CallbackInfoReturnable info) { if (EntityAttributes.GENERIC_MAX_HEALTH.equals(attribute) && ((LivingEntity) (Object) this).getDataTracker().get(HAS_VERIDIUM_POISON)) { info.setReturnValue(0.5d); } } }