Skip to content

Commit

Permalink
add cuboid.get_blocks[<material>]
Browse files Browse the repository at this point in the history
A way to simplify cuboid block reading
  • Loading branch information
mcmonkey4eva committed Oct 17, 2014
1 parent 0de864c commit 0abb8c8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
52 changes: 40 additions & 12 deletions src/main/java/net/aufdemrand/denizen/objects/dCuboid.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ public dList getOutline() {


public dList getBlocks() {
return getBlocks(null);
}

private boolean matchesMaterialList(Location loc, List<dMaterial> materials) {
if (materials == null)
return true;
dMaterial mat = dMaterial.getMaterialFrom(loc.getBlock().getType(), loc.getBlock().getData());
for (dMaterial material: materials) {
if (mat.equals(material)) {
return true;
}
}
return false;
}

public dList getBlocks(List<dMaterial> materials) {
dLocation loc;
dList list = new dList();

Expand All @@ -385,16 +401,23 @@ public dList getBlocks() {
for (int x = 0; x != x_distance + 1; x++) {
for (int z = 0; z != z_distance + 1; z++) {
for (int y = 0; y != y_distance + 1; y++) {
loc = new dLocation(loc_1.clone()
.add(x, y, z));
loc = new dLocation(loc_1.clone().add(x, y, z));
if (!filter.isEmpty()) {
// Check filter
for (dObject material : filter)
for (dObject material : filter) {
if (loc.getBlock().getType().name().equalsIgnoreCase(((dMaterial) material)
.getMaterial().name()))
list.add(loc.identify());
} else
list.add(loc.identify());
.getMaterial().name())) {
if (matchesMaterialList(loc, materials)) {
list.add(loc.identify());
}
}
}
}
else {
if (matchesMaterialList(loc, materials)) {
list.add(loc.identify());
}
}
}
}
}
Expand Down Expand Up @@ -613,14 +636,19 @@ public String getAttribute(Attribute attribute) {
if (attribute == null) return null;

// <--[tag]
// @attribute <cu@cuboid.get_blocks>
// @attribute <cu@cuboid.get_blocks[<material>...]>
// @returns dList(dLocation)
// @description
// Returns each block location within the dCuboid.
// -->
if (attribute.startsWith("get_blocks"))
return new dList(getBlocks())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("get_blocks")) {
if (attribute.hasContext(1))
return new dList(getBlocks(dList.valueOf(attribute.getContext(1)).filter(dMaterial.class)))
.getAttribute(attribute.fulfill(1));
else
return new dList(getBlocks())
.getAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <cu@cuboid.members_size>
Expand Down Expand Up @@ -784,7 +812,7 @@ public String getAttribute(Attribute attribute) {

// <--[tag]
// @attribute <cu@cuboid.include[<location>]>
// @returns dLocation
// @returns dCuboid
// @description
// Expands the first member of the dCuboid to contain the given location.
// -->
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/aufdemrand/denizen/objects/dMaterial.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,17 +577,17 @@ public static boolean matches(String arg) {
}

/**
* TODO: Needs testing.
*
* @param object object-fetchable String of a valid dMaterial, or a dMaterial object
* @return true if the dMaterials are the same.
*/
@Override
public boolean equals(Object object) {
if (object instanceof dMaterial)
return ((dMaterial) object).identify().equals(this.identify());
else
return valueOf(object.toString()) != null && valueOf(object.toString()).identify().equals(this.identify());
else {
dMaterial parsed = valueOf(object.toString());
return parsed != null && parsed.identify().equals(this.identify());
}
}


Expand Down

0 comments on commit 0abb8c8

Please sign in to comment.