2012-11-11 02:23:16 +00:00
|
|
|
package net.minecraftforge.liquids;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2013-02-23 21:53:05 +00:00
|
|
|
import net.minecraft.block.Block;
|
2012-11-13 19:27:31 +00:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.Event;
|
|
|
|
|
2013-03-12 20:07:52 +00:00
|
|
|
import com.google.common.collect.BiMap;
|
|
|
|
import com.google.common.collect.HashBiMap;
|
2012-11-13 19:27:31 +00:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
|
|
2012-11-11 02:23:16 +00:00
|
|
|
/**
|
|
|
|
* When creating liquids you should register them with this class.
|
|
|
|
*
|
|
|
|
* @author CovertJaguar <railcraft.wikispaces.com>
|
|
|
|
*/
|
|
|
|
public abstract class LiquidDictionary
|
|
|
|
{
|
|
|
|
|
2013-03-12 20:07:52 +00:00
|
|
|
private static BiMap<String, LiquidStack> liquids = HashBiMap.create();
|
2012-11-11 02:23:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When creating liquids you should call this function.
|
|
|
|
*
|
|
|
|
* Upon passing it a name and liquid item it will return either
|
|
|
|
* a preexisting implementation of that liquid or the liquid passed in.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param name the name of the liquid
|
|
|
|
* @param liquid the liquid to use if one doesn't exist
|
2013-01-22 02:56:04 +00:00
|
|
|
* @return the matching liquid stack
|
2012-11-11 02:23:16 +00:00
|
|
|
*/
|
|
|
|
public static LiquidStack getOrCreateLiquid(String name, LiquidStack liquid)
|
|
|
|
{
|
|
|
|
LiquidStack existing = liquids.get(name);
|
|
|
|
if(existing != null) {
|
|
|
|
return existing.copy();
|
|
|
|
}
|
|
|
|
liquids.put(name, liquid.copy());
|
2013-03-12 20:07:52 +00:00
|
|
|
|
2012-11-13 19:27:31 +00:00
|
|
|
MinecraftForge.EVENT_BUS.post(new LiquidRegisterEvent(name, liquid));
|
2012-11-11 02:23:16 +00:00
|
|
|
return liquid;
|
|
|
|
}
|
2012-11-13 19:27:31 +00:00
|
|
|
|
2012-11-11 02:23:16 +00:00
|
|
|
/**
|
|
|
|
* Returns the liquid matching the name,
|
|
|
|
* if such a liquid exists.
|
2012-11-13 19:27:31 +00:00
|
|
|
*
|
2012-11-11 02:23:16 +00:00
|
|
|
* Can return null.
|
2012-11-13 19:27:31 +00:00
|
|
|
*
|
2012-11-11 02:23:16 +00:00
|
|
|
* @param name the name of the liquid
|
|
|
|
* @param amount the amout of liquid
|
2013-01-22 02:56:04 +00:00
|
|
|
* @return a liquidstack for the requested liquid
|
2012-11-11 02:23:16 +00:00
|
|
|
*/
|
|
|
|
public static LiquidStack getLiquid(String name, int amount)
|
|
|
|
{
|
|
|
|
LiquidStack liquid = liquids.get(name);
|
2012-11-13 19:27:31 +00:00
|
|
|
if(liquid == null)
|
2012-11-11 02:23:16 +00:00
|
|
|
return null;
|
2012-11-13 19:27:31 +00:00
|
|
|
|
2012-11-11 02:23:16 +00:00
|
|
|
liquid = liquid.copy();
|
|
|
|
liquid.amount = amount;
|
|
|
|
return liquid;
|
|
|
|
}
|
2012-11-13 19:27:31 +00:00
|
|
|
|
2013-03-12 20:07:52 +00:00
|
|
|
public static LiquidStack getCanonicalLiquid(String name)
|
|
|
|
{
|
|
|
|
return liquids.get(name);
|
|
|
|
}
|
2012-12-06 21:14:15 +00:00
|
|
|
/**
|
|
|
|
* Get an immutable list of the liquids defined
|
|
|
|
*
|
|
|
|
* @return the defined liquids
|
|
|
|
*/
|
|
|
|
public static Map<String, LiquidStack> getLiquids()
|
|
|
|
{
|
|
|
|
return ImmutableMap.copyOf(liquids);
|
|
|
|
}
|
2012-11-13 19:27:31 +00:00
|
|
|
/**
|
|
|
|
* Fired when a new liquid is created
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static class LiquidRegisterEvent extends Event
|
|
|
|
{
|
|
|
|
public final String Name;
|
|
|
|
public final LiquidStack Liquid;
|
|
|
|
|
|
|
|
public LiquidRegisterEvent(String name, LiquidStack liquid)
|
|
|
|
{
|
|
|
|
this.Name = name;
|
|
|
|
this.Liquid = liquid.copy();
|
|
|
|
}
|
|
|
|
}
|
2013-03-12 20:07:52 +00:00
|
|
|
|
2013-02-23 21:53:05 +00:00
|
|
|
static
|
|
|
|
{
|
|
|
|
getOrCreateLiquid("Water", new LiquidStack(Block.waterStill, LiquidContainerRegistry.BUCKET_VOLUME));
|
|
|
|
getOrCreateLiquid("Lava", new LiquidStack(Block.lavaStill, LiquidContainerRegistry.BUCKET_VOLUME));
|
|
|
|
}
|
2013-03-12 20:07:52 +00:00
|
|
|
|
|
|
|
public static String findLiquidName(LiquidStack reference)
|
|
|
|
{
|
|
|
|
return liquids.inverse().get(reference);
|
|
|
|
}
|
2012-11-11 02:23:16 +00:00
|
|
|
}
|