-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
NMS.java
452 lines (396 loc) · 17.2 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
package net.citizensnpcs.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.WeakHashMap;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.entity.EntityHumanNPC;
import net.minecraft.server.v1_6_R2.AttributeInstance;
import net.minecraft.server.v1_6_R2.ControllerJump;
import net.minecraft.server.v1_6_R2.DamageSource;
import net.minecraft.server.v1_6_R2.EnchantmentManager;
import net.minecraft.server.v1_6_R2.Entity;
import net.minecraft.server.v1_6_R2.EntityHorse;
import net.minecraft.server.v1_6_R2.EntityHuman;
import net.minecraft.server.v1_6_R2.EntityInsentient;
import net.minecraft.server.v1_6_R2.EntityLiving;
import net.minecraft.server.v1_6_R2.EntityPlayer;
import net.minecraft.server.v1_6_R2.EntityTypes;
import net.minecraft.server.v1_6_R2.GenericAttributes;
import net.minecraft.server.v1_6_R2.MathHelper;
import net.minecraft.server.v1_6_R2.MobEffectList;
import net.minecraft.server.v1_6_R2.Navigation;
import net.minecraft.server.v1_6_R2.NetworkManager;
import net.minecraft.server.v1_6_R2.Packet;
import net.minecraft.server.v1_6_R2.PathfinderGoalSelector;
import net.minecraft.server.v1_6_R2.World;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_6_R2.CraftServer;
import org.bukkit.craftbukkit.v1_6_R2.CraftWorld;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity;
import org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginLoadOrder;
@SuppressWarnings("unchecked")
public class NMS {
private NMS() {
// util class
}
public static void addOrRemoveFromPlayerList(LivingEntity bukkitEntity, boolean remove) {
if (bukkitEntity == null)
return;
EntityLiving handle = getHandle(bukkitEntity);
if (handle.world == null)
return;
if (remove) {
handle.world.players.remove(handle);
} else if (!handle.world.players.contains(handle)) {
handle.world.players.add(handle);
}
}
public static void attack(EntityLiving handle, Entity target) {
AttributeInstance attribute = handle.getAttributeInstance(GenericAttributes.e);
float damage = (float) (attribute == null ? 1D : attribute.getValue());
if (handle.hasEffect(MobEffectList.INCREASE_DAMAGE)) {
damage += 3 << handle.getEffect(MobEffectList.INCREASE_DAMAGE).getAmplifier();
}
if (handle.hasEffect(MobEffectList.WEAKNESS)) {
damage -= 2 << handle.getEffect(MobEffectList.WEAKNESS).getAmplifier();
}
int knockbackLevel = 0;
if (target instanceof EntityLiving) {
damage += EnchantmentManager.a(handle, (EntityLiving) target);
knockbackLevel += EnchantmentManager.getKnockbackEnchantmentLevel(handle, (EntityLiving) target);
}
boolean success = target.damageEntity(DamageSource.mobAttack(handle), damage);
if (!success)
return;
if (knockbackLevel > 0) {
target.g(-MathHelper.sin((float) (handle.yaw * Math.PI / 180.0F)) * knockbackLevel * 0.5F, 0.1D,
MathHelper.cos((float) (handle.yaw * Math.PI / 180.0F)) * knockbackLevel * 0.5F);
handle.motX *= 0.6D;
handle.motZ *= 0.6D;
}
int fireAspectLevel = EnchantmentManager.getFireAspectEnchantmentLevel(handle);
if (fireAspectLevel > 0)
target.setOnFire(fireAspectLevel * 4);
}
public static void changeWorlds(org.bukkit.entity.Entity entity, org.bukkit.World world) {
getHandle(entity).world = ((CraftWorld) world).getHandle();
}
public static void clearGoals(PathfinderGoalSelector... goalSelectors) {
if (GOAL_FIELD == null || goalSelectors == null)
return;
for (PathfinderGoalSelector selector : goalSelectors) {
try {
List<?> list = (List<?>) NMS.GOAL_FIELD.get(selector);
list.clear();
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_CLEARING_GOALS, e.getLocalizedMessage());
}
}
}
public static void openHorseScreen(Horse horse, Player equipper) {
((EntityHorse) getHandle(horse)).f((EntityHuman) NMS.getHandle(equipper));
}
private static Constructor<?> getCustomEntityConstructor(Class<?> clazz, EntityType type) throws SecurityException,
NoSuchMethodException {
Constructor<?> constructor = ENTITY_CONSTRUCTOR_CACHE.get(clazz);
if (constructor == null) {
constructor = clazz.getConstructor(World.class);
constructor.setAccessible(true);
ENTITY_CLASS_TO_INT.put(clazz, (int) type.getTypeId());
ENTITY_CONSTRUCTOR_CACHE.put(clazz, constructor);
}
return constructor;
}
public static Field getField(Class<?> clazz, String field) {
Field f = null;
try {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_FIELD, field, e.getLocalizedMessage());
}
return f;
}
public static EntityLiving getHandle(LivingEntity entity) {
return ((CraftLivingEntity) entity).getHandle();
}
public static net.minecraft.server.v1_6_R2.Entity getHandle(org.bukkit.entity.Entity entity) {
return ((CraftEntity) entity).getHandle();
}
public static float getHeadYaw(EntityLiving handle) {
return handle.aP;
}
public static Navigation getNavigation(EntityLiving handle) {
return handle instanceof EntityInsentient ? ((EntityInsentient) handle).getNavigation()
: handle instanceof EntityHumanNPC ? ((EntityHumanNPC) handle).getNavigation() : null;
}
public static org.bukkit.entity.Entity getRootVehicle(org.bukkit.entity.Entity entity) {
Entity handle = NMS.getHandle(entity);
while (handle.vehicle != null) {
handle = handle.vehicle;
}
return handle.getBukkitEntity();
}
public static float getSpeedFor(NPC npc) {
if (!npc.isSpawned())
return DEFAULT_SPEED;
// this is correct, but too slow. TODO: investigate
// return (float)
// NMS.getHandle(npc.getBukkitEntity()).getAttributeInstance(GenericAttributes.d).getValue();
return DEFAULT_SPEED;
}
public static boolean inWater(LivingEntity entity) {
EntityLiving mcEntity = getHandle(entity);
return mcEntity.G() || mcEntity.I();
}
public static boolean isNavigationFinished(Navigation navigation) {
return navigation.g();
}
public static void loadPlugins() {
((CraftServer) Bukkit.getServer()).enablePlugins(PluginLoadOrder.POSTWORLD);
}
public static void look(EntityLiving handle, Entity target) {
if (handle instanceof EntityInsentient) {
((EntityInsentient) handle).getControllerLook().a(target, 10.0F, ((EntityInsentient) handle).bp());
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).setTargetLook(target, 10F, 40);
}
}
public static void look(LivingEntity bukkitEntity, float yaw, float pitch) {
EntityLiving handle = getHandle(bukkitEntity);
handle.yaw = yaw;
setHeadYaw(handle, yaw);
handle.pitch = pitch;
}
public static float modifiedSpeed(float baseSpeed, NPC npc) {
return npc == null ? baseSpeed : baseSpeed * npc.getNavigator().getLocalParameters().speedModifier();
}
public static void mount(org.bukkit.entity.Entity entity, org.bukkit.entity.Entity passenger) {
NMS.getHandle(passenger).mount(NMS.getHandle(entity));
}
public static void registerEntityClass(Class<?> clazz) {
if (ENTITY_CLASS_TO_INT.containsKey(clazz))
return;
Class<?> search = clazz;
while ((search = search.getSuperclass()) != null && Entity.class.isAssignableFrom(search)) {
if (!ENTITY_CLASS_TO_INT.containsKey(search))
continue;
int code = ENTITY_CLASS_TO_INT.get(search);
ENTITY_INT_TO_CLASS.put(code, clazz);
ENTITY_CLASS_TO_INT.put(clazz, code);
return;
}
throw new IllegalArgumentException("unable to find valid entity superclass");
}
public static void removeFromServerPlayerList(Player player) {
EntityPlayer handle = ((CraftPlayer) player).getHandle();
((CraftServer) Bukkit.getServer()).getHandle().players.remove(handle);
}
public static void sendPacket(Player player, Packet packet) {
if (packet == null)
return;
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
}
public static void sendPacketNearby(Location location, Packet packet) {
NMS.sendPacketsNearby(location, Arrays.asList(packet), 64);
}
public static void sendPacketsNearby(Location location, Collection<Packet> packets) {
NMS.sendPacketsNearby(location, packets, 64);
}
public static void sendPacketsNearby(Location location, Collection<Packet> packets, double radius) {
radius *= radius;
final org.bukkit.World world = location.getWorld();
for (Player ply : Bukkit.getServer().getOnlinePlayers()) {
if (ply == null || world != ply.getWorld()) {
continue;
}
if (location.distanceSquared(ply.getLocation(PACKET_CACHE_LOCATION)) > radius) {
continue;
}
for (Packet packet : packets) {
sendPacket(ply, packet);
}
}
}
public static void sendPacketsNearby(Location location, Packet... packets) {
NMS.sendPacketsNearby(location, Arrays.asList(packets), 64);
}
public static void sendToOnline(Packet... packets) {
Validate.notNull(packets, "packets cannot be null");
for (Player player : Bukkit.getOnlinePlayers()) {
if (player == null || !player.isOnline())
continue;
for (Packet packet : packets) {
sendPacket(player, packet);
}
}
}
public static void setDestination(LivingEntity bukkitEntity, double x, double y, double z, float speed) {
EntityLiving handle = ((CraftLivingEntity) bukkitEntity).getHandle();
if (handle instanceof EntityInsentient) {
((EntityInsentient) handle).getControllerMove().a(x, y, z, speed);
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).setMoveDestination(x, y, z, speed);
}
}
public static void setHeadYaw(EntityLiving handle, float yaw) {
while (yaw < -180.0F) {
yaw += 360.0F;
}
while (yaw >= 180.0F) {
yaw -= 360.0F;
}
handle.aP = yaw;
if (!(handle instanceof EntityHuman))
handle.aN = yaw;
handle.aQ = yaw;
}
public static void setShouldJump(LivingEntity entity) {
EntityLiving handle = getHandle(entity);
if (handle instanceof EntityInsentient) {
ControllerJump controller = ((EntityInsentient) handle).getControllerJump();
controller.a();
} else if (handle instanceof EntityHumanNPC) {
((EntityHumanNPC) handle).setShouldJump();
}
}
public static boolean shouldJump(net.minecraft.server.v1_6_R2.Entity entity) {
if (JUMP_FIELD == null || !(entity instanceof EntityLiving))
return false;
try {
return JUMP_FIELD.getBoolean(entity);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
public static org.bukkit.entity.Entity spawnCustomEntity(org.bukkit.World world, Location at,
Class<? extends Entity> clazz, EntityType type) {
World handle = ((CraftWorld) world).getHandle();
Entity entity = null;
try {
Constructor<?> constructor = getCustomEntityConstructor(clazz, type);
entity = (Entity) constructor.newInstance(handle);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_SPAWNING_CUSTOM_ENTITY, e.getMessage());
return null;
}
entity.setLocation(at.getX(), at.getY(), at.getZ(), at.getYaw(), at.getPitch());
handle.addEntity(entity);
entity.setLocation(at.getX(), at.getY(), at.getZ(), at.getYaw(), at.getPitch());
return entity.getBukkitEntity();
}
public static void stopNavigation(Navigation navigation) {
navigation.h();
}
public static void stopNetworkThreads(NetworkManager manager) {
if (THREAD_STOPPER == null)
return;
try {
THREAD_STOPPER.set(manager, false);
THREAD_STOPPER_2.set(manager, true);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_STOPPING_NETWORK_THREADS, e.getMessage());
}
}
public static void trySwim(LivingEntity handle) {
trySwim(handle, 0.04F);
}
public static void trySwim(LivingEntity entity, float power) {
Entity handle = getHandle(entity);
if (RANDOM.nextFloat() < 0.8F && inWater(entity)) {
handle.motY += power;
}
}
public static void updateAI(EntityLiving entity) {
if (entity instanceof EntityInsentient) {
EntityInsentient handle = (EntityInsentient) entity;
handle.getEntitySenses().a();
NMS.updateNavigation(handle.getNavigation());
handle.getControllerMove().c();
handle.getControllerLook().a();
handle.getControllerJump().b();
} else if (entity instanceof EntityHumanNPC) {
((EntityHumanNPC) entity).updateAI();
}
}
public static void updateNavigation(Navigation navigation) {
navigation.f();
}
public static void updateNavigationWorld(LivingEntity entity, org.bukkit.World world) {
if (NAVIGATION_WORLD_FIELD == null)
return;
EntityLiving en = ((CraftLivingEntity) entity).getHandle();
if (!(en instanceof EntityInsentient))
return;
EntityInsentient handle = (EntityInsentient) en;
World worldHandle = ((CraftWorld) world).getHandle();
try {
NAVIGATION_WORLD_FIELD.set(handle.getNavigation(), worldHandle);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_UPDATING_NAVIGATION_WORLD, e.getMessage());
}
}
public static void updatePathfindingRange(NPC npc, float pathfindingRange) {
if (!npc.isSpawned())
return;
EntityLiving en = ((CraftLivingEntity) npc.getBukkitEntity()).getHandle();
if (!(en instanceof EntityInsentient)) {
if (en instanceof EntityHumanNPC) {
((EntityHumanNPC) en).updatePathfindingRange(pathfindingRange);
}
return;
}
if (PATHFINDING_RANGE == null)
return;
EntityInsentient handle = (EntityInsentient) en;
Navigation navigation = handle.getNavigation();
try {
AttributeInstance inst = (AttributeInstance) PATHFINDING_RANGE.get(navigation);
inst.setValue(pathfindingRange);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private static final float DEFAULT_SPEED = 1F;
private static Map<Class<?>, Integer> ENTITY_CLASS_TO_INT;
private static final Map<Class<?>, Constructor<?>> ENTITY_CONSTRUCTOR_CACHE = new WeakHashMap<Class<?>, Constructor<?>>();
private static Map<Integer, Class<?>> ENTITY_INT_TO_CLASS;
private static Field GOAL_FIELD = getField(PathfinderGoalSelector.class, "a");
private static final Field JUMP_FIELD = getField(EntityLiving.class, "bd");
private static Field NAVIGATION_WORLD_FIELD = getField(Navigation.class, "b");
private static final Location PACKET_CACHE_LOCATION = new Location(null, 0, 0, 0);
private static Field PATHFINDING_RANGE = getField(Navigation.class, "e");
private static final Random RANDOM = Util.getFastRandom();
private static Field THREAD_STOPPER = getField(NetworkManager.class, "n");
private static Field THREAD_STOPPER_2 = getField(NetworkManager.class, "t");
// true field above false and three synchronised lists
static {
try {
Field field = getField(EntityTypes.class, "d");
ENTITY_INT_TO_CLASS = (Map<Integer, Class<?>>) field.get(null);
field = getField(EntityTypes.class, "e");
ENTITY_CLASS_TO_INT = (Map<Class<?>, Integer>) field.get(null);
} catch (Exception e) {
Messaging.logTr(Messages.ERROR_GETTING_ID_MAPPING, e.getMessage());
}
}
}