    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        static {
    023            initVanillaEntries();
    024        }
    025
    026        public static void initVanillaEntries(){
    027            registerOre("woodLog", new ItemStack(Block.wood, 1, 0));
    028            registerOre("woodLog", new ItemStack(Block.wood, 1, 1));
    029            registerOre("woodLog", new ItemStack(Block.wood, 1, 2));
    030            registerOre("woodLog", new ItemStack(Block.wood, 1, 3));
    031
    032            registerOre("woodPlank", new ItemStack(Block.planks, 1, 0));
    033            registerOre("woodPlank", new ItemStack(Block.planks, 1, 1));
    034            registerOre("woodPlank", new ItemStack(Block.planks, 1, 2));
    035            registerOre("woodPlank", new ItemStack(Block.planks, 1, 3));
    036
    037            registerOre("woodSlab", new ItemStack(Block.woodSingleSlab, 1, 0));
    038            registerOre("woodSlab", new ItemStack(Block.woodSingleSlab, 1, 1));
    039            registerOre("woodSlab", new ItemStack(Block.woodSingleSlab, 1, 2));
    040            registerOre("woodSlab", new ItemStack(Block.woodSingleSlab, 1, 3));
    041
    042            registerOre("woodStair", Block.stairCompactPlanks);
    043            registerOre("woodStair", Block.stairsWoodBirch);
    044            registerOre("woodStair", Block.stairsWoodJungle);
    045            registerOre("woodStair", Block.stairsWoodSpruce);
    046
    047            registerOre("dyeBlack", new ItemStack(Item.dyePowder, 1, 0));
    048            registerOre("dyeRed", new ItemStack(Item.dyePowder, 1, 1));
    049            registerOre("dyeGreen", new ItemStack(Item.dyePowder, 1, 2));
    050            registerOre("dyeBrown", new ItemStack(Item.dyePowder, 1, 3));
    051            registerOre("dyeBlue", new ItemStack(Item.dyePowder, 1, 4));
    052            registerOre("dyePurple", new ItemStack(Item.dyePowder, 1, 5));
    053            registerOre("dyeCyan", new ItemStack(Item.dyePowder, 1, 6));
    054            registerOre("dyeLightGrey", new ItemStack(Item.dyePowder, 1, 7));
    055            registerOre("dyeGrey", new ItemStack(Item.dyePowder, 1, 8));
    056            registerOre("dyePink", new ItemStack(Item.dyePowder, 1, 9));
    057            registerOre("dyeLime", new ItemStack(Item.dyePowder, 1, 10));
    058            registerOre("dyeYellow", new ItemStack(Item.dyePowder, 1, 11));
    059            registerOre("dyeLightBlue", new ItemStack(Item.dyePowder, 1, 12));
    060            registerOre("dyeMagenta", new ItemStack(Item.dyePowder, 1, 13));
    061            registerOre("dyeOrange", new ItemStack(Item.dyePowder, 1, 14));
    062            registerOre("dyeWhite", new ItemStack(Item.dyePowder, 1, 15));
    063        }
    064
    065        /**
    066         * Gets the integer ID for the specified ore name.
    067         * If the name does not have a ID it assigns it a new one.
    068         *
    069         * @param name The unique name for this ore 'oreIron', 'ingotIron', etc..
    070         * @return A number representing the ID for this ore type
    071         */
    072        public static int getOreID(String name)
    073        {
    074            Integer val = oreIDs.get(name);
    075            if (val == null)
    076            {
    077                val = maxID++;
    078                oreIDs.put(name, val);
    079                oreStacks.put(val, new ArrayList<ItemStack>());
    080            }
    081            return val;
    082        }
    083
    084        /**
    085         * Reverse of getOreID, will not create new entries.
    086         *
    087         * @param id The ID to translate to a string
    088         * @return The String name, or "Unknown" if not found.
    089         */
    090        public static String getOreName(int id)
    091        {
    092            for (Map.Entry<String, Integer> entry : oreIDs.entrySet())
    093            {
    094                if (id == entry.getValue())
    095                {
    096                    return entry.getKey();
    097                }
    098            }
    099            return "Unknown";
    100        }
    101
    102        /**
    103         * Retrieves the ArrayList of items that are registered to this ore type.
    104         * Creates the list as empty if it did not exist.
    105         *
    106         * @param name The ore name, directly calls getOreID
    107         * @return An arrayList containing ItemStacks registered for this ore
    108         */
    109        public static ArrayList<ItemStack> getOres(String name)
    110        {
    111            return getOres(getOreID(name));
    112        }
    113
    114        /**
    115         * Retrieves a list of all unique ore names that are already registered.
    116         *
    117         * @return All unique ore names that are currently registered.
    118         */
    119        public static String[] getOreNames()
    120        {
    121            return oreIDs.keySet().toArray(new String[0]);
    122        }
    123
    124        /**
    125         * Retrieves the ArrayList of items that are registered to this ore type.
    126         * Creates the list as empty if it did not exist.
    127         *
    128         * @param id The ore ID, see getOreID
    129         * @return An arrayList containing ItemStacks registered for this ore
    130         */
    131        public static ArrayList<ItemStack> getOres(Integer id)
    132        {
    133            ArrayList<ItemStack> val = oreStacks.get(id);
    134            if (val == null)
    135            {
    136                val = new ArrayList<ItemStack>();
    137                oreStacks.put(id, val);
    138            }
    139            return val;
    140        }
    141
    142        //Convenience functions that make for cleaner code mod side. They all drill down to registerOre(String, int, ItemStack)
    143        public static void registerOre(String name, Item      ore){ registerOre(name, new ItemStack(ore));  }
    144        public static void registerOre(String name, Block     ore){ registerOre(name, new ItemStack(ore));  }
    145        public static void registerOre(String name, ItemStack ore){ registerOre(name, getOreID(name), ore); }
    146        public static void registerOre(int    id,   Item      ore){ registerOre(id,   new ItemStack(ore));  }
    147        public static void registerOre(int    id,   Block     ore){ registerOre(id,   new ItemStack(ore));  }
    148        public static void registerOre(int    id,   ItemStack ore){ registerOre(getOreName(id), id, ore);   }
    149
    150        /**
    151         * Registers a ore item into the dictionary.
    152         * Raises the registerOre function in all registered handlers.
    153         *
    154         * @param name The name of the ore
    155         * @param id The ID of the ore
    156         * @param ore The ore's ItemStack
    157         */
    158        private static void registerOre(String name, int id, ItemStack ore)
    159        {
    160            ArrayList<ItemStack> ores = getOres(id);
    161            ore = ore.copy();
    162            ores.add(ore);
    163            MinecraftForge.EVENT_BUS.post(new OreRegisterEvent(name, ore));
    164        }
    165
    166        public static class OreRegisterEvent extends Event
    167        {
    168            public final String Name;
    169            public final ItemStack Ore;
    170
    171            public OreRegisterEvent(String name, ItemStack ore)
    172            {
    173                this.Name = name;
    174                this.Ore = ore;
    175            }
    176        }
    177    }