From b234eb322f449c0b15b39066239e547362c505b7 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Sun, 31 Mar 2024 19:57:13 -0400 Subject: [PATCH] Switch to @ModifyVariable to produce better bytecode - @Inject + @WrapOperation worked but the output was pretty bad, this change does what it used to but in a cleaner way --- .../entity/mixin/common/MobMixin.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/mixin/common/MobMixin.java b/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/mixin/common/MobMixin.java index f2c0762a..89afd44a 100644 --- a/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/mixin/common/MobMixin.java +++ b/modules/entity/src/main/java/io/github/fabricators_of_create/porting_lib/entity/mixin/common/MobMixin.java @@ -3,13 +3,9 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyVariable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import com.llamalad7.mixinextras.sugar.Share; -import com.llamalad7.mixinextras.sugar.ref.LocalRef; - import io.github.fabricators_of_create.porting_lib.entity.events.LivingEntityEvents.ChangeTarget.ChangeTargetEvent; import io.github.fabricators_of_create.porting_lib.entity.events.LivingEntityEvents.ChangeTarget.ChangeTargetEvent.LivingTargetType; import io.github.fabricators_of_create.porting_lib.entity.events.MobEntitySetTargetCallback; @@ -18,17 +14,13 @@ @Mixin(Mob.class) public abstract class MobMixin { - @Inject(method = "setTarget", at = @At("HEAD"), cancellable = true) - private void port_lib$onChangeTarget(LivingEntity target, CallbackInfo ci, @Share("changeTargetEvent") LocalRef changeTargetEvent) { - changeTargetEvent.set(new ChangeTargetEvent((Mob) (Object) this, target, LivingTargetType.MOB_TARGET)); - changeTargetEvent.get().sendEvent(); - if (changeTargetEvent.get().isCanceled()) - ci.cancel(); - } - - @WrapOperation(method = "setTarget", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/Mob;target:Lnet/minecraft/world/entity/LivingEntity;")) - private void port_lib$wrapSetTarget(Mob instance, LivingEntity value, Operation original, @Share("changeTargetEvent") LocalRef changeTargetEvent) { - original.call(instance, changeTargetEvent.get().getNewTarget()); + @ModifyVariable(method = "setTarget", at = @At("HEAD"), argsOnly = true) + private LivingEntity port_lib$onChangeTarget(LivingEntity value) { + ChangeTargetEvent changeTargetEvent = new ChangeTargetEvent((Mob) (Object) this, value, LivingTargetType.MOB_TARGET); + changeTargetEvent.sendEvent(); + if (changeTargetEvent.isCanceled()) + return null; + return changeTargetEvent.getNewTarget(); } @Inject(method = "setTarget", at = @At("TAIL"))