Skip to content

Commit

Permalink
Added depth checks to Pipes.
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed May 15, 2017
1 parent e032371 commit 076bb19
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/main/java/com/sk89q/craftbook/mechanics/pipe/Pipes.java
Expand Up @@ -118,7 +118,8 @@ else if (block.getRelative(face).getType() == Material.SIGN_POST && face != Bloc
return null;
}

private void searchNearbyPipes(Block block, Set<Vector> visitedPipes, List<ItemStack> items, Set<ItemStack> filters, Set<ItemStack> exceptions) {
private void searchNearbyPipes(Block block, Set<Vector> visitedPipes, List<ItemStack> items, Set<ItemStack> filters, Set<ItemStack> exceptions,
int depth) {
LinkedList<Block> searchQueue = new LinkedList<Block>();

//Enumerate the search queue.
Expand Down Expand Up @@ -192,7 +193,12 @@ else if (off.getType() == Material.THIN_GLASS || off.getType() == Material.STAIN
//Use the queue to search blocks.
for(Block bl : searchQueue) {
if (bl.getType() == Material.GLASS || bl.getType() == Material.STAINED_GLASS) {
searchNearbyPipes(bl, visitedPipes, items, filters, exceptions);
try {
searchNearbyPipes(bl, visitedPipes, items, filters, exceptions, depth + 1);
} catch (StackOverflowError e) {
CraftBookPlugin.logger().warning("Pipes encountered a StackOverflowError at position: " + bl.getLocation().toString() + ". "
+ "This occured at a depth of: " + depth);
}
} else if (bl.getType() == Material.PISTON_BASE) {

PistonBaseMaterial p = (PistonBaseMaterial) bl.getState().getData();
Expand Down Expand Up @@ -252,7 +258,7 @@ else if (off.getType() == Material.THIN_GLASS || off.getType() == Material.STAIN
items.addAll(newItems);
}

if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, filters, exceptions);
if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, filters, exceptions, depth + 1);
} else if (bl.getType() == Material.DROPPER) {

ChangedSign sign = getSignOnPiston(bl);
Expand Down Expand Up @@ -288,7 +294,7 @@ else if (off.getType() == Material.THIN_GLASS || off.getType() == Material.STAIN
items.removeAll(filteredItems);
items.addAll(newItems);

if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, filters, exceptions);
if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, filters, exceptions, depth + 1);
}
}
}
Expand Down Expand Up @@ -347,7 +353,7 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
items.addAll(event.getItems());
if(!event.isCancelled()) {
visitedPipes.add(fac.getLocation().toVector());
searchNearbyPipes(block, visitedPipes, items, filters, exceptions);
searchNearbyPipes(block, visitedPipes, items, filters, exceptions, 0);
}

if (!items.isEmpty()) {
Expand All @@ -370,7 +376,7 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
items.addAll(event.getItems());
if(!event.isCancelled()) {
visitedPipes.add(fac.getLocation().toVector());
searchNearbyPipes(block, visitedPipes, items, filters, exceptions);
searchNearbyPipes(block, visitedPipes, items, filters, exceptions, 0);
}

if (!items.isEmpty()) {
Expand All @@ -396,7 +402,7 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {

if (!event.isCancelled()) {
visitedPipes.add(fac.getLocation().toVector());
searchNearbyPipes(block, visitedPipes, items, filters, exceptions);
searchNearbyPipes(block, visitedPipes, items, filters, exceptions, 0);
}

if (!items.isEmpty()) {
Expand All @@ -413,7 +419,7 @@ private void startPipe(Block block, List<ItemStack> items, boolean request) {
items.addAll(event.getItems());
if(!event.isCancelled() && !items.isEmpty()) {
visitedPipes.add(fac.getLocation().toVector());
searchNearbyPipes(block, visitedPipes, items, filters, exceptions);
searchNearbyPipes(block, visitedPipes, items, filters, exceptions, 0);
}
leftovers.addAll(items);
}
Expand Down

0 comments on commit 076bb19

Please sign in to comment.