Skip to content

Commit

Permalink
Replace RunAroundLikeCrazyGoalMixin::tick overwrite with injects (#4151)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelind3 authored Nov 14, 2024
1 parent 772c198 commit bc6baad
Showing 1 changed file with 33 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 comment has been minimized.

Copy link
@Zidane

Zidane Nov 14, 2024

Member

You wouldn't like me when I'm angry

this.horse.level().broadcastEntityEvent(this.horse, (byte)6);
}

ci.cancel();
}
}

0 comments on commit bc6baad

Please sign in to comment.