    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        LiquidStack existing = liquids.get(name);
    038        if(existing != null) {
    039            return existing.copy();
    040        }
    041        liquids.put(name, liquid.copy());
    042
    043        MinecraftForge.EVENT_BUS.post(new LiquidRegisterEvent(name, liquid));
    044        return liquid;
    045    }
    046
    047    /**
    048     * Returns the liquid matching the name,
    049     * if such a liquid exists.
    050     *
    051     * Can return null.
    052     *
    053     * @param name the name of the liquid
    054     * @param amount the amout of liquid
    055     * @return a liquidstack for the requested liquid
    056     */
    057    public static LiquidStack getLiquid(String name, int amount)
    058    {
    059        LiquidStack liquid = liquids.get(name);
    060        if(liquid == null)
    061            return null;
    062
    063        liquid = liquid.copy();
    064        liquid.amount = amount;
    065        return liquid;
    066    }
    067
    068    public static LiquidStack getCanonicalLiquid(String name)
    069    {
    070        return liquids.get(name);
    071    }
    072    /**
    073     * Get an immutable list of the liquids defined
    074     *
    075     * @return the defined liquids
    076     */
    077    public static Map<String, LiquidStack> getLiquids()
    078    {
    079        return ImmutableMap.copyOf(liquids);
    080    }
    081    /**
    082     * Fired when a new liquid is created
    083     *
    084     */
    085    public static class LiquidRegisterEvent extends Event
    086    {
    087        public final String Name;
    088        public final LiquidStack Liquid;
    089
    090        public LiquidRegisterEvent(String name, LiquidStack liquid)
    091        {
    092            this.Name = name;
    093            this.Liquid = liquid.copy();
    094        }
    095    }
    096
    097    static
    098    {
    099        getOrCreateLiquid("Water", new LiquidStack(Block.waterStill, LiquidContainerRegistry.BUCKET_VOLUME));
    100        getOrCreateLiquid("Lava", new LiquidStack(Block.lavaStill, LiquidContainerRegistry.BUCKET_VOLUME));
    101    }
    102
    103    public static String findLiquidName(LiquidStack reference)
    104    {
    105        return liquids.inverse().get(reference);
    106    }
    107}