    001
    002package net.minecraftforge.liquids;
    003
    004import java.util.ArrayList;
    005import java.util.Arrays;
    006import java.util.HashMap;
    007import java.util.HashSet;
    008import java.util.List;
    009import java.util.Map;
    010import java.util.Set;
    011
    012import net.minecraft.block.Block;
    013import net.minecraft.item.Item;
    014import net.minecraft.item.ItemStack;
    015
    016public class LiquidContainerRegistry
    017{
    018    public static final int BUCKET_VOLUME = 1000;
    019    public static final ItemStack EMPTY_BUCKET = new ItemStack(Item.bucketEmpty);
    020
    021    private static Map<List, LiquidContainerData> mapFilledItemFromLiquid = new HashMap();
    022    private static Map<List, LiquidContainerData> mapLiquidFromFilledItem = new HashMap();
    023    private static Set<List> setContainerValidation = new HashSet();
    024    private static Set<List> setLiquidValidation = new HashSet();
    025    private static ArrayList<LiquidContainerData> liquids = new ArrayList();
    026
    027    /**
    028     * Default registrations
    029     */
    030    static
    031    {
    032        registerLiquid(new LiquidContainerData(new LiquidStack(Block.waterStill, LiquidContainerRegistry.BUCKET_VOLUME), new ItemStack(Item.bucketWater), new ItemStack(Item.bucketEmpty)));
    033        registerLiquid(new LiquidContainerData(new LiquidStack(Block.lavaStill, LiquidContainerRegistry.BUCKET_VOLUME),  new ItemStack(Item.bucketLava), new ItemStack(Item.bucketEmpty)));
    034        registerLiquid(new LiquidContainerData(new LiquidStack(Block.waterStill, LiquidContainerRegistry.BUCKET_VOLUME), new ItemStack(Item.potion), new ItemStack(Item.glassBottle)));
    035        // registerLiquid(new LiquidContainerData(new LiquidStack(Item.bucketMilk, LiquidContainerRegistry.BUCKET_VOLUME), new ItemStack(Item.bucketMilk), new ItemStack(Item.bucketEmpty)));
    036    }
    037
    038    /**
    039     * To register a container with a non-bucket size, the LiquidContainerData entry simply needs to use a size other than LiquidManager.BUCKET_VOLUME
    040     */
    041    public static void registerLiquid(LiquidContainerData data)
    042    {
    043        mapFilledItemFromLiquid.put(Arrays.asList(data.container.itemID, data.container.getItemDamage(), data.stillLiquid.itemID, data.stillLiquid.itemMeta), data);
    044        mapLiquidFromFilledItem.put(Arrays.asList(data.filled.itemID, data.filled.getItemDamage()), data);
    045        setContainerValidation.add(Arrays.asList(data.container.itemID, data.container.getItemDamage()));
    046        setLiquidValidation.add(Arrays.asList(data.stillLiquid.itemID, data.stillLiquid.itemMeta));
    047
    048        liquids.add(data);
    049    }
    050
    051    public static LiquidStack getLiquidForFilledItem(ItemStack filledContainer)
    052    {
    053        if (filledContainer == null)
    054        {
    055            return null;
    056        }
    057
    058        LiquidContainerData ret = mapLiquidFromFilledItem.get(Arrays.asList(filledContainer.itemID, filledContainer.getItemDamage()));
    059        return ret == null ? null : ret.stillLiquid.copy();
    060    }
    061
    062    public static ItemStack fillLiquidContainer(LiquidStack liquid, ItemStack emptyContainer)
    063    {
    064        if (emptyContainer == null || liquid == null)
    065        {
    066            return null;
    067        }
    068
    069        LiquidContainerData ret = mapFilledItemFromLiquid.get(Arrays.asList(emptyContainer.itemID, emptyContainer.getItemDamage(), liquid.itemID, liquid.itemMeta));
    070
    071        if (ret != null && liquid.amount >= ret.stillLiquid.amount)
    072        {
    073            return ret.filled.copy();
    074        }
    075
    076        return null;
    077    }
    078
    079    public static boolean containsLiquid(ItemStack filledContainer, LiquidStack liquid)
    080    {
    081        if (filledContainer == null || liquid == null)
    082        {
    083            return false;
    084        }
    085
    086        LiquidContainerData ret = mapLiquidFromFilledItem.get(Arrays.asList(filledContainer.itemID, filledContainer.getItemDamage()));
    087
    088        return ret != null && ret.stillLiquid.isLiquidEqual(liquid);
    089    }
    090
    091    public static boolean isBucket(ItemStack container)
    092    {
    093        if (container == null)
    094        {
    095            return false;
    096        }
    097
    098        if (container.isItemEqual(EMPTY_BUCKET))
    099        {
    100            return true;
    101        }
    102
    103        LiquidContainerData ret = mapLiquidFromFilledItem.get(Arrays.asList(container.itemID, container.getItemDamage()));
    104        return ret != null && ret.container.isItemEqual(EMPTY_BUCKET);
    105    }
    106
    107    public static boolean isContainer(ItemStack container)
    108    {
    109        return isEmptyContainer(container) || isFilledContainer(container);
    110    }
    111
    112    public static boolean isEmptyContainer(ItemStack emptyContainer)
    113    {
    114        return emptyContainer != null && setContainerValidation.contains(Arrays.asList(emptyContainer.itemID, emptyContainer.getItemDamage()));
    115    }
    116
    117    public static boolean isFilledContainer(ItemStack filledContainer)
    118    {
    119        return filledContainer != null && getLiquidForFilledItem(filledContainer) != null;
    120    }
    121
    122    public static boolean isLiquid(ItemStack item)
    123    {
    124        return item != null && setLiquidValidation.contains(Arrays.asList(item.itemID, item.getItemDamage()));
    125    }
    126
    127    public static LiquidContainerData[] getRegisteredLiquidContainerData()
    128    {
    129        return liquids.toArray(new LiquidContainerData[liquids.size()]);
    130    }
    131}