Massively more aggressive ore dictionary filtering heuristics; several performance improvements in ore dictionary-based item matches.

This commit is contained in:
Justin Aquadro committed 2015-04-16 00:21:06 -04:00
1 parent 5bef1a8dde
commit 3591261bcb
4 files changed
+91 -50

No files matched your search

@@ -107,6 +107,8 @@ storageDrawers.config.prop.enableSidedInput=Enable Sided Input
storageDrawers.config.prop.enableSidedInput.tooltip=Other blocks can insert items into drawers.
storageDrawers.config.prop.enableSidedOutput=Enable Sided Output
storageDrawers.config.prop.enableSidedOutput.tooltip=Other blocks can extract items from drawers.
storageDrawers.config.prop.enableItemConversion=Enable Item Conversion
storageDrawers.config.prop.enableItemConversion.tooltip=Drawers auto-convert ore dictionary compatible items.
storageDrawers.config.prop.invertShift=Invert Shift
storageDrawers.config.prop.invertShift.tooltip=Inverts click and shift+click behavior.
storageDrawers.config.prop.enableDebugLogging=Enable Debug Logging
@@ -57,6 +57,7 @@ public class ConfigManager
public boolean creativeTabVanillaWoods;
public boolean enableSidedInput;
public boolean enableSidedOutput;
public boolean enableItemConversion;
public boolean enableWailaIntegration;
public boolean enableAE2Integration;
public boolean enableThaumcraftIntegration;
@@ -119,6 +120,7 @@ public class ConfigManager
cache.creativeTabVanillaWoods = config.get(Configuration.CATEGORY_GENERAL, "creativeTabVanillaWoods", true).setLanguageKey(LANG_PREFIX + "prop.creativeTabVanillaWoods").getBoolean();
cache.enableSidedInput = config.get(Configuration.CATEGORY_GENERAL, "enableSidedInput", true).setLanguageKey(LANG_PREFIX + "prop.enableSidedInput").getBoolean();
cache.enableSidedOutput = config.get(Configuration.CATEGORY_GENERAL, "enableSidedOutput", true).setLanguageKey(LANG_PREFIX + "prop.enableSidedOutput").getBoolean();
cache.enableItemConversion = config.get(Configuration.CATEGORY_GENERAL, "enableItemConversion", true).setLanguageKey(LANG_PREFIX + "prop.enableItemConversion").getBoolean();
cache.invertShift = config.get(Configuration.CATEGORY_GENERAL, "invertShift", false,
"Inverts how shift works with drawers. If this is true, shifting will only give one item, where regular clicks will give a full stack. Leave false for default behavior.")
.setLanguageKey(LANG_PREFIX + "prop.invertShift").getBoolean();
@@ -1,5 +1,8 @@
package com.jaquadro.minecraft.storagedrawers.config;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -66,8 +69,6 @@ public class OreDictRegistry
public boolean isEntryBlacklisted (String 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))) {
@@ -76,8 +77,55 @@ public class OreDictRegistry
}
}
graylistCache.add(entry);
return false;
}
public boolean isEntryValid (String entry) {
if (graylistCache.contains(entry))
return true;
if (isEntryBlacklisted(entry))
return false;
if (!isValidForEquiv(entry)) {
blacklistCache.add(entry);
return false;
}
graylistCache.add(entry);
return true;
}
private boolean isValidForEquiv (String oreName) {
List<ItemStack> oreList = OreDictionary.getOres(oreName);
if (oreList.size() == 0)
return false;
// Fail entries that have any wildcard items registered to them.
for (int i = 0, n = oreList.size(); i < n; i++) {
if (oreList.get(i).getItemDamage() == OreDictionary.WILDCARD_VALUE)
return false;
}
// Fail entries where the keys in at least one stack are not the super-set of all other stacks.
// Can be determined by merging all keys and testing cardinality.
HashSet<Integer> mergedIds = new HashSet<Integer>();
int maxKeyCount = 0;
for (int i = 0, n = oreList.size(); i < n; i++) {
int[] ids = OreDictionary.getOreIDs(oreList.get(i));
maxKeyCount = Math.max(maxKeyCount, ids.length);
for (int id : ids)
mergedIds.add(id);
}
if (maxKeyCount < mergedIds.size())
return false;
return true;
}
}
@@ -27,10 +27,6 @@ public abstract class BaseDrawerData implements IDrawer, IInventoryAdapter
inventoryStack.init();
}
public boolean areItemsEqual (ItemStack item) {
return areItemsEqual(getStoredItemPrototype(), item);
}
protected void reset () {
oreDictMatches = null;
inventoryStack.reset();
@@ -55,10 +51,11 @@ public abstract class BaseDrawerData implements IDrawer, IInventoryAdapter
else {
oreDictMatches = new ArrayList<ItemStack>();
for (int id : oreIDs) {
if (StorageDrawers.oreDictRegistry.isEntryBlacklisted(OreDictionary.getOreName(id)))
String oreName = OreDictionary.getOreName(id);
if (!StorageDrawers.oreDictRegistry.isEntryValid(oreName))
continue;
List<ItemStack> list = OreDictionary.getOres(OreDictionary.getOreName(id));
List<ItemStack> list = OreDictionary.getOres(oreName);
for (int i = 0, n = list.size(); i < n; i++) {
if (list.get(i).getItemDamage() == OreDictionary.WILDCARD_VALUE)
continue;
@@ -108,31 +105,34 @@ 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;
public boolean areItemsEqual (ItemStack item) {
ItemStack protoStack = getStoredItemPrototype();
if (protoStack == null || item == null)
return false;
if (protoStack.getItem() == null || item.getItem() == null)
return false;
if (set2.length < smaller.length) {
smaller = set2;
larger = set1;
}
if (!protoStack.isItemEqual(item)) {
if (!StorageDrawers.config.cache.enableItemConversion)
return false;
if (oreDictMatches == null)
return false;
if (protoStack.getItem() == item.getItem())
return false;
for (int val1 : smaller) {
boolean found = false;
for (int val2 : larger) {
if (val1 == val2) {
found = true;
boolean oreMatch = false;
for (int i = 0, n = oreDictMatches.size(); i < n; i++) {
if (item.isItemEqual(oreDictMatches.get(i))) {
oreMatch = true;
break;
}
}
if (!found)
return null;
if (!oreMatch)
return false;
}
return smaller;
return ItemStack.areItemStackTagsEqual(protoStack, item);
}
public static boolean areItemsEqual (ItemStack stack1, ItemStack stack2) {
@@ -142,44 +142,33 @@ public abstract class BaseDrawerData implements IDrawer, IInventoryAdapter
return false;
if (!stack1.isItemEqual(stack2)) {
if (!StorageDrawers.config.cache.enableItemConversion)
return false;
if (stack1.getItemDamage() == OreDictionary.WILDCARD_VALUE || stack2.getItemDamage() == OreDictionary.WILDCARD_VALUE)
return false;
if (stack1.getItem() == stack2.getItem())
return false;
int[] ids1 = OreDictionary.getOreIDs(stack1);
int[] ids2 = OreDictionary.getOreIDs(stack2);
if (ids1.length == 0 || ids2.length == 0)
return false;
int[] commonIds = isSubsetEitherWay(ids1, ids2);
if (commonIds == null)
return false;
boolean oreMatch = false;
for (int oreIndex : commonIds) {
if (StorageDrawers.oreDictRegistry.isEntryBlacklisted(OreDictionary.getOreName(oreIndex)))
continue;
for (int id1 : ids1) {
for (int id2 : ids2) {
if (id1 != id2)
continue;
List<ItemStack> 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;
String name = OreDictionary.getOreName(id1);
if (StorageDrawers.oreDictRegistry.isEntryValid(name)) {
oreMatch = 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;
if (oreMatch)
break;
}
}
if (!oreMatch)