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