From 1739fd5fa57451e6ba02b8d0e39c92f8311cf464 Mon Sep 17 00:00:00 2001 From: DarkGuardsman Date: Thu, 12 Sep 2013 21:44:14 -0400 Subject: [PATCH] Cleaned up fluidstack merge code --- .../network/fluid/FluidNetworkHelper.java | 45 ++++++++++++------- .../core/network/fluid/NetworkFluidTiles.java | 4 +- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/dark/core/network/fluid/FluidNetworkHelper.java b/src/dark/core/network/fluid/FluidNetworkHelper.java index 04712c851..df30d8f5f 100644 --- a/src/dark/core/network/fluid/FluidNetworkHelper.java +++ b/src/dark/core/network/fluid/FluidNetworkHelper.java @@ -49,61 +49,66 @@ public class FluidNetworkHelper } /** Merges two fluids together that don't result in damage to the network */ - public static FluidStack mergeFluids(FluidStack stackOne, FluidStack stackTwo) + public static FluidStack mergeFluidStacks(FluidStack stackOne, FluidStack stackTwo) { - FluidStack stack = null; + FluidStack resultStack = null; if (stackTwo != null && stackOne != null && stackOne.isFluidEqual(stackTwo)) { - stack = stackOne.copy(); - stack.amount += stackTwo.amount; + resultStack = stackOne.copy(); + resultStack.amount += stackTwo.amount; } else if (stackOne == null && stackTwo != null) { - stack = stackTwo.copy(); + resultStack = stackTwo.copy(); } else if (stackOne != null && stackTwo == null) { - stack = stackOne.copy(); + resultStack = stackOne.copy(); } else if (stackTwo != null && stackOne != null && !stackOne.isFluidEqual(stackTwo)) { - Fluid waste = FluidRegistry.getFluid("waste"); /* Try to merge fluids by mod defined rules first */ if (fluidMergeResults.containsKey(new Pair(stackOne.getFluid(), stackTwo.getFluid()))) { Object result = fluidMergeResults.get(new Pair(stackOne.getFluid(), stackTwo.getFluid())); if (result instanceof Fluid) { - stack = new FluidStack(((Fluid) result).getID(), stackOne.amount + stackTwo.amount); + resultStack = new FluidStack(((Fluid) result).getID(), stackOne.amount + stackTwo.amount); } else if (result instanceof FluidStack) { - stack = ((FluidStack) result).copy(); - stack.amount = stackOne.amount + stackTwo.amount; + resultStack = ((FluidStack) result).copy(); + resultStack.amount = stackOne.amount + stackTwo.amount; } else if (result instanceof String && ((String) result).startsWith("Liquid:")) { - stack = new FluidStack(FluidRegistry.getFluid(((String) result).replace("Liquid:", "")), stackOne.amount + stackTwo.amount); + resultStack = new FluidStack(FluidRegistry.getFluid(((String) result).replace("Liquid:", "")), stackOne.amount + stackTwo.amount); } } - if (stack != null) + if (resultStack == null) { + Fluid waste = FluidRegistry.getFluid("waste"); + if (waste == null) + { + System.out.println("[FluidNetworkHelper] Attempted to merge two fluids into a waste fluid stack but Forge fluid registry return null for waste. Possible that waste fluid was disabled or not registered correctly."); + return null; + } /* If both liquids are waste then copy fluidStack lists then merge */ if (stackTwo.fluidID == waste.getID() && stackOne.fluidID == waste.getID()) { List stacks = new ArrayList(); stacks.addAll(getStacksFromWaste(stackOne.copy())); stacks.addAll(getStacksFromWaste(stackTwo.copy())); - stack = createNewWasteStack(stacks.toArray(new FluidStack[stacks.size()])); + resultStack = createNewWasteStack(stacks.toArray(new FluidStack[stacks.size()])); } else { - stack = createNewWasteStack(stackOne.copy(), stackTwo.copy()); + resultStack = createNewWasteStack(stackOne.copy(), stackTwo.copy()); } } } - return stack; + return resultStack; } /** Gets the fluidStacks that make up a waste FluidStack */ @@ -158,8 +163,14 @@ public class FluidNetworkHelper return stack; } - /** Checks if the liquid can be merged without damage */ - public static Object canMergeFluids(FluidStack stackOne, FluidStack stackTwo) + /** Gets the result of the merge of the two fluids, order of merge does matter and will produce + * diffrent results. + * + * @param stackOne - Receiving fluid, eg the one that is not moving + * @param stackTwo - Flowing fluid, eg the one moving into the first fluid + * @return Object result of the merge, can be anything from string, ItemStack, Item, Block, or + * enum action */ + public static Object getMergeResult(FluidStack stackOne, FluidStack stackTwo) { if (stackOne != null && stackTwo != null && !stackOne.equals(stackTwo)) { diff --git a/src/dark/core/network/fluid/NetworkFluidTiles.java b/src/dark/core/network/fluid/NetworkFluidTiles.java index e7a45329c..778ef4ae6 100644 --- a/src/dark/core/network/fluid/NetworkFluidTiles.java +++ b/src/dark/core/network/fluid/NetworkFluidTiles.java @@ -224,7 +224,7 @@ public class NetworkFluidTiles extends NetworkTileEntities this.readDataFromTiles(); network.readDataFromTiles(); - Object result = FluidNetworkHelper.canMergeFluids(this.combinedStorage().getFluid(), network.combinedStorage().getFluid()); + Object result = FluidNetworkHelper.getMergeResult(this.combinedStorage().getFluid(), network.combinedStorage().getFluid()); if (mergePoint instanceof TileEntity) { World world = ((TileEntity) mergePoint).worldObj; @@ -309,7 +309,7 @@ public class NetworkFluidTiles extends NetworkTileEntities newNetwork.getNetworkMemebers().addAll(network.getNetworkMemebers()); newNetwork.cleanUpMembers(); - newNetwork.combinedStorage().setFluid(FluidNetworkHelper.mergeFluids(one, two)); + newNetwork.combinedStorage().setFluid(FluidNetworkHelper.mergeFluidStacks(one, two)); newNetwork.writeDataToTiles(); }