From bc6baadaee9c0507bb33f50ad67fb6b7d7a5e055 Mon Sep 17 00:00:00 2001 From: Nel <57587152+nelind3@users.noreply.github.com> Date: Thu, 14 Nov 2024 20:53:25 +0100 Subject: [PATCH] Replace RunAroundLikeCrazyGoalMixin::tick overwrite with injects (#4151) --- .../ai/goal/RunAroundLikeCrazyGoalMixin.java | 78 ++++++++----------- 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/src/mixins/java/org/spongepowered/common/mixin/core/world/entity/ai/goal/RunAroundLikeCrazyGoalMixin.java b/src/mixins/java/org/spongepowered/common/mixin/core/world/entity/ai/goal/RunAroundLikeCrazyGoalMixin.java index 82e86c1939a..f9055bde296 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/core/world/entity/ai/goal/RunAroundLikeCrazyGoalMixin.java +++ b/src/mixins/java/org/spongepowered/common/mixin/core/world/entity/ai/goal/RunAroundLikeCrazyGoalMixin.java @@ -24,19 +24,18 @@ */ package org.spongepowered.common.mixin.core.world.entity.ai.goal; -import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.ai.goal.RunAroundLikeCrazyGoal; import net.minecraft.world.entity.animal.horse.AbstractHorse; -import net.minecraft.world.entity.player.Player; import org.spongepowered.api.entity.living.animal.horse.HorseLike; import org.spongepowered.api.event.CauseStackManager; import org.spongepowered.api.event.SpongeEventFactory; import org.spongepowered.api.event.cause.entity.DismountTypes; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Mutable; -import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.common.SpongeCommon; import org.spongepowered.common.bridge.world.entity.EntityBridge; import org.spongepowered.common.event.tracking.PhaseTracker; @@ -45,51 +44,40 @@ public abstract class RunAroundLikeCrazyGoalMixin extends GoalMixin { // @formatter:off - @Shadow @Final @Mutable private AbstractHorse horse; + @Shadow @Final private AbstractHorse horse; // @formatter:on - /** - * @author rexbut - December 16th, 2016 - * @author i509VCB - February 18th, 2020 - 1.14.4 - * - * @reason - adjusted to support {@link DismountTypes} - */ - @Overwrite - public void tick() { - if (!this.horse.isTamed() && this.horse.getRandom().nextInt(50) == 0) { - Entity entity = this.horse.getPassengers().get(0); - - if (entity == null) { - return; - } - - if (entity instanceof Player) { - int i = this.horse.getTemper(); - int j = this.horse.getMaxTemper(); - - if (j > 0 && this.horse.getRandom().nextInt(j) < i) { - // Sponge start - Fire Tame Entity event - try (CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) { - frame.pushCause(entity); - if (SpongeCommon.post(SpongeEventFactory.createTameEntityEvent(frame.currentCause(), (HorseLike) this.horse))) { - return; - } - } - // Sponge end - this.horse.tameWithName((Player)entity); - return; - } - - this.horse.modifyTemper(5); + @Inject( + method = "tick", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/world/entity/animal/horse/AbstractHorse;tameWithName(Lnet/minecraft/world/entity/player/Player;)Z" + ), + cancellable = true + ) + private void impl$throwTameEntityEvent(final CallbackInfo ci) { + try (CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) { + frame.pushCause(this.horse.getFirstPassenger()); + if (SpongeCommon.post(SpongeEventFactory.createTameEntityEvent(frame.currentCause(), (HorseLike) this.horse))) { + ci.cancel(); } + } + } - // Sponge start - Throw an event before calling entity states - // this.horseHost.ejectPassengers(); // Vanilla - if (((EntityBridge) this.horse).bridge$removePassengers(DismountTypes.DERAIL.get())) { - // Sponge end - this.horse.makeMad(); - this.horse.level().broadcastEntityEvent(this.horse, (byte)6); - } + @Inject( + method = "tick", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/world/entity/animal/horse/AbstractHorse;ejectPassengers()V" + ), + cancellable = true + ) + private void impl$handleDismountTypes(final CallbackInfo ci) { + if (((EntityBridge) this.horse).bridge$removePassengers(DismountTypes.DERAIL.get())) { + this.horse.makeMad(); + this.horse.level().broadcastEntityEvent(this.horse, (byte)6); } + + ci.cancel(); } }