-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
NMS.java
615 lines (517 loc) · 21.6 KB
/
NMS.java
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
package net.citizensnpcs.util;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Enderman;
import org.bukkit.entity.Entity;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Wither;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Vector;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.GameProfileRepository;
import net.citizensnpcs.api.ai.NavigatorParameters;
import net.citizensnpcs.api.command.CommandManager;
import net.citizensnpcs.api.command.exception.CommandException;
import net.citizensnpcs.api.npc.BlockBreaker;
import net.citizensnpcs.api.npc.BlockBreaker.BlockBreakerConfiguration;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.util.BoundingBox;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.ai.MCNavigationStrategy.MCNavigator;
import net.citizensnpcs.npc.ai.MCTargetStrategy.TargetNavigator;
import net.citizensnpcs.npc.skin.SkinnableEntity;
public class NMS {
private NMS() {
// util class
}
public static boolean addEntityToWorld(org.bukkit.entity.Entity entity, SpawnReason custom) {
return BRIDGE.addEntityToWorld(entity, custom);
}
public static void addOrRemoveFromPlayerList(org.bukkit.entity.Entity entity, boolean remove) {
BRIDGE.addOrRemoveFromPlayerList(entity, remove);
}
public static void attack(LivingEntity attacker, LivingEntity bukkitTarget) {
BRIDGE.attack(attacker, bukkitTarget);
}
/*
* Yggdrasil's default implementation of this method silently fails instead of throwing
* an Exception like it should.
*/
public static GameProfile fillProfileProperties(GameProfile profile, boolean requireSecure) throws Throwable {
return BRIDGE.fillProfileProperties(profile, requireSecure);
}
public static BlockBreaker getBlockBreaker(Entity entity, Block targetBlock, BlockBreakerConfiguration config) {
return BRIDGE.getBlockBreaker(entity, targetBlock, config);
}
public static Object getBossBar(Entity entity) {
return BRIDGE.getBossBar(entity);
}
public static BoundingBox getBoundingBox(org.bukkit.entity.Entity handle) {
return BRIDGE.getBoundingBox(handle);
}
public static BoundingBox getCollisionBox(Block block) {
return BRIDGE.getCollisionBox(block).add(block.getX(), block.getY(), block.getZ());
}
public static Field getField(Class<?> clazz, String field) {
return getField(clazz, field, true);
}
public static Field getField(Class<?> clazz, String field, boolean log) {
if (clazz == null)
return null;
Field f = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
return f;
} catch (Exception e) {
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getLocalizedMessage());
}
return null;
}
}
public static MethodHandle getFinalSetter(Class<?> clazz, String field) {
return getFinalSetter(clazz, field, true);
}
public static MethodHandle getFinalSetter(Class<?> clazz, String field, boolean log) {
Field f;
if (MODIFIERS_FIELD == null) {
if (UNSAFE == null) {
try {
UNSAFE = NMS.getField(Class.forName("sun.misc.Unsafe"), "theUnsafe").get(null);
} catch (Exception e) {
e.printStackTrace();
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getLocalizedMessage());
}
return null;
}
UNSAFE_STATIC_FIELD_OFFSET = NMS
.getMethodHandle(UNSAFE.getClass(), "staticFieldOffset", true, Field.class).bindTo(UNSAFE);
UNSAFE_FIELD_OFFSET = NMS.getMethodHandle(UNSAFE.getClass(), "objectFieldOffset", true, Field.class)
.bindTo(UNSAFE);
UNSAFE_PUT_OBJECT = NMS
.getMethodHandle(UNSAFE.getClass(), "putObject", true, Object.class, long.class, Object.class)
.bindTo(UNSAFE);
}
f = NMS.getField(clazz, field, log);
if (f == null) {
return null;
}
try {
boolean isStatic = Modifier.isStatic(f.getModifiers());
long offset = (long) (isStatic ? UNSAFE_STATIC_FIELD_OFFSET.invoke(f) : UNSAFE_FIELD_OFFSET.invoke(f));
return isStatic ? MethodHandles.insertArguments(UNSAFE_PUT_OBJECT, 0, clazz, offset)
: MethodHandles.insertArguments(UNSAFE_PUT_OBJECT, 1, offset);
} catch (Throwable t) {
t.printStackTrace();
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, t.getLocalizedMessage());
}
return null;
}
}
f = getField(clazz, field, log);
if (f == null) {
return null;
}
try {
MODIFIERS_FIELD.setInt(f, f.getModifiers() & ~Modifier.FINAL);
} catch (Exception e) {
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getLocalizedMessage());
}
return null;
}
try {
return LOOKUP.unreflectSetter(f);
} catch (Exception e) {
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getLocalizedMessage());
}
}
return null;
}
private static Field getFirstFieldMatchingType(Class<?> clazz, Class<?> type, boolean allowStatic) {
Field found = null;
for (Field field : clazz.getDeclaredFields()) {
if (allowStatic ^ Modifier.isStatic(field.getModifiers()))
continue;
if (field.getType() == type) {
found = field;
break;
}
}
if (found != null) {
found.setAccessible(true);
}
return found;
}
public static MethodHandle getFirstGetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type, false);
if (found == null)
return null;
return LOOKUP.unreflectGetter(found);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
return null;
}
public static MethodHandle getFirstMethodHandle(Class<?> clazz, boolean log, Class<?>... params) {
if (clazz == null)
return null;
try {
Method first = null;
for (Method method : clazz.getDeclaredMethods()) {
Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes.length == params.length) {
first = method;
for (int i = 0; i < paramTypes.length; i++) {
if (paramTypes[i] != params[i]) {
first = null;
}
}
if (first != null) {
break;
}
}
}
if (first == null)
return null;
first.setAccessible(true);
return LOOKUP.unreflect(first);
} catch (Exception e) {
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_METHOD, e.getLocalizedMessage());
}
}
return null;
}
public static MethodHandle getFirstSetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type, false);
if (found == null)
return null;
return LOOKUP.unreflectSetter(found);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
return null;
}
public static MethodHandle getFirstStaticGetter(Class<?> clazz, Class<?> type) {
try {
Field found = getFirstFieldMatchingType(clazz, type, true);
if (found == null)
return null;
return LOOKUP.unreflectGetter(found);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, type, e.getLocalizedMessage());
}
return null;
}
public static GameProfileRepository getGameProfileRepository() {
return BRIDGE.getGameProfileRepository();
}
public static MethodHandle getGetter(Class<?> clazz, String name) {
return getGetter(clazz, name, true);
}
public static MethodHandle getGetter(Class<?> clazz, String name, boolean log) {
try {
return LOOKUP.unreflectGetter(getField(clazz, name, log));
} catch (Exception e) {
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, name, e.getLocalizedMessage());
}
}
return null;
}
public static float getHeadYaw(org.bukkit.entity.Entity entity) {
return BRIDGE.getHeadYaw(entity);
}
public static double getHeight(Entity entity) {
return BRIDGE.getHeight(entity);
}
public static float getHorizontalMovement(org.bukkit.entity.Entity bukkitEntity) {
return BRIDGE.getHorizontalMovement(bukkitEntity);
}
public static Method getMethod(Class<?> clazz, String method, boolean log, Class<?>... params) {
if (clazz == null)
return null;
Method f = null;
try {
f = clazz.getDeclaredMethod(method, params);
f.setAccessible(true);
} catch (Exception e) {
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_METHOD, method, e.getLocalizedMessage());
}
}
return f;
}
public static MethodHandle getMethodHandle(Class<?> clazz, String method, boolean log, Class<?>... params) {
if (clazz == null)
return null;
try {
return LOOKUP.unreflect(getMethod(clazz, method, log, params));
} catch (Exception e) {
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_METHOD, method, e.getLocalizedMessage());
}
}
return null;
}
public static NPC getNPC(Entity entity) {
return BRIDGE.getNPC(entity);
}
public static List<org.bukkit.entity.Entity> getPassengers(org.bukkit.entity.Entity entity) {
return BRIDGE.getPassengers(entity);
}
public static GameProfile getProfile(SkullMeta meta) {
return BRIDGE.getProfile(meta);
}
public static MethodHandle getSetter(Class<?> clazz, String name) {
return getSetter(clazz, name, true);
}
public static MethodHandle getSetter(Class<?> clazz, String name, boolean log) {
try {
return LOOKUP.unreflectSetter(getField(clazz, name, log));
} catch (Exception e) {
if (log) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, name, e.getLocalizedMessage());
}
}
return null;
}
public static String getSound(String flag) throws CommandException {
return BRIDGE.getSound(flag);
}
public static float getSpeedFor(NPC npc) {
return BRIDGE.getSpeedFor(npc);
}
public static float getStepHeight(org.bukkit.entity.Entity entity) {
return BRIDGE.getStepHeight(entity);
}
public static MCNavigator getTargetNavigator(Entity entity, Iterable<Vector> dest, NavigatorParameters params) {
return BRIDGE.getTargetNavigator(entity, dest, params);
}
public static MCNavigator getTargetNavigator(Entity entity, Location dest, NavigatorParameters params) {
return BRIDGE.getTargetNavigator(entity, dest, params);
}
public static TargetNavigator getTargetNavigator(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity target,
NavigatorParameters parameters) {
return BRIDGE.getTargetNavigator(entity, target, parameters);
}
public static org.bukkit.entity.Entity getVehicle(org.bukkit.entity.Entity entity) {
return BRIDGE.getVehicle(entity);
}
public static float getVerticalMovement(org.bukkit.entity.Entity bukkitEntity) {
return BRIDGE.getVerticalMovement(bukkitEntity);
}
public static double getWidth(Entity entity) {
return BRIDGE.getWidth(entity);
}
public static float getYaw(Entity entity) {
return BRIDGE.getYaw(entity);
}
public static void giveReflectiveAccess(Class<?> from, Class<?> to) {
try {
if (GET_MODULE == null) {
Class<?> module = Class.forName("java.lang.Module");
GET_MODULE = Class.class.getMethod("getModule");
ADD_OPENS = module.getMethod("addOpens", String.class, module);
}
ADD_OPENS.invoke(GET_MODULE.invoke(from), from.getPackage().getName(), GET_MODULE.invoke(to));
} catch (Exception e) {
}
}
public static boolean isOnGround(org.bukkit.entity.Entity entity) {
return BRIDGE.isOnGround(entity);
}
public static boolean isSolid(Block in) {
return BRIDGE.isSolid(in);
}
public static boolean isValid(Entity entity) {
return BRIDGE.isValid(entity);
}
public static void load(CommandManager commands) {
BRIDGE.load(commands);
}
public static void loadBridge(String rev) throws Exception {
Class<?> entity = null;
try {
entity = Class.forName("net.minecraft.server.v" + rev + ".Entity");
} catch (ClassNotFoundException ex) {
entity = Class.forName("net.minecraft.world.entity.Entity");
}
giveReflectiveAccess(entity, NMS.class);
BRIDGE = (NMSBridge) Class.forName("net.citizensnpcs.nms.v" + rev + ".util.NMSImpl").getConstructor()
.newInstance();
}
public static void loadPlugins() {
BRIDGE.loadPlugins();
}
public static void look(Entity entity, float yaw, float pitch) {
BRIDGE.look(entity, yaw, pitch);
}
public static void look(org.bukkit.entity.Entity entity, Location to, boolean headOnly, boolean immediate) {
BRIDGE.look(entity, to, headOnly, immediate);
}
public static void look(org.bukkit.entity.Entity bhandle, org.bukkit.entity.Entity btarget) {
BRIDGE.look(bhandle, btarget);
}
public static void mount(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity passenger) {
BRIDGE.mount(entity, passenger);
}
public static void openHorseScreen(Tameable horse, Player equipper) {
BRIDGE.openHorseScreen(horse, equipper);
}
public static void playAnimation(PlayerAnimation animation, Player player, int radius) {
BRIDGE.playAnimation(animation, player, radius);
}
public static void playerTick(Player entity) {
BRIDGE.playerTick(entity);
}
public static void registerEntityClass(Class<?> clazz) {
BRIDGE.registerEntityClass(clazz);
}
public static void remove(Entity entity) {
BRIDGE.remove(entity);
}
public static void removeFromServerPlayerList(Player player) {
BRIDGE.removeFromServerPlayerList(player);
}
public static void removeFromWorld(org.bukkit.entity.Entity entity) {
BRIDGE.removeFromWorld(entity);
}
public static void removeHookIfNecessary(NPCRegistry npcRegistry, FishHook entity) {
BRIDGE.removeHookIfNecessary(npcRegistry, entity);
}
public static void replaceTrackerEntry(Player player) {
BRIDGE.replaceTrackerEntry(player);
}
public static void sendPositionUpdate(Player excluding, org.bukkit.entity.Entity from, Location storedLocation) {
BRIDGE.sendPositionUpdate(excluding, from, storedLocation);
}
public static void sendTabListAdd(Player recipient, Player listPlayer) {
BRIDGE.sendTabListAdd(recipient, listPlayer);
}
public static void sendTabListRemove(Player recipient, Collection<? extends SkinnableEntity> skinnableNPCs) {
BRIDGE.sendTabListRemove(recipient, skinnableNPCs);
}
public static void sendTabListRemove(Player recipient, Player listPlayer) {
BRIDGE.sendTabListRemove(recipient, listPlayer);
}
public static void sendTeamPacket(Player recipient, Team team, int mode) {
BRIDGE.sendTeamPacket(recipient, team, mode);
}
public static void setBodyYaw(Entity entity, float yaw) {
BRIDGE.setBodyYaw(entity, yaw);
}
public static void setDestination(org.bukkit.entity.Entity entity, double x, double y, double z, float speed) {
BRIDGE.setDestination(entity, x, y, z, speed);
}
public static void setEndermanAngry(Enderman enderman, boolean angry) {
BRIDGE.setEndermanAngry(enderman, angry);
}
public static void setHeadYaw(org.bukkit.entity.Entity entity, float yaw) {
BRIDGE.setHeadYaw(entity, yaw);
}
public static void setKnockbackResistance(org.bukkit.entity.LivingEntity entity, double d) {
BRIDGE.setKnockbackResistance(entity, d);
}
public static void setLyingDown(Entity cat, boolean lying) {
BRIDGE.setLyingDown(cat, lying);
}
public static void setNavigationTarget(org.bukkit.entity.Entity handle, org.bukkit.entity.Entity target,
float speed) {
BRIDGE.setNavigationTarget(handle, target, speed);
}
public static void setNoGravity(Entity entity, boolean enabled) {
BRIDGE.setNoGravity(entity, enabled);
}
public static void setPandaSitting(Entity entity, boolean sitting) {
BRIDGE.setPandaSitting(entity, sitting);
}
public static void setPeekShulker(org.bukkit.entity.Entity entity, int peek) {
if (!entity.getType().name().equals("SHULKER")) {
throw new IllegalArgumentException("entity must be a shulker");
}
BRIDGE.setPeekShulker(entity, peek);
}
public static void setPolarBearRearing(Entity entity, boolean rearing) {
BRIDGE.setPolarBearRearing(entity, rearing);
}
public static void setProfile(SkullMeta meta, GameProfile profile) {
BRIDGE.setProfile(meta, profile);
}
public static void setShouldJump(org.bukkit.entity.Entity entity) {
BRIDGE.setShouldJump(entity);
}
public static void setSitting(Ocelot ocelot, boolean sitting) {
BRIDGE.setSitting(ocelot, sitting);
}
public static void setSitting(Tameable tameable, boolean sitting) {
BRIDGE.setSitting(tameable, sitting);
}
public static void setStepHeight(org.bukkit.entity.Entity entity, float height) {
BRIDGE.setStepHeight(entity, height);
}
public static void setTeamNameTagVisible(Team team, boolean visible) {
BRIDGE.setTeamNameTagVisible(team, visible);
}
public static void setVerticalMovement(org.bukkit.entity.Entity bukkitEntity, double d) {
BRIDGE.setVerticalMovement(bukkitEntity, d);
}
public static void setWitherCharged(Wither wither, boolean charged) {
BRIDGE.setWitherCharged(wither, charged);
}
public static boolean shouldJump(org.bukkit.entity.Entity entity) {
return BRIDGE.shouldJump(entity);
}
public static void shutdown() {
BRIDGE.shutdown();
}
public static boolean tick(Entity next) {
return BRIDGE.tick(next);
}
public static void trySwim(org.bukkit.entity.Entity entity) {
BRIDGE.trySwim(entity);
}
public static void trySwim(org.bukkit.entity.Entity entity, float power) {
BRIDGE.trySwim(entity, power);
}
public static void updateNavigationWorld(org.bukkit.entity.Entity entity, org.bukkit.World world) {
BRIDGE.updateNavigationWorld(entity, world);
}
public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
BRIDGE.updatePathfindingRange(npc, pathfindingRange);
}
private static Method ADD_OPENS;
private static NMSBridge BRIDGE;
private static Method GET_MODULE;
private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static Field MODIFIERS_FIELD;
private static Object UNSAFE;
private static MethodHandle UNSAFE_FIELD_OFFSET;
private static MethodHandle UNSAFE_PUT_OBJECT;
private static MethodHandle UNSAFE_STATIC_FIELD_OFFSET;
static {
giveReflectiveAccess(Field.class, NMS.class);
MODIFIERS_FIELD = NMS.getField(Field.class, "modifiers", false);
}
}