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