From 16c5284923657aab2b53e47948ffe158b595b018 Mon Sep 17 00:00:00 2001 From: mezz Date: Mon, 23 May 2016 23:37:32 -0700 Subject: [PATCH] Fix #1215 Stack overflow when validating greenhouse structure --- .../multiblock/GreenhouseController.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java b/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java index eda9b61c6..30bd17b5b 100644 --- a/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java +++ b/src/main/java/forestry/greenhouse/multiblock/GreenhouseController.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.Stack; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; @@ -635,13 +636,20 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im internalBlocks.clear(); if (isNextRoof) { - checkInternalBlock(createInternalBlock(new InternalBlock(getMinimumCoord().add(1, 1, 1)))); + Stack blocksToCheck = new Stack<>(); + IInternalBlock internalBlock = createInternalBlock(new InternalBlock(getMinimumCoord().add(1, 1, 1))); + blocksToCheck.add(internalBlock); + while (!blocksToCheck.isEmpty()) { + IInternalBlock blockToCheck = blocksToCheck.pop(); + List newBlocksToCheck = checkInternalBlock(blockToCheck); + blocksToCheck.addAll(newBlocksToCheck); + } } + if (internalBlocks.isEmpty()) { throw new MultiblockValidationException(Translator.translateToLocalFormatted("for.multiblock.error.space.closed")); } - this.internalBlocks.addAll(internalBlocks); - + int hatches = 0; for (IMultiblockComponent comp : connectedParts) { if (comp instanceof IGreenhouseComponent.ButterflyHatch) { @@ -656,8 +664,14 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im @Override protected void updateClient(int tickCount) { } - - private void checkInternalBlock(IInternalBlock blockToCheck) throws MultiblockValidationException { + + /** + * Returns a list of more internal blocks to check. + * @throws MultiblockValidationException + */ + private List checkInternalBlock(IInternalBlock blockToCheck) throws MultiblockValidationException { + List newBlocksToCheck = new ArrayList<>(); + internalBlocks.add(blockToCheck); BlockPos posRoot = blockToCheck.getPos(); @@ -692,11 +706,12 @@ public class GreenhouseController extends RectangularMultiblockControllerBase im if (internalBlocks.contains(internalBlock)) { faceToCheck.setTested(true); } else { - checkInternalBlock(internalBlock); + newBlocksToCheck.add(internalBlock); } } } } + return newBlocksToCheck; } private IInternalBlock createInternalBlock(IInternalBlock internalBlock) {