/
EntityHelper.java
477 lines (401 loc) · 16.6 KB
/
EntityHelper.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
package com.denizenscript.denizen.nms.interfaces;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.util.BoundingBox;
import com.denizenscript.denizen.nms.util.jnbt.CompoundTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import java.util.*;
public abstract class EntityHelper {
public abstract double getAbsorption(LivingEntity entity);
public abstract void setAbsorption(LivingEntity entity, double value);
public void setSneaking(Player player, boolean sneak) {
player.setSneaking(sneak);
}
public abstract double getDamageTo(LivingEntity attacker, Entity target);
public abstract String getRawHoverText(Entity entity);
public List<String> getDiscoveredRecipes(Player player) {
throw new UnsupportedOperationException();
}
public void setRiptide(Entity entity, boolean state) {
Debug.echoError("Riptide control not available on this server version.");
}
public void setCarriedItem(Enderman entity, ItemStack item) {
entity.setCarriedMaterial(item.getData());
}
public abstract int getBodyArrows(Entity entity);
public abstract void setBodyArrows(Entity entity, int numArrows);
public abstract double getArrowDamage(Entity arrow);
public abstract void setArrowDamage(Entity arrow, double damage);
public abstract String getArrowPickupStatus(Entity entity);
public abstract void setArrowPickupStatus(Entity entity, String status);
public abstract Entity getFishHook(PlayerFishEvent event);
public abstract ItemStack getItemFromTrident(Entity entity);
public abstract void setItemForTrident(Entity entity, ItemStack item);
public abstract void forceInteraction(Player player, Location location);
public abstract Entity getEntity(World world, UUID uuid);
public abstract void setTarget(Creature entity, LivingEntity target);
public abstract CompoundTag getNbtData(Entity entity);
public abstract void setNbtData(Entity entity, CompoundTag compoundTag);
public abstract void stopFollowing(Entity follower);
public abstract void stopWalking(Entity entity);
public abstract double getSpeed(Entity entity);
public abstract void setSpeed(Entity entity, double speed);
public abstract void follow(final Entity target, final Entity follower, final double speed, final double lead,
final double maxRange, final boolean allowWander);
public abstract void walkTo(final LivingEntity entity, Location location, double speed, final Runnable callback);
public class EnforcePlayerHides implements Listener {
public Plugin denizenPlugin;
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
for (UUID id : hiddenByDefaultPlayers) {
Entity pTarget = Bukkit.getEntity(id);
if (pTarget instanceof Player) {
event.getPlayer().hidePlayer(Denizen.getInstance(), (Player) pTarget);
}
}
final Player pl = event.getPlayer();
final Set<UUID> hides = hiddenEntitiesPlEnt.get(pl.getUniqueId());
if (hides == null || hides.isEmpty()) {
return;
}
new BukkitRunnable() {
@Override
public void run() {
if (pl.isOnline()) {
for (UUID id : hides) {
Entity ent = Bukkit.getEntity(id);
if (ent != null) {
sendHidePacket(pl, ent);
}
}
}
}
}.runTaskLater(denizenPlugin, 5);
}
}
public void ensurePlayerHiding() {
if (EPH == null) {
Plugin pl = Bukkit.getPluginManager().getPlugin("Denizen"); // Very lazy way to get the correct plugin instance
EPH = new EnforcePlayerHides();
EPH.denizenPlugin = pl;
Bukkit.getPluginManager().registerEvents(EPH, pl);
}
}
public boolean addHide(UUID player, UUID entity) {
Set<UUID> hidden = hiddenEntitiesEntPl.get(entity);
if (hidden == null) {
hidden = new HashSet<>();
hiddenEntitiesEntPl.put(entity, hidden);
}
if (player.equals(DEFAULT_HIDE)) {
for (UUID pl : hidden) {
Set<UUID> plHid = hiddenEntitiesPlEnt.get(pl);
if (plHid != null) {
plHid.remove(entity);
}
}
hidden.clear();
}
else {
Set<UUID> plHid = hiddenEntitiesPlEnt.get(player);
if (plHid == null) {
plHid = new HashSet<>();
hiddenEntitiesPlEnt.put(player, plHid);
}
plHid.add(entity);
}
return hidden.add(player);
}
public void hideEntity(Player player, Entity entity) {
if (player == null) {
addHide(DEFAULT_HIDE, entity.getUniqueId());
if (entity instanceof Player) {
hiddenByDefaultPlayers.add(entity.getUniqueId());
}
for (Player pl : Bukkit.getOnlinePlayers()) {
sendHidePacket(pl, entity);
}
return;
}
if (isHiddenByDefault(entity.getUniqueId())) {
removeHide(player.getUniqueId(), entity.getUniqueId());
}
else {
addHide(player.getUniqueId(), entity.getUniqueId());
}
sendHidePacket(player, entity);
}
public static boolean removeHide(UUID player, UUID entity) {
Set<UUID> hidden = hiddenEntitiesEntPl.get(entity);
if (hidden == null) {
return false;
}
boolean toRet = hidden.remove(player);
if (player.equals(DEFAULT_HIDE)) {
for (UUID pl : hidden) {
Set<UUID> plHid = hiddenEntitiesPlEnt.get(pl);
if (plHid != null) {
plHid.remove(entity);
}
}
hidden.clear();
}
else {
Set<UUID> plHid = hiddenEntitiesPlEnt.get(player);
if (plHid != null) {
plHid.remove(entity);
}
}
return toRet;
}
public void unhideEntity(Player player, Entity entity) {
if (player == null) {
removeHide(DEFAULT_HIDE, entity.getUniqueId());
if (entity instanceof Player) {
hiddenByDefaultPlayers.remove(entity.getUniqueId());
}
for (Player pl : Bukkit.getOnlinePlayers()) {
sendShowPacket(pl, entity);
}
return;
}
if (isHiddenByDefault(entity.getUniqueId())) {
addHide(player.getUniqueId(), entity.getUniqueId());
}
else {
removeHide(player.getUniqueId(), entity.getUniqueId());
}
sendShowPacket(player, entity);
}
public static UUID DEFAULT_HIDE = new UUID(0, 0);
public boolean isHiddenByDefault(UUID id) {
Set<UUID> hiding = hiddenEntitiesEntPl.get(id);
return hiding != null && hiding.contains(DEFAULT_HIDE);
}
public boolean isHidden(Player player, UUID id) {
if (isHiddenByDefault(id)) {
Set<UUID> hiding = hiddenEntitiesEntPl.get(id);
return hiding == null || !hiding.contains(player.getUniqueId());
}
Set<UUID> hiding = hiddenEntitiesEntPl.get(id);
return hiding != null && hiding.contains(player.getUniqueId());
}
public static Map<UUID, Set<UUID>> hiddenEntitiesEntPl = new HashMap<>();
public static Map<UUID, Set<UUID>> hiddenEntitiesPlEnt = new HashMap<>();
public static EnforcePlayerHides EPH = null;
public static Set<UUID> hiddenByDefaultPlayers = new HashSet<>();
public abstract void sendHidePacket(Player pl, Entity entity);
public abstract void sendShowPacket(Player pl, Entity entity);
/**
* Rotates an entity.
*
* @param entity The Entity you want to rotate.
* @param yaw The new yaw of the entity.
* @param pitch The new pitch of the entity.
*/
public abstract void rotate(Entity entity, float yaw, float pitch);
public abstract float getBaseYaw(Entity entity);
// Taken from C2 NMS class for less dependency on C2
public abstract void look(Entity entity, float yaw, float pitch);
public static class MapTraceResult {
public Location hitLocation;
public BlockFace angle;
}
public abstract boolean canTrace(World world, Vector start, Vector end);
public abstract MapTraceResult mapTrace(LivingEntity from, double range);
public Location faceLocation(Location from, Location at) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
Location newLocation = from.clone();
newLocation.setYaw(180 - (float) Math.toDegrees(Math.atan2(direction.getX(), direction.getZ())));
newLocation.setPitch(90 - (float) Math.toDegrees(Math.acos(direction.getY())));
return newLocation;
}
/**
* Changes an entity's yaw and pitch to make it face a location.
*
* @param from The Entity whose yaw and pitch you want to change.
* @param at The Location it should be looking at.
*/
public void faceLocation(Entity from, Location at) {
if (from.getWorld() != at.getWorld()) {
return;
}
Location origin = from instanceof LivingEntity ? ((LivingEntity) from).getEyeLocation()
: new LocationTag(from.getLocation()).getBlockLocation().add(0.5, 0.5, 0.5);
Location rotated = faceLocation(origin, at);
rotate(from, rotated.getYaw(), rotated.getPitch());
}
public boolean isFacingLocation(Location from, Location at, float yawLimitDegrees, float pitchLimitDegrees) {
Vector direction = from.toVector().subtract(at.toVector()).normalize();
float pitch = 90 - (float) Math.toDegrees(Math.acos(direction.getY()));
if (from.getPitch() > pitch + pitchLimitDegrees
|| from.getPitch() < pitch - pitchLimitDegrees) {
return false;
}
return isFacingLocation(from, at, yawLimitDegrees);
}
/**
* Checks if a Location's yaw is facing another Location.
* <p/>
* Note: do not use a player's location as the first argument,
* because player yaws need to modified. Use the method
* below this one instead.
*
* @param from The Location we check.
* @param at The Location we want to know if the first Location's yaw
* is facing
* @param degreeLimit How many degrees can be between the direction the
* first location's yaw is facing and the direction
* we check if it is facing.
* @return Returns a boolean.
*/
public boolean isFacingLocation(Location from, Location at, float degreeLimit) {
double currentYaw = normalizeYaw(from.getYaw());
double requiredYaw = normalizeYaw(getYaw(at.toVector().subtract(
from.toVector()).normalize()));
return (Math.abs(requiredYaw - currentYaw) < degreeLimit ||
Math.abs(requiredYaw + 360 - currentYaw) < degreeLimit ||
Math.abs(currentYaw + 360 - requiredYaw) < degreeLimit);
}
/**
* Checks if an Entity is facing a Location.
*
* @param from The Entity we check.
* @param at The Location we want to know if it is looking at.
* @param degreeLimit How many degrees can be between the direction the
* Entity is facing and the direction we check if it
* is facing.
* @return Returns a boolean.
*/
public boolean isFacingLocation(Entity from, Location at, float degreeLimit) {
return isFacingLocation(from.getLocation(), at, degreeLimit);
}
/**
* Checks if an Entity is facing another Entity.
*
* @param from The Entity we check.
* @param at The Entity we want to know if it is looking at.
* @param degreeLimit How many degrees can be between the direction the
* Entity is facing and the direction we check if it
* is facing.
* @return Returns a boolean.
*/
public boolean isFacingEntity(Entity from, Entity at, float degreeLimit) {
return isFacingLocation(from.getLocation(), at.getLocation(), degreeLimit);
}
/**
* Normalizes Mincraft's yaws (which can be negative or can exceed 360)
* by turning them into proper yaw values that only go from 0 to 359.
*
* @param yaw The original yaw.
* @return The normalized yaw.
*/
public float normalizeYaw(float yaw) {
yaw = yaw % 360;
if (yaw < 0) {
yaw += 360.0;
}
return yaw;
}
/**
* Converts a vector to a yaw.
* <p/>
* Thanks to bergerkiller.
*
* @param vector The vector you want to get a yaw from.
* @return The yaw.
*/
public float getYaw(Vector vector) {
double dx = vector.getX();
double dz = vector.getZ();
double yaw = 0;
// Set yaw
if (dx != 0) {
// Set yaw start value based on dx
if (dx < 0) {
yaw = 1.5 * Math.PI;
}
else {
yaw = 0.5 * Math.PI;
}
yaw -= Math.atan(dz / dx);
}
else if (dz < 0) {
yaw = Math.PI;
}
return (float) (-yaw * 180 / Math.PI);
}
/**
* Converts a yaw to a cardinal direction name.
*
* @param yaw The yaw you want to get a cardinal direction from.
* @return The name of the cardinal direction as a String.
*/
public String getCardinal(float yaw) {
yaw = normalizeYaw(yaw);
// Compare yaws, return closest direction.
if (0 <= yaw && yaw < 22.5) {
return "south";
}
else if (22.5 <= yaw && yaw < 67.5) {
return "southwest";
}
else if (67.5 <= yaw && yaw < 112.5) {
return "west";
}
else if (112.5 <= yaw && yaw < 157.5) {
return "northwest";
}
else if (157.5 <= yaw && yaw < 202.5) {
return "north";
}
else if (202.5 <= yaw && yaw < 247.5) {
return "northeast";
}
else if (247.5 <= yaw && yaw < 292.5) {
return "east";
}
else if (292.5 <= yaw && yaw < 337.5) {
return "southeast";
}
else if (337.5 <= yaw && yaw < 360.0) {
return "south";
}
else {
return null;
}
}
public abstract void snapPositionTo(Entity entity, Vector vector);
public abstract void move(Entity entity, Vector vector);
public abstract void teleport(Entity entity, Vector vector);
public abstract BoundingBox getBoundingBox(Entity entity);
public abstract void setBoundingBox(Entity entity, BoundingBox boundingBox);
public abstract boolean isChestedHorse(Entity horse);
public abstract boolean isCarryingChest(Entity horse);
public abstract void setCarryingChest(Entity horse, boolean carrying);
public List<Player> getPlayersThatSee(Entity entity) {
throw new UnsupportedOperationException();
}
public void setTicksLived(Entity entity, int ticks) {
entity.setTicksLived(ticks);
}
public int getShulkerPeek(Entity entity) {
throw new UnsupportedOperationException();
}
public void setShulkerPeek(Entity entity, int peek) {
throw new UnsupportedOperationException();
}
}