-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
EntityHelper.java
307 lines (249 loc) · 10.2 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
package net.aufdemrand.denizen.nms.interfaces;
import net.aufdemrand.denizen.nms.util.BoundingBox;
import net.aufdemrand.denizen.nms.util.jnbt.CompoundTag;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Creature;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerFishEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import java.util.UUID;
public interface EntityHelper {
void setRiptide(Entity entity, boolean state);
int getBodyArrows(Entity entity);
void setBodyArrows(Entity entity, int numArrows);
Entity getFishHook(PlayerFishEvent event);
void forceInteraction(Player player, Location location);
Entity getEntity(World world, UUID uuid);
boolean isBreeding(Animals entity);
void setBreeding(Animals entity, boolean breeding);
void setTarget(Creature entity, LivingEntity target);
CompoundTag getNbtData(Entity entity);
void setNbtData(Entity entity, CompoundTag compoundTag);
void setSilent(Entity entity, boolean silent);
boolean isSilent(Entity entity);
ItemStack getItemInHand(LivingEntity entity);
void setItemInHand(LivingEntity entity, ItemStack itemStack);
ItemStack getItemInOffHand(LivingEntity entity);
void setItemInOffHand(LivingEntity entity, ItemStack itemStack);
void stopFollowing(Entity follower);
void stopWalking(Entity entity);
void toggleAI(Entity entity, boolean hasAI);
boolean isAIDisabled(Entity entity);
double getSpeed(Entity entity);
void setSpeed(Entity entity, double speed);
void follow(final Entity target, final Entity follower, final double speed, final double lead,
final double maxRange, final boolean allowWander);
void walkTo(final Entity entity, Location location, double speed, final Runnable callback);
void hideEntity(Player player, Entity entity, boolean keepInTabList);
void unhideEntity(Player player, Entity entity);
boolean isHidden(Player player, 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.
*/
void rotate(Entity entity, float yaw, float pitch);
float getBaseYaw(Entity entity);
// Taken from C2 NMS class for less dependency on C2
void look(Entity entity, float yaw, float pitch);
class MapTraceResult {
public Location hitLocation;
public BlockFace angle;
}
boolean canTrace(World world, Vector start, Vector end);
MapTraceResult mapTrace(LivingEntity from, double range);
/**
* Gets the precise location in the specified direction.
*
* @param start The location to start the check from.
* @param direction The one-length vector to use as a direction.
* @param range The maximum distance between the start and end.
* @return The location, or null if it isn't in range.
*/
Location rayTrace(Location start, Vector direction, double range);
Location getImpactNormal(Location start, Vector direction, double range);
/**
* Gets the precise location a LivingEntity is looking at.
*
* @param from The LivingEntity to start the trace from.
* @param range The maximum distance between the LivingEntity and the location.
* @return The location, or null if it isn't in range.
*/
Location eyeTrace(LivingEntity from, double range);
default 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.
*/
void faceLocation(Entity from, Location at);
/**
* Changes an entity's yaw and pitch to make it face another entity.
*
* @param entity The Entity whose yaw and pitch you want to change.
* @param target The Entity it should be looking at.
*/
void faceEntity(Entity entity, Entity target);
default 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.
*/
default 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.
*/
default 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.
*/
default 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.
*/
default 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.
*/
default 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.
*/
default 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;
}
}
void move(Entity entity, Vector vector);
void teleport(Entity entity, Vector vector);
BoundingBox getBoundingBox(Entity entity);
void setBoundingBox(Entity entity, BoundingBox boundingBox);
boolean isChestedHorse(Entity horse);
boolean isCarryingChest(Entity horse);
void setCarryingChest(Entity horse, boolean carrying);
}