Skip to content

Commit

Permalink
Switch to @share
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Mar 31, 2024
1 parent af7816f commit f06fd2e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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;

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;
Expand All @@ -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(
Expand All @@ -37,11 +36,10 @@ public class StartAttackingMixin {
),
cancellable = true
)
private static <E extends Mob> void port_lib$onChangeTarget(Predicate<E> predicate, Function<E, Optional<? extends LivingEntity>> function, MemoryAccessor<Const.Mu<Unit>, LivingEntity> memoryAccessor, MemoryAccessor<OptionalBox.Mu, Long> memoryAccessor2, ServerLevel serverLevel, Mob mob, long l, CallbackInfoReturnable<Boolean> 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 <E extends Mob> void port_lib$onChangeTarget(Predicate<E> predicate, Function<E, Optional<? extends LivingEntity>> function, MemoryAccessor<Const.Mu<Unit>, LivingEntity> memoryAccessor, MemoryAccessor<OptionalBox.Mu, Long> memoryAccessor2, ServerLevel serverLevel, Mob mob, long l, CallbackInfoReturnable<Boolean> cir, @Local LivingEntity livingEntity, @Share("changeTargetEvent") LocalRef<ChangeTargetEvent> changeTargetEvent) {
changeTargetEvent.set(new ChangeTargetEvent(mob, livingEntity, LivingTargetType.BEHAVIOR_TARGET));
changeTargetEvent.get().sendEvent();
if (changeTargetEvent.get().isCanceled())
cir.setReturnValue(false);
}

Expand All @@ -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<Const.Mu<Unit>, LivingEntity> instance, Object object, Operation<Void> original) {
original.call(instance, port_lib$changeTargetEvent.getNewTarget());
private static void port_lib$wrapToChangeTarget(MemoryAccessor<Const.Mu<Unit>, LivingEntity> instance, Object object, Operation<Void> original, @Share("changeTargetEvent") LocalRef<ChangeTargetEvent> changeTargetEvent) {
original.call(instance, changeTargetEvent.get().getNewTarget());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
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<Void> original) {
original.call(instance, port_lib$changeTargetEvent.getNewTarget());
private void port_lib$wrapSetTarget(Mob instance, LivingEntity value, Operation<Void> original, @Share("changeTargetEvent") LocalRef<ChangeTargetEvent> changeTargetEvent) {
original.call(instance, changeTargetEvent.get().getNewTarget());
}

@Inject(method = "setTarget", at = @At("TAIL"))
Expand Down

0 comments on commit f06fd2e

Please sign in to comment.