001package net.minecraftforge.liquids;
002
003import java.util.HashMap;
004import java.util.Map;
005
006import net.minecraft.block.Block;
007import net.minecraftforge.common.MinecraftForge;
008import net.minecraftforge.event.Event;
009
010import com.google.common.collect.ImmutableMap;
011
012/**
013 * When creating liquids you should register them with this class.
014 *
015 * @author CovertJaguar <railcraft.wikispaces.com>
016 */
017public abstract class LiquidDictionary
018{
019
020    private static Map<String, LiquidStack> liquids = new HashMap<String, LiquidStack>();
021
022    /**
023     * When creating liquids you should call this function.
024     *
025     * Upon passing it a name and liquid item it will return either
026     * a preexisting implementation of that liquid or the liquid passed in.
027     *
028     *
029     * @param name the name of the liquid
030     * @param liquid the liquid to use if one doesn't exist
031     * @return the matching liquid stack
032     */
033    public static LiquidStack getOrCreateLiquid(String name, LiquidStack liquid)
034    {
035        LiquidStack existing = liquids.get(name);
036        if(existing != null) {
037            return existing.copy();
038        }
039        liquids.put(name, liquid.copy());
040        MinecraftForge.EVENT_BUS.post(new LiquidRegisterEvent(name, liquid));
041        return liquid;
042    }
043
044    /**
045     * Returns the liquid matching the name,
046     * if such a liquid exists.
047     *
048     * Can return null.
049     *
050     * @param name the name of the liquid
051     * @param amount the amout of liquid
052     * @return a liquidstack for the requested liquid
053     */
054    public static LiquidStack getLiquid(String name, int amount)
055    {
056        LiquidStack liquid = liquids.get(name);
057        if(liquid == null)
058            return null;
059
060        liquid = liquid.copy();
061        liquid.amount = amount;
062        return liquid;
063    }
064
065    /**
066     * Get an immutable list of the liquids defined
067     *
068     * @return the defined liquids
069     */
070    public static Map<String, LiquidStack> getLiquids()
071    {
072        return ImmutableMap.copyOf(liquids);
073    }
074    /**
075     * Fired when a new liquid is created
076     *
077     */
078    public static class LiquidRegisterEvent extends Event
079    {
080        public final String Name;
081        public final LiquidStack Liquid;
082
083        public LiquidRegisterEvent(String name, LiquidStack liquid)
084        {
085            this.Name = name;
086            this.Liquid = liquid.copy();
087        }
088    }
089    
090    static
091    {
092        getOrCreateLiquid("Water", new LiquidStack(Block.waterStill, LiquidContainerRegistry.BUCKET_VOLUME));
093        getOrCreateLiquid("Lava", new LiquidStack(Block.lavaStill, LiquidContainerRegistry.BUCKET_VOLUME));
094    }
095}