Permalink
Browse files

Added a limitation to the length of a pipe to prevent massive crashes.

  • Loading branch information...
me4502 committed May 21, 2017
1 parent e384579 commit 6467c8b4c4694e4b842cf1e7b30ed1ead8e386e7
Showing with 13 additions and 10 deletions.
  1. +1 −1 pom.xml
  2. +12 −9 src/main/java/com/sk89q/craftbook/mechanics/pipe/Pipes.java
View
@@ -58,7 +58,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12-pre2-SNAPSHOT</version>
<version>1.12-pre5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sk89q</groupId>
@@ -118,8 +118,11 @@ 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,
int depth) {
private void searchNearbyPipes(Block block, Set<Vector> visitedPipes, List<ItemStack> items, int depth) {
if (depth > 1200) {
CraftBookPlugin.logger().info("Pipe of excessive length found at " + block.getLocation().toString());
return;
}
LinkedList<Block> searchQueue = new LinkedList<>();
//Enumerate the search queue.
@@ -194,7 +197,7 @@ else if (off.getType() == Material.THIN_GLASS || off.getType() == Material.STAIN
for(Block bl : searchQueue) {
if (bl.getType() == Material.GLASS || bl.getType() == Material.STAINED_GLASS) {
try {
searchNearbyPipes(bl, visitedPipes, items, filters, exceptions, depth + 1);
searchNearbyPipes(bl, visitedPipes, items, depth + 1);
} catch (StackOverflowError e) {
CraftBookPlugin.logger().warning("Pipes encountered a StackOverflowError at position: " + bl.getLocation().toString() + ". "
+ "This occured at a depth of: " + depth);
@@ -258,7 +261,7 @@ else if (off.getType() == Material.THIN_GLASS || off.getType() == Material.STAIN
items.addAll(newItems);
}
if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, filters, exceptions, depth + 1);
if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, depth + 1);
} else if (bl.getType() == Material.DROPPER) {
ChangedSign sign = getSignOnPiston(bl);
@@ -295,7 +298,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, depth + 1);
if (!items.isEmpty()) searchNearbyPipes(block, visitedPipes, items, depth + 1);
}
}
}
@@ -354,7 +357,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, 0);
searchNearbyPipes(block, visitedPipes, items, 0);
}
if (!items.isEmpty()) {
@@ -377,7 +380,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, 0);
searchNearbyPipes(block, visitedPipes, items, 0);
}
if (!items.isEmpty()) {
@@ -403,7 +406,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, 0);
searchNearbyPipes(block, visitedPipes, items, 0);
}
if (!items.isEmpty()) {
@@ -420,7 +423,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, 0);
searchNearbyPipes(block, visitedPipes, items, 0);
}
leftovers.addAll(items);
}

0 comments on commit 6467c8b

Please sign in to comment.