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.BiMap;
011import com.google.common.collect.HashBiMap;
012import com.google.common.collect.ImmutableMap;
013
014/**
015 * When creating liquids you should register them with this class.
016 *
017 * @author CovertJaguar <railcraft.wikispaces.com>
018 */
019public abstract class LiquidDictionary
020{
021
022    private static BiMap<String, LiquidStack> liquids = HashBiMap.create();
023
024    /**
025     * When creating liquids you should call this function.
026     *
027     * Upon passing it a name and liquid item it will return either
028     * a preexisting implementation of that liquid or the liquid passed in.
029     *
030     *
031     * @param name the name of the liquid
032     * @param liquid the liquid to use if one doesn't exist
033     * @return the matching liquid stack
034     */
035    public static LiquidStack getOrCreateLiquid(String name, LiquidStack liquid)
036    {
037        if (liquid == null)
038        {
039            throw new NullPointerException("You cannot register a null LiquidStack");
040        }
041        LiquidStack existing = liquids.get(name);
042        if(existing != null) {
043            return existing.copy();
044        }
045        liquids.put(name, liquid.copy());
046
047        MinecraftForge.EVENT_BUS.post(new LiquidRegisterEvent(name, liquid));
048        return liquid;
049    }
050
051    /**
052     * Returns the liquid matching the name,
053     * if such a liquid exists.
054     *
055     * Can return null.
056     *
057     * @param name the name of the liquid
058     * @param amount the amout of liquid
059     * @return a liquidstack for the requested liquid
060     */
061    public static LiquidStack getLiquid(String name, int amount)
062    {
063        LiquidStack liquid = liquids.get(name);
064        if(liquid == null)
065            return null;
066
067        liquid = liquid.copy();
068        liquid.amount = amount;
069        return liquid;
070    }
071
072    public static LiquidStack getCanonicalLiquid(String name)
073    {
074        return liquids.get(name);
075    }
076    /**
077     * Get an immutable list of the liquids defined
078     *
079     * @return the defined liquids
080     */
081    public static Map<String, LiquidStack> getLiquids()
082    {
083        return ImmutableMap.copyOf(liquids);
084    }
085    /**
086     * Fired when a new liquid is created
087     *
088     */
089    public static class LiquidRegisterEvent extends Event
090    {
091        public final String Name;
092        public final LiquidStack Liquid;
093
094        public LiquidRegisterEvent(String name, LiquidStack liquid)
095        {
096            this.Name = name;
097            this.Liquid = liquid.copy();
098        }
099    }
100
101    static
102    {
103        getOrCreateLiquid("Water", new LiquidStack(Block.waterStill, LiquidContainerRegistry.BUCKET_VOLUME));
104        getOrCreateLiquid("Lava", new LiquidStack(Block.lavaStill, LiquidContainerRegistry.BUCKET_VOLUME));
105    }
106
107    public static String findLiquidName(LiquidStack reference)
108    {
109        return liquids.inverse().get(reference);
110    }
111}