|
| 1 | +package io.github.lounode.eventwrapper.fabric.mixin.eventposter.entity.living.shield_block_event; |
| 2 | + |
| 3 | +import com.llamalad7.mixinextras.injector.WrapWithCondition; |
| 4 | +import com.llamalad7.mixinextras.sugar.Local; |
| 5 | +import com.llamalad7.mixinextras.sugar.Share; |
| 6 | +import com.llamalad7.mixinextras.sugar.ref.LocalFloatRef; |
| 7 | +import com.llamalad7.mixinextras.sugar.ref.LocalRef; |
| 8 | + |
| 9 | +import net.minecraft.world.damagesource.DamageSource; |
| 10 | +import net.minecraft.world.entity.LivingEntity; |
| 11 | + |
| 12 | +import org.spongepowered.asm.mixin.Mixin; |
| 13 | +import org.spongepowered.asm.mixin.injection.At; |
| 14 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 15 | +import org.spongepowered.asm.mixin.injection.ModifyVariable; |
| 16 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 17 | + |
| 18 | + |
| 19 | +import io.github.lounode.eventwrapper.event.entity.living.ShieldBlockEventWrapper; |
| 20 | +import io.github.lounode.eventwrapper.fabric.EventWrapperHooks; |
| 21 | + |
| 22 | +@Mixin(LivingEntity.class) |
| 23 | +public class EventPosterLivingEntity { |
| 24 | + |
| 25 | + @Inject( |
| 26 | + method = "hurt", |
| 27 | + at = @At( |
| 28 | + value = "INVOKE", |
| 29 | + target = "Lnet/minecraft/world/entity/LivingEntity;isDamageSourceBlocked(Lnet/minecraft/world/damagesource/DamageSource;)Z", |
| 30 | + shift = At.Shift.AFTER |
| 31 | + ) |
| 32 | + ) |
| 33 | + private void onAfterTestBlock(DamageSource source, float amount, CallbackInfoReturnable<Boolean> cir, @Share("wrapper") LocalRef<ShieldBlockEventWrapper> wrapper) { |
| 34 | + var event = EventWrapperHooks.onShieldBlock((LivingEntity) (Object) this, source, amount); |
| 35 | + wrapper.set(event); |
| 36 | + } |
| 37 | + |
| 38 | + @WrapWithCondition( |
| 39 | + method = "hurt", |
| 40 | + at = @At( |
| 41 | + value = "INVOKE", |
| 42 | + target = "Lnet/minecraft/world/entity/LivingEntity;hurtCurrentlyUsedShield(F)V" |
| 43 | + ) |
| 44 | + ) |
| 45 | + private boolean onHurtCurrentlyUsedShield(LivingEntity instance, float damageAmount, @Share("wrapper") LocalRef<ShieldBlockEventWrapper> wrapper) { |
| 46 | + var event = wrapper.get(); |
| 47 | + if (event.isCanceled() || !event.shieldTakesDamage()) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + //BUG FEATURE: We ignore stats damage change, It's too hard and may course some issue |
| 55 | + |
| 56 | + @WrapWithCondition( |
| 57 | + method = "hurt", |
| 58 | + at = @At( |
| 59 | + value = "INVOKE", |
| 60 | + target = "Lnet/minecraft/world/entity/LivingEntity;blockUsingShield(Lnet/minecraft/world/entity/LivingEntity;)V" |
| 61 | + ) |
| 62 | + ) |
| 63 | + private boolean onNonProjectileBlocked(LivingEntity instance, LivingEntity attacker, @Local(argsOnly = true) DamageSource source, @Share("wrapper") LocalRef<ShieldBlockEventWrapper> wrapper) { |
| 64 | + var event = wrapper.get(); |
| 65 | + if (event.isCanceled()) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + @ModifyVariable( |
| 72 | + method = "hurt", |
| 73 | + at = @At( |
| 74 | + value = "STORE", |
| 75 | + ordinal = 1 |
| 76 | + ), |
| 77 | + ordinal = 0 |
| 78 | + ) |
| 79 | + private boolean modifyBlFlag(boolean original, @Local(argsOnly = true) LocalFloatRef amount, @Share("wrapper") LocalRef<ShieldBlockEventWrapper> wrapper) { |
| 80 | + var event = wrapper.get(); |
| 81 | + if (!event.isCanceled() && original) { |
| 82 | + float am = event.getOriginalBlockedDamage() - event.getBlockedDamage(); |
| 83 | + amount.set(am); |
| 84 | + return am <= 0; |
| 85 | + } |
| 86 | + return original; |
| 87 | + } |
| 88 | +} |
0 commit comments