This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathRouteNavigator.java
More file actions
391 lines (342 loc) · 14.7 KB
/
RouteNavigator.java
File metadata and controls
391 lines (342 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package com.jelly.mightyminerv2.feature.impl;
import com.jelly.mightyminerv2.config.MightyMinerConfig;
import com.jelly.mightyminerv2.event.PacketEvent;
import com.jelly.mightyminerv2.feature.AbstractFeature;
import com.jelly.mightyminerv2.handler.RotationHandler;
import com.jelly.mightyminerv2.macro.MacroManager;
import com.jelly.mightyminerv2.macro.impl.GlacialMacro.GlacialMacro;
import com.jelly.mightyminerv2.util.AngleUtil;
import com.jelly.mightyminerv2.util.BlockUtil;
import com.jelly.mightyminerv2.util.KeyBindUtil;
import com.jelly.mightyminerv2.util.PlayerUtil;
import com.jelly.mightyminerv2.util.helper.RotationConfiguration;
import com.jelly.mightyminerv2.util.helper.RotationConfiguration.RotationType;
import com.jelly.mightyminerv2.util.helper.Target;
import com.jelly.mightyminerv2.util.helper.route.Route;
import com.jelly.mightyminerv2.util.helper.route.RouteWaypoint;
import com.jelly.mightyminerv2.util.helper.route.WaypointType;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.init.Blocks;
import net.minecraft.network.play.server.S08PacketPlayerPosLook;
import net.minecraft.util.BlockPos;
import net.minecraft.util.Vec3;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
// This works under the assumption that the blocks between every node are clear and traversable.
// Checks to determine if the player can move between two nodes must be done beforehand; otherwise, it will cause bugs.
public class RouteNavigator extends AbstractFeature {
private static RouteNavigator instance;
private Route routeToFollow;
private int currentRouteIndex = -1;
private int targetRouteIndex = -1;
private State state = State.STARTING;
private boolean isQueued = false;
@Getter
private NavError navError = NavError.NONE;
@Setter
private RotationType rotationType = RotationType.CLIENT;
private Vec3 rotationTarget = null;
public static RouteNavigator getInstance() {
if (instance == null) {
instance = new RouteNavigator();
}
return instance;
}
@Override
public String getName() {
return "RouteNavigator";
}
@Override
public void resetStatesAfterStop() {
this.state = State.STARTING;
this.rotationType = RotationType.CLIENT;
KeyBindUtil.releaseAllExcept();
RotationHandler.getInstance().stop();
}
@Override
public boolean shouldNotCheckForFailsafe() {
return this.state == State.AOTV_VERIFY;
}
public void queueRoute(final Route routeToFollow) {
if (this.enabled) {
return;
}
this.routeToFollow = routeToFollow;
this.currentRouteIndex = -1;
this.targetRouteIndex = -1;
this.isQueued = true;
}
public void goTo(final int index) {
if (this.routeToFollow == null || this.routeToFollow.isEmpty()) {
sendError("No Route Was Selected or its empty.");
return;
}
this.targetRouteIndex = index;
this.currentRouteIndex = this.getCurrentIndex(PlayerUtil.getBlockStandingOn()) - 1;
this.normalizeIndices();
this.navError = NavError.NONE;
this.enabled = true;
this.start();
}
public void gotoNext() {
this.goTo(this.targetRouteIndex + 1);
}
public void start(final Route routeToFollow) {
this.routeToFollow = routeToFollow;
this.enabled = true;
this.targetRouteIndex = -1;
this.normalizeIndices();
this.currentRouteIndex = -1;
this.navError = NavError.NONE;
this.start();
send("Enabling RouteNavigator.");
}
public void pause() {
this.enabled = false;
this.timer.reset();
this.resetStatesAfterStop();
send("Pausing RouteNavigator");
}
@Override
public void stop() {
if (!this.enabled) {
return;
}
this.enabled = false;
this.isQueued = false;
this.routeToFollow = null;
this.timer.reset();
this.targetRouteIndex = -1;
this.currentRouteIndex = -1;
this.rotationTarget = null;
this.resetStatesAfterStop();
send("RouteNavigator Stopped");
}
public void stop(NavError error) {
this.navError = error;
this.stop();
}
private void normalizeIndices() {
this.targetRouteIndex = this.normalizeIndex(this.targetRouteIndex);
this.currentRouteIndex = this.normalizeIndex(this.currentRouteIndex);
if (this.targetRouteIndex < this.currentRouteIndex) {
this.targetRouteIndex += this.routeToFollow.size();
}
}
private int normalizeIndex(final int index) {
return (index + this.routeToFollow.size()) % this.routeToFollow.size();
}
private int getLookTime(final RouteWaypoint waypoint) {
if (this.rotationType == RotationType.SERVER) {
return MightyMinerConfig.delayAutoAotvServerRotation;
}
if (waypoint.getTransportMethod().ordinal() == 0) {
return MightyMinerConfig.delayAutoAotvLookDelay;
}
return MightyMinerConfig.delayAutoAotvEtherwarpLookDelay;
}
public int getCurrentIndex(final BlockPos playerBlock) {
int index = this.routeToFollow.indexOf(new RouteWaypoint(playerBlock, WaypointType.ETHERWARP));
if (index != -1) {
return index;
}
return this.routeToFollow.getClosest(playerBlock).map(routeWaypoint -> this.routeToFollow.indexOf(routeWaypoint)).orElse(-1);
}
public boolean succeeded() {
return !this.enabled && this.navError == NavError.NONE;
}
@SubscribeEvent
protected void onTick(ClientTickEvent event) {
if (!this.enabled) {
return;
}
if (mc.thePlayer == null || mc.theWorld == null) {
log("Player or World is null, stopping RouteNavigator.");
this.stop(NavError.NONE);
return;
}
switch (this.state) {
case STARTING: {
this.swapState(State.DETECT_ROUTE, 0);
break;
}
case DETECT_ROUTE: {
if (this.currentRouteIndex++ == this.targetRouteIndex) {
this.swapState(State.END_VERIFY, 0);
return;
}
if (this.routeToFollow.get(this.currentRouteIndex).getTransportMethod() == WaypointType.WALK) {
this.swapState(State.WALK, 0);
} else {
this.swapState(State.ROTATION, 0);
}
log("Going To Index: " + this.currentRouteIndex);
break;
}
case ROTATION: {
RouteWaypoint nextPoint = this.routeToFollow.get(this.currentRouteIndex);
if (nextPoint.getTransportMethod() == WaypointType.ETHERWARP) {
BlockPos block = nextPoint.toBlockPos();
if (mc.theWorld.getBlockState(block).getBlock() == Blocks.snow_layer) {
block = block.down();
}
this.rotationTarget = BlockUtil.getClosestVisibleSidePos(block);
} else this.rotationTarget = BlockUtil.getClosestVisibleSidePos(nextPoint.toBlockPos());
RotationConfiguration config = new RotationConfiguration(new Target(this.rotationTarget),
this.getLookTime(nextPoint),
this.rotationType,
null)
.followTarget(true);
RotationHandler.getInstance().easeTo(config);
this.swapState(State.ROTATION_VERIFY, 2000);
log("Rotating to " + this.rotationTarget);
break;
}
case ROTATION_VERIFY: {
if (this.timer.isScheduled() && this.timer.passed()) {
sendError("Could not look in time. Disabling.");
this.stop(NavError.TIME_FAIL);
return;
}
if (!AngleUtil.isLookingAt(this.rotationTarget, 0.5f) && !RotationHandler.getInstance().isFollowingTarget()) {
return;
}
System.out.println("IsLookingAt: " + AngleUtil.isLookingAtDebug(this.rotationTarget, 0.5f));
System.out.println("Following: " + RotationHandler.getInstance().isFollowingTarget());
int sneakTime = 0;
RouteWaypoint target = this.routeToFollow.get(this.currentRouteIndex);
if (target.getTransportMethod() == WaypointType.ETHERWARP && !mc.gameSettings.keyBindSneak.isKeyDown()) {
KeyBindUtil.setKeyBindState(mc.gameSettings.keyBindSneak, true);
sneakTime = 250;
}
this.swapState(State.AOTV, sneakTime);
break;
}
case AOTV: {
if (this.timer.isScheduled() && !this.timer.passed()) {
return;
}
// Todo: test Etherwarp
if (this.routeToFollow.get(this.currentRouteIndex + 1).getTransportMethod() != WaypointType.ETHERWARP) {
KeyBindUtil.setKeyBindState(mc.gameSettings.keyBindSneak, false);
}
KeyBindUtil.rightClick();
System.out.println("clicked");
this.swapState(State.AOTV_VERIFY, 2000);
break;
}
case AOTV_VERIFY: {
if (this.timer.isScheduled() && this.timer.passed()) {
// Check if the current macro is the GlacialMacro to see if we can walk to the target instead
if (MacroManager.getInstance().getCurrentMacro() instanceof GlacialMacro) {
sendError("Did not receive teleport packet in time. Attempting to walk to the target instead.");
this.swapState(State.WALK, 0);
} else {
// Keep the original behavior for all other macros.
sendError("Did not receive teleport packet in time. Disabling");
this.stop(NavError.TIME_FAIL);
}
return;
}
break;
}
case WALK:
BlockPos source = this.routeToFollow.get(this.currentRouteIndex).toBlockPos();
if (this.currentRouteIndex == 0) {
log("queued first");
BlockPos p = PlayerUtil.getBlockStandingOn();
Pathfinder.getInstance().queue(p, source);
}
if (this.currentRouteIndex + 1 <= this.targetRouteIndex) {
log("queued next");
RouteWaypoint target = this.routeToFollow.get(this.currentRouteIndex + 1);
Pathfinder.getInstance().queue(source, target.toBlockPos());
}
if (!Pathfinder.getInstance().isRunning()) {
log("Started");
Pathfinder.getInstance().setInterpolationState(true);
Pathfinder.getInstance().start();
}
this.swapState(State.WALK_VERIFY, 2000);
log("Walking");
break;
case WALK_VERIFY:
BlockPos targetPos = this.routeToFollow.get(this.currentRouteIndex).toBlockPos();
if (Pathfinder.getInstance().completedPathTo(targetPos) || (!Pathfinder.getInstance().isRunning() && Pathfinder.getInstance().succeeded()) || PlayerUtil.getBlockStandingOn().equals(targetPos)) {
log("Completed path. going to next");
this.swapState(State.STARTING, 0);
log("Done Walking");
return;
}
if (Pathfinder.getInstance().failed()) {
sendError("Pathfinding failed");
this.stop(NavError.PATHFIND_FAILED);
return;
}
// Check for timeout - if pathfinding is taking too long, increase the timer
if (this.timer.isScheduled() && this.timer.passed()) {
if (Pathfinder.getInstance().isRunning()) {
log("Pathfinding still in progress, extending timer");
this.timer.schedule(5000); // Extend by 5 more seconds
} else {
logError("Pathfinding timeout - pathfinder stopped without completion");
this.stop(NavError.TIME_FAIL);
return;
}
}
break;
case END_VERIFY: {
// Todo: add something to verify if player is at the end or not preferably something that checks for distance from end
if (this.isQueued) {
this.pause();
} else {
this.stop();
}
break;
}
}
}
@SubscribeEvent
protected void onPacketReceive(PacketEvent.Received event) {
if (!this.enabled) {
return;
}
if (this.state != State.AOTV_VERIFY) {
return;
}
if (!(event.packet instanceof S08PacketPlayerPosLook)) {
return;
}
this.swapState(State.STARTING, 0);
S08PacketPlayerPosLook packet = (S08PacketPlayerPosLook) event.packet;
RotationHandler.getInstance().stop();
Vec3 playerPos = mc.thePlayer.getPositionVector();
Vec3 pos = new Vec3(
packet.getX() + (packet.func_179834_f().contains(S08PacketPlayerPosLook.EnumFlags.X) ? playerPos.xCoord : 0),
packet.getY() + (packet.func_179834_f().contains(S08PacketPlayerPosLook.EnumFlags.Y) ? playerPos.yCoord : 0),
packet.getZ() + (packet.func_179834_f().contains(S08PacketPlayerPosLook.EnumFlags.Z) ? playerPos.zCoord : 0)
);
if (pos.distanceTo(this.routeToFollow.get(this.currentRouteIndex).toVec3()) > 6) {
this.swapState(State.ROTATION, 0);
}
}
@SubscribeEvent
protected void onRender(RenderWorldLastEvent event) {
if (!this.isQueued) {
return;
}
this.routeToFollow.drawRoute();
}
public void swapState(final State toState, final int delay) {
this.state = toState;
this.timer.schedule(delay);
}
enum State {
STARTING, DETECT_ROUTE, ROTATION, ROTATION_VERIFY, AOTV, AOTV_VERIFY, WALK, WALK_VERIFY, END_VERIFY
}
public enum NavError {
NONE, TIME_FAIL, PATHFIND_FAILED
}
}