Moved fluid merge code into FluidNetworkHelper

HydraulicNetworkHelper renamed to FluidNetworkHelper

Moved this to clear up some code as well provide easy access the the
fluid merge code.
This commit is contained in:
DarkGuardsman 2013-09-12 21:27:00 -04:00
parent 4017098901
commit f3749fcaa9
6 changed files with 185 additions and 176 deletions

View file

@ -0,0 +1,175 @@
package dark.core.network.fluid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import universalelectricity.core.vector.Vector3;
import universalelectricity.core.vector.VectorHelper;
import dark.api.parts.INetworkPart;
import dark.core.prefab.helpers.Pair;
import dark.core.prefab.tilenetwork.NetworkTileEntities;
public class FluidNetworkHelper
{
/** Map of results of two different liquids merging */
public static HashMap<Pair<Fluid, Fluid>, Object> fluidMergeResults = new HashMap<Pair<Fluid, Fluid>, Object>();
static
{
fluidMergeResults.put(new Pair<Fluid, Fluid>(FluidRegistry.WATER, FluidRegistry.LAVA), Block.obsidian);
fluidMergeResults.put(new Pair<Fluid, Fluid>(FluidRegistry.LAVA, FluidRegistry.WATER), Block.cobblestone);
}
/** Invalidates a TileEntity that is part of a fluid network */
public static void invalidate(TileEntity tileEntity)
{
for (int i = 0; i < 6; i++)
{
ForgeDirection direction = ForgeDirection.getOrientation(i);
TileEntity checkTile = VectorHelper.getConnectorFromSide(tileEntity.worldObj, new Vector3(tileEntity), direction);
if (checkTile instanceof INetworkPart)
{
NetworkTileEntities network = ((INetworkPart) checkTile).getTileNetwork();
if (network != null && network instanceof NetworkFluidTiles)
{
network.removeTile(tileEntity);
}
}
}
}
/** Merges two fluids together that don't result in damage to the network */
public static FluidStack mergeFluids(FluidStack stackOne, FluidStack stackTwo)
{
FluidStack stack = null;
if (stackTwo != null && stackOne != null && stackOne.isFluidEqual(stackTwo))
{
stack = stackOne.copy();
stack.amount += stackTwo.amount;
}
else if (stackOne == null && stackTwo != null)
{
stack = stackTwo.copy();
}
else if (stackOne != null && stackTwo == null)
{
stack = 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<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid())))
{
Object result = fluidMergeResults.get(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid()));
if (result instanceof Fluid)
{
stack = new FluidStack(((Fluid) result).getID(), stackOne.amount + stackTwo.amount);
}
else if (result instanceof FluidStack)
{
stack = ((FluidStack) result).copy();
stack.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);
}
}
if (stack != null)
{
/* If both liquids are waste then copy fluidStack lists then merge */
if (stackTwo.fluidID == waste.getID() && stackOne.fluidID == waste.getID())
{
List<FluidStack> stacks = new ArrayList<FluidStack>();
stacks.addAll(getStacksFromWaste(stackOne.copy()));
stacks.addAll(getStacksFromWaste(stackTwo.copy()));
stack = createNewWasteStack(stacks.toArray(new FluidStack[stacks.size()]));
}
else
{
stack = createNewWasteStack(stackOne.copy(), stackTwo.copy());
}
}
}
return stack;
}
/** Gets the fluidStacks that make up a waste FluidStack */
public static List<FluidStack> getStacksFromWaste(FluidStack wasteStack)
{
List<FluidStack> stacks = new ArrayList<FluidStack>();
if (wasteStack.fluidID == FluidRegistry.getFluidID("waste"))
{
for (int i = 1; i <= wasteStack.tag.getInteger("liquids"); i++)
{
FluidStack readStack = FluidStack.loadFluidStackFromNBT(wasteStack.tag.getCompoundTag("Liquid" + i));
if (readStack != null)
{
stacks.add(readStack);
}
}
}
return stacks;
}
/** Creates a new waste stack from the listed fluidStacks */
public static FluidStack createNewWasteStack(FluidStack... liquids)
{
FluidStack stack = new FluidStack(FluidRegistry.getFluid("waste"), 0);
stack.tag = new NBTTagCompound();
if (liquids != null)
{
int count = 0;
for (int i = 0; i < liquids.length; i++)
{
if (liquids[i] != null)
{
if (!liquids[i].getFluid().equals(stack.getFluid()))
{
count++;
stack.tag.setCompoundTag("Liquids" + count, liquids[i].writeToNBT(new NBTTagCompound()));
stack.amount += liquids[i].amount;
}
else
{
for (FluidStack loadStack : getStacksFromWaste(liquids[i]))
{
count++;
stack.tag.setCompoundTag("Liquids" + count, loadStack.writeToNBT(new NBTTagCompound()));
stack.amount += loadStack.amount;
}
}
}
}
stack.tag.setInteger("liquids", count);
}
return stack;
}
/** Checks if the liquid can be merged without damage */
public static Object canMergeFluids(FluidStack stackOne, FluidStack stackTwo)
{
if (stackOne != null && stackTwo != null && !stackOne.equals(stackTwo))
{
if (fluidMergeResults.containsKey(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid())))
{
//TODO add volume calculation too see if merge can happen resulting in one liquid just vanishing
//Case 100mb of fuel 10000mb of lava will result in fuel being consumed with no major results
return fluidMergeResults.get(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid()));
}
}
return null;
}
}

View file

@ -1,33 +0,0 @@
package dark.core.network.fluid;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
import universalelectricity.core.vector.VectorHelper;
import dark.api.parts.INetworkPart;
import dark.core.prefab.tilenetwork.NetworkTileEntities;
public class HydraulicNetworkHelper
{
/** Invalidates a TileEntity */
public static void invalidate(TileEntity tileEntity)
{
for (int i = 0; i < 6; i++)
{
ForgeDirection direction = ForgeDirection.getOrientation(i);
TileEntity checkTile = VectorHelper.getConnectorFromSide(tileEntity.worldObj, new Vector3(tileEntity), direction);
if (checkTile instanceof INetworkPart)
{
NetworkTileEntities network = ((INetworkPart) checkTile).getTileNetwork();
if (network != null && network instanceof NetworkFluidTiles)
{
network.removeTile(tileEntity);
}
}
}
}
}

View file

@ -35,17 +35,12 @@ public class NetworkFluidTiles extends NetworkTileEntities
public final List<IFluidHandler> connectedTanks = new ArrayList<IFluidHandler>();
/** Collective storage of all fluid tiles */
public FluidTank sharedTank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME);
/** Map of results of two different liquids merging */
public static HashMap<Pair<Fluid, Fluid>, Object> mergeResult = new HashMap<Pair<Fluid, Fluid>, Object>();
/** Color code of the network, mainly used for connection rules */
public ColorCode color = ColorCode.NONE;
/** Has the collective tank been loaded yet */
protected boolean loadedLiquids = false;
static
{
mergeResult.put(new Pair<Fluid, Fluid>(FluidRegistry.WATER, FluidRegistry.LAVA), Block.obsidian);
mergeResult.put(new Pair<Fluid, Fluid>(FluidRegistry.LAVA, FluidRegistry.WATER), Block.cobblestone);
}
public NetworkFluidTiles(ColorCode color, INetworkPart... parts)
{
@ -211,135 +206,7 @@ public class NetworkFluidTiles extends NetworkTileEntities
return this.connectedTanks.contains(tileEntity);
}
public boolean isPartOfNetwork(TileEntity ent)
{
return super.isPartOfNetwork(ent) || this.connectedTanks.contains(ent);
}
/** Merges two fluids together that don't result in damage to the network */
public static FluidStack mergeFluids(FluidStack stackOne, FluidStack stackTwo)
{
FluidStack stack = null;
if (stackTwo != null && stackOne != null && stackOne.isFluidEqual(stackTwo))
{
stack = stackOne.copy();
stack.amount += stackTwo.amount;
}
else if (stackOne == null && stackTwo != null)
{
stack = stackTwo.copy();
}
else if (stackOne != null && stackTwo == null)
{
stack = 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 (mergeResult.containsKey(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid())))
{
Object result = mergeResult.get(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid()));
if (result instanceof Fluid)
{
stack = new FluidStack(((Fluid) result).getID(), stackOne.amount + stackTwo.amount);
}
else if (result instanceof FluidStack)
{
stack = ((FluidStack) result).copy();
stack.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);
}
}
if (stack != null)
{
/* If both liquids are waste then copy fluidStack lists then merge */
if (stackTwo.fluidID == waste.getID() && stackOne.fluidID == waste.getID())
{
List<FluidStack> stacks = new ArrayList<FluidStack>();
stacks.addAll(getStacksFromWaste(stackOne.copy()));
stacks.addAll(getStacksFromWaste(stackTwo.copy()));
stack = createNewWasteStack(stacks.toArray(new FluidStack[stacks.size()]));
}
else
{
stack = createNewWasteStack(stackOne.copy(), stackTwo.copy());
}
}
}
return stack;
}
/** Gets the fluidStacks that make up a waste FluidStack */
public static List<FluidStack> getStacksFromWaste(FluidStack wasteStack)
{
List<FluidStack> stacks = new ArrayList<FluidStack>();
if (wasteStack.fluidID == FluidRegistry.getFluidID("waste"))
{
for (int i = 1; i <= wasteStack.tag.getInteger("liquids"); i++)
{
FluidStack readStack = FluidStack.loadFluidStackFromNBT(wasteStack.tag.getCompoundTag("Liquid" + i));
if (readStack != null)
{
stacks.add(readStack);
}
}
}
return stacks;
}
/** Creates a new waste stack from the listed fluidStacks */
public static FluidStack createNewWasteStack(FluidStack... liquids)
{
FluidStack stack = new FluidStack(FluidRegistry.getFluid("waste"), 0);
stack.tag = new NBTTagCompound();
if (liquids != null)
{
int count = 0;
for (int i = 0; i < liquids.length; i++)
{
if (liquids[i] != null)
{
if (!liquids[i].getFluid().equals(stack.getFluid()))
{
count++;
stack.tag.setCompoundTag("Liquids" + count, liquids[i].writeToNBT(new NBTTagCompound()));
stack.amount += liquids[i].amount;
}
else
{
for (FluidStack loadStack : getStacksFromWaste(liquids[i]))
{
count++;
stack.tag.setCompoundTag("Liquids" + count, loadStack.writeToNBT(new NBTTagCompound()));
stack.amount += loadStack.amount;
}
}
}
}
stack.tag.setInteger("liquids", count);
}
return stack;
}
/** Checks if the liquid can be merged without damage */
public Object canMergeFluids(FluidStack stackOne, FluidStack stackTwo)
{
if (stackOne != null && stackTwo != null && !stackOne.equals(stackTwo))
{
if (this.mergeResult.containsKey(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid())))
{
//TODO add volume calculation too see if merge can happen resulting in one liquid just vanishing
//Case 100mb of fuel 10000mb of lava will result in fuel being comsumed with no explosion
return this.mergeResult.get(new Pair<Fluid, Fluid>(stackOne.getFluid(), stackTwo.getFluid()));
}
}
return null;
}
@Override
public void init()
@ -357,7 +224,7 @@ public class NetworkFluidTiles extends NetworkTileEntities
this.readDataFromTiles();
network.readDataFromTiles();
Object result = this.canMergeFluids(this.combinedStorage().getFluid(), network.combinedStorage().getFluid());
Object result = FluidNetworkHelper.canMergeFluids(this.combinedStorage().getFluid(), network.combinedStorage().getFluid());
if (mergePoint instanceof TileEntity)
{
World world = ((TileEntity) mergePoint).worldObj;
@ -442,7 +309,7 @@ public class NetworkFluidTiles extends NetworkTileEntities
newNetwork.getNetworkMemebers().addAll(network.getNetworkMemebers());
newNetwork.cleanUpMembers();
newNetwork.combinedStorage().setFluid(mergeFluids(one, two));
newNetwork.combinedStorage().setFluid(FluidNetworkHelper.mergeFluids(one, two));
newNetwork.writeDataToTiles();
}

View file

@ -7,7 +7,7 @@ import net.minecraftforge.common.ForgeDirection;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import dark.api.IToolReadOut;
import dark.api.parts.ITileConnector;
import dark.core.network.fluid.HydraulicNetworkHelper;
import dark.core.network.fluid.FluidNetworkHelper;
public abstract class TileEntityFluidDevice extends TileEntityAdvanced implements IToolReadOut, ITileConnector
{
@ -17,7 +17,7 @@ public abstract class TileEntityFluidDevice extends TileEntityAdvanced implement
public void invalidate()
{
super.invalidate();
HydraulicNetworkHelper.invalidate(this);
FluidNetworkHelper.invalidate(this);
}
@Override

View file

@ -18,7 +18,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.core.network.fluid.HydraulicNetworkHelper;
import dark.core.network.fluid.FluidNetworkHelper;
import dark.core.prefab.helpers.Pair;
import dark.fluid.client.render.BlockRenderHelper;
import dark.fluid.common.BlockFM;
@ -126,7 +126,7 @@ public class BlockConstructionPump extends BlockFM
TileEntity entity = world.getBlockTileEntity(x, y, z);
if (entity instanceof TileEntityConstructionPump)
{
HydraulicNetworkHelper.invalidate(entity);
FluidNetworkHelper.invalidate(entity);
}
if (meta == 3)

View file

@ -18,7 +18,7 @@ import universalelectricity.core.vector.VectorHelper;
import dark.api.fluid.IDrain;
import dark.api.fluid.INetworkPipe;
import dark.api.parts.ITileConnector;
import dark.core.network.fluid.HydraulicNetworkHelper;
import dark.core.network.fluid.FluidNetworkHelper;
import dark.core.network.fluid.NetworkFluidTiles;
import dark.core.prefab.helpers.MetaGroup;
import dark.core.prefab.helpers.Pair;
@ -173,7 +173,7 @@ public class TileEntityConstructionPump extends TileEntityStarterPump implements
public void invalidate()
{
super.invalidate();
HydraulicNetworkHelper.invalidate(this);
FluidNetworkHelper.invalidate(this);
}
@Override