Skip to content

Commit

Permalink
Make Look command work on players. Fix rotation for non-Player entiti…
Browse files Browse the repository at this point in the history
…es. Make almost all locations use doubles instead of integers.
  • Loading branch information
davidcernat committed Mar 12, 2013
1 parent bfcfd15 commit e081432
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -44,31 +42,32 @@ 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<Integer, Integer> taskMap = new ConcurrentHashMap<Integer, Integer>();



@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);
dB.echoDebug("...look duration set to '%s'.", arg);
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);
Expand All @@ -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);
}


}


Expand Down Expand Up @@ -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;
}
*/
}

Original file line number Diff line number Diff line change
Expand Up @@ -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 + "'");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/net/aufdemrand/denizen/tags/core/LocationTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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")) {
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/net/aufdemrand/denizen/tags/core/NPCTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Loading

0 comments on commit e081432

Please sign in to comment.