|
| 1 | +package org.example.module; |
| 2 | + |
| 3 | +import com.github.rfresh2.EventConsumer; |
| 4 | +import com.zenith.event.client.ClientBotTick; |
| 5 | +import com.zenith.feature.pathfinder.goals.GoalXZ; |
| 6 | +import com.zenith.module.api.Module; |
| 7 | +import com.zenith.util.math.MathHelper; |
| 8 | +import com.zenith.util.timer.Timer; |
| 9 | +import com.zenith.util.timer.Timers; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | +import java.util.concurrent.ThreadLocalRandom; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +import static com.github.rfresh2.EventConsumer.of; |
| 16 | +import static com.zenith.Globals.BARITONE; |
| 17 | +import static com.zenith.Globals.CACHE; |
| 18 | +import static org.example.ExamplePlugin.PLUGIN_CONFIG; |
| 19 | + |
| 20 | +public class ExampleWanderModule extends Module { |
| 21 | + private final Timer pathTimer = Timers.tickTimer(); |
| 22 | + private long lastPathTime = 0L; |
| 23 | + private long lastStuckWarning = 0L; |
| 24 | + public GoalXZ goal = new GoalXZ(0, 0); |
| 25 | + |
| 26 | + @Override |
| 27 | + public List<EventConsumer<?>> registerEvents() { |
| 28 | + return List.of( |
| 29 | + of(ClientBotTick.class, this::handleBotTick), |
| 30 | + of(ClientBotTick.Starting.class, this::handleBotTickStarting) |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public boolean enabledSetting() { |
| 36 | + return PLUGIN_CONFIG.wander.enabled; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void onDisable() { |
| 41 | + if (BARITONE.isGoalActive(goal)) { |
| 42 | + debug("Stopping active pathing goal"); |
| 43 | + BARITONE.stop(); |
| 44 | + } |
| 45 | + lastPathTime = 0L; |
| 46 | + lastStuckWarning = 0L; |
| 47 | + } |
| 48 | + |
| 49 | + private void handleBotTickStarting(ClientBotTick.Starting event) { |
| 50 | + lastPathTime = 0L; |
| 51 | + lastStuckWarning = 0L; |
| 52 | + } |
| 53 | + |
| 54 | + private void handleBotTick(ClientBotTick event) { |
| 55 | + if (!BARITONE.isActive() && pathTimer.tick(20L)) { |
| 56 | + if (System.currentTimeMillis() - lastPathTime < TimeUnit.MINUTES.toMillis(1)) { |
| 57 | + if (System.currentTimeMillis() - lastStuckWarning > TimeUnit.MINUTES.toMillis(5)) { |
| 58 | + warn("we are likely stuck :("); |
| 59 | + lastStuckWarning = System.currentTimeMillis(); |
| 60 | + } |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + int currentX = MathHelper.floorI(CACHE.getPlayerCache().getX()); |
| 65 | + int currentZ = MathHelper.floorI(CACHE.getPlayerCache().getZ()); |
| 66 | + int radius = PLUGIN_CONFIG.wander.radius; |
| 67 | + int minRadius = PLUGIN_CONFIG.wander.minRadius; |
| 68 | + int bound = radius - minRadius; |
| 69 | + int goalX = ThreadLocalRandom.current().nextInt(currentX - bound, currentX + bound); |
| 70 | + // shift goalX to be within the bounds of the active radius (area between radius and minRadius) |
| 71 | + goalX += goalX < currentX ? -minRadius : minRadius; |
| 72 | + int goalZ = ThreadLocalRandom.current().nextInt(currentZ - bound, currentZ + bound); |
| 73 | + goalZ += goalZ < currentZ ? -minRadius : minRadius; |
| 74 | + goal = new GoalXZ(goalX, goalZ); |
| 75 | + info("Pathing to goal: [{}, {}]", goalX, goalZ); |
| 76 | + BARITONE.pathTo(goal).addExecutedListener(f -> { |
| 77 | + info("Reached wander goal! [{}, {}]", goal.x(), goal.z()); |
| 78 | + pathTimer.skip(); |
| 79 | + }); |
| 80 | + lastPathTime = System.currentTimeMillis(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments