From 699c78d4ac947bbd243b648d32ee853551b4d3e6 Mon Sep 17 00:00:00 2001 From: AlgorithmX2 Date: Sun, 10 Aug 2014 00:16:02 -0500 Subject: [PATCH] Prevent Fluid Tunnels from looping into them selves infinitely. --- parts/p2p/PartP2PLiquids.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/parts/p2p/PartP2PLiquids.java b/parts/p2p/PartP2PLiquids.java index eb17ca6a4..759484239 100644 --- a/parts/p2p/PartP2PLiquids.java +++ b/parts/p2p/PartP2PLiquids.java @@ -3,6 +3,7 @@ package appeng.parts.p2p; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Stack; import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; @@ -141,9 +142,29 @@ public class PartP2PLiquids extends PartP2PTunnel implements IFl return null; } + static final ThreadLocal> depth = new ThreadLocal>(); + + private Stack getDepth() + { + Stack s = depth.get(); + + if ( s == null ) + depth.set( s = new Stack() ); + + return s; + } + @Override public int fill(ForgeDirection from, FluidStack resource, boolean doFill) { + Stack stack = getDepth(); + + for (PartP2PLiquids t : stack) + if ( t == this ) + return 0; + + stack.push( this ); + List list = getOutputs( resource.getFluid() ); int requestTotal = 0; @@ -164,10 +185,20 @@ public class PartP2PLiquids extends PartP2PTunnel implements IFl } if ( requestTotal <= 0 ) + { + if ( stack.pop() != this ) + throw new RuntimeException( "Invalid Recursion detected." ); + return 0; + } if ( !doFill ) + { + if ( stack.pop() != this ) + throw new RuntimeException( "Invalid Recursion detected." ); + return Math.min( resource.amount, requestTotal ); + } int avilable = resource.amount; int used = 0; @@ -192,6 +223,9 @@ public class PartP2PLiquids extends PartP2PTunnel implements IFl used += insert.amount; } + if ( stack.pop() != this ) + throw new RuntimeException( "Invalid Recursion detected." ); + return used; }