/
Rotation.java
372 lines (315 loc) · 13.4 KB
/
Rotation.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
package net.aufdemrand.denizen.utilities.entity;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.minecraft.server.v1_8_R3.EntityHuman;
import net.minecraft.server.v1_8_R3.EntityLiving;
import net.minecraft.server.v1_8_R3.MovingObjectPosition;
import net.minecraft.server.v1_8_R3.Vec3D;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEntity;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
/**
* Utilities related to entity yaws and pitches.
*
* @author David Cernat, fullwall
*/
public class Rotation {
/**
* 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 static void rotate(Entity entity, float yaw, float pitch) {
// If this entity is a real player instead of a player type NPC,
// it will appear to be online
if (entity instanceof Player && ((Player) entity).isOnline()) {
Location location = entity.getLocation();
location.setYaw(yaw);
location.setPitch(pitch);
// The only way to change a player's yaw and pitch in Bukkit
// is to use teleport on him/her
entity.teleport(location);
}
else if (entity instanceof LivingEntity) {
if (entity instanceof EnderDragon) yaw = normalizeYaw(yaw - 180);
look(entity, yaw, pitch);
}
else {
net.minecraft.server.v1_8_R3.Entity handle = ((CraftEntity) entity).getHandle();
handle.yaw = yaw;
handle.pitch = pitch;
}
}
// Taken from C2 NMS class for less dependency on C2
private static void look(Entity entity, float yaw, float pitch) {
net.minecraft.server.v1_8_R3.Entity handle = ((CraftEntity) entity).getHandle();
if (handle != null) {
handle.yaw = yaw;
if (handle instanceof EntityLiving) {
EntityLiving livingHandle = (EntityLiving) handle;
while (yaw < -180.0F) {
yaw += 360.0F;
}
while (yaw >= 180.0F) {
yaw -= 360.0F;
}
livingHandle.aK = yaw;
if (!(handle instanceof EntityHuman))
livingHandle.aI = yaw;
livingHandle.aL = yaw;
}
handle.pitch = pitch;
}
else {
dB.echoError("Rotation.java#look: NPC has null handle!");
}
}
public static MovingObjectPosition rayTrace(World world, Vector start, Vector end) {
return ((CraftWorld) world).getHandle().rayTrace(new Vec3D(start.getX(), start.getY(), start.getZ()),
new Vec3D(end.getX(), end.getY(), end.getZ()));
}
public static class MapTraceResult {
public Location hitLocation;
public BlockFace angle;
}
public static MapTraceResult mapTrace(LivingEntity from, double range) {
Location start = from.getEyeLocation();
Vector startVec = start.toVector();
double xzLen = Math.cos((start.getPitch() % 360) * (Math.PI / 180));
double nx = xzLen * Math.sin(-start.getYaw() * (Math.PI / 180));
double ny = Math.sin(start.getPitch() * (Math.PI / 180));
double nz = xzLen * Math.cos(start.getYaw() * (Math.PI / 180));
Vector endVec = startVec.clone().add(new Vector(nx, -ny, nz).multiply(range));
MovingObjectPosition l = rayTrace(start.getWorld(), startVec, endVec);
if (l == null || l.pos == null) return null;
Vector finalVec = new Vector(l.pos.a, l.pos.b, l.pos.c);
MapTraceResult mtr = new MapTraceResult();
switch (l.direction) {
case NORTH:
mtr.angle = BlockFace.NORTH;
break;
case SOUTH:
mtr.angle = BlockFace.SOUTH;
break;
case EAST:
mtr.angle = BlockFace.EAST;
break;
case WEST:
mtr.angle = BlockFace.WEST;
break;
}
// wallPosition - ((end - start).normalize() * 0.072)
Vector hit = finalVec.clone().subtract((endVec.clone().subtract(startVec)).normalize().multiply(0.072));
mtr.hitLocation = new Location(start.getWorld(), hit.getX(), hit.getY(), hit.getZ());
return mtr;
}
/**
* 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.
*/
public static Location rayTrace(Location start, Vector direction, double range) {
Vector startVec = start.toVector();
MovingObjectPosition l = rayTrace(start.getWorld(), startVec, startVec.clone().add(direction.multiply(range)));
if (l != null && l.pos != null) {
return new Location(start.getWorld(), l.pos.a, l.pos.b, l.pos.c);
}
return null;
}
public static Location getImpactNormal(Location start, Vector direction, double range) {
Vector startVec = start.toVector();
MovingObjectPosition l = rayTrace(start.getWorld(), startVec, startVec.clone().add(direction.multiply(range)));
if (l != null && l.direction != null) {
return new Location(start.getWorld(), l.direction.getAdjacentX(), l.direction.getAdjacentY(), l.direction.getAdjacentZ());
}
return null;
}
/**
* 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.
*/
public static Location eyeTrace(LivingEntity from, double range) {
Location start = from.getEyeLocation();
double xzLen = Math.cos((start.getPitch() % 360) * (Math.PI / 180));
double nx = xzLen * Math.sin(-start.getYaw() * (Math.PI / 180));
double ny = Math.sin(start.getPitch() * (Math.PI / 180));
double nz = xzLen * Math.cos(start.getYaw() * (Math.PI / 180));
return rayTrace(start, new Vector(nx, -ny, nz), range);
}
public static Location faceLocation(Location from, Location at) {
double xDiff = at.getX() - from.getX();
double yDiff = at.getY() - from.getY();
double zDiff = at.getZ() - from.getZ();
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ));
double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 70;
if (zDiff < 0.0) {
yaw = yaw + (Math.abs(180 - yaw) * 2);
}
return new Location(from.getWorld(), from.getX(), from.getY(), from.getZ(), (float)yaw - 90, (float)pitch);
}
/**
* 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 static void faceLocation(Entity from, Location at) {
if (from.getWorld() != at.getWorld()) return;
Location loc = from.getLocation().getBlock().getLocation().clone().add(0.5, 0.5, 0.5);
double xDiff = at.getX() - loc.getX();
double yDiff = at.getY() - loc.getY();
double zDiff = at.getZ() - loc.getZ();
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ));
double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 70;
if (zDiff < 0.0) {
yaw = yaw + (Math.abs(180 - yaw) * 2);
}
rotate(from, (float) yaw - 90, (float) pitch);
}
/**
* 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.
*/
public static void faceEntity(Entity entity, Entity target) {
faceLocation(entity, target.getLocation());
}
/**
* 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 static 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 static 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 static 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 static 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 static 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 static 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;
}
}