Skip to content

Commit

Permalink
Move Rotation to module
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Sep 21, 2016
1 parent ba7ecc5 commit ad261bb
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 398 deletions.
Expand Up @@ -3,11 +3,13 @@
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.util.Vector;

import java.util.UUID;

Expand Down Expand Up @@ -49,4 +51,132 @@ void follow(final Entity target, final Entity follower, final double speed, fina
void unhideEntity(Player player, Entity entity);

boolean isHidden(Player player, UUID 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);

// 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);

Location faceLocation(Location from, Location at);

/**
* 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);

/**
* 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.
*/
boolean isFacingLocation(Location from, Location at, float 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.
*/
boolean isFacingLocation(Entity from, Location at, float 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.
*/
boolean isFacingEntity(Entity from, Entity at, float 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.
*/
float normalizeYaw(float 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.
*/
float getYaw(Vector vector);

/**
* 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.
*/
String getCardinal(float yaw);
}
Expand Up @@ -2,6 +2,7 @@

import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.interfaces.EntityHelper;
import net.aufdemrand.denizen.nms.interfaces.FakePlayer;
import net.aufdemrand.denizen.objects.properties.entity.EntityAge;
import net.aufdemrand.denizen.objects.properties.entity.EntityColor;
Expand All @@ -12,7 +13,6 @@
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizen.utilities.entity.DenizenEntityType;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.aufdemrand.denizen.utilities.nbt.CustomNBT;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.Property;
Expand Down Expand Up @@ -1733,7 +1733,7 @@ else if (getLivingEntity().getType() == EntityType.PIG) {
// Each coordinate is in the range of 0 to 128.
// -->
if (attribute.startsWith("map_trace")) {
Rotation.MapTraceResult mtr = Rotation.mapTrace(getLivingEntity(), 200);
EntityHelper.MapTraceResult mtr = NMSHandler.getInstance().getEntityHelper().mapTrace(getLivingEntity(), 200);
if (mtr != null) {
double x = 0;
double y = 0;
Expand Down
25 changes: 13 additions & 12 deletions plugin/src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Expand Up @@ -2,6 +2,7 @@

import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.interfaces.EntityHelper;
import net.aufdemrand.denizen.nms.util.PlayerProfile;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.tags.BukkitTagContext;
Expand All @@ -10,7 +11,6 @@
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.DenizenEntityType;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.notable.Notable;
import net.aufdemrand.denizencore.objects.notable.Note;
Expand Down Expand Up @@ -811,7 +811,7 @@ else if (type == Material.TRAP_DOOR
double nx = xzLen * Math.sin(-getYaw() * (Math.PI / 180));
double ny = Math.sin(getPitch() * (Math.PI / 180));
double nz = xzLen * Math.cos(getYaw() * (Math.PI / 180));
Location location = Rotation.getImpactNormal(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
Location location = NMSHandler.getInstance().getEntityHelper().getImpactNormal(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
if (location != null) {
return new dLocation(location).getAttribute(attribute.fulfill(1));
}
Expand All @@ -836,7 +836,7 @@ else if (type == Material.TRAP_DOOR
double nx = xzLen * Math.sin(-getYaw() * (Math.PI / 180));
double ny = Math.sin(getPitch() * (Math.PI / 180));
double nz = xzLen * Math.cos(getYaw() * (Math.PI / 180));
Location location = Rotation.rayTrace(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
Location location = NMSHandler.getInstance().getEntityHelper().rayTrace(this, new org.bukkit.util.Vector(nx, -ny, nz), range);
if (location != null) {
return new dLocation(location).getAttribute(attribute.fulfill(1));
}
Expand Down Expand Up @@ -910,7 +910,7 @@ else if (type == Material.TRAP_DOOR
if (attribute.startsWith("line_of_sight") && attribute.hasContext(1)) {
dLocation location = dLocation.valueOf(attribute.getContext(1));
if (location != null) {
return new Element(Rotation.rayTrace(getWorld(), toVector(), location.toVector()) == null)
return new Element(NMSHandler.getInstance().getEntityHelper().canTrace(getWorld(), toVector(), location.toVector()))
.getAttribute(attribute.fulfill(1));
}
}
Expand Down Expand Up @@ -944,28 +944,29 @@ else if (type == Material.TRAP_DOOR
// not the other way around
dLocation target = dLocation.valueOf(attribute.getContext(1));
attribute = attribute.fulfill(1);
EntityHelper entityHelper = NMSHandler.getInstance().getEntityHelper();
// <--[tag]
// @attribute <l@location.direction[<location>].yaw>
// @returns Element(Decimal)
// @description
// Returns the yaw direction between two locations.
// -->
if (attribute.startsWith("yaw")) {
return new Element(Rotation.normalizeYaw(Rotation.getYaw
return new Element(entityHelper.normalizeYaw(entityHelper.getYaw
(target.toVector().subtract(this.toVector())
.normalize())))
.getAttribute(attribute.fulfill(1));
}
else {
return new Element(Rotation.getCardinal(Rotation.getYaw
return new Element(entityHelper.getCardinal(entityHelper.getYaw
(target.toVector().subtract(this.toVector())
.normalize())))
.getAttribute(attribute);
}
}
// Get a cardinal direction from this location's yaw
else {
return new Element(Rotation.getCardinal(getYaw()))
return new Element(NMSHandler.getInstance().getEntityHelper().getCardinal(getYaw()))
.getAttribute(attribute.fulfill(1));
}
}
Expand All @@ -980,7 +981,7 @@ else if (type == Material.TRAP_DOOR
if (attribute.startsWith("face")
&& attribute.hasContext(1)) {
Location two = dLocation.valueOf(attribute.getContext(1));
return new dLocation(Rotation.faceLocation(this, two))
return new dLocation(NMSHandler.getInstance().getEntityHelper().faceLocation(this, two))
.getAttribute(attribute.fulfill(1));
}

Expand Down Expand Up @@ -1016,12 +1017,12 @@ else if (type == Material.TRAP_DOOR
}

if (dLocation.matches(attribute.getContext(1))) {
return new Element(Rotation.isFacingLocation
return new Element(NMSHandler.getInstance().getEntityHelper().isFacingLocation
(this, dLocation.valueOf(attribute.getContext(1)), degrees))
.getAttribute(attribute.fulfill(attributePos));
}
else if (dEntity.matches(attribute.getContext(1))) {
return new Element(Rotation.isFacingLocation
return new Element(NMSHandler.getInstance().getEntityHelper().isFacingLocation
(this, dEntity.valueOf(attribute.getContext(1))
.getBukkitEntity().getLocation(), degrees))
.getAttribute(attribute.fulfill(attributePos));
Expand Down Expand Up @@ -1074,7 +1075,7 @@ else if (context.split(",").length == 2) {
// Returns the yaw as 'North', 'South', 'East', or 'West'.
// -->
if (attribute.startsWith("yaw.simple")) {
float yaw = Rotation.normalizeYaw(getYaw());
float yaw = NMSHandler.getInstance().getEntityHelper().normalizeYaw(getYaw());
if (yaw < 45) {
return new Element("South")
.getAttribute(attribute.fulfill(2));
Expand Down Expand Up @@ -1115,7 +1116,7 @@ else if (yaw < 315) {
// Returns the normalized yaw of the object at the location.
// -->
if (attribute.startsWith("yaw")) {
return new Element(Rotation.normalizeYaw(getYaw()))
return new Element(NMSHandler.getInstance().getEntityHelper().normalizeYaw(getYaw()))
.getAttribute(attribute.fulfill(1));
}

Expand Down
@@ -1,14 +1,14 @@
package net.aufdemrand.denizen.scripts.commands.entity;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.Conversion;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.Position;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
Expand Down Expand Up @@ -237,9 +237,9 @@ public void run() {

// To avoid excessive turbulence, only have the entity rotate
// when it really needs to
if (!Rotation.isFacingLocation(entity, location, rotationThreshold)) {
if (!NMSHandler.getInstance().getEntityHelper().isFacingLocation(entity, location, rotationThreshold)) {

Rotation.faceLocation(entity, location);
NMSHandler.getInstance().getEntityHelper().faceLocation(entity, location);
}

Vector v1 = entity.getLocation().toVector();
Expand Down
@@ -1,11 +1,11 @@
package net.aufdemrand.denizen.scripts.commands.entity;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

for (dEntity entity : entities) {
if (entity.isSpawned()) {
Rotation.faceLocation(entity.getBukkitEntity(), loc);
NMSHandler.getInstance().getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
}
}
if (duration != null && duration.getTicks() > 2) {
Expand All @@ -87,7 +87,7 @@ public void run() {
}
for (dEntity entity : entities) {
if (entity.isSpawned()) {
Rotation.faceLocation(entity.getBukkitEntity(), loc);
NMSHandler.getInstance().getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
}
}
}
Expand Down
Expand Up @@ -9,7 +9,6 @@
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.Position;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
Expand Down Expand Up @@ -204,7 +203,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
entityList.add(entity.toString());

if (!no_rotate) {
Rotation.faceLocation(entity.getBukkitEntity(), destination);
NMSHandler.getInstance().getEntityHelper().faceLocation(entity.getBukkitEntity(), destination);
}

// If the current entity is a projectile, set its shooter
Expand Down
@@ -1,10 +1,10 @@
package net.aufdemrand.denizen.scripts.commands.entity;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.entity.Rotation;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
Expand Down Expand Up @@ -150,8 +150,8 @@ public void run() {
else if (infinite || ticks < maxTicks) {
for (dEntity entity : entities) {
if (entity.isSpawned() && rotatingEntities.contains(entity.getUUID())) {
Rotation.rotate(entity.getBukkitEntity(),
Rotation.normalizeYaw(entity.getLocation().getYaw() + yaw.asFloat()),
NMSHandler.getInstance().getEntityHelper().rotate(entity.getBukkitEntity(),
NMSHandler.getInstance().getEntityHelper().normalizeYaw(entity.getLocation().getYaw() + yaw.asFloat()),
entity.getLocation().getPitch() + pitch.asFloat());
}
else {
Expand Down

0 comments on commit ad261bb

Please sign in to comment.