From fddfd3bffe0cf3c0a15ab77a7bef9f73b5461c32 Mon Sep 17 00:00:00 2001 From: IThundxr Date: Sun, 31 Mar 2024 15:22:10 -0400 Subject: [PATCH] Properly set the field --- .../porting_lib/mixin/common/MobMixin.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/MobMixin.java b/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/MobMixin.java index 085c9861..a3b5fbe2 100644 --- a/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/MobMixin.java +++ b/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/MobMixin.java @@ -1,10 +1,14 @@ package io.github.fabricators_of_create.porting_lib.mixin.common; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; + import io.github.fabricators_of_create.porting_lib.event.common.LivingEntityEvents; import io.github.fabricators_of_create.porting_lib.event.common.LivingEntityEvents.ChangeTarget.ChangeTargetEvent; import io.github.fabricators_of_create.porting_lib.event.common.LivingEntityEvents.ChangeTarget.ChangeTargetEvent.LivingTargetType; import org.spongepowered.asm.mixin.Mixin; +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; @@ -16,14 +20,21 @@ @Mixin(Mob.class) public abstract class MobMixin { + @Unique private ChangeTargetEvent port_lib$changeTargetEvent; + @Inject(method = "setTarget", at = @At("HEAD"), cancellable = true) private void port_lib$onChangeTarget(LivingEntity target, CallbackInfo ci) { - ChangeTargetEvent event = new LivingEntityEvents.ChangeTarget.ChangeTargetEvent((Mob) (Object) this, target, LivingTargetType.MOB_TARGET); - event.sendEvent(); - if (event.isCanceled()) + port_lib$changeTargetEvent = new LivingEntityEvents.ChangeTarget.ChangeTargetEvent((Mob) (Object) this, target, LivingTargetType.MOB_TARGET); + port_lib$changeTargetEvent.sendEvent(); + if (port_lib$changeTargetEvent.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) { + original.call(instance, port_lib$changeTargetEvent.getNewTarget()); + } + @Inject(method = "setTarget", at = @At("TAIL")) private void port_lib$setTarget(LivingEntity target, CallbackInfo ci) { MobEntitySetTargetCallback.EVENT.invoker().onMobEntitySetTarget((Mob) (Object) this, target);