From 8ea6ebf4a7050d4b5734915ad023d61a40f158b8 Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Mon, 2 Oct 2023 09:23:22 -0400 Subject: [PATCH] close #4259 --- .../vazkii/quark/mixin/TemptGoalMixin.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/vazkii/quark/mixin/TemptGoalMixin.java b/src/main/java/vazkii/quark/mixin/TemptGoalMixin.java index 7735930f1d..3eec34087c 100644 --- a/src/main/java/vazkii/quark/mixin/TemptGoalMixin.java +++ b/src/main/java/vazkii/quark/mixin/TemptGoalMixin.java @@ -1,16 +1,17 @@ package vazkii.quark.mixin; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.entity.PathfinderMob; +import net.minecraft.world.entity.ai.goal.TemptGoal; +import net.minecraft.world.entity.player.Player; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import net.minecraft.server.level.ServerLevel; -import net.minecraft.world.entity.PathfinderMob; -import net.minecraft.world.entity.ai.goal.TemptGoal; -import net.minecraft.world.entity.player.Player; import vazkii.quark.content.automation.module.FeedingTroughModule; @Mixin(TemptGoal.class) @@ -23,9 +24,23 @@ public class TemptGoalMixin { @Final public PathfinderMob mob; + @Unique + private long nextScheduledStart; + + private static final int RATE = 20; + @Inject(method = "canUse", at = @At(value = "FIELD", target = "Lnet/minecraft/world/entity/ai/goal/TemptGoal;player:Lnet/minecraft/world/entity/player/Player;", ordinal = 0, shift = At.Shift.AFTER)) private void findTroughs(CallbackInfoReturnable callbackInfoReturnable) { - if (mob.level instanceof ServerLevel level) - player = FeedingTroughModule.temptWithTroughs((TemptGoal) (Object) this, player, level); + if (mob.level instanceof ServerLevel level) { + if (nextScheduledStart == 0L) { + nextScheduledStart = level.getGameTime() + level.random.nextInt(RATE); + } else if (level.getGameTime() >= nextScheduledStart) + player = FeedingTroughModule.temptWithTroughs((TemptGoal) (Object) this, player, level); + } + } + + @Inject(method = "start", at = @At(value = "HEAD")) + private void updateSchedule(CallbackInfo ci) { + nextScheduledStart = mob.level.getGameTime() + mob.level.random.nextInt(RATE); } }