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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
//! Minecraft bindings.
//!
//! 26.2 ships unobfuscated, so every name below is the real one from the game's
//! source. Field and method IDs stay valid for the life of the class, so they
//! are resolved once; object references never are, and get re-read each frame.
//!
//! Anything that fails to resolve is recorded in `missing` rather than being
//! treated as fatal — one renamed field should cost you that feature, not the
//! whole menu.
use jni_sys::{jclass, jfieldID, jmethodID, jobject, jvalue};
use crate::jni::Jni;
const MINECRAFT: &str = "net/minecraft/client/Minecraft";
const LOCAL_PLAYER: &str = "net/minecraft/client/player/LocalPlayer";
const ENTITY: &str = "net/minecraft/world/entity/Entity";
const LIVING: &str = "net/minecraft/world/entity/LivingEntity";
const ABILITIES: &str = "net/minecraft/world/entity/player/Abilities";
const VEC3: &str = "net/minecraft/world/phys/Vec3";
const GAME_MODE: &str = "net/minecraft/client/multiplayer/MultiPlayerGameMode";
const WINDOW: &str = "com/mojang/blaze3d/platform/Window";
/// Everything resolved once at startup.
pub struct Mc {
pub minecraft: jclass,
pub local_player: jclass,
pub living: jclass,
f_instance: jfieldID,
f_player: jfieldID,
f_level: jfieldID,
f_game_mode: jfieldID,
f_window: jfieldID,
f_mouse_handler: Option<jfieldID>,
f_mouse_grabbed: Option<jfieldID>,
f_singleplayer: Option<jfieldID>,
f_abilities: jfieldID,
f_position: jfieldID,
f_x_rot: jfieldID,
f_y_rot: jfieldID,
f_y_rot_o: Option<jfieldID>,
f_x_rot_o: Option<jfieldID>,
f_y_head_rot: Option<jfieldID>,
f_y_head_rot_o: Option<jfieldID>,
f_y_body_rot: Option<jfieldID>,
f_y_body_rot_o: Option<jfieldID>,
f_smart_cull: Option<jfieldID>,
f_gui: Option<jfieldID>,
f_hud: Option<jfieldID>,
f_hud_hidden: Option<jfieldID>,
f_xo: Option<jfieldID>,
f_yo: Option<jfieldID>,
f_zo: Option<jfieldID>,
f_x_old: Option<jfieldID>,
f_y_old: Option<jfieldID>,
f_z_old: Option<jfieldID>,
f_on_ground: Option<jfieldID>,
f_no_physics: Option<jfieldID>,
f_fall_distance: Option<jfieldID>,
f_hurt_time: Option<jfieldID>,
f_flying: jfieldID,
f_may_fly: jfieldID,
f_fly_speed: jfieldID,
f_walk_speed: jfieldID,
f_instabuild: Option<jfieldID>,
pub f_vx: jfieldID,
pub f_vy: jfieldID,
pub f_vz: jfieldID,
f_destroy_delay: Option<jfieldID>,
m_set_delta: Option<jmethodID>,
m_get_delta: Option<jmethodID>,
m_set_sprinting: Option<jmethodID>,
m_is_sprinting: Option<jmethodID>,
m_get_health: Option<jmethodID>,
m_set_pos: Option<jmethodID>,
m_start_attack: Option<jmethodID>,
m_respawn: Option<jmethodID>,
m_attack_strength: Option<jmethodID>,
m_line_of_sight: Option<jmethodID>,
f_tick_count: Option<jfieldID>,
f_x_last: Option<jfieldID>,
f_y_last: Option<jfieldID>,
f_z_last: Option<jfieldID>,
f_yrot_last: Option<jfieldID>,
f_xrot_last: Option<jfieldID>,
f_position_reminder: Option<jfieldID>,
f_jumping: Option<jfieldID>,
m_window_width: Option<jmethodID>,
m_window_height: Option<jmethodID>,
pub missing: Vec<String>,
}
/// Resolve a required id, or bail out of construction.
macro_rules! need {
($miss:expr, $what:expr, $expr:expr) => {
match $expr {
Some(v) => v,
None => {
$miss.push($what.to_string());
return Err($miss.join(", "));
}
}
};
}
/// Resolve an optional id, recording a miss and carrying on.
macro_rules! want {
($miss:expr, $what:expr, $expr:expr) => {
match $expr {
Some(v) => Some(v),
None => {
$miss.push($what.to_string());
None
}
}
};
}
impl Mc {
pub fn resolve(j: &Jni) -> Result<Mc, String> {
let mut missing: Vec<String> = Vec::new();
let minecraft = need!(missing, "class Minecraft", class(j, MINECRAFT));
let local_player = need!(missing, "class LocalPlayer", class(j, LOCAL_PLAYER));
let entity = need!(missing, "class Entity", class(j, ENTITY));
let living = need!(missing, "class LivingEntity", class(j, LIVING));
let abilities = need!(missing, "class Abilities", class(j, ABILITIES));
let vec3 = need!(missing, "class Vec3", class(j, VEC3));
let game_mode = need!(missing, "class MultiPlayerGameMode", class(j, GAME_MODE));
let window = class(j, WINDOW);
let f_instance = need!(
missing,
"Minecraft.instance",
j.static_field(minecraft, "instance", "Lnet/minecraft/client/Minecraft;")
);
let f_player = need!(
missing,
"Minecraft.player",
j.field(minecraft, "player", "Lnet/minecraft/client/player/LocalPlayer;")
);
let f_level = need!(
missing,
"Minecraft.level",
j.field(minecraft, "level", "Lnet/minecraft/client/multiplayer/ClientLevel;")
);
let f_game_mode = need!(
missing,
"Minecraft.gameMode",
j.field(
minecraft,
"gameMode",
"Lnet/minecraft/client/multiplayer/MultiPlayerGameMode;"
)
);
let f_window = need!(
missing,
"Minecraft.window",
j.field(minecraft, "window", "Lcom/mojang/blaze3d/platform/Window;")
);
// 26.2 has no Minecraft.screen field; whether the mouse is grabbed is
// the same question in practice — grabbed means you are in the world
// and not in a GUI.
let f_mouse_handler = want!(
missing,
"Minecraft.mouseHandler",
j.field(minecraft, "mouseHandler", "Lnet/minecraft/client/MouseHandler;")
);
let f_mouse_grabbed = match class(j, "net/minecraft/client/MouseHandler") {
Some(c) => want!(
missing,
"MouseHandler.mouseGrabbed",
j.field(c, "mouseGrabbed", "Z")
),
None => None,
};
let f_singleplayer = want!(
missing,
"Minecraft.singleplayerServer",
j.field(
minecraft,
"singleplayerServer",
"Lnet/minecraft/client/server/IntegratedServer;"
)
);
let f_abilities = need!(
missing,
"Player.abilities",
j.field(
local_player,
"abilities",
"Lnet/minecraft/world/entity/player/Abilities;"
)
);
let f_position = need!(
missing,
"Entity.position",
j.field(entity, "position", "Lnet/minecraft/world/phys/Vec3;")
);
let f_x_rot = need!(missing, "Entity.xRot", j.field(entity, "xRot", "F"));
let f_y_rot = need!(missing, "Entity.yRot", j.field(entity, "yRot", "F"));
let f_y_head_rot = j.field(living, "yHeadRot", "F");
let f_y_head_rot_o = j.field(living, "yHeadRotO", "F");
let f_y_body_rot = j.field(living, "yBodyRot", "F");
let f_y_body_rot_o = j.field(living, "yBodyRotO", "F");
let f_smart_cull = j.field(minecraft, "smartCull", "Z");
// 26.2 moved the HUD out of Gui into its own class; isHidden is what F1
// toggles.
let f_gui = j.field(minecraft, "gui", "Lnet/minecraft/client/gui/Gui;");
let f_hud = class(j, "net/minecraft/client/gui/Gui")
.and_then(|c| j.field(c, "hud", "Lnet/minecraft/client/gui/Hud;"));
let f_hud_hidden = class(j, "net/minecraft/client/gui/Hud")
.and_then(|c| j.field(c, "isHidden", "Z"));
let f_y_rot_o = j.field(entity, "yRotO", "F");
let f_x_rot_o = j.field(entity, "xRotO", "F");
let f_xo = j.field(entity, "xo", "D");
let f_yo = j.field(entity, "yo", "D");
let f_zo = j.field(entity, "zo", "D");
let f_x_old = j.field(entity, "xOld", "D");
let f_y_old = j.field(entity, "yOld", "D");
let f_z_old = j.field(entity, "zOld", "D");
let f_on_ground = want!(missing, "Entity.onGround", j.field(entity, "onGround", "Z"));
let f_no_physics = want!(missing, "Entity.noPhysics", j.field(entity, "noPhysics", "Z"));
let f_fall_distance = want!(
missing,
"Entity.fallDistance",
j.field(entity, "fallDistance", "D")
);
let f_hurt_time = want!(missing, "LivingEntity.hurtTime", j.field(living, "hurtTime", "I"));
let f_flying = need!(missing, "Abilities.flying", j.field(abilities, "flying", "Z"));
let f_may_fly = need!(missing, "Abilities.mayfly", j.field(abilities, "mayfly", "Z"));
let f_fly_speed = need!(
missing,
"Abilities.flyingSpeed",
j.field(abilities, "flyingSpeed", "F")
);
let f_walk_speed = need!(
missing,
"Abilities.walkingSpeed",
j.field(abilities, "walkingSpeed", "F")
);
let f_instabuild = want!(
missing,
"Abilities.instabuild",
j.field(abilities, "instabuild", "Z")
);
let f_vx = need!(missing, "Vec3.x", j.field(vec3, "x", "D"));
let f_vy = need!(missing, "Vec3.y", j.field(vec3, "y", "D"));
let f_vz = need!(missing, "Vec3.z", j.field(vec3, "z", "D"));
let f_destroy_delay = want!(
missing,
"MultiPlayerGameMode.destroyDelay",
j.field(game_mode, "destroyDelay", "I")
);
let m_set_delta = want!(
missing,
"Entity.setDeltaMovement(DDD)",
j.method(entity, "setDeltaMovement", "(DDD)V")
);
let m_get_delta = want!(
missing,
"Entity.getDeltaMovement()",
j.method(entity, "getDeltaMovement", "()Lnet/minecraft/world/phys/Vec3;")
);
let m_set_sprinting = want!(
missing,
"Entity.setSprinting(Z)",
j.method(entity, "setSprinting", "(Z)V")
);
let m_is_sprinting = want!(
missing,
"Entity.isSprinting()",
j.method(entity, "isSprinting", "()Z")
);
let m_set_pos = want!(
missing,
"Entity.setPos(DDD)",
j.method(entity, "setPos", "(DDD)V")
);
let m_start_attack = want!(
missing,
"Minecraft.startAttack()",
j.method(minecraft, "startAttack", "()Z")
);
let f_jumping = want!(missing, "LivingEntity.jumping", j.field(living, "jumping", "Z"));
let m_respawn = want!(
missing,
"LocalPlayer.respawn()",
j.method(local_player, "respawn", "()V")
);
let m_attack_strength = match class(j, "net/minecraft/world/entity/player/Player") {
Some(c) => want!(
missing,
"Player.getAttackStrengthScale(F)",
j.method(c, "getAttackStrengthScale", "(F)F")
),
None => None,
};
let m_line_of_sight = want!(
missing,
"LivingEntity.hasLineOfSight(Entity)",
j.method(
living,
"hasLineOfSight",
"(Lnet/minecraft/world/entity/Entity;)Z"
)
);
let f_tick_count = want!(missing, "Entity.tickCount", j.field(entity, "tickCount", "I"));
// What LocalPlayer.sendPosition() compares against to decide whether a
// movement packet is needed at all.
let f_x_last = want!(missing, "LocalPlayer.xLast", j.field(local_player, "xLast", "D"));
let f_y_last = want!(missing, "LocalPlayer.yLast", j.field(local_player, "yLast", "D"));
let f_z_last = want!(missing, "LocalPlayer.zLast", j.field(local_player, "zLast", "D"));
let f_yrot_last = j.field(local_player, "yRotLast", "F");
let f_xrot_last = j.field(local_player, "xRotLast", "F");
let f_position_reminder = want!(
missing,
"LocalPlayer.positionReminder",
j.field(local_player, "positionReminder", "I")
);
let m_get_health = want!(
missing,
"LivingEntity.getHealth()",
j.method(living, "getHealth", "()F")
);
let (m_window_width, m_window_height) = match window {
Some(w) => (
want!(missing, "Window.getWidth()", j.method(w, "getWidth", "()I")),
want!(missing, "Window.getHeight()", j.method(w, "getHeight", "()I")),
),
None => {
missing.push("class Window".into());
(None, None)
}
};
Ok(Mc {
minecraft,
local_player,
living,
f_instance,
f_player,
f_level,
f_game_mode,
f_window,
f_mouse_handler,
f_mouse_grabbed,
f_singleplayer,
f_abilities,
f_position,
f_x_rot,
f_y_rot,
f_y_rot_o,
f_x_rot_o,
f_y_head_rot,
f_y_head_rot_o,
f_y_body_rot,
f_y_body_rot_o,
f_smart_cull,
f_gui,
f_hud,
f_hud_hidden,
f_xo,
f_yo,
f_zo,
f_x_old,
f_y_old,
f_z_old,
f_on_ground,
f_no_physics,
f_fall_distance,
f_hurt_time,
f_flying,
f_may_fly,
f_fly_speed,
f_walk_speed,
f_instabuild,
f_vx,
f_vy,
f_vz,
f_destroy_delay,
m_set_delta,
m_get_delta,
m_set_sprinting,
m_is_sprinting,
m_get_health,
m_set_pos,
m_start_attack,
m_respawn,
m_attack_strength,
m_line_of_sight,
f_tick_count,
f_x_last,
f_y_last,
f_z_last,
f_yrot_last,
f_xrot_last,
f_position_reminder,
f_jumping,
m_window_width,
m_window_height,
missing,
})
}
// ---- navigation --------------------------------------------------------
pub fn instance(&self, j: &Jni) -> Option<jobject> {
j.static_obj_field(self.minecraft, self.f_instance)
}
pub fn player(&self, j: &Jni, mc: jobject) -> Option<jobject> {
j.obj_field(mc, self.f_player)
}
pub fn level(&self, j: &Jni, mc: jobject) -> Option<jobject> {
j.obj_field(mc, self.f_level)
}
pub fn game_mode(&self, j: &Jni, mc: jobject) -> Option<jobject> {
j.obj_field(mc, self.f_game_mode)
}
pub fn abilities(&self, j: &Jni, player: jobject) -> Option<jobject> {
j.obj_field(player, self.f_abilities)
}
/// True while a GUI screen (inventory, pause menu, chat) is open — the
/// game releases the mouse for exactly those.
pub fn screen_open(&self, j: &Jni, mc: jobject) -> bool {
let (Some(fh), Some(fg)) = (self.f_mouse_handler, self.f_mouse_grabbed) else {
return false;
};
match j.obj_field(mc, fh) {
Some(h) => !j.bool_field(h, fg).unwrap_or(true),
None => false,
}
}
pub fn single_player(&self, j: &Jni, mc: jobject) -> bool {
match self.f_singleplayer {
Some(f) => j.obj_field(mc, f).is_some(),
None => false,
}
}
pub fn window_size(&self, j: &Jni, mc: jobject) -> Option<(i32, i32)> {
let w = j.obj_field(mc, self.f_window)?;
let width = j.call_int(w, self.m_window_width?, &[])?;
let height = j.call_int(w, self.m_window_height?, &[])?;
Some((width, height))
}
// ---- entity state ------------------------------------------------------
pub fn position(&self, j: &Jni, entity: jobject) -> Option<(f64, f64, f64)> {
let v = j.obj_field(entity, self.f_position)?;
Some((
j.double_field(v, self.f_vx)?,
j.double_field(v, self.f_vy)?,
j.double_field(v, self.f_vz)?,
))
}
pub fn rotation(&self, j: &Jni, entity: jobject) -> (f32, f32) {
(
j.float_field(entity, self.f_y_rot).unwrap_or(0.0),
j.float_field(entity, self.f_x_rot).unwrap_or(0.0),
)
}
pub fn on_ground(&self, j: &Jni, entity: jobject) -> bool {
self.f_on_ground
.and_then(|f| j.bool_field(entity, f))
.unwrap_or(false)
}
pub fn health(&self, j: &Jni, entity: jobject) -> Option<f32> {
j.call_float(entity, self.m_get_health?, &[])
}
pub fn is_sprinting(&self, j: &Jni, entity: jobject) -> bool {
self.m_is_sprinting
.and_then(|m| j.call_bool(entity, m, &[]))
.unwrap_or(false)
}
// ---- writes ------------------------------------------------------------
/// Point an entity somewhere. yHeadRot matters too, or the body and head
/// disagree and the result looks wrong to everyone else.
pub fn set_rotation(&self, j: &Jni, entity: jobject, yaw: f32, pitch: f32) {
j.set_float(entity, self.f_y_rot, yaw);
j.set_float(entity, self.f_x_rot, pitch.clamp(-90.0, 90.0));
if let Some(f) = self.f_y_head_rot {
j.set_float(entity, f, yaw);
}
}
pub fn set_sprinting(&self, j: &Jni, entity: jobject, on: bool) {
if let Some(m) = self.m_set_sprinting {
j.call_void(entity, m, &[jvalue { z: on as u8 }]);
}
}
pub fn set_delta_movement(&self, j: &Jni, entity: jobject, x: f64, y: f64, z: f64) {
if let Some(m) = self.m_set_delta {
j.call_void(entity, m, &[jvalue { d: x }, jvalue { d: y }, jvalue { d: z }]);
}
}
pub fn delta_movement(&self, j: &Jni, entity: jobject) -> Option<(f64, f64, f64)> {
let v = j.call_obj(entity, self.m_get_delta?, &[])?;
Some((
j.double_field(v, self.f_vx)?,
j.double_field(v, self.f_vy)?,
j.double_field(v, self.f_vz)?,
))
}
/// Hide the whole HUD — hotbar, hearts, hunger, experience bar — the way
/// F1 does. Freecam wants this: you are not looking through your own eyes,
/// so your own status bars have no business being on screen.
pub fn set_hud_hidden(&self, j: &Jni, instance: jobject, hidden: bool) {
let (Some(fg), Some(fh), Some(fi)) = (self.f_gui, self.f_hud, self.f_hud_hidden) else {
return;
};
let Some(gui) = j.obj_field(instance, fg) else {
return;
};
let Some(hud) = j.obj_field(gui, fh) else {
return;
};
j.set_bool(hud, fi, hidden);
}
pub fn hud_hidden(&self, j: &Jni, instance: jobject) -> bool {
let (Some(fg), Some(fh), Some(fi)) = (self.f_gui, self.f_hud, self.f_hud_hidden) else {
return false;
};
j.obj_field(instance, fg)
.and_then(|gui| j.obj_field(gui, fh))
.and_then(|hud| j.bool_field(hud, fi))
.unwrap_or(false)
}
/// Occlusion culling. With it off, the renderer stops skipping chunk
/// sections it thinks are hidden behind others — which is what lets you see
/// a cave from inside the rock instead of a wall of black.
pub fn set_smart_cull(&self, j: &Jni, instance: jobject, on: bool) {
if let Some(f) = self.f_smart_cull {
j.set_bool(instance, f, on);
}
}
pub fn smart_cull(&self, j: &Jni, instance: jobject) -> bool {
self.f_smart_cull
.and_then(|f| j.bool_field(instance, f))
.unwrap_or(true)
}
/// Point a camera entity, with every rotation the renderer might read.
///
/// `LivingEntity.getViewYRot` does not use `yRot` at all — it interpolates
/// `yHeadRotO` to `yHeadRot`. Setting only `yRot` leaves the view lerping
/// between a stale head angle and the current one every frame, which reads
/// as the camera swinging wildly on its own.
pub fn aim_camera(&self, j: &Jni, entity: jobject, yaw: f32, pitch: f32) {
let pitch = pitch.clamp(-90.0, 90.0);
for (f, v) in [
(self.f_y_rot, yaw),
(self.f_x_rot, pitch),
] {
j.set_float(entity, f, v);
}
for (f, v) in [
(self.f_y_rot_o, yaw),
(self.f_x_rot_o, pitch),
(self.f_y_head_rot, yaw),
(self.f_y_head_rot_o, yaw),
(self.f_y_body_rot, yaw),
(self.f_y_body_rot_o, yaw),
] {
if let Some(f) = f {
j.set_float(entity, f, v);
}
}
}
/// Move an entity's previous-tick and render-previous positions with it,
/// so nothing interpolates from where it used to be.
pub fn set_old_position(&self, j: &Jni, entity: jobject, pos: (f64, f64, f64)) {
for (f, v) in [
(self.f_xo, pos.0),
(self.f_yo, pos.1),
(self.f_zo, pos.2),
(self.f_x_old, pos.0),
(self.f_y_old, pos.1),
(self.f_z_old, pos.2),
] {
if let Some(f) = f {
j.set_double(entity, f, v);
}
}
}
pub fn set_pos(&self, j: &Jni, entity: jobject, x: f64, y: f64, z: f64) -> bool {
match self.m_set_pos {
Some(m) => {
j.call_void(entity, m, &[jvalue { d: x }, jvalue { d: y }, jvalue { d: z }]);
true
}
None => false,
}
}
pub fn jumping(&self, j: &Jni, entity: jobject) -> bool {
self.f_jumping
.and_then(|f| j.bool_field(entity, f))
.unwrap_or(false)
}
/// The game's own "swing at whatever is under the crosshair", so the
/// attack goes through exactly the path a real click takes.
pub fn start_attack(&self, j: &Jni, instance: jobject) {
if let Some(m) = self.m_start_attack {
let _ = j.call_bool(instance, m, &[]);
}
}
/// The game's tick counter. Physics runs per tick, not per frame, so any
/// module that *adds* to velocity has to act once a tick or it compounds
/// with the frame rate.
pub fn tick_count(&self, j: &Jni, entity: jobject) -> i32 {
self.f_tick_count
.and_then(|f| j.int_field(entity, f))
.unwrap_or(0)
}
/// How far the attack cooldown has recharged, 0..1. Swinging below 1.0
/// deals a fraction of the damage, which is why a fast aura hits for
/// almost nothing in modern Minecraft.
pub fn attack_strength(&self, j: &Jni, player: jobject) -> f32 {
self.m_attack_strength
.and_then(|m| j.call_float(player, m, &[jvalue { f: 0.0 }]))
.unwrap_or(1.0)
}
pub fn has_line_of_sight(&self, j: &Jni, from: jobject, to: jobject) -> bool {
self.m_line_of_sight
.and_then(|m| j.call_bool(from, m, &[jvalue { l: to }]))
.unwrap_or(true)
}
pub fn respawn(&self, j: &Jni, player: jobject) {
if let Some(m) = self.m_respawn {
j.call_void(player, m, &[]);
}
}
pub fn set_jumping(&self, j: &Jni, entity: jobject, on: bool) {
if let Some(f) = self.f_jumping {
j.set_bool(entity, f, on);
}
}
/// Stop the client telling the server where it is.
///
/// `sendPosition()` only builds a packet when the current position differs
/// from `xLast/yLast/zLast`, or when `positionReminder` reaches 20. Writing
/// the current position into those every frame makes both tests fail, so no
/// movement packet goes out and the server keeps the last position it saw.
/// This is what makes freecam actually free rather than a rubber-band.
pub fn freeze_sent_position(&self, j: &Jni, player: jobject, pos: (f64, f64, f64)) {
if let (Some(fx), Some(fy), Some(fz)) = (self.f_x_last, self.f_y_last, self.f_z_last) {
j.set_double(player, fx, pos.0);
j.set_double(player, fy, pos.1);
j.set_double(player, fz, pos.2);
}
if let Some(f) = self.f_position_reminder {
j.set_int(player, f, 0);
}
}
/// Freeze the rotation the client reports, the same way.
pub fn freeze_sent_rotation(&self, j: &Jni, player: jobject, yaw: f32, pitch: f32) {
if let Some(f) = self.f_yrot_last {
j.set_float(player, f, yaw);
}
if let Some(f) = self.f_xrot_last {
j.set_float(player, f, pitch);
}
}
pub fn set_no_physics(&self, j: &Jni, entity: jobject, on: bool) {
if let Some(f) = self.f_no_physics {
j.set_bool(entity, f, on);
}
}
pub fn set_fall_distance(&self, j: &Jni, entity: jobject, v: f64) {
if let Some(f) = self.f_fall_distance {
j.set_double(entity, f, v);
}
}
pub fn fall_distance(&self, j: &Jni, entity: jobject) -> f64 {
self.f_fall_distance
.and_then(|f| j.double_field(entity, f))
.unwrap_or(0.0)
}
pub fn hurt_time(&self, j: &Jni, entity: jobject) -> i32 {
self.f_hurt_time
.and_then(|f| j.int_field(entity, f))
.unwrap_or(0)
}
pub fn set_hurt_time(&self, j: &Jni, entity: jobject, v: i32) {
if let Some(f) = self.f_hurt_time {
j.set_int(entity, f, v);
}
}
pub fn set_destroy_delay(&self, j: &Jni, game_mode: jobject, v: i32) {
if let Some(f) = self.f_destroy_delay {
j.set_int(game_mode, f, v);
}
}
// ---- abilities ---------------------------------------------------------
pub fn flying(&self, j: &Jni, ab: jobject) -> bool {
j.bool_field(ab, self.f_flying).unwrap_or(false)
}
pub fn may_fly(&self, j: &Jni, ab: jobject) -> bool {
j.bool_field(ab, self.f_may_fly).unwrap_or(false)
}
pub fn set_flying(&self, j: &Jni, ab: jobject, v: bool) {
j.set_bool(ab, self.f_flying, v);
}
pub fn set_may_fly(&self, j: &Jni, ab: jobject, v: bool) {
j.set_bool(ab, self.f_may_fly, v);
}
pub fn fly_speed(&self, j: &Jni, ab: jobject) -> f32 {
j.float_field(ab, self.f_fly_speed).unwrap_or(0.05)
}
pub fn set_fly_speed(&self, j: &Jni, ab: jobject, v: f32) {
j.set_float(ab, self.f_fly_speed, v);
}
pub fn walk_speed(&self, j: &Jni, ab: jobject) -> f32 {
j.float_field(ab, self.f_walk_speed).unwrap_or(0.1)
}
pub fn set_walk_speed(&self, j: &Jni, ab: jobject, v: f32) {
j.set_float(ab, self.f_walk_speed, v);
}
pub fn set_instabuild(&self, j: &Jni, ab: jobject, v: bool) {
if let Some(f) = self.f_instabuild {
j.set_bool(ab, f, v);
}
}
pub fn instabuild(&self, j: &Jni, ab: jobject) -> bool {
self.f_instabuild
.and_then(|f| j.bool_field(ab, f))
.unwrap_or(false)
}
}
/// Find a class and pin it with a global reference: field and method IDs are
/// only valid while their class stays loaded.
fn class(j: &Jni, name: &str) -> Option<jclass> {
let local = j.find_class(name)?;
j.global(local).map(|g| g as jclass)
}
// ---------------------------------------------------------------------------
// World scanning and combat
//
// Everything below is resolved the same way as the core bindings: optional, so
// a rename costs one feature rather than the whole client.
// ---------------------------------------------------------------------------
/// The entity classes we sort targets into, plus the calls needed to walk the
/// level's entity list and to hit something.
pub struct World {
pub player_class: jclass,
monster_class: Option<jclass>,
animal_class: Option<jclass>,
item_class: Option<jclass>,
living_class: jclass,
arrow_class: Option<jclass>,
m_entities_for_rendering: Option<jmethodID>,
m_iterator: Option<jmethodID>,
m_has_next: Option<jmethodID>,
m_next: Option<jmethodID>,
m_is_alive: Option<jmethodID>,
m_get_bounding_box: Option<jmethodID>,
m_get_eye_position: Option<jmethodID>,
m_get_name: Option<jmethodID>,
m_component_string: Option<jmethodID>,
m_get_max_health: Option<jmethodID>,
f_min_x: Option<jfieldID>,
f_min_y: Option<jfieldID>,
f_min_z: Option<jfieldID>,
f_max_x: Option<jfieldID>,
f_max_y: Option<jfieldID>,
f_max_z: Option<jfieldID>,
m_attack: Option<jmethodID>,
m_swing: Option<jmethodID>,
main_hand: Option<jobject>,
f_game_renderer: Option<jfieldID>,
f_main_camera: Option<jfieldID>,
f_cam_position: Option<jfieldID>,
f_cam_x_rot: Option<jfieldID>,
f_cam_y_rot: Option<jfieldID>,
f_options: Option<jfieldID>,
f_opt_fov: Option<jfieldID>,
f_opt_gamma: Option<jfieldID>,
f_opt_bob_view: Option<jfieldID>,
f_opt_render_distance: Option<jfieldID>,
f_server_render_distance: Option<jfieldID>,
m_broadcast_options: Option<jmethodID>,
m_set_server_render_distance: Option<jmethodID>,
f_chunk_source: Option<jfieldID>,
m_update_view_radius: Option<jmethodID>,
m_loaded_chunks: Option<jmethodID>,
boolean_class: Option<jclass>,
m_bool_value_of: Option<jmethodID>,
m_bool_value: Option<jmethodID>,
m_opt_get: Option<jmethodID>,
m_opt_set: Option<jmethodID>,
double_class: Option<jclass>,
m_double_value_of: Option<jmethodID>,
m_double_value: Option<jmethodID>,
m_int_value_of: Option<jmethodID>,
integer_class: Option<jclass>,
m_int_value: Option<jmethodID>,
f_attributes: Option<jfieldID>,
m_attr_instance: Option<jmethodID>,
m_attr_set_base: Option<jmethodID>,
m_attr_get_base: Option<jmethodID>,
pub holder_entity_reach: Option<jobject>,
pub holder_block_reach: Option<jobject>,
pub holder_step_height: Option<jobject>,
f_rain_level: Option<jfieldID>,
f_thunder_level: Option<jfieldID>,
f_o_rain_level: Option<jfieldID>,
f_o_thunder_level: Option<jfieldID>,
f_touching_water: Option<jfieldID>,
f_horizontal_collision: Option<jfieldID>,
// Freecam: a detached camera entity, and the input that must stop driving
// the body while the camera is the thing moving.
m_set_camera_entity: Option<jmethodID>,
armor_stand_class: Option<jclass>,
m_armor_stand_init: Option<jmethodID>,
f_client_input: Option<jfieldID>,
f_key_presses: Option<jfieldID>,
f_move_vector: Option<jfieldID>,
input_class: Option<jclass>,
input_empty: Option<jobject>,
vec2_zero: Option<jobject>,
f_in_forward: Option<jfieldID>,
f_in_back: Option<jfieldID>,
f_in_left: Option<jfieldID>,
f_in_right: Option<jfieldID>,
f_in_jump: Option<jfieldID>,
f_in_shift: Option<jfieldID>,
f_hit_result: Option<jfieldID>,
entity_hit_class: Option<jclass>,
m_hit_get_entity: Option<jmethodID>,
}
impl World {
pub fn resolve(j: &Jni, mc: &Mc, missing: &mut Vec<String>) -> Option<World> {
let client_level = class(j, "net/minecraft/client/multiplayer/ClientLevel")?;
let iterable = class(j, "java/lang/Iterable")?;
let iterator = class(j, "java/util/Iterator")?;
let entity = class(j, "net/minecraft/world/entity/Entity")?;
let living = class(j, "net/minecraft/world/entity/LivingEntity")?;
let player_class = class(j, "net/minecraft/world/entity/player/Player")?;
let game_mode = class(j, "net/minecraft/client/multiplayer/MultiPlayerGameMode")?;
let aabb = class(j, "net/minecraft/world/phys/AABB")?;
let component = class(j, "net/minecraft/network/chat/Component");
let camera = class(j, "net/minecraft/client/Camera");
let renderer = class(j, "net/minecraft/client/renderer/GameRenderer");
let options = class(j, "net/minecraft/client/Options");
let option_instance = class(j, "net/minecraft/client/OptionInstance");
let main_hand = class(j, "net/minecraft/world/InteractionHand").and_then(|c| {
let f = j.static_field(c, "MAIN_HAND", "Lnet/minecraft/world/InteractionHand;")?;
let v = j.static_obj_field(c, f)?;
j.global(v)
});
if main_hand.is_none() {
missing.push("InteractionHand.MAIN_HAND".into());
}
let double_class = class(j, "java/lang/Double");
let integer_class = class(j, "java/lang/Integer");
let attribute_instance =
class(j, "net/minecraft/world/entity/ai/attributes/AttributeInstance");
let attributes = class(j, "net/minecraft/world/entity/ai/attributes/Attributes");
let entity_hit = class(j, "net/minecraft/world/phys/EntityHitResult");
let level_class = class(j, "net/minecraft/world/level/Level");
let armor_stand = class(j, "net/minecraft/world/entity/decoration/ArmorStand");
let client_input = class(j, "net/minecraft/client/player/ClientInput");
let input_cls = class(j, "net/minecraft/world/entity/player/Input");
Some(World {
player_class,
monster_class: class(j, "net/minecraft/world/entity/monster/Monster"),
// Not loaded until something shoots; FindClass loads it for us.
arrow_class: class(j, "net/minecraft/world/entity/projectile/AbstractArrow"),
animal_class: class(j, "net/minecraft/world/entity/animal/Animal"),
item_class: class(j, "net/minecraft/world/entity/item/ItemEntity"),
living_class: living,
m_entities_for_rendering: want!(
missing,
"ClientLevel.entitiesForRendering()",
j.method(client_level, "entitiesForRendering", "()Ljava/lang/Iterable;")
),
m_iterator: want!(
missing,
"Iterable.iterator()",
j.method(iterable, "iterator", "()Ljava/util/Iterator;")
),
m_has_next: want!(missing, "Iterator.hasNext()", j.method(iterator, "hasNext", "()Z")),
m_next: want!(
missing,
"Iterator.next()",
j.method(iterator, "next", "()Ljava/lang/Object;")
),
m_is_alive: want!(missing, "Entity.isAlive()", j.method(entity, "isAlive", "()Z")),
m_get_bounding_box: want!(
missing,
"Entity.getBoundingBox()",
j.method(entity, "getBoundingBox", "()Lnet/minecraft/world/phys/AABB;")
),
m_get_eye_position: want!(
missing,
"Entity.getEyePosition()",
j.method(entity, "getEyePosition", "()Lnet/minecraft/world/phys/Vec3;")
),
m_get_name: want!(
missing,
"Entity.getName()",
j.method(entity, "getName", "()Lnet/minecraft/network/chat/Component;")
),
m_component_string: component
.and_then(|c| j.method(c, "getString", "()Ljava/lang/String;")),
m_get_max_health: want!(
missing,
"LivingEntity.getMaxHealth()",
j.method(living, "getMaxHealth", "()F")
),
f_min_x: j.field(aabb, "minX", "D"),
f_min_y: j.field(aabb, "minY", "D"),
f_min_z: j.field(aabb, "minZ", "D"),
f_max_x: j.field(aabb, "maxX", "D"),
f_max_y: j.field(aabb, "maxY", "D"),
f_max_z: j.field(aabb, "maxZ", "D"),
m_attack: want!(
missing,
"MultiPlayerGameMode.attack(Player,Entity)",
j.method(
game_mode,
"attack",
"(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/entity/Entity;)V"
)
),
m_swing: want!(
missing,
"LivingEntity.swing(InteractionHand)",
j.method(living, "swing", "(Lnet/minecraft/world/InteractionHand;)V")
),
main_hand,
f_game_renderer: j.field(
mc.minecraft,
"gameRenderer",
"Lnet/minecraft/client/renderer/GameRenderer;",
),
f_main_camera: renderer
.and_then(|c| j.field(c, "mainCamera", "Lnet/minecraft/client/Camera;")),
f_cam_position: camera
.and_then(|c| j.field(c, "position", "Lnet/minecraft/world/phys/Vec3;")),
f_cam_x_rot: camera.and_then(|c| j.field(c, "xRot", "F")),
f_cam_y_rot: camera.and_then(|c| j.field(c, "yRot", "F")),
f_options: j.field(mc.minecraft, "options", "Lnet/minecraft/client/Options;"),
f_opt_fov: options
.and_then(|c| j.field(c, "fov", "Lnet/minecraft/client/OptionInstance;")),
f_opt_gamma: options
.and_then(|c| j.field(c, "gamma", "Lnet/minecraft/client/OptionInstance;")),
f_opt_bob_view: options
.and_then(|c| j.field(c, "bobView", "Lnet/minecraft/client/OptionInstance;")),
f_opt_render_distance: options.and_then(|c| {
j.field(c, "renderDistance", "Lnet/minecraft/client/OptionInstance;")
}),
f_server_render_distance: options
.and_then(|c| j.field(c, "serverRenderDistance", "I")),
m_broadcast_options: options.and_then(|c| j.method(c, "broadcastOptions", "()V")),
m_set_server_render_distance: options
.and_then(|c| j.method(c, "setServerRenderDistance", "(I)V")),
f_chunk_source: class(j, "net/minecraft/client/multiplayer/ClientLevel").and_then(|c| {
j.field(
c,
"chunkSource",
"Lnet/minecraft/client/multiplayer/ClientChunkCache;",
)
}),
m_update_view_radius: class(j, "net/minecraft/client/multiplayer/ClientChunkCache")
.and_then(|c| j.method(c, "updateViewRadius", "(I)V")),
m_loaded_chunks: class(j, "net/minecraft/client/multiplayer/ClientChunkCache")
.and_then(|c| j.method(c, "getLoadedChunksCount", "()I")),
boolean_class: class(j, "java/lang/Boolean"),
m_bool_value_of: class(j, "java/lang/Boolean")
.and_then(|c| j.static_method(c, "valueOf", "(Z)Ljava/lang/Boolean;")),
m_bool_value: class(j, "java/lang/Boolean")
.and_then(|c| j.method(c, "booleanValue", "()Z")),
m_opt_get: option_instance
.and_then(|c| j.method(c, "get", "()Ljava/lang/Object;")),
m_opt_set: option_instance
.and_then(|c| j.method(c, "set", "(Ljava/lang/Object;)V")),
double_class,
m_double_value_of: double_class
.and_then(|c| j.static_method(c, "valueOf", "(D)Ljava/lang/Double;")),
m_double_value: double_class.and_then(|c| j.method(c, "doubleValue", "()D")),
integer_class,
m_int_value_of: integer_class
.and_then(|c| j.static_method(c, "valueOf", "(I)Ljava/lang/Integer;")),
m_int_value: integer_class.and_then(|c| j.method(c, "intValue", "()I")),
f_attributes: j.field(
living,
"attributes",
"Lnet/minecraft/world/entity/ai/attributes/AttributeMap;",
),
m_attr_instance: class(j, "net/minecraft/world/entity/ai/attributes/AttributeMap")
.and_then(|c| {
j.method(
c,
"getInstance",
"(Lnet/minecraft/core/Holder;)Lnet/minecraft/world/entity/ai/attributes/AttributeInstance;",
)
}),
m_attr_set_base: attribute_instance
.and_then(|c| j.method(c, "setBaseValue", "(D)V")),
m_attr_get_base: attribute_instance
.and_then(|c| j.method(c, "getBaseValue", "()D")),
holder_entity_reach: holder(j, attributes, "ENTITY_INTERACTION_RANGE"),
holder_block_reach: holder(j, attributes, "BLOCK_INTERACTION_RANGE"),
holder_step_height: holder(j, attributes, "STEP_HEIGHT"),
f_rain_level: level_class.and_then(|c| j.field(c, "rainLevel", "F")),
f_thunder_level: level_class.and_then(|c| j.field(c, "thunderLevel", "F")),
f_o_rain_level: level_class.and_then(|c| j.field(c, "oRainLevel", "F")),
f_o_thunder_level: level_class.and_then(|c| j.field(c, "oThunderLevel", "F")),
f_touching_water: j.field(entity, "wasTouchingWater", "Z"),
f_horizontal_collision: j.field(entity, "horizontalCollision", "Z"),
m_set_camera_entity: j.method(
mc.minecraft,
"setCameraEntity",
"(Lnet/minecraft/world/entity/Entity;)V",
),
armor_stand_class: armor_stand,
m_armor_stand_init: armor_stand
.and_then(|c| j.method(c, "<init>", "(Lnet/minecraft/world/level/Level;DDD)V")),
f_client_input: j.field(
mc.local_player,
"input",
"Lnet/minecraft/client/player/ClientInput;",
),
f_key_presses: client_input.and_then(|c| {
j.field(c, "keyPresses", "Lnet/minecraft/world/entity/player/Input;")
}),
f_move_vector: client_input
.and_then(|c| j.field(c, "moveVector", "Lnet/minecraft/world/phys/Vec2;")),
input_class: input_cls,
input_empty: input_cls.and_then(|c| {
let f = j.static_field(c, "EMPTY", "Lnet/minecraft/world/entity/player/Input;")?;
let v = j.static_obj_field(c, f)?;
j.global(v)
}),
vec2_zero: class(j, "net/minecraft/world/phys/Vec2").and_then(|c| {
let f = j.static_field(c, "ZERO", "Lnet/minecraft/world/phys/Vec2;")?;
let v = j.static_obj_field(c, f)?;
j.global(v)
}),
f_in_forward: input_cls.and_then(|c| j.field(c, "forward", "Z")),
f_in_back: input_cls.and_then(|c| j.field(c, "backward", "Z")),
f_in_left: input_cls.and_then(|c| j.field(c, "left", "Z")),
f_in_right: input_cls.and_then(|c| j.field(c, "right", "Z")),
f_in_jump: input_cls.and_then(|c| j.field(c, "jump", "Z")),
f_in_shift: input_cls.and_then(|c| j.field(c, "shift", "Z")),
f_hit_result: j.field(
mc.minecraft,
"hitResult",
"Lnet/minecraft/world/phys/HitResult;",
),
entity_hit_class: entity_hit,
m_hit_get_entity: entity_hit.and_then(|c| {
j.method(c, "getEntity", "()Lnet/minecraft/world/entity/Entity;")
}),
})
}
/// The level's entity iterator, ready to walk.
pub fn entity_iterator(&self, j: &Jni, level: jobject) -> Option<jobject> {
let iterable = j.call_obj(level, self.m_entities_for_rendering?, &[])?;
let it = j.call_obj(iterable, self.m_iterator?, &[]);
j.delete_local(iterable);
it
}
pub fn iter_next(&self, j: &Jni, iterator: jobject) -> Option<jobject> {
if !j.call_bool(iterator, self.m_has_next?, &[])? {
return None;
}
j.call_obj(iterator, self.m_next?, &[])
}
pub fn is_alive(&self, j: &Jni, entity: jobject) -> bool {
self.m_is_alive
.and_then(|m| j.call_bool(entity, m, &[]))
.unwrap_or(false)
}
pub fn classify(&self, j: &Jni, entity: jobject) -> TargetKind {
if j.is_instance(entity, self.player_class) {
return TargetKind::Player;
}
if let Some(c) = self.item_class {
if j.is_instance(entity, c) {
return TargetKind::Item;
}
}
if let Some(c) = self.monster_class {
if j.is_instance(entity, c) {
return TargetKind::Mob;
}
}
if let Some(c) = self.animal_class {
if j.is_instance(entity, c) {
return TargetKind::Animal;
}
}
TargetKind::Other
}
pub fn is_arrow(&self, j: &Jni, entity: jobject) -> bool {
match self.arrow_class {
Some(c) => j.is_instance(entity, c),
None => false,
}
}
pub fn is_living(&self, j: &Jni, entity: jobject) -> bool {
j.is_instance(entity, self.living_class)
}
/// World-space bounding box, which is what an ESP box is drawn from.
pub fn bounding_box(&self, j: &Jni, entity: jobject) -> Option<((f64, f64, f64), (f64, f64, f64))> {
let bb = j.call_obj(entity, self.m_get_bounding_box?, &[])?;
let out = (
(
j.double_field(bb, self.f_min_x?)?,
j.double_field(bb, self.f_min_y?)?,
j.double_field(bb, self.f_min_z?)?,
),
(
j.double_field(bb, self.f_max_x?)?,
j.double_field(bb, self.f_max_y?)?,
j.double_field(bb, self.f_max_z?)?,
),
);
j.delete_local(bb);
Some(out)
}
pub fn name(&self, j: &Jni, entity: jobject) -> Option<String> {
let component = j.call_obj(entity, self.m_get_name?, &[])?;
let s = j.call_obj(component, self.m_component_string?, &[])?;
let out = j.rust_string(s);
j.delete_local(s);
j.delete_local(component);
out
}
pub fn max_health(&self, j: &Jni, entity: jobject) -> f32 {
self.m_get_max_health
.and_then(|m| j.call_float(entity, m, &[]))
.unwrap_or(0.0)
}
/// Hit something, through the game's own attack path.
pub fn attack(&self, j: &Jni, game_mode: jobject, player: jobject, target: jobject) {
let (Some(attack), Some(swing), Some(hand)) = (self.m_attack, self.m_swing, self.main_hand)
else {
return;
};
j.call_void(
game_mode,
attack,
&[jvalue { l: player }, jvalue { l: target }],
);
j.call_void(player, swing, &[jvalue { l: hand }]);
}
/// Where the camera actually is, which is not the player when the view is
/// in third person.
pub fn camera(&self, j: &Jni, mc: &Mc, instance: jobject) -> Option<((f64, f64, f64), f32, f32)> {
let renderer = j.obj_field(instance, self.f_game_renderer?)?;
let camera = j.obj_field(renderer, self.f_main_camera?)?;
let pos = j.obj_field(camera, self.f_cam_position?)?;
let out = (
(
j.double_field(pos, mc.f_vx)?,
j.double_field(pos, mc.f_vy)?,
j.double_field(pos, mc.f_vz)?,
),
j.float_field(camera, self.f_cam_y_rot?)?,
j.float_field(camera, self.f_cam_x_rot?)?,
);
Some(out)
}
fn option(&self, j: &Jni, instance: jobject, which: Option<jfieldID>) -> Option<jobject> {
let options = j.obj_field(instance, self.f_options?)?;
j.obj_field(options, which?)
}
pub fn fov(&self, j: &Jni, instance: jobject) -> Option<f32> {
let opt = self.option(j, instance, self.f_opt_fov)?;
let boxed = j.call_obj(opt, self.m_opt_get?, &[])?;
let v = j.call_int(boxed, self.m_int_value?, &[])?;
j.delete_local(boxed);
Some(v as f32)
}
pub fn set_fov(&self, j: &Jni, instance: jobject, value: i32) {
let (Some(opt), Some(set), Some(value_of), Some(cls)) = (
self.option(j, instance, self.f_opt_fov),
self.m_opt_set,
self.m_int_value_of,
self.integer_class,
) else {
return;
};
// SAFETY: a static call with a matching descriptor.
if let Some(boxed) = j.call_static_obj(cls, value_of, &[jvalue { i: value }]) {
j.call_void(opt, set, &[jvalue { l: boxed }]);
j.delete_local(boxed);
}
}
pub fn bob_view(&self, j: &Jni, instance: jobject) -> Option<bool> {
let opt = self.option(j, instance, self.f_opt_bob_view)?;
let boxed = j.call_obj(opt, self.m_opt_get?, &[])?;
let v = j.call_bool(boxed, self.m_bool_value?, &[])?;
j.delete_local(boxed);
Some(v)
}
pub fn set_bob_view(&self, j: &Jni, instance: jobject, value: bool) {
let (Some(opt), Some(set), Some(value_of), Some(cls)) = (
self.option(j, instance, self.f_opt_bob_view),
self.m_opt_set,
self.m_bool_value_of,
self.boolean_class,
) else {
return;
};
if let Some(boxed) = j.call_static_obj(cls, value_of, &[jvalue { z: value as u8 }]) {
j.call_void(opt, set, &[jvalue { l: boxed }]);
j.delete_local(boxed);
}
}
// ---- view distance ---------------------------------------------------
//
// A server sends chunks out to the smaller of its own view distance and the
// one the client asked for in its settings packet. So asking for more is
// not a trick: it is the documented way to get more, and it works whenever
// the server's limit is above what you were requesting.
pub fn render_distance(&self, j: &Jni, instance: jobject) -> Option<i32> {
let opt = self.option(j, instance, self.f_opt_render_distance)?;
let boxed = j.call_obj(opt, self.m_opt_get?, &[])?;
let v = j.call_int(boxed, self.m_int_value?, &[])?;
j.delete_local(boxed);
Some(v)
}
/// Ask for `chunks` of view distance and tell the server about it.
pub fn request_view_distance(&self, j: &Jni, instance: jobject, chunks: i32) {
let Some(options) = self.f_options.and_then(|f| j.obj_field(instance, f)) else {
return;
};
if let (Some(opt), Some(set), Some(value_of), Some(cls)) = (
self.option(j, instance, self.f_opt_render_distance),
self.m_opt_set,
self.m_int_value_of,
self.integer_class,
) {
if let Some(boxed) = j.call_static_obj(cls, value_of, &[jvalue { i: chunks }]) {
j.call_void(opt, set, &[jvalue { l: boxed }]);
j.delete_local(boxed);
}
}
// The effective distance is capped by whatever the server announced, so
// raise our copy of that too or the extra chunks are held back locally.
if let Some(m) = self.m_set_server_render_distance {
j.call_void(options, m, &[jvalue { i: chunks }]);
} else if let Some(f) = self.f_server_render_distance {
j.set_int(options, f, chunks);
}
// And send the settings packet, which is the actual request.
if let Some(m) = self.m_broadcast_options {
j.call_void(options, m, &[]);
}
}
/// Grow the client's chunk store so what does arrive is kept rather than
/// dropped the moment you move.
pub fn update_view_radius(&self, j: &Jni, level: jobject, chunks: i32) {
let (Some(f), Some(m)) = (self.f_chunk_source, self.m_update_view_radius) else {
return;
};
let Some(source) = j.obj_field(level, f) else {
return;
};
j.call_void(source, m, &[jvalue { i: chunks }]);
}
pub fn loaded_chunks(&self, j: &Jni, level: jobject) -> i32 {
let (Some(f), Some(m)) = (self.f_chunk_source, self.m_loaded_chunks) else {
return 0;
};
let Some(source) = j.obj_field(level, f) else {
return 0;
};
j.call_int(source, m, &[]).unwrap_or(0)
}
pub fn gamma(&self, j: &Jni, instance: jobject) -> Option<f64> {
let opt = self.option(j, instance, self.f_opt_gamma)?;
let boxed = j.call_obj(opt, self.m_opt_get?, &[])?;
let v = j.call_double(boxed, self.m_double_value?, &[])?;
j.delete_local(boxed);
Some(v)
}
pub fn set_gamma(&self, j: &Jni, instance: jobject, value: f64) {
let (Some(opt), Some(set), Some(value_of), Some(cls)) = (
self.option(j, instance, self.f_opt_gamma),
self.m_opt_set,
self.m_double_value_of,
self.double_class,
) else {
return;
};
if let Some(boxed) = j.call_static_obj(cls, value_of, &[jvalue { d: value }]) {
j.call_void(opt, set, &[jvalue { l: boxed }]);
j.delete_local(boxed);
}
}
}
impl World {
/// One attribute of an entity, e.g. how far it can reach.
fn attribute(&self, j: &Jni, entity: jobject, holder: Option<jobject>) -> Option<jobject> {
let map = j.obj_field(entity, self.f_attributes?)?;
j.call_obj(map, self.m_attr_instance?, &[jvalue { l: holder? }])
}
pub fn attribute_base(&self, j: &Jni, entity: jobject, holder: Option<jobject>) -> Option<f64> {
let inst = self.attribute(j, entity, holder)?;
j.call_double(inst, self.m_attr_get_base?, &[])
}
pub fn set_attribute_base(
&self,
j: &Jni,
entity: jobject,
holder: Option<jobject>,
value: f64,
) -> bool {
let (Some(inst), Some(set)) = (self.attribute(j, entity, holder), self.m_attr_set_base)
else {
return false;
};
j.call_void(inst, set, &[jvalue { d: value }]);
true
}
pub fn in_water(&self, j: &Jni, entity: jobject) -> bool {
self.f_touching_water
.and_then(|f| j.bool_field(entity, f))
.unwrap_or(false)
}
pub fn hitting_wall(&self, j: &Jni, entity: jobject) -> bool {
self.f_horizontal_collision
.and_then(|f| j.bool_field(entity, f))
.unwrap_or(false)
}
/// Rain and thunder are plain interpolated floats on the level; zeroing
/// both (and their previous-tick copies, or it flickers) clears the sky.
pub fn set_weather(&self, j: &Jni, level: jobject, value: f32) {
for f in [
self.f_rain_level,
self.f_thunder_level,
self.f_o_rain_level,
self.f_o_thunder_level,
]
.into_iter()
.flatten()
{
j.set_float(level, f, value);
}
}
// ---- freecam ---------------------------------------------------------
/// Build a camera to fly around with. It is never added to the level, so
/// nothing ticks it, nothing renders it, and nothing about it reaches the
/// server — it exists only for the camera to sit on.
pub fn new_camera_entity(
&self,
j: &Jni,
level: jobject,
pos: (f64, f64, f64),
) -> Option<jobject> {
let local = j.new_object(
self.armor_stand_class?,
self.m_armor_stand_init?,
&[
jvalue { l: level },
jvalue { d: pos.0 },
jvalue { d: pos.1 },
jvalue { d: pos.2 },
],
)?;
let global = j.global(local);
j.delete_local(local);
global
}
pub fn set_camera_entity(&self, j: &Jni, instance: jobject, entity: jobject) {
if let Some(m) = self.m_set_camera_entity {
j.call_void(instance, m, &[jvalue { l: entity }]);
}
}
/// How far above an entity's feet its eyes sit, so the camera can be placed
/// by where you want to be looking from.
pub fn eye_offset(&self, j: &Jni, mc: &Mc, entity: jobject) -> f64 {
let Some(m) = self.m_get_eye_position else {
return 0.0;
};
let Some(eye) = j.call_obj(entity, m, &[]) else {
return 0.0;
};
let y = j.double_field(eye, mc.f_vy).unwrap_or(0.0);
j.delete_local(eye);
let feet = mc.position(j, entity).map(|p| p.1).unwrap_or(y);
y - feet
}
/// What the player is pressing this frame.
pub fn movement_keys(&self, j: &Jni, player: jobject) -> Keys {
let mut keys = Keys::default();
let Some(input) = self.f_client_input.and_then(|f| j.obj_field(player, f)) else {
return keys;
};
let Some(presses) = self.f_key_presses.and_then(|f| j.obj_field(input, f)) else {
return keys;
};
let read = |field: Option<jfieldID>| {
field.and_then(|f| j.bool_field(presses, f)).unwrap_or(false)
};
keys.forward = read(self.f_in_forward);
keys.backward = read(self.f_in_back);
keys.left = read(self.f_in_left);
keys.right = read(self.f_in_right);
keys.up = read(self.f_in_jump);
keys.down = read(self.f_in_shift);
keys
}
/// Take the controls away from the body, so it stands still while the
/// camera flies.
pub fn clear_movement(&self, j: &Jni, player: jobject) {
let Some(input) = self.f_client_input.and_then(|f| j.obj_field(player, f)) else {
return;
};
if let (Some(f), Some(empty)) = (self.f_key_presses, self.input_empty) {
j.set_obj(input, f, empty);
}
if let (Some(f), Some(zero)) = (self.f_move_vector, self.vec2_zero) {
j.set_obj(input, f, zero);
}
}
/// Place an entity with no interpolation smear: the previous-tick and
/// render-previous copies are moved with it.
pub fn place(&self, j: &Jni, mc: &Mc, entity: jobject, pos: (f64, f64, f64)) {
mc.set_pos(j, entity, pos.0, pos.1, pos.2);
for (name, v) in [("xo", pos.0), ("yo", pos.1), ("zo", pos.2)] {
let _ = name;
let _ = v;
}
mc.set_old_position(j, entity, pos);
}
/// Point an entity, with no interpolation smear.
pub fn aim(&self, j: &Jni, mc: &Mc, entity: jobject, yaw: f32, pitch: f32) {
mc.aim_camera(j, entity, yaw, pitch);
}
/// Whatever the crosshair is on, if it is an entity.
pub fn crosshair_entity(&self, j: &Jni, instance: jobject) -> Option<jobject> {
let hit = j.obj_field(instance, self.f_hit_result?)?;
if !j.is_instance(hit, self.entity_hit_class?) {
return None;
}
j.call_obj(hit, self.m_hit_get_entity?, &[])
}
}
/// A static `Holder` constant off the Attributes class, pinned for reuse.
fn holder(j: &Jni, attributes: Option<jclass>, name: &str) -> Option<jobject> {
let c = attributes?;
let f = j.static_field(c, name, "Lnet/minecraft/core/Holder;")?;
let v = j.static_obj_field(c, f)?;
j.global(v)
}
/// Which way the player is asking to move.
#[derive(Default, Clone, Copy)]
pub struct Keys {
pub forward: bool,
pub backward: bool,
pub left: bool,
pub right: bool,
pub up: bool,
pub down: bool,
}
/// What a scanned entity is, for filtering and colouring.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum TargetKind {
Player,
Mob,
Animal,
Item,
Other,
}