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 pathPathExecutor.java
More file actions
376 lines (318 loc) · 13.5 KB
/
PathExecutor.java
File metadata and controls
376 lines (318 loc) · 13.5 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
package com.jelly.mightyminerv2.feature.impl;
import com.jelly.mightyminerv2.config.MightyMinerConfig;
import com.jelly.mightyminerv2.handler.RotationHandler;
import com.jelly.mightyminerv2.pathfinder.calculate.Path;
import com.jelly.mightyminerv2.util.*;
import com.jelly.mightyminerv2.util.helper.Angle;
import com.jelly.mightyminerv2.util.helper.Clock;
import com.jelly.mightyminerv2.util.helper.RotationConfiguration;
import kotlin.Pair;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.util.BlockPos;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import java.awt.*;
import java.util.*;
import java.util.List;
public class PathExecutor {
private static PathExecutor instance;
private final Minecraft mc = Minecraft.getMinecraft();
@Getter
private final Deque<Path> pathQueue = new LinkedList<>();
private final Map<Long, List<Long>> map = new HashMap<>();
private final List<BlockPos> blockPath = new ArrayList<>();
private final Clock stuckTimer = new Clock();
@Getter
private boolean enabled = false;
private Path prev;
private Path curr;
private boolean failed = false;
private boolean succeeded = false;
private boolean pastTarget = false;
private Random random = new Random();
private double lastPitch = 10 + (15 - 10) * random.nextDouble();
private Clock dynamicPitch = new Clock();
private int target = 0;
private int previous = -1;
private long nodeChangeTime = 0;
private boolean interpolated = true;
private float interpolYawDiff = 0f;
@Getter
private State state = State.STARTING_PATH;
@Setter
private boolean allowSprint = true;
@Setter
private boolean allowInterpolation = false;
public static PathExecutor getInstance() {
if (instance == null) {
instance = new PathExecutor();
}
return instance;
}
public void queuePath(Path path) {
if (path.getPath().isEmpty()) {
error("Path is empty");
failed = true;
return;
}
BlockPos start = path.getStart();
Path lastPath = (this.curr != null) ? this.curr : this.pathQueue.peekLast();
if (lastPath != null && !lastPath.getGoal().isAtGoal(start.getX(), start.getY(), start.getZ())) {
error("This path segment does not start at last path's goal. LastpathGoal: " + lastPath.getGoal() + ", ThisPathStart: " + start);
failed = true;
return;
}
this.pathQueue.offer(path);
}
public void start() {
this.state = State.STARTING_PATH;
this.enabled = true;
}
public void stop() {
this.enabled = false;
this.pathQueue.clear();
this.blockPath.clear();
this.map.clear();
this.curr = null;
this.prev = null;
this.target = 0;
this.previous = -1;
this.pastTarget = false;
this.state = State.END;
this.interpolYawDiff = 0f;
this.allowSprint = true;
this.allowInterpolation = false;
this.nodeChangeTime = 0;
this.interpolated = true;
StrafeUtil.enabled = false;
RotationHandler.getInstance().stop();
KeyBindUtil.releaseAllExcept();
}
public void clearQueue() {
this.pathQueue.clear();
this.curr = null;
this.succeeded = true;
this.failed = false;
this.interpolated = false;
this.target = 0;
this.previous = -1;
}
public boolean onTick() {
if (!enabled) {
return false;
}
if (this.stuckTimer.isScheduled() && this.stuckTimer.passed()) {
log("Was Stuck For a Second.");
this.failed = true;
this.succeeded = false;
this.stop();
}
BlockPos playerPos = PlayerUtil.getBlockStandingOn();
if (this.curr != null) {
// this is utterly useless but im useless as well
List<Long> blockHashes = this.map.get(this.pack(playerPos.getX(), playerPos.getZ()));
int current = -1;
if (blockHashes != null && !blockHashes.isEmpty()) {
int bestY = -1;
double playerY = mc.thePlayer.posY;
for (Long blockHash : blockHashes) {
Pair<Integer, Integer> block = this.unpack(blockHash);
int blockY = block.getFirst();
int blockTarget = block.getSecond();
if (blockTarget > this.previous) {
if (bestY == -1 || (blockY < playerY && blockY > bestY) || (blockY >= playerY && blockY < bestY)) {
bestY = block.getFirst();
current = blockTarget;
}
}
}
}
if (current != -1 && current > previous) {
this.previous = current;
this.target = current + 1;
this.state = State.TRAVERSING;
this.pastTarget = false;
this.interpolated = false;
this.interpolYawDiff = 0;
this.nodeChangeTime = System.currentTimeMillis();
log("changed target from " + this.previous + " to " + this.target);
RotationHandler.getInstance().stop();
}
if (Math.hypot(mc.thePlayer.motionX, mc.thePlayer.motionZ) < 0.05) {
if (!this.stuckTimer.isScheduled()) {
this.stuckTimer.schedule(1000);
}
} else {
this.stuckTimer.reset();
}
} else {
if (this.stuckTimer.isScheduled()) {
this.stuckTimer.reset();
}
if (this.pathQueue.isEmpty()) {
return true;
}
}
if (this.curr == null || this.target == this.blockPath.size()) {
log("Path traversed");
if (this.pathQueue.isEmpty()) {
log("Pathqueue is empty");
if (this.curr != null) {
this.curr = null;
this.target = 0;
this.previous = -1;
}
this.state = State.WAITING;
return true;
}
this.succeeded = true;
this.failed = false;
this.prev = this.curr;
this.target = 1;
this.previous = 0;
loadPath(this.pathQueue.poll());
if (this.target == this.blockPath.size()) {
return true;
}
log("loaded new path target: " + this.target + ", prev: " + this.previous);
}
BlockPos target = this.blockPath.get(this.target);
if (this.target < this.blockPath.size() - 1) {
BlockPos nextTarget = this.blockPath.get(this.target + 1);
double playerDistToNext = playerPos.distanceSq(nextTarget);
double targetDistToNext = target.distanceSq(nextTarget);
if ((this.pastTarget || (this.pastTarget = playerDistToNext > targetDistToNext)) && playerDistToNext < targetDistToNext) {
this.previous = this.target;
this.target++;
target = this.blockPath.get(this.target);
log("walked past target");
}
}
boolean onGround = mc.thePlayer.onGround;
int targetX = target.getX();
int targetZ = target.getZ();
double horizontalDistToTarget = Math.hypot(mc.thePlayer.posX - targetX - 0.5, mc.thePlayer.posZ - targetZ - 0.5);
float yaw = AngleUtil.getRotationYaw360(mc.thePlayer.getPositionVector(), new Vec3(targetX + 0.5, 0.0, targetZ + 0.5));
float yawDiff = Math.abs(AngleUtil.get360RotationYaw() - yaw);
if (this.interpolYawDiff == 0) {
this.interpolYawDiff = yaw - AngleUtil.get360RotationYaw();
}
// Disable StrafeUtil for realistic client-side movement
StrafeUtil.enabled = false;
// Rotate player to face target direction
if (yawDiff > 3 && !RotationHandler.getInstance().isEnabled()) {
float rotYaw = yaw;
// look at a block thats at least 5 blocks away instead of looking at the target which helps reduce buggy rotation
for (int i = this.target; i < this.blockPath.size(); i++) {
BlockPos rotationTarget = this.blockPath.get(i);
if (Math.hypot(mc.thePlayer.posX - rotationTarget.getX(), mc.thePlayer.posZ - rotationTarget.getZ()) > 5) {
rotYaw = AngleUtil.getRotation(rotationTarget).yaw;
break;
}
}
float time = MightyMinerConfig.fixrot ? MightyMinerConfig.rottime : Math.max(300, (long) (400 - horizontalDistToTarget * MightyMinerConfig.rotmult));
if (!dynamicPitch.isScheduled() || dynamicPitch.passed()) {
lastPitch = 10 + (15 - 10) * random.nextDouble();
dynamicPitch.schedule(1000);
}
// TODO: Implement back route miner
RotationHandler.getInstance().easeTo(
new RotationConfiguration(
new Angle(rotYaw, (float) lastPitch),
(long) time, null
)
);
}
// Calculate which WASD keys to press based on current player rotation (not target direction)
// This makes movement purely client-side and realistic
Vec3 targetVec = new Vec3(targetX + 0.5, mc.thePlayer.posY, targetZ + 0.5);
List<KeyBinding> neededKeys = KeyBindUtil.getNeededKeyPresses(mc.thePlayer.getPositionVector(), targetVec);
List<KeyBinding> keyBindings = new ArrayList<>(neededKeys);
// Preserve attack/use item state
if (mc.gameSettings.keyBindUseItem.isKeyDown()) {
keyBindings.add(mc.gameSettings.keyBindUseItem);
}
if (mc.gameSettings.keyBindAttack.isKeyDown()) {
keyBindings.add(mc.gameSettings.keyBindAttack);
}
// Check if we need to jump based on path analysis
Vec3 pos = new Vec3(mc.thePlayer.posX, playerPos.getY() + 0.5, mc.thePlayer.posZ);
Vec3 vec4Rot = AngleUtil.getVectorForRotation(mc.thePlayer.rotationYaw);
boolean shouldJump = BlockUtil.canWalkBetween(this.curr.getCtx(), pos, pos.addVector(vec4Rot.xCoord, 1, vec4Rot.zCoord));
if (shouldJump && onGround) {
keyBindings.add(mc.gameSettings.keyBindJump);
this.state = State.JUMPING;
}
// Apply all the calculated key presses
KeyBindUtil.holdThese(keyBindings.toArray(new KeyBinding[0]));
// Handle sprinting - only sprint when moving relatively straight
KeyBindUtil.setKeyBindState(mc.gameSettings.keyBindSprint, this.allowSprint && yawDiff < 40 && !shouldJump);
return playerPos.distanceSqToCenter(target.getX(), target.getY(), target.getZ()) < 100;
}
public void loadPath(Path path) {
this.blockPath.clear();
this.map.clear();
this.curr = path;
this.blockPath.addAll(this.curr.getSmoothedPath());
for (int i = 0; i < this.blockPath.size(); i++) {
BlockPos pos = this.blockPath.get(i);
this.map.computeIfAbsent(this.pack(pos.getX(), pos.getZ()), k -> new ArrayList<>()).add(this.pack(pos.getY(), i));
}
}
public void onRender() {
System.out.println("OnRender");
if (this.target != -1 && this.target < this.blockPath.size()) {
System.out.println("valtarg");
BlockPos playerPos = PlayerUtil.getBlockStandingOn();
BlockPos target = this.blockPath.get(this.target);
int targetX = target.getX();
int targetZ = target.getZ();
float yaw = AngleUtil.getRotationYaw360(mc.thePlayer.getPositionVector(), new Vec3(targetX + 0.5, 0.0, targetZ + 0.5));
Vec3 pos = new Vec3(mc.thePlayer.posX, playerPos.getY() + 0.5, mc.thePlayer.posZ);
Vec3 vec4Rot = AngleUtil.getVectorForRotation(yaw);
Vec3 newV = pos.addVector(vec4Rot.xCoord, playerPos.getY() + 1, vec4Rot.zCoord);
RenderUtil.drawBlock(new BlockPos(MathHelper.floor_double(newV.xCoord), MathHelper.floor_double(newV.yCoord), MathHelper.floor_double(newV.zCoord)),
new Color(255, 0, 0, 255));
RenderUtil.drawBlock(playerPos, new Color(255, 255, 0, 100));
}
}
public Path getPreviousPath() {
return this.prev;
}
public Path getCurrentPath() {
return this.curr;
}
public boolean failed() {
return !this.enabled && this.failed;
}
public boolean ended() {
return !this.enabled && this.succeeded;
}
private long pack(int x, int z) {
return ((long) x << 32) | (z & 0xFFFFFFFFL);
}
public Pair<Integer, Integer> unpack(long packed) {
return new Pair<>((int) (packed >> 32), (int) packed);
}
void log(String message) {
Logger.sendLog(getMessage(message));
}
void send(String message) {
Logger.sendMessage(getMessage(message));
}
void error(String message) {
Logger.sendError(getMessage(message));
}
void note(String message) {
Logger.sendNote(getMessage(message));
}
String getMessage(String message) {
return "[PathExecutor] " + message;
}
enum State {
STARTING_PATH, TRAVERSING, JUMPING, WAITING, END
}
}