001    package net.minecraftforge.liquids;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    
006    import net.minecraft.src.ItemStack;
007    import net.minecraftforge.common.MinecraftForge;
008    import net.minecraftforge.event.Event;
009    import net.minecraftforge.oredict.OreDictionary.OreRegisterEvent;
010    
011    import 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     */
018    public 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         * @see LiquidDictionary#getLiquids()
071         */
072        @Deprecated
073        public Map<String, LiquidStack> getDefinedLiquids()
074        {
075            return getLiquids();
076        }
077    
078    
079        /**
080         * Get an immutable list of the liquids defined
081         *
082         * @return the defined liquids
083         */
084        public static Map<String, LiquidStack> getLiquids()
085        {
086            return ImmutableMap.copyOf(liquids);
087        }
088        /**
089         * Fired when a new liquid is created
090         *
091         * @author cpw
092         *
093         */
094        public static class LiquidRegisterEvent extends Event
095        {
096            public final String Name;
097            public final LiquidStack Liquid;
098    
099            public LiquidRegisterEvent(String name, LiquidStack liquid)
100            {
101                this.Name = name;
102                this.Liquid = liquid.copy();
103            }
104        }
105    }