Skip to content

Commit

Permalink
better locationtag/etc block loaded checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 19, 2019
1 parent 6b448c1 commit 8956f48
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 128 deletions.
Expand Up @@ -126,7 +126,7 @@ public static boolean matches(String string) {
public Chunk getChunkForTag(Attribute attribute) {
if (!isLoaded()) {
if (!attribute.hasAlternative()) {
Debug.echoError("Cannot get chunk at " + chunkX + ", " + chunkZ + ": Chunk is not loaded. Use 'chunkload' command to ensure the chunk is loaded.");
Debug.echoError("Cannot get chunk at " + chunkX + ", " + chunkZ + ": Chunk is not loaded. Use the 'chunkload' command to ensure the chunk is loaded.");
}
return null;
}
Expand Down
Expand Up @@ -492,15 +492,15 @@ public ListTag getOutline() {
}


public ListTag getBlocks() {
return getBlocks(null);
public ListTag getBlocks(Attribute attribute) {
return getBlocks(null, attribute);
}

private boolean matchesMaterialList(Location loc, List<MaterialTag> materials) {
private boolean matchesMaterialList(Location loc, List<MaterialTag> materials, Attribute attribute) {
if (materials == null) {
return true;
}
MaterialTag mat = new MaterialTag(loc.getBlock());
MaterialTag mat = new MaterialTag(new LocationTag(loc).getBlockForTag(attribute));
for (MaterialTag material : materials) {
if (mat.equals(material) || mat.getMaterial() == material.getMaterial()) {
return true;
Expand All @@ -509,16 +509,16 @@ private boolean matchesMaterialList(Location loc, List<MaterialTag> materials) {
return false;
}

public ListTag getBlocks(List<MaterialTag> materials) {
List<LocationTag> locs = getBlocks_internal(materials);
public ListTag getBlocks(List<MaterialTag> materials, Attribute attribute) {
List<LocationTag> locs = getBlocks_internal(materials, attribute);
ListTag list = new ListTag();
for (LocationTag loc : locs) {
list.add(loc.identify());
}
return list;
}

public List<LocationTag> getBlocks_internal(List<MaterialTag> materials) {
public List<LocationTag> getBlocks_internal(List<MaterialTag> materials, Attribute attribute) {
int max = Settings.blockTagsMaxBlocks();
LocationTag loc;
List<LocationTag> list = new ArrayList<>();
Expand All @@ -541,15 +541,15 @@ public List<LocationTag> getBlocks_internal(List<MaterialTag> materials) {
if (!filter.isEmpty()) { // TODO: Should 'filter' exist?
// Check filter
for (ObjectTag material : filter) {
if (((MaterialTag) material).matchesBlock(loc.getBlock())) {
if (matchesMaterialList(loc, materials)) {
if (((MaterialTag) material).matchesBlock(loc.getBlockForTag(attribute))) {
if (matchesMaterialList(loc, materials, attribute)) {
list.add(loc);
}
}
}
}
else {
if (matchesMaterialList(loc, materials)) {
if (matchesMaterialList(loc, materials, attribute)) {
list.add(loc);
}
}
Expand Down Expand Up @@ -612,29 +612,52 @@ public BlockData getBlockAt(double nX, double nY, double nZ, List<BlockData> mat
return null;
}

public List<LocationTag> getBlockLocations() {
public List<LocationTag> getBlockLocationsUnfiltered() {
int max = Settings.blockTagsMaxBlocks();
LocationTag loc;
List<LocationTag> list = new ArrayList<>();
int index = 0;

for (LocationPair pair : pairs) {

LocationTag loc_1 = pair.low;
int y_distance = pair.y_distance;
int z_distance = pair.z_distance;
int x_distance = pair.x_distance;
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 LocationTag(loc_1.clone().add(x, y, z));
list.add(loc);
index++;
if (index > max) {
return list;
}
}
}
}
}
return list;
}

public List<LocationTag> getBlockLocations(Attribute attribute) {
int max = Settings.blockTagsMaxBlocks();
LocationTag loc;
List<LocationTag> list = new ArrayList<>();
int index = 0;

for (LocationPair pair : pairs) {
LocationTag loc_1 = pair.low;
int y_distance = pair.y_distance;
int z_distance = pair.z_distance;
int x_distance = pair.x_distance;
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 LocationTag(loc_1.clone()
.add(x, y, z));
loc = new LocationTag(loc_1.clone().add(x, y, z));
if (!filter.isEmpty()) {
// Check filter
for (ObjectTag material : filter) {
if (loc.getBlock().getType().name().equalsIgnoreCase(((MaterialTag) material)
.getMaterial().name())) {
if (loc.getBlockForTag(attribute).getType().name().equalsIgnoreCase(((MaterialTag) material).getMaterial().name())) {
list.add(loc);
}
}
Expand All @@ -649,15 +672,13 @@ public List<LocationTag> getBlockLocations() {
}
}
}

}

return list;
}


public ListTag getSpawnableBlocks() {
return getSpawnableBlocks(null);
public ListTag getSpawnableBlocks(Attribute attribute) {
return getSpawnableBlocks(null, attribute);
}

/**
Expand All @@ -668,7 +689,7 @@ public ListTag getSpawnableBlocks() {
* @return The ListTag
*/

public ListTag getSpawnableBlocks(List<MaterialTag> mats) {
public ListTag getSpawnableBlocks(List<MaterialTag> mats, Attribute attribute) {
int max = Settings.blockTagsMaxBlocks();
LocationTag loc;
ListTag list = new ListTag();
Expand All @@ -688,10 +709,10 @@ public ListTag getSpawnableBlocks(List<MaterialTag> mats) {
loc = new LocationTag(loc_1.clone()
.add(x, y, z));

if (blockHelper.isSafeBlock(loc.getBlock().getType())
&& blockHelper.isSafeBlock(loc.clone().add(0, 1, 0).getBlock().getType())
&& loc.clone().add(0, -1, 0).getBlock().getType().isSolid()
&& matchesMaterialList(loc.clone().add(0, -1, 0), mats)) {
if (blockHelper.isSafeBlock(loc.getBlockForTag(attribute).getType())
&& blockHelper.isSafeBlock(new LocationTag(loc.clone().add(0, 1, 0)).getBlockForTag(attribute).getType())
&& new LocationTag(loc.clone().add(0, -1, 0)).getBlockForTag(attribute).getType().isSolid()
&& matchesMaterialList(loc.clone().add(0, -1, 0), mats, attribute)) {
// Get the center of the block, so the entity won't suffocate
// inside the edges for a couple of seconds
loc.add(0.5, 0, 0.5);
Expand Down Expand Up @@ -859,11 +880,11 @@ public static void registerTags() {
@Override
public String run(Attribute attribute, ObjectTag object) {
if (attribute.hasContext(1)) {
return new ListTag(((CuboidTag) object).getBlocks(ListTag.valueOf(attribute.getContext(1)).filter(MaterialTag.class, attribute.context)))
return new ListTag(((CuboidTag) object).getBlocks(ListTag.valueOf(attribute.getContext(1)).filter(MaterialTag.class, attribute.context), attribute))
.getAttribute(attribute.fulfill(1));
}
else {
return new ListTag(((CuboidTag) object).getBlocks())
return new ListTag(((CuboidTag) object).getBlocks(attribute))
.getAttribute(attribute.fulfill(1));
}
}
Expand Down Expand Up @@ -897,11 +918,11 @@ public String run(Attribute attribute, ObjectTag object) {
@Override
public String run(Attribute attribute, ObjectTag object) {
if (attribute.hasContext(1)) {
return new ListTag(((CuboidTag) object).getSpawnableBlocks(ListTag.valueOf(attribute.getContext(1)).filter(MaterialTag.class, attribute.context)))
return new ListTag(((CuboidTag) object).getSpawnableBlocks(ListTag.valueOf(attribute.getContext(1)).filter(MaterialTag.class, attribute.context), attribute))
.getAttribute(attribute.fulfill(1));
}
else {
return new ListTag(((CuboidTag) object).getSpawnableBlocks())
return new ListTag(((CuboidTag) object).getSpawnableBlocks(attribute))
.getAttribute(attribute.fulfill(1));
}
}
Expand Down
Expand Up @@ -16,7 +16,6 @@
import java.util.HashMap;
import java.util.List;


public class EllipsoidTag implements ObjectTag, Notable {

// <--[language]
Expand Down Expand Up @@ -131,16 +130,16 @@ public EllipsoidTag(LocationTag loc, LocationTag size) {

private LocationTag size;

public ListTag getBlocks() {
return getBlocks(null);
public ListTag getBlocks(Attribute attribute) {
return getBlocks(null, attribute);
}

public ListTag getBlocks(List<MaterialTag> materials) {
public ListTag getBlocks(List<MaterialTag> materials, Attribute attribute) {
List<LocationTag> initial = new CuboidTag(new Location(loc.getWorld(),
loc.getX() - size.getX(), loc.getY() - size.getY(), loc.getZ() - size.getZ()),
new Location(loc.getWorld(),
loc.getX() + size.getX(), loc.getY() + size.getY(), loc.getZ() + size.getZ()))
.getBlocks_internal(materials);
.getBlocks_internal(materials, attribute);
ListTag list = new ListTag();
for (LocationTag loc : initial) {
if (contains(loc)) {
Expand All @@ -150,12 +149,27 @@ public ListTag getBlocks(List<MaterialTag> materials) {
return list;
}

public List<LocationTag> getBlockLocations() {
public List<LocationTag> getBlockLocationsUnfiltered() {
List<LocationTag> initial = new CuboidTag(new Location(loc.getWorld(),
loc.getX() - size.getX(), loc.getY() - size.getY(), loc.getZ() - size.getZ()),
new Location(loc.getWorld(),
loc.getX() + size.getX(), loc.getY() + size.getY(), loc.getZ() + size.getZ()))
.getBlockLocationsUnfiltered();
List<LocationTag> locations = new ArrayList<>();
for (LocationTag loc : initial) {
if (contains(loc)) {
locations.add(loc);
}
}
return locations;
}

public List<LocationTag> getBlockLocations(Attribute attribute) {
List<LocationTag> initial = new CuboidTag(new Location(loc.getWorld(),
loc.getX() - size.getX(), loc.getY() - size.getY(), loc.getZ() - size.getZ()),
new Location(loc.getWorld(),
loc.getX() + size.getX(), loc.getY() + size.getY(), loc.getZ() + size.getZ()))
.getBlocks_internal(null);
.getBlocks_internal(null, attribute);
List<LocationTag> locations = new ArrayList<>();
for (LocationTag loc : initial) {
if (contains(loc)) {
Expand Down Expand Up @@ -264,11 +278,11 @@ public static void registerTags() {
@Override
public String run(Attribute attribute, ObjectTag object) {
if (attribute.hasContext(1)) {
return new ListTag(((EllipsoidTag) object).getBlocks(ListTag.valueOf(attribute.getContext(1)).filter(MaterialTag.class, attribute.context)))
return new ListTag(((EllipsoidTag) object).getBlocks(ListTag.valueOf(attribute.getContext(1)).filter(MaterialTag.class, attribute.context), attribute))
.getAttribute(attribute.fulfill(1));
}
else {
return new ListTag(((EllipsoidTag) object).getBlocks())
return new ListTag(((EllipsoidTag) object).getBlocks(attribute))
.getAttribute(attribute.fulfill(1));
}
}
Expand Down

0 comments on commit 8956f48

Please sign in to comment.