    001    package net.minecraftforge.oredict;
    002
    003    import java.util.ArrayList;
    004    import java.util.HashMap;
    005    import java.util.Iterator;
    006    import java.util.List;
    007    import java.util.Map;
    008    import java.util.Map.Entry;
    009
    010    import net.minecraft.src.Block;
    011    import net.minecraft.src.Item;
    012    import net.minecraft.src.ItemStack;
    013    import net.minecraftforge.common.MinecraftForge;
    014    import net.minecraftforge.event.Event;
    015
    016    public class OreDictionary
    017    {
    018        private static int maxID = 0;
    019        private static HashMap<String, Integer> oreIDs = new HashMap<String, Integer>();
    020        private static HashMap<Integer, ArrayList<ItemStack>> oreStacks = new HashMap<Integer, ArrayList<ItemStack>>();
    021
    022        /**
    023         * Gets the integer ID for the specified ore name.
    024         * If the name does not have a ID it assigns it a new one.
    025         *
    026         * @param name The unique name for this ore 'oreIron', 'ingotIron', etc..
    027         * @return A number representing the ID for this ore type
    028         */
    029        public static int getOreID(String name)
    030        {
    031            Integer val = oreIDs.get(name);
    032            if (val == null)
    033            {
    034                val = maxID++;
    035                oreIDs.put(name, val);
    036                oreStacks.put(val, new ArrayList<ItemStack>());
    037            }
    038            return val;
    039        }
    040
    041        /**
    042         * Reverse of getOreID, will not create new entries.
    043         *
    044         * @param id The ID to translate to a string
    045         * @return The String name, or "Unknown" if not found.
    046         */
    047        public static String getOreName(int id)
    048        {
    049            for (Map.Entry<String, Integer> entry : oreIDs.entrySet())
    050            {
    051                if (id == entry.getValue())
    052                {
    053                    return entry.getKey();
    054                }
    055            }
    056            return "Unknown";
    057        }
    058
    059        /**
    060         * Retrieves the ArrayList of items that are registered to this ore type.
    061         * Creates the list as empty if it did not exist.
    062         *
    063         * @param name The ore name, directly calls getOreID
    064         * @return An arrayList containing ItemStacks registered for this ore
    065         */
    066        public static ArrayList<ItemStack> getOres(String name)
    067        {
    068            return getOres(getOreID(name));
    069        }
    070
    071        /**
    072         * Retrieves a list of all unique ore names that are already registered.
    073         *
    074         * @return All unique ore names that are currently registered.
    075         */
    076        public static String[] getOreNames()
    077        {
    078            return oreIDs.keySet().toArray(new String[0]);
    079        }
    080
    081        /**
    082         * Retrieves the ArrayList of items that are registered to this ore type.
    083         * Creates the list as empty if it did not exist.
    084         *
    085         * @param id The ore ID, see getOreID
    086         * @return An arrayList containing ItemStacks registered for this ore
    087         */
    088        public static ArrayList<ItemStack> getOres(Integer id)
    089        {
    090            ArrayList<ItemStack> val = oreStacks.get(id);
    091            if (val == null)
    092            {
    093                val = new ArrayList<ItemStack>();
    094                oreStacks.put(id, val);
    095            }
    096            return val;
    097        }
    098
    099        //Convenience functions that make for cleaner code mod side. They all drill down to registerOre(String, int, ItemStack)
    100        public static void registerOre(String name, Item      ore){ registerOre(name, new ItemStack(ore));  }
    101        public static void registerOre(String name, Block     ore){ registerOre(name, new ItemStack(ore));  }
    102        public static void registerOre(String name, ItemStack ore){ registerOre(name, getOreID(name), ore); }
    103        public static void registerOre(int    id,   Item      ore){ registerOre(id,   new ItemStack(ore));  }
    104        public static void registerOre(int    id,   Block     ore){ registerOre(id,   new ItemStack(ore));  }
    105        public static void registerOre(int    id,   ItemStack ore){ registerOre(getOreName(id), id, ore);   }
    106
    107        /**
    108         * Registers a ore item into the dictionary.
    109         * Raises the registerOre function in all registered handlers.
    110         *
    111         * @param name The name of the ore
    112         * @param id The ID of the ore
    113         * @param ore The ore's ItemStack
    114         */
    115        private static void registerOre(String name, int id, ItemStack ore)
    116        {
    117            ArrayList<ItemStack> ores = getOres(id);
    118            ore = ore.copy();
    119            ores.add(ore);
    120            MinecraftForge.EVENT_BUS.post(new OreRegisterEvent(name, ore));
    121        }
    122
    123        public static class OreRegisterEvent extends Event
    124        {
    125            public final String Name;
    126            public final ItemStack Ore;
    127
    128            public OreRegisterEvent(String name, ItemStack ore)
    129            {
    130                this.Name = name;
    131                this.Ore = ore;
    132            }
    133        }
    134    }