diff --git a/src/com/jaquadro/minecraft/storagedrawers/config/OreDictRegistry.java b/src/com/jaquadro/minecraft/storagedrawers/config/OreDictRegistry.java index 6559b37..1011623 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/config/OreDictRegistry.java +++ b/src/com/jaquadro/minecraft/storagedrawers/config/OreDictRegistry.java @@ -10,6 +10,9 @@ public class OreDictRegistry private Set blacklist = new HashSet(); private List blacklistPrefix = new ArrayList(); + private Set blacklistCache = new HashSet(); + private Set graylistCache = new HashSet(); + public OreDictRegistry () { register("logWood"); register("plankWood"); @@ -32,6 +35,9 @@ public class OreDictRegistry if (entry == null) return false; + blacklistCache.add(entry); + graylistCache.remove(entry); + return blacklist.add(entry); } @@ -42,10 +48,14 @@ public class OreDictRegistry if (blacklistPrefix.contains(entry)) return false; + graylistCache.clear(); + return blacklistPrefix.add(entry); } public boolean unregister (String entry) { + blacklistCache.remove(entry); + return blacklist.remove(entry); } @@ -54,14 +64,20 @@ public class OreDictRegistry } public boolean isEntryBlacklisted (String entry) { - if (blacklist.contains(entry)) + if (blacklistCache.contains(entry)) return true; + if (graylistCache.contains(entry)) + return false; for (int i = 0, n = blacklistPrefix.size(); i < n; i++) { - if (entry.startsWith(blacklistPrefix.get(i))) + if (entry.startsWith(blacklistPrefix.get(i))) { + blacklistCache.add(entry); return true; + } } + graylistCache.add(entry); + return false; } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java b/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java index d354703..a7e8dd4 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java +++ b/src/com/jaquadro/minecraft/storagedrawers/storage/BaseDrawerData.java @@ -108,6 +108,33 @@ public abstract class BaseDrawerData implements IDrawer, IInventoryAdapter auxData.put(key, data); } + // Check if either set1 is a subset of set2, or set2 is a subset of set1, and return the smaller of the two sets. + // If neither is a subset, return null. + private static int[] isSubsetEitherWay (int[] set1, int[] set2) { + int[] smaller = set1; + int[] larger = set2; + + if (set2.length < smaller.length) { + smaller = set2; + larger = set1; + } + + for (int val1 : smaller) { + boolean found = false; + for (int val2 : larger) { + if (val1 == val2) { + found = true; + break; + } + } + + if (!found) + return null; + } + + return smaller; + } + public static boolean areItemsEqual (ItemStack stack1, ItemStack stack2) { if (stack1 == null || stack2 == null) return false; @@ -115,50 +142,44 @@ public abstract class BaseDrawerData implements IDrawer, IInventoryAdapter return false; if (!stack1.isItemEqual(stack2)) { + if (stack1.getItemDamage() == OreDictionary.WILDCARD_VALUE || stack2.getItemDamage() == OreDictionary.WILDCARD_VALUE) + return false; + int[] ids1 = OreDictionary.getOreIDs(stack1); int[] ids2 = OreDictionary.getOreIDs(stack2); if (ids1.length == 0 || ids2.length == 0) return false; - boolean oreMatch = false; + int[] commonIds = isSubsetEitherWay(ids1, ids2); + if (commonIds == null) + return false; - BRK_ORE_MATCH: - for (int oreIndexLeft : ids1) { - if (StorageDrawers.oreDictRegistry.isEntryBlacklisted(OreDictionary.getOreName(oreIndexLeft))) + boolean oreMatch = false; + for (int oreIndex : commonIds) { + if (StorageDrawers.oreDictRegistry.isEntryBlacklisted(OreDictionary.getOreName(oreIndex))) continue; - for (int oreIndexRight : ids2) { - if (StorageDrawers.oreDictRegistry.isEntryBlacklisted(OreDictionary.getOreName(oreIndexRight))) - continue; - - if (oreIndexLeft == oreIndexRight) { - List oreList = OreDictionary.getOres(OreDictionary.getOreName(oreIndexLeft)); - for (int i = 0, n = oreList.size(); i < n; i++) { - if (stack1.isItemEqual(oreList.get(i))) - oreMatch = true; - } - if (!oreMatch) - continue; - - oreMatch = false; - for (int i = 0, n = oreList.size(); i < n; i++) { - if (stack2.isItemEqual(oreList.get(i))) - oreMatch = true; - } - if (!oreMatch) - continue; - - oreMatch = false; - for (int i = 0, n = oreList.size(); i < n; i++) { - if (oreList.get(i).getItemDamage() != OreDictionary.WILDCARD_VALUE) - oreMatch = true; - } - if (!oreMatch) - continue; - - break BRK_ORE_MATCH; + List oreList = OreDictionary.getOres(OreDictionary.getOreName(oreIndex)); + boolean match1 = false; + for (int i = 0, n = oreList.size(); i < n; i++) { + if (stack1.isItemEqual(oreList.get(i))) { + match1 = true; + break; } } + + boolean match2 = false; + for (int i = 0, n = oreList.size(); i < n; i++) { + if (stack2.isItemEqual(oreList.get(i))) { + match2 = true; + break; + } + } + + if (match1 && match2) { + oreMatch = true; + break; + } } if (!oreMatch)