From cd3bbfb02c9fcd4ce4bbf00f460dfdd6a386d107 Mon Sep 17 00:00:00 2001 From: King Lemming Date: Thu, 16 Apr 2015 17:18:37 -0400 Subject: [PATCH] Adds some new Ore querying functionality. Also attempts to size initial Hashmaps in a logical manner. Signed-off-by: King Lemming --- .../minecraftforge/oredict/OreDictionary.java | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/minecraftforge/oredict/OreDictionary.java b/src/main/java/net/minecraftforge/oredict/OreDictionary.java index fe1b46b06..5b5006092 100644 --- a/src/main/java/net/minecraftforge/oredict/OreDictionary.java +++ b/src/main/java/net/minecraftforge/oredict/OreDictionary.java @@ -34,10 +34,10 @@ public class OreDictionary { private static boolean hasInit = false; private static List idToName = new ArrayList(); - private static Map nameToId = new HashMap(); + private static Map nameToId = new HashMap(128); private static List> idToStack = Lists.newArrayList(); //ToDo: Unqualify to List when possible {1.8} private static List> idToStackUn = Lists.newArrayList(); //ToDo: Unqualify to List when possible {1.8} - private static Map> stackToId = Maps.newHashMap(); + private static Map> stackToId = Maps.newHashMapWithExpectedSize(96); // Calculated from 128 * 0.75 public static final ArrayList EMPTY_LIST = new UnmodifiableArrayList(Lists.newArrayList()); //ToDo: Unqualify to List when possible {1.8} /** @@ -347,6 +347,44 @@ public class OreDictionary return getOres(getOreID(name)); } + /** + * Retrieves the List of items that are registered to this ore type at this instant. + * If the flag is TRUE, then it will create the list as empty if it did not exist. + * + * This option should be used by modders who are doing blanket scans in postInit. + * It greatly reduces clutter in the OreDictionary is the responsible and proper + * way to use the dictionary in a large number of cases. + * + * The other function above is utilized in OreRecipe and is required for the + * operation of that code. + * + * @param name The ore name, directly calls getOreID if the flag is TRUE + * @param alwaysCreateEntry Flag - should a new entry be created if empty + * @return An arraylist containing ItemStacks registered for this ore + */ + public static List getOres(String name, boolean alwaysCreateEntry) + { + if (alwaysCreateEntry) { + return getOres(getOreID(name)); + } + return nameToId.get(name) != null ? getOres(getOreID(name)) : EMPTY_LIST; + } + + /** + * Returns whether or not an oreName exists in the dictionary. + * This function can be used to safely query the Ore Dictionary without + * adding needless clutter to the underlying map structure. + * + * Please use this when possible and appropriate. + * + * @param name The ore name + * @return Whether or not that name is in the Ore Dictionary. + */ + public static boolean doesOreNameExist(String name) + { + return nameToId.get(name) != null; + } + /** * Retrieves a list of all unique ore names that are already registered. *