Skip to content

Commit

Permalink
Clean element.is.than error handling
Browse files Browse the repository at this point in the history
Two fixes:
- 'less' is now permitted in addition to 'LESS' (case-insensitive)
- a fully bad operator (EG '.is[potato]') will respond with a simple
error message rather than a full stacktrace.

Also, player.target, return Element.NULL rather than a real null
pointer.
  • Loading branch information
mcmonkey4eva committed Dec 10, 2013
1 parent b2f4d1a commit 456e9b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
19 changes: 15 additions & 4 deletions src/main/java/net/aufdemrand/denizen/objects/Element.java
Expand Up @@ -236,11 +236,22 @@ public String getAttribute(Attribute attribute) {

// Operator is the value of the .is[] context. Valid are Comparable.Operators, same
// as used by the IF command.
com.setOperator(Comparable.Operator.valueOf(operator
.replace("==", "EQUALS").replace(">=", "OR_MORE").replace("<=", "OR_LESS")
.replace("<", "LESS").replace(">", "MORE").replace("=", "EQUALS")));
Comparable.Operator comparableOperator = null;
try {
comparableOperator = Comparable.Operator.valueOf(operator.replace("==", "EQUALS")
.replace(">=", "OR_MORE").replace("<=", "OR_LESS").replace("<", "LESS")
.replace(">", "MORE").replace("=", "EQUALS").toUpperCase());
}
catch (IllegalArgumentException e) { }

if (comparableOperator != null) {
com.setOperator(comparableOperator);

return new Element(com.determineOutcome()).getAttribute(attribute.fulfill(2));
return new Element(com.determineOutcome()).getAttribute(attribute.fulfill(2));
}
else {
dB.echoError("Unknown operator '" + operator + "'.");
}
}


Expand Down
16 changes: 8 additions & 8 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -412,14 +412,14 @@ public String getAttribute(Attribute attribute) {
/////////////////////
// ENTITY LIST ATTRIBUTES
/////////////////

// <--[tag]
// @attribute <p@player.target>
// @returns dEntity
// @description
// Returns Entity that the player is looking at in 50 range.
// -->

if (attribute.startsWith("target")) {
int range = 50;
int attribs = 1;
Expand All @@ -443,27 +443,27 @@ public String getAttribute(Attribute attribute) {
possibleTargets.add((LivingEntity) entity);
}
}

// Find the valid target
BlockIterator bi;
try {
bi = new BlockIterator(getPlayerEntity(), range);
}
catch (IllegalStateException e) {
return null;
return Element.NULL.getAttribute(attribute.fulfill(attribs));
}
Block b;
Location l;
int bx, by, bz;
double ex, ey, ez;

// Loop through player's line of sight
while (bi.hasNext()) {
b = bi.next();
bx = b.getX();
by = b.getY();
bz = b.getZ();

if (b.getType() != Material.AIR) {
// Line of sight is broken
break;
Expand All @@ -475,7 +475,7 @@ public String getAttribute(Attribute attribute) {
ex = l.getX();
ey = l.getY();
ez = l.getZ();

if ((bx - .75 <= ex && ex <= bx + 1.75) &&
(bz - .75 <= ez && ez <= bz + 1.75) &&
(by - 1 <= ey && ey <= by + 2.5)) {
Expand All @@ -487,7 +487,7 @@ public String getAttribute(Attribute attribute) {
}
return Element.NULL.getAttribute(attribute.fulfill(attribs));
}

// <--[tag]
// @attribute <p@player.list>
// @returns dList(dPlayer)
Expand Down

0 comments on commit 456e9b3

Please sign in to comment.