From f06fd2e0590f47e4c2a7320ae94adeef38e45bcd Mon Sep 17 00:00:00 2001 From: IThundxr Date: Sun, 31 Mar 2024 15:30:53 -0400 Subject: [PATCH] Switch to @Share --- .../mixin/common/StartAttackingMixin.java | 18 ++++++++---------- .../entity/mixin/common/MobMixin.java | 17 ++++++++--------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/StartAttackingMixin.java b/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/StartAttackingMixin.java index afa5eddc..9926c6fb 100644 --- a/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/StartAttackingMixin.java +++ b/modules/base/src/main/java/io/github/fabricators_of_create/porting_lib/mixin/common/StartAttackingMixin.java @@ -5,7 +5,6 @@ import java.util.function.Predicate; 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.CallbackInfoReturnable; @@ -13,6 +12,8 @@ import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import com.llamalad7.mixinextras.sugar.Local; +import com.llamalad7.mixinextras.sugar.Share; +import com.llamalad7.mixinextras.sugar.ref.LocalRef; import com.mojang.datafixers.kinds.Const; import com.mojang.datafixers.kinds.OptionalBox; import com.mojang.datafixers.util.Unit; @@ -27,8 +28,6 @@ @Mixin(StartAttacking.class) public class StartAttackingMixin { - @Unique private static ChangeTargetEvent port_lib$changeTargetEvent; - @Inject( method = "method_47123(Ljava/util/function/Predicate;Ljava/util/function/Function;Lnet/minecraft/world/entity/ai/behavior/declarative/MemoryAccessor;Lnet/minecraft/world/entity/ai/behavior/declarative/MemoryAccessor;Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/entity/Mob;J)Z", at = @At( @@ -37,11 +36,10 @@ public class StartAttackingMixin { ), cancellable = true ) - private static void port_lib$onChangeTarget(Predicate predicate, Function> function, MemoryAccessor, LivingEntity> memoryAccessor, MemoryAccessor memoryAccessor2, ServerLevel serverLevel, Mob mob, long l, CallbackInfoReturnable cir, @Local LivingEntity livingEntity) { - port_lib$changeTargetEvent = new ChangeTargetEvent(mob, livingEntity, LivingTargetType.BEHAVIOR_TARGET); - port_lib$changeTargetEvent.sendEvent(); - - if (port_lib$changeTargetEvent.isCanceled()) + private static void port_lib$onChangeTarget(Predicate predicate, Function> function, MemoryAccessor, LivingEntity> memoryAccessor, MemoryAccessor memoryAccessor2, ServerLevel serverLevel, Mob mob, long l, CallbackInfoReturnable cir, @Local LivingEntity livingEntity, @Share("changeTargetEvent") LocalRef changeTargetEvent) { + changeTargetEvent.set(new ChangeTargetEvent(mob, livingEntity, LivingTargetType.BEHAVIOR_TARGET)); + changeTargetEvent.get().sendEvent(); + if (changeTargetEvent.get().isCanceled()) cir.setReturnValue(false); } @@ -52,7 +50,7 @@ public class StartAttackingMixin { target = "Lnet/minecraft/world/entity/ai/behavior/declarative/MemoryAccessor;set(Ljava/lang/Object;)V" ) ) - private static void port_lib$wrapToChangeTarget(MemoryAccessor, LivingEntity> instance, Object object, Operation original) { - original.call(instance, port_lib$changeTargetEvent.getNewTarget()); + private static void port_lib$wrapToChangeTarget(MemoryAccessor, LivingEntity> instance, Object object, Operation original, @Share("changeTargetEvent") LocalRef changeTargetEvent) { + original.call(instance, changeTargetEvent.get().getNewTarget()); } } 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 a05ea031..f2c0762a 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 @@ -1,13 +1,14 @@ package io.github.fabricators_of_create.porting_lib.entity.mixin.common; 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; 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; @@ -17,19 +18,17 @@ @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) { - port_lib$changeTargetEvent = new ChangeTargetEvent((Mob) (Object) this, target, LivingTargetType.MOB_TARGET); - port_lib$changeTargetEvent.sendEvent(); - if (port_lib$changeTargetEvent.isCanceled()) + 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) { - original.call(instance, port_lib$changeTargetEvent.getNewTarget()); + private void port_lib$wrapSetTarget(Mob instance, LivingEntity value, Operation original, @Share("changeTargetEvent") LocalRef changeTargetEvent) { + original.call(instance, changeTargetEvent.get().getNewTarget()); } @Inject(method = "setTarget", at = @At("TAIL"))