Skip to content

Commit

Permalink
Allow dLocation .within[<#>] tags to use <#.#>
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Dec 20, 2013
1 parent 70f1b72 commit ef88099
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
56 changes: 28 additions & 28 deletions src/main/java/net/aufdemrand/denizen/objects/dLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ else if (getYaw() < 315)
attribute.fulfill(1);

// <--[tag]
// @attribute <l@location.find.blocks[<block>|...].within[<#>]>
// @attribute <l@location.find.blocks[<block>|...].within[<#.#>]>
// @returns dList
// @description
// Returns a list of matching blocks within a radius.
Expand All @@ -636,24 +636,24 @@ else if (getYaw() < 315)
&& attribute.getAttribute(2).startsWith("within")
&& attribute.hasContext(2)) {
ArrayList<dLocation> found = new ArrayList<dLocation>();
int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10;
double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
List<dObject> materials = new ArrayList<dObject>();
if (attribute.hasContext(1))
materials = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class);

// dB.log(materials + " " + radius + " ");
attribute.fulfill(2);

for (int x = -(radius); x <= radius; x++)
for (int y = -(radius); y <= radius; y++)
for (int z = -(radius); z <= radius; z++)
for (double x = -(radius); x <= radius; x++)
for (double y = -(radius); y <= radius; y++)
for (double z = -(radius); z <= radius; z++)
if (!materials.isEmpty()) {
for (dObject material : materials)
if (((dMaterial) material).matchesMaterialData(getBlock()
.getRelative(x,y,z).getType().getNewData(getBlock()
.getRelative(x,y,z).getData())))
found.add(new dLocation(getBlock().getRelative(x,y,z).getLocation()));
} else found.add(new dLocation(getBlock().getRelative(x,y,z).getLocation()));
.getLocation().add(x,y,z).getBlock().getType().getNewData(getBlock()
.getLocation().add(x,y,z).getBlock().getData())))
found.add(new dLocation(getBlock().getLocation().add(x,y,z)));
} else found.add(new dLocation(getBlock().getLocation().add(x,y,z)));

Collections.sort(found, new Comparator<dLocation>() {
@Override
Expand All @@ -666,7 +666,7 @@ public int compare(dLocation loc1, dLocation loc2) {
}

// <--[tag]
// @attribute <l@location.find.surface_blocks[<block>|...].within[<#>]>
// @attribute <l@location.find.surface_blocks[<block>|...].within[<#.#>]>
// @returns dList
// @description
// Returns a list of matching surface blocks within a radius.
Expand All @@ -675,31 +675,31 @@ else if (attribute.startsWith("surface_blocks")
&& attribute.getAttribute(2).startsWith("within")
&& attribute.hasContext(2)) {
ArrayList<dLocation> found = new ArrayList<dLocation>();
int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10;
double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
List<dObject> materials = new ArrayList<dObject>();
if (attribute.hasContext(1))
materials = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class);

attribute.fulfill(2);

for (int x = -(radius); x <= radius; x++)
for (int y = -(radius); y <= radius; y++)
for (int z = -(radius); z <= radius; z++)
for (double x = -(radius); x <= radius; x++)
for (double y = -(radius); y <= radius; y++)
for (double z = -(radius); z <= radius; z++)
if (!materials.isEmpty()) {
for (dObject material : materials)
if (((dMaterial) material).matchesMaterialData(getBlock()
.getRelative(x,y,z).getType().getNewData(getBlock()
.getRelative(x,y,z).getData()))) {
Location l = getBlock().getRelative(x,y,z).getLocation();
.getLocation().add(x,y,z).getBlock().getType().getNewData(getBlock()
.getLocation().add(x,y,z).getBlock().getData()))) {
Location l = getBlock().getLocation().add(x,y,z);
if (l.add(0,1,0).getBlock().getType() == Material.AIR
&& l.add(0,1,0).getBlock().getType() == Material.AIR)
found.add(new dLocation(getBlock().getRelative(x,y,z).getLocation()));
found.add(new dLocation(getBlock().getLocation().add(x,y,z)));
}
} else {
Location l = getBlock().getRelative(x,y,z).getLocation();
Location l = getBlock().getLocation().add(x,y,z);
if (l.add(0,1,0).getBlock().getType() == Material.AIR
&& l.add(0,1,0).getBlock().getType() == Material.AIR)
found.add(new dLocation(getBlock().getRelative(x,y,z).getLocation()));
found.add(new dLocation(getBlock().getLocation().add(x,y,z)));
}

Collections.sort(found, new Comparator<dLocation>() {
Expand All @@ -713,7 +713,7 @@ public int compare(dLocation loc1, dLocation loc2) {
}

// <--[tag]
// @attribute <l@location.find.players.within[<#>]>
// @attribute <l@location.find.players.within[<#.#>]>
// @returns dList
// @description
// Returns a list of players within a radius.
Expand All @@ -722,7 +722,7 @@ else if (attribute.startsWith("players")
&& attribute.getAttribute(2).startsWith("within")
&& attribute.hasContext(2)) {
ArrayList<dPlayer> found = new ArrayList<dPlayer>();
int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10;
double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
attribute.fulfill(2);
for (Player player : Bukkit.getOnlinePlayers())
if (!player.isDead() && Utilities.checkLocation(this, player.getLocation(), radius))
Expand All @@ -739,7 +739,7 @@ public int compare(dPlayer pl1, dPlayer pl2) {
}

// <--[tag]
// @attribute <l@location.find.npcs.within[<#>]>
// @attribute <l@location.find.npcs.within[<#.#>]>
// @returns dList
// @description
// Returns a list of NPCs within a radius.
Expand All @@ -748,7 +748,7 @@ else if (attribute.startsWith("npcs")
&& attribute.getAttribute(2).startsWith("within")
&& attribute.hasContext(2)) {
ArrayList<dNPC> found = new ArrayList<dNPC>();
int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10;
double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
attribute.fulfill(2);
for (dNPC npc : DenizenAPI.getSpawnedNPCs())
if (Utilities.checkLocation(this, npc.getLocation(), radius))
Expand All @@ -765,7 +765,7 @@ public int compare(dNPC npc1, dNPC npc2) {
}

// <--[tag]
// @attribute <l@location.find.entities[<entity>|...].within[<#>]>
// @attribute <l@location.find.entities[<entity>|...].within[<#.#>]>
// @returns dList
// @description
// Returns a list of entities within a radius, with an optional search parameter
Expand All @@ -782,7 +782,7 @@ else if (attribute.startsWith("entities")
}
}
ArrayList<dEntity> found = new ArrayList<dEntity>();
int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10;
double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
attribute.fulfill(2);
for (Entity entity : getWorld().getEntities()) {
if (Utilities.checkLocation(this, entity.getLocation(), radius)) {
Expand Down Expand Up @@ -811,7 +811,7 @@ public int compare(dEntity ent1, dEntity ent2) {
}

// <--[tag]
// @attribute <l@location.find.living_entities.within[<#>]>
// @attribute <l@location.find.living_entities.within[<#.#>]>
// @returns dList
// @description
// Returns a list of living entities within a radius.
Expand All @@ -820,7 +820,7 @@ else if (attribute.startsWith("living_entities")
&& attribute.getAttribute(2).startsWith("within")
&& attribute.hasContext(2)) {
ArrayList<dEntity> found = new ArrayList<dEntity>();
int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10;
double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10;
attribute.fulfill(2);
for (Entity entity : getWorld().getEntities())
if (entity instanceof LivingEntity
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/net/aufdemrand/denizen/tags/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ public String getContext(int attribute) {

public int getIntContext(int attribute) {
try {
if (hasContext(attribute))
return Integer.valueOf(getAttribute(attribute).split("\\[", 2)[1].replace("]", ""));
if (hasContext(attribute))
return Integer.valueOf(getContext(attribute));
} catch (Exception e) { }

return 0;
}

public double getDoubleContext(int attribute) {
try {
if (hasContext(attribute))
return Double.valueOf(getContext(attribute));
} catch (Exception e) { }
return 0;
}

public String getAttribute(int num) {
if (attributes.size() < num) return "";
else return attributes.get(num - 1);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/aufdemrand/denizen/utilities/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public static Set<dNPC> getClosestNPCs (Location location, int maxRange) {
*
* @return true if within the specified location, false otherwise.
*/
public static boolean checkLocation(LivingEntity entity, Location theLocation, int theLeeway) {
public static boolean checkLocation(LivingEntity entity, Location theLocation, double theLeeway) {
if (entity.getWorld() != theLocation.getWorld())
return false;

Expand All @@ -369,7 +369,7 @@ public static boolean checkLocation(LivingEntity entity, Location theLocation, i
*
* @return true if within the specified location, false otherwise.
*/
public static boolean checkLocation(Location baseLocation, Location theLocation, int theLeeway) {
public static boolean checkLocation(Location baseLocation, Location theLocation, double theLeeway) {

if (!baseLocation.getWorld().getName().equals(theLocation.getWorld().getName()))
return false;
Expand Down

0 comments on commit ef88099

Please sign in to comment.