Skip to content

Commit

Permalink
Switch to @ModifyVariable to produce better bytecode
Browse files Browse the repository at this point in the history
- @Inject + @WrapOperation worked but the output was pretty bad, this change does what it used to but in a cleaner way
  • Loading branch information
IThundxr committed Mar 31, 2024
1 parent f06fd2e commit b234eb3
Showing 1 changed file with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
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, @Share("changeTargetEvent") LocalRef<ChangeTargetEvent> 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"))
Expand Down

0 comments on commit b234eb3

Please sign in to comment.