From ef8809925ec65026c238116cc0a77f394c2a3bb9 Mon Sep 17 00:00:00 2001 From: MorphanOne Date: Thu, 19 Dec 2013 19:23:45 -0500 Subject: [PATCH] Allow dLocation .within[<#>] tags to use <#.#> --- .../aufdemrand/denizen/objects/dLocation.java | 56 +++++++++---------- .../aufdemrand/denizen/tags/Attribute.java | 12 +++- .../denizen/utilities/Utilities.java | 4 +- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/objects/dLocation.java b/src/main/java/net/aufdemrand/denizen/objects/dLocation.java index 192c54c86a..db8b26874c 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dLocation.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dLocation.java @@ -627,7 +627,7 @@ else if (getYaw() < 315) attribute.fulfill(1); // <--[tag] - // @attribute |...].within[<#>]> + // @attribute |...].within[<#.#>]> // @returns dList // @description // Returns a list of matching blocks within a radius. @@ -636,7 +636,7 @@ else if (getYaw() < 315) && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) { ArrayList found = new ArrayList(); - int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10; + double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10; List materials = new ArrayList(); if (attribute.hasContext(1)) materials = dList.valueOf(attribute.getContext(1)).filter(dMaterial.class); @@ -644,16 +644,16 @@ else if (getYaw() < 315) // 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() { @Override @@ -666,7 +666,7 @@ public int compare(dLocation loc1, dLocation loc2) { } // <--[tag] - // @attribute |...].within[<#>]> + // @attribute |...].within[<#.#>]> // @returns dList // @description // Returns a list of matching surface blocks within a radius. @@ -675,31 +675,31 @@ else if (attribute.startsWith("surface_blocks") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) { ArrayList found = new ArrayList(); - int radius = aH.matchesInteger(attribute.getContext(2)) ? attribute.getIntContext(2) : 10; + double radius = aH.matchesDouble(attribute.getContext(2)) ? attribute.getDoubleContext(2) : 10; List materials = new ArrayList(); 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() { @@ -713,7 +713,7 @@ public int compare(dLocation loc1, dLocation loc2) { } // <--[tag] - // @attribute ]> + // @attribute ]> // @returns dList // @description // Returns a list of players within a radius. @@ -722,7 +722,7 @@ else if (attribute.startsWith("players") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) { ArrayList found = new ArrayList(); - 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)) @@ -739,7 +739,7 @@ public int compare(dPlayer pl1, dPlayer pl2) { } // <--[tag] - // @attribute ]> + // @attribute ]> // @returns dList // @description // Returns a list of NPCs within a radius. @@ -748,7 +748,7 @@ else if (attribute.startsWith("npcs") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) { ArrayList found = new ArrayList(); - 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)) @@ -765,7 +765,7 @@ public int compare(dNPC npc1, dNPC npc2) { } // <--[tag] - // @attribute |...].within[<#>]> + // @attribute |...].within[<#.#>]> // @returns dList // @description // Returns a list of entities within a radius, with an optional search parameter @@ -782,7 +782,7 @@ else if (attribute.startsWith("entities") } } ArrayList found = new ArrayList(); - 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)) { @@ -811,7 +811,7 @@ public int compare(dEntity ent1, dEntity ent2) { } // <--[tag] - // @attribute ]> + // @attribute ]> // @returns dList // @description // Returns a list of living entities within a radius. @@ -820,7 +820,7 @@ else if (attribute.startsWith("living_entities") && attribute.getAttribute(2).startsWith("within") && attribute.hasContext(2)) { ArrayList found = new ArrayList(); - 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 diff --git a/src/main/java/net/aufdemrand/denizen/tags/Attribute.java b/src/main/java/net/aufdemrand/denizen/tags/Attribute.java index 889b9d2a2b..d0f9d0c04d 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/Attribute.java +++ b/src/main/java/net/aufdemrand/denizen/tags/Attribute.java @@ -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); diff --git a/src/main/java/net/aufdemrand/denizen/utilities/Utilities.java b/src/main/java/net/aufdemrand/denizen/utilities/Utilities.java index 53db295e5c..abafb35e59 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/Utilities.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/Utilities.java @@ -344,7 +344,7 @@ public static Set 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; @@ -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;