From e081432fedf31ba1ee3aaf82abb059833b352d18 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Tue, 12 Mar 2013 02:10:46 +0200 Subject: [PATCH] Make Look command work on players. Fix rotation for non-Player entities. Make almost all locations use doubles instead of integers. --- .../scripts/commands/CommandRegistry.java | 2 +- .../scripts/commands/core/LookCommand.java | 112 ++++++++---------- .../commands/core/PlaySoundCommand.java | 4 +- .../scripts/commands/core/PoseCommand.java | 12 +- .../scripts/commands/core/ShootCommand.java | 4 +- .../triggers/core/LocationTrigger.java | 18 +-- .../triggers/core/ProximityTrigger.java | 6 +- .../denizen/tags/core/AnchorTags.java | 2 +- .../denizen/tags/core/LocationTags.java | 12 +- .../aufdemrand/denizen/tags/core/NPCTags.java | 24 ++-- .../denizen/tags/core/OfflinePlayerTags.java | 6 +- .../denizen/tags/core/PlayerTags.java | 30 ++--- .../denizen/utilities/Utilities.java | 107 ++++++++++++----- .../denizen/utilities/arguments/Location.java | 12 +- 14 files changed, 194 insertions(+), 157 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java index a58b1a79c6..295937c20a 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java @@ -119,7 +119,7 @@ public void registerCoreMembers() { "LISTEN", "listen [listener_type] [id:listener_id] (...) +--> see documentation - http://bit.ly/XJlKwm", 2); registerCoreMember(LookCommand.class, - "LOOK", "look [location:x,y,z,world]", 1); + "LOOK", "look (player) [location:x,y,z,world]", 1); registerCoreMember(LookcloseCommand.class, "LOOKCLOSE", "lookclose [toggle:true|false]", 1); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/LookCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/LookCommand.java index 8316c4ca84..98fc2ef9ac 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/LookCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/LookCommand.java @@ -3,6 +3,7 @@ import net.aufdemrand.denizen.npc.dNPC; import org.bukkit.Location; import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; import net.aufdemrand.denizen.exceptions.CommandExecutionException; import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; @@ -11,25 +12,22 @@ import net.aufdemrand.denizen.utilities.Utilities; import net.aufdemrand.denizen.utilities.arguments.aH; import net.aufdemrand.denizen.utilities.debugging.dB; +import net.aufdemrand.denizen.utilities.debugging.dB.Messages; +import net.citizensnpcs.trait.LookClose; +import net.citizensnpcs.trait.Poses; +import net.citizensnpcs.util.Util; /** - * Controls Denizen's heads. + * Controls Denizens' heads. * * @author Jeremy Schroeder * */ -enum Direction { UP, DOWN, LEFT, RIGHT, NORTH, SOUTH, EAST, WEST, BACK, AT, CLOSE, AWAY } - public class LookCommand extends AbstractCommand { // TODO: Finish - - @Override - public void onEnable() { - //nothing to do here - } - + /* LOOK [[DIRECTION]|[BOOKMARK]:'LOCATION BOOKMARK'|[CLOSE|AWAY]]*/ /* Arguments: [] - Required, () - Optional @@ -44,24 +42,20 @@ public void onEnable() { * (DURATION:#) Reverts to the previous head position after # amount of seconds. */ - // Initialize variables + private enum TargetType { NPC, PLAYER } + private enum Direction { UP, DOWN, LEFT, RIGHT, NORTH, SOUTH, EAST, WEST, BACK, AT, CLOSE, AWAY } - Integer duration = null; - Direction direction = null; - Location location = null; - LivingEntity theEntity = null; - dNPC theDenizen = null; - - // private Map taskMap = new ConcurrentHashMap(); - - @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { + + TargetType targetType = TargetType.NPC; + Integer duration = null; + Direction direction = null; + Location location = null; + for (String arg : scriptEntry.getArguments()) { - theDenizen = scriptEntry.getNPC(); - // If argument is a duration if (aH.matchesDuration(arg)) { duration = aH.getIntegerFrom(arg); @@ -69,6 +63,11 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException continue; } + else if (aH.matchesArg("PLAYER", arg)) { + targetType = TargetType.PLAYER; + dB.echoDebug("... will affect the player!"); + } + // If argument is a LOCATION modifier else if (aH.matchesLocation(arg)) { location = aH.getLocationFrom(arg); @@ -84,26 +83,40 @@ else if (aH.matchesLocation(arg)) { } } } - } + } + + // If TARGET is NPC/PLAYER and no NPC/PLAYER available, throw exception. + if (targetType == TargetType.PLAYER && scriptEntry.getPlayer() == null) throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER); + else if (targetType == TargetType.NPC && scriptEntry.getNPC() == null) throw new InvalidArgumentsException(Messages.ERROR_NO_NPCID); + scriptEntry.addObject("target", targetType) + .addObject("location", location); } @Override public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { - /* - * This is only a temporary fix. Someone requested - * the ability to look at a specfic location. Feel - * free to erase anything to accomodate the proper - * implementation of this command. - * - Jeebs - */ - if (location != null) { - /* - * Ideally this turns lookclose off first, - * however I couldnt figure our how D: - */ - Utilities.faceLocation(theDenizen.getCitizen().getBukkitEntity(), location); - } + TargetType target = (TargetType) scriptEntry.getObject("target"); + Location location = (Location) scriptEntry.getObject("location"); + LivingEntity entity = null; + + if (target.name() == "NPC") + { + entity = scriptEntry.getNPC().getCitizen().getBukkitEntity(); + + // Turn off the NPC's lookclose + scriptEntry.getNPC().getCitizen().getTrait(LookClose.class).lookClose(false); + } + else + { + entity = scriptEntry.getPlayer(); + } + + if (location != null) + { + Utilities.faceLocation(entity, location); + } + + } @@ -243,33 +256,6 @@ public void run(dNPC denizen, Location location, Boolean lookClose, Float checkY } - - - // Thanks fullwall - - private void faceEntity(Entity from, Entity at) { - if (from.getWorld() != at.getWorld()) - return; - Location loc = from.getLocation(); - - double xDiff = at.getLocation().getX() - loc.getX(); - double yDiff = at.getLocation().getY() - loc.getY(); - double zDiff = at.getLocation().getZ() - loc.getZ(); - - double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff); - double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff); - - double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI); - double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90; - if (zDiff < 0.0) { - yaw = yaw + (Math.abs(180 - yaw) * 2); - } - - EntityLiving handle = ((CraftLivingEntity) from).getHandle(); - handle.yaw = (float) yaw - 90; - handle.pitch = (float) pitch; - handle.az = handle.yaw; - } */ } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/PlaySoundCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/PlaySoundCommand.java index 43e7e97108..41d5d29993 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/PlaySoundCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/PlaySoundCommand.java @@ -84,8 +84,8 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { // Debugger dB.echoApproval("Executing '" + getName() + "': " - + "Location='" + location.getBlockX() + "," + location.getBlockY() - + "," + location.getBlockZ() + "," + location.getWorld().getName() + "', " + + "Location='" + location.getX() + "," + location.getY() + + "," + location.getZ() + "," + location.getWorld().getName() + "', " + "Sound='" + sound.toString() + ", " + "Volume/Pitch='" + volume + "/" + pitch + "'"); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/PoseCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/PoseCommand.java index 15e43ede5a..d44eb7047f 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/PoseCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/PoseCommand.java @@ -54,25 +54,25 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException // If TARGET is NPC/PLAYER and no NPC/PLAYER available, throw exception. if (targetType == TargetType.PLAYER && scriptEntry.getPlayer() == null) throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER); else if (targetType == TargetType.NPC && scriptEntry.getNPC() == null) throw new InvalidArgumentsException(Messages.ERROR_NO_NPCID); - scriptEntry.addObject("action", action) - .addObject("id", id).addObject("target", targetType); + scriptEntry.addObject("target", targetType) + .addObject("action", action).addObject("id", id); } @SuppressWarnings("incomplete-switch") @Override public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { // Get objects + + TargetType target = (TargetType) scriptEntry.getObject("target"); + dNPC npc = scriptEntry.getNPC(); Action action = (Action) scriptEntry.getObject("action"); String id = (String) scriptEntry.getObject("id"); - TargetType target = (TargetType) scriptEntry.getObject("target"); // Report to dB dB.report(getName(), - aH.debugObj(target.toString(), scriptEntry.getNPC().toString()) + aH.debugObj(target.toString(), npc.toString()) + aH.debugObj("Action", action.toString()) + aH.debugObj("Id", id)); - - dNPC npc = scriptEntry.getNPC(); switch (action) { diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ShootCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ShootCommand.java index beea2be7b8..db798709e3 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ShootCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ShootCommand.java @@ -157,8 +157,8 @@ public void run(ScriptEntry scriptEntry, Entity entity, Location location) { entity.setVelocity(v3); addRuns(); - if (Math.abs(v2.getBlockX() - v1.getBlockX()) < 2 && Math.abs(v2.getBlockY() - v1.getBlockY()) < 2 - && Math.abs(v2.getBlockZ() - v1.getBlockZ()) < 2) + if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2 + && Math.abs(v2.getZ() - v1.getZ()) < 2) { setRuns(40); } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/triggers/core/LocationTrigger.java b/src/main/java/net/aufdemrand/denizen/scripts/triggers/core/LocationTrigger.java index 33a5a16806..60f86f0814 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/triggers/core/LocationTrigger.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/triggers/core/LocationTrigger.java @@ -20,22 +20,22 @@ public class LocationTrigger extends AbstractTrigger implements Listener { // private class Trigger { - private int x; - private int y; - private int z; + private double x; + private double y; + private double z; private String world; public Trigger(Location location) { - x = location.getBlockX(); - y = location.getBlockY(); - z = location.getBlockZ(); + x = location.getX(); + y = location.getY(); + z = location.getZ(); world = location.getWorld().getName(); } public boolean matches(Location location) { - if (Math.abs(location.getBlockX() - x) > maximumLocationDistanceSetting()) return false; - if (Math.abs(location.getBlockY() - y) > maximumLocationDistanceSetting()) return false; - if (Math.abs(location.getBlockZ() - z) > maximumLocationDistanceSetting()) return false; + if (Math.abs(location.getX() - x) > maximumLocationDistanceSetting()) return false; + if (Math.abs(location.getY() - y) > maximumLocationDistanceSetting()) return false; + if (Math.abs(location.getZ() - z) > maximumLocationDistanceSetting()) return false; if (!location.getWorld().getName().equals(world)) return false; return true; } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/triggers/core/ProximityTrigger.java b/src/main/java/net/aufdemrand/denizen/scripts/triggers/core/ProximityTrigger.java index c9642c5916..ad4a6b5053 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/triggers/core/ProximityTrigger.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/triggers/core/ProximityTrigger.java @@ -269,9 +269,9 @@ else if (!hasExitedProximityOf(event.getPlayer(), npc) private boolean isCloseEnough(Player player, dNPC npc) { Location pLoc = player.getLocation(); Location nLoc = npc.getLocation(); - if (Math.abs(pLoc.getBlockX() - nLoc.getBlockX()) > maxProximityDistance) return false; - if (Math.abs(pLoc.getBlockY() - nLoc.getBlockY()) > maxProximityDistance) return false; - if (Math.abs(pLoc.getBlockZ() - nLoc.getBlockZ()) > maxProximityDistance) return false; + if (Math.abs(pLoc.getX() - nLoc.getX()) > maxProximityDistance) return false; + if (Math.abs(pLoc.getY() - nLoc.getY()) > maxProximityDistance) return false; + if (Math.abs(pLoc.getZ() - nLoc.getZ()) > maxProximityDistance) return false; return true; } diff --git a/src/main/java/net/aufdemrand/denizen/tags/core/AnchorTags.java b/src/main/java/net/aufdemrand/denizen/tags/core/AnchorTags.java index 44cfba57d0..fc1fb3b721 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/core/AnchorTags.java +++ b/src/main/java/net/aufdemrand/denizen/tags/core/AnchorTags.java @@ -31,7 +31,7 @@ else if (event.getNPC() != null) Location anchor = null; if (npc.getTrait(Anchors.class).getAnchor(event.getValue()) != null) { anchor = npc.getTrait(Anchors.class).getAnchor(event.getValue()).getLocation(); - event.setReplaced(anchor.getBlockX() + "," + anchor.getBlockY() + "," + anchor.getBlockZ() + "," + anchor.getWorld().getName()); + event.setReplaced(anchor.getX() + "," + anchor.getY() + "," + anchor.getZ() + "," + anchor.getWorld().getName()); } } } \ No newline at end of file diff --git a/src/main/java/net/aufdemrand/denizen/tags/core/LocationTags.java b/src/main/java/net/aufdemrand/denizen/tags/core/LocationTags.java index 25b8014427..ace1af6c86 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/core/LocationTags.java +++ b/src/main/java/net/aufdemrand/denizen/tags/core/LocationTags.java @@ -99,9 +99,9 @@ else if (subType.equals("HORIZONTAL")) } else if (type.equals("FORMATTED")) - event.setReplaced("X '" + fromLocation.getBlockX() - + "', Y '" + fromLocation.getBlockY() - + "', Z '" + fromLocation.getBlockZ() + event.setReplaced("X '" + fromLocation.getX() + + "', Y '" + fromLocation.getY() + + "', Z '" + fromLocation.getZ() + "', in world '" + fromLocation.getWorld().getName() + "'"); else if (type.equals("IS_LIQUID")) @@ -138,13 +138,13 @@ else if (type.equals("WORLD")) event.setReplaced(fromLocation.getWorld().getName()); else if (type.equals("X")) - event.setReplaced(String.valueOf(fromLocation.getBlockX())); + event.setReplaced(String.valueOf(fromLocation.getX())); else if (type.equals("Y")) - event.setReplaced(String.valueOf(fromLocation.getBlockY())); + event.setReplaced(String.valueOf(fromLocation.getY())); else if (type.equals("Z")) - event.setReplaced(String.valueOf(fromLocation.getBlockZ())); + event.setReplaced(String.valueOf(fromLocation.getZ())); else if (type.equals("BLOCK")) { if (subType.equals("BELOW")) { diff --git a/src/main/java/net/aufdemrand/denizen/tags/core/NPCTags.java b/src/main/java/net/aufdemrand/denizen/tags/core/NPCTags.java index 6c7a276052..4fc7f6ac64 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/core/NPCTags.java +++ b/src/main/java/net/aufdemrand/denizen/tags/core/NPCTags.java @@ -63,29 +63,29 @@ public void npcTags(ReplaceableTagEvent event) { } else if (type.equals("LOCATION")) { Location loc = n.getLocation(); - event.setReplaced(loc.getBlockX() - + "," + loc.getBlockY() - + "," + loc.getBlockZ() + event.setReplaced(loc.getX() + + "," + loc.getY() + + "," + loc.getZ() + "," + n.getWorld().getName()); if (subType.equals("FORMATTED")) - event.setReplaced("X '" + loc.getBlockX() - + "', Y '" + loc.getBlockY() - + "', Z '" + loc.getBlockZ() + event.setReplaced("X '" + loc.getX() + + "', Y '" + loc.getY() + + "', Z '" + loc.getZ() + "', in world '" + n.getWorld().getName() + "'"); else if (subType.equals("X")) - event.setReplaced(String.valueOf(n.getLocation().getBlockX())); + event.setReplaced(String.valueOf(n.getLocation().getX())); else if (subType.equals("Y")) - event.setReplaced(String.valueOf(n.getLocation().getBlockY())); + event.setReplaced(String.valueOf(n.getLocation().getY())); else if (subType.equals("Z")) - event.setReplaced(String.valueOf(n.getLocation().getBlockZ())); + event.setReplaced(String.valueOf(n.getLocation().getZ())); else if (subType.equals("STANDING_ON")) event.setReplaced(loc.add(0, -1, 0).getBlock().getType().name()); else if (subType.equals("STANDING_ON_DISPLAY")) event.setReplaced(n.getLocation().add(0, -1, 0).getBlock().getType().name().toLowerCase().replace('_', ' ')); else if (subType.equals("WORLD_SPAWN")) - event.setReplaced(n.getWorld().getSpawnLocation().getBlockX() - + "," + n.getWorld().getSpawnLocation().getBlockY() - + "," + n.getWorld().getSpawnLocation().getBlockZ() + event.setReplaced(n.getWorld().getSpawnLocation().getX() + + "," + n.getWorld().getSpawnLocation().getY() + + "," + n.getWorld().getSpawnLocation().getZ() + "," + n.getWorld().getName()); else if (subType.equals("WORLD")) event.setReplaced(n.getWorld().getName()); diff --git a/src/main/java/net/aufdemrand/denizen/tags/core/OfflinePlayerTags.java b/src/main/java/net/aufdemrand/denizen/tags/core/OfflinePlayerTags.java index d2deb6c584..7854bfd966 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/core/OfflinePlayerTags.java +++ b/src/main/java/net/aufdemrand/denizen/tags/core/OfflinePlayerTags.java @@ -67,9 +67,9 @@ else if (type.equals("CHAT_HISTORY")) { if (subType.equals("BED_SPAWN")) if (p.getBedSpawnLocation() != null) { - event.setReplaced(p.getBedSpawnLocation().getBlockX() - + "," + p.getBedSpawnLocation().getBlockY() - + "," + p.getBedSpawnLocation().getBlockZ() + event.setReplaced(p.getBedSpawnLocation().getX() + + "," + p.getBedSpawnLocation().getY() + + "," + p.getBedSpawnLocation().getZ() + "," + p.getBedSpawnLocation().getWorld().getName()); } diff --git a/src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java b/src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java index 8d6a02a4f0..bf6e2e9be6 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java +++ b/src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java @@ -262,21 +262,21 @@ else if (subType.equalsIgnoreCase("LIST")) } else if (type.equalsIgnoreCase("LOCATION")) { - event.setReplaced(p.getLocation().getBlockX() - + "," + p.getLocation().getBlockY() - + "," + p.getLocation().getBlockZ() + event.setReplaced(p.getLocation().getX() + + "," + p.getLocation().getY() + + "," + p.getLocation().getZ() + "," + p.getWorld().getName()); if (subType.equalsIgnoreCase("FORMATTED")) - event.setReplaced("X '" + p.getLocation().getBlockX() - + "', Y '" + p.getLocation().getBlockY() - + "', Z '" + p.getLocation().getBlockZ() + event.setReplaced("X '" + p.getLocation().getX() + + "', Y '" + p.getLocation().getY() + + "', Z '" + p.getLocation().getZ() + "', in world '" + p.getWorld().getName() + "'"); else if (subType.equalsIgnoreCase("X")) - event.setReplaced(String.valueOf(p.getLocation().getBlockX())); + event.setReplaced(String.valueOf(p.getLocation().getX())); else if (subType.equalsIgnoreCase("Y")) - event.setReplaced(String.valueOf(p.getLocation().getBlockY())); + event.setReplaced(String.valueOf(p.getLocation().getY())); else if (subType.equalsIgnoreCase("Z")) - event.setReplaced(String.valueOf(p.getLocation().getBlockZ())); + event.setReplaced(String.valueOf(p.getLocation().getZ())); else if (subType.equalsIgnoreCase("WORLD")) event.setReplaced(p.getWorld().getName()); else if (subType.equalsIgnoreCase("CURSOR_ON")) { @@ -291,14 +291,14 @@ else if (subType.equalsIgnoreCase("STANDING_ON")) else event.setReplaced(p.getLocation().add(0, -1, 0).getBlock().getType().name()); else if (subType.equalsIgnoreCase("WORLD_SPAWN")) - event.setReplaced(p.getWorld().getSpawnLocation().getBlockX() - + "," + p.getWorld().getSpawnLocation().getBlockY() - + "," + p.getWorld().getSpawnLocation().getBlockZ() + event.setReplaced(p.getWorld().getSpawnLocation().getX() + + "," + p.getWorld().getSpawnLocation().getY() + + "," + p.getWorld().getSpawnLocation().getZ() + "," + p.getWorld().getName()); else if (subType.equalsIgnoreCase("BED_SPAWN") && p.getBedSpawnLocation() != null) - event.setReplaced(p.getBedSpawnLocation().getBlockX() - + "," + p.getBedSpawnLocation().getBlockY() - + "," + p.getBedSpawnLocation().getBlockZ() + event.setReplaced(p.getBedSpawnLocation().getX() + + "," + p.getBedSpawnLocation().getY() + + "," + p.getBedSpawnLocation().getZ() + "," + p.getWorld().getName()); diff --git a/src/main/java/net/aufdemrand/denizen/utilities/Utilities.java b/src/main/java/net/aufdemrand/denizen/utilities/Utilities.java index 67be539df0..52f4a279ec 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/Utilities.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/Utilities.java @@ -9,10 +9,13 @@ import net.aufdemrand.denizen.scripts.ScriptRegistry; import net.aufdemrand.denizen.scripts.containers.core.TaskScriptContainer; import net.aufdemrand.denizen.utilities.arguments.aH; +import net.aufdemrand.denizen.utilities.debugging.dB; +import net.citizensnpcs.trait.Poses; import net.minecraft.server.v1_4_R1.EntityLiving; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.OfflinePlayer; import org.bukkit.craftbukkit.v1_4_R1.entity.CraftEntity; import org.bukkit.craftbukkit.v1_4_R1.entity.CraftLivingEntity; import org.bukkit.entity.Entity; @@ -201,15 +204,73 @@ public static int countItems(String item, Inventory inventory) return 0; } + + /** + * Rotates an entity. + * + * @param player 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); + return; + } + + if (entity instanceof LivingEntity) + { + EntityLiving handle = ((CraftLivingEntity) entity).getHandle(); + handle.yaw = (float) yaw; + handle.pitch = (float) pitch; + handle.az = handle.yaw; // The head's yaw + + if (!(entity instanceof Player)) + { + // Obfuscated variable used in head turning. If not set to + // be equal to the yaw, non-Player entities will not rotate. + // But do not use on Player entities, because it will break + // their rotation. + // + // In case it ever gets renamed, this EntityLiving line is + // the one with it: + // + // float f5 = MathHelper.g(this.yaw - this.ax); + + handle.ax = handle.yaw; + } + } + + else + { + net.minecraft.server.v1_4_R1.Entity handle = ((CraftEntity) entity).getHandle(); + handle.yaw = (float) yaw; + handle.pitch = (float) pitch; + } + } + /** * Changes an entity's yaw and pitch to make it face a location. * * Thanks to fullwall. * - * @param from the Entity whose yaw and pitch you want to change - * @param at the Location it should be looking at + * @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) { @@ -223,23 +284,13 @@ public static void faceLocation(Entity from, Location at) { double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff); double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff); - double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI); - double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90; + double yaw = Math.toDegrees(Math.acos(xDiff / distanceXZ)); + double pitch = Math.toDegrees(Math.acos(yDiff / distanceY)) - 90; if (zDiff < 0.0) { yaw = yaw + (Math.abs(180 - yaw) * 2); } - if (from instanceof LivingEntity) { - EntityLiving handle = ((CraftLivingEntity) from).getHandle(); - handle.yaw = (float) yaw - 90; - handle.pitch = (float) pitch; - handle.az = handle.yaw; - } else { - net.minecraft.server.v1_4_R1.Entity handle = ((CraftEntity) from).getHandle(); - handle.yaw = (float) yaw - 90; - handle.pitch = (float) pitch; - } - + rotate(from, (float) yaw - 90, (float) pitch); } @@ -248,8 +299,8 @@ public static void faceLocation(Entity from, Location at) { * * Thanks to fullwall. * - * @param entity the Entity whose yaw and pitch you want to change - * @param target the Entity it should be looking at + * @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) { @@ -358,11 +409,11 @@ public static boolean isFacingLocation(Entity from, Location at, float degreeLim /** * 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. + * @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. */ @@ -455,11 +506,11 @@ public static boolean checkLocation(LivingEntity entity, Location theLocation, i Location entityLocation = entity.getLocation(); - if (Math.abs(entityLocation.getBlockX() - theLocation.getBlockX()) + if (Math.abs(entityLocation.getX() - theLocation.getX()) > theLeeway) return false; - if (Math.abs(entityLocation.getBlockY() - theLocation.getBlockY()) + if (Math.abs(entityLocation.getY() - theLocation.getY()) > theLeeway) return false; - if (Math.abs(entityLocation.getBlockZ() - theLocation.getBlockZ()) + if (Math.abs(entityLocation.getZ() - theLocation.getZ()) > theLeeway) return false; return true; @@ -481,11 +532,11 @@ public static boolean checkLocation(Location baseLocation, Location theLocation, Location entityLocation = baseLocation; - if (Math.abs(entityLocation.getBlockX() - theLocation.getBlockX()) + if (Math.abs(entityLocation.getX() - theLocation.getX()) > theLeeway) return false; - if (Math.abs(entityLocation.getBlockY() - theLocation.getBlockY()) + if (Math.abs(entityLocation.getY() - theLocation.getY()) > theLeeway) return false; - if (Math.abs(entityLocation.getBlockZ() - theLocation.getBlockZ()) + if (Math.abs(entityLocation.getZ() - theLocation.getZ()) > theLeeway) return false; return true; diff --git a/src/main/java/net/aufdemrand/denizen/utilities/arguments/Location.java b/src/main/java/net/aufdemrand/denizen/utilities/arguments/Location.java index 2de7bb2d06..79ec5cec2b 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/arguments/Location.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/arguments/Location.java @@ -186,16 +186,16 @@ public String getDefaultPrefix() { @Override public String debug() { - return (Id != null ? "" + prefix + "='" + Id + "(" + getBlockX() + "," + getBlockY() - + "," + getBlockZ() + "," + getWorld().getName() + ")' " - : "" + prefix + "='" + getBlockX() + "," + getBlockY() - + "," + getBlockZ() + "," + getWorld().getName() + "' "); + return (Id != null ? "" + prefix + "='" + Id + "(" + getX() + "," + getY() + + "," + getZ() + "," + getWorld().getName() + ")' " + : "" + prefix + "='" + getX() + "," + getY() + + "," + getZ() + "," + getWorld().getName() + "' "); } @Override public String as_dScriptArg() { - return getBlockX() + "," + getBlockY() - + "," + getBlockZ() + "," + getWorld().getName(); + return getX() + "," + getY() + + "," + getZ() + "," + getWorld().getName(); } public String dScriptArgValue() {