resonant-induction/minecraft/liquidmechanics/common/handlers/LiquidHandler.java
Rseifert a3c43609ea Got rid of Enum system
What i have is not much diffrent but its a start to allowing more liquid
types without having to add them myself. The current method has 3
defualt liquids that are preset. The new system also uses String names
to ID liquid instead of Enums. A new class Called LiquidData will keep
track of the data need to ID, and use the Liquids.

In the process i also fixed a few crafting recipes that were
removed/messed up in a patch a while back.

Plan for new system
*Have default liquid type that come with textures/renders
*Have several univeral pipes that can accept all Liquid types
*Have a way of placeing a universal pipe and then converting to a
regulated pipe, pipe that only take one liquid type
*Have a tool for doing the above
*Change the release Valve to be univeral with a GUI to restrict flow and
Liquid type extracted
2013-01-03 12:18:47 -05:00

150 lines
4.5 KiB
Java

package liquidmechanics.common.handlers;
import java.util.ArrayList;
import java.util.List;
import liquidmechanics.common.LiquidMechanics;
import liquidmechanics.common.tileentity.TileEntityPipe;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.ForgeSubscribe;
import net.minecraftforge.liquids.LiquidDictionary;
import net.minecraftforge.liquids.LiquidStack;
import net.minecraftforge.liquids.LiquidDictionary.LiquidRegisterEvent;
public class LiquidHandler
{
// Active list of all Liquid that can be used//
public static List<LiquidData> allowedLiquids = new ArrayList<LiquidData>();
// PreDefinned Liquids//
public static LiquidData steam;
public static LiquidData water;
public static LiquidData lava;
public static LiquidData air;
// public static LiquidData oil; TODO add
// public static LiquidData fuel;
/**
* Called to add the default liquids to the allowed list
*/
public static void addDefaultLiquids()
{
steam = new LiquidData("Steam", LiquidDictionary.getOrCreateLiquid("Steam", new LiquidStack(LiquidMechanics.blockSteamBlock, 1)), true, 100);
allowedLiquids.add(steam);
water = new LiquidData("water", LiquidDictionary.getOrCreateLiquid("Water", new LiquidStack(Block.waterStill, 1)), false, 32);
allowedLiquids.add(water);
lava = new LiquidData("Lava", LiquidDictionary.getOrCreateLiquid("Lava", new LiquidStack(Block.lavaStill, 1)), false, 20);
allowedLiquids.add(lava);
air = new LiquidData("Air", LiquidDictionary.getOrCreateLiquid("Air", new LiquidStack(0, 1)), false, 0);
allowedLiquids.add(air);
}
@ForgeSubscribe
public void liquidRegisterEvent(LiquidRegisterEvent event)
{
// TODO use this to add new liquid types to the data list
// or something along the lines of IDing liquids for use
}
/**
* Gets the LiquidData linked to the liquid by name
*
* @param name
* - String name, not case sensitive
*/
public static LiquidData get(String name)
{
for (LiquidData data : LiquidHandler.allowedLiquids)
{
if (LiquidData.getName(data).equalsIgnoreCase(name)) { return data; }
}
return air;
}
public static LiquidData get(LiquidStack stack)
{
for (LiquidData data : LiquidHandler.allowedLiquids)
{
if (isEqual(stack, data)) { return data; }
}
return air;
}
/**
* gets a liquid stack of type & volume
*/
public static LiquidStack getStack(LiquidData type, int vol)
{
if(type == null) return null;
return new LiquidStack(LiquidData.getStack(type).itemID, vol, LiquidData.getStack(type).itemMeta);
}
public static int getMeta(LiquidData type)
{
if (type == LiquidHandler.steam) return 0;
if (type == LiquidHandler.water) return 1;
if (type == LiquidHandler.lava) return 2;
return 20;
}
public static LiquidData getFromMeta(int meta)
{
switch (meta)
{
case 0:
return steam;
case 1:
return water;
case 2:
return lava;
}
return air;
}
public static LiquidData getFromBlockID(int id)
{
for (LiquidData data : allowedLiquids)
{
if (LiquidData.getStack(data).itemID == id) { return data; }
}
return air;
}
/**
* compare a stack with a liquid type to see if there the same
*
* @param stack
* @param type
* @return
*/
public static boolean isEqual(LiquidStack stack, LiquidData type)
{
if (stack == null || type == null)
return false;
if (LiquidData.getStack(type).itemID == stack.itemID && LiquidData.getStack(type).itemMeta == stack.itemMeta) { return true; }
return false;
}
public static boolean isEqual(LiquidStack stack, LiquidStack type)
{
if (stack == null || type == null)
return false;
if (type.itemID == stack.itemID && type.itemMeta == stack.itemMeta) { return true; }
return false;
}
public static ItemStack consumeItem(ItemStack stack)
{
if (stack.stackSize == 1)
{
if (stack.getItem().hasContainerItem()) return stack.getItem().getContainerItemStack(stack);
else return null;
}
else
{
stack.splitStack(1);
return stack;
}
}
}