Memoize isModLoaded lookups

This commit is contained in:
NotMyWing
2024-06-07 17:09:18 +11:00
parent 68de8fa1a2
commit f7562db2e9
12 changed files with 101 additions and 134 deletions
@@ -9,7 +9,6 @@ import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.oredict.OreDictionary;
@@ -24,65 +23,30 @@ public class NonBlockingItems {
private NonBlockingItems() {
String[] strings = AEConfig.instance().getNonBlockingItems();
String[] modids = new String[0];
if (strings.length > 0) {
for (String s : strings) {
if (s.startsWith("[") && s.endsWith("]")) {
modids = s.substring(1, s.length() - 1).split("\\|");
} else {
for (String modid : modids) {
if (!Loader.isModLoaded(modid)) {
continue;
}
NON_BLOCKING_MAP.putIfAbsent(modid, new Object2ObjectOpenHashMap<>());
for (String s : strings) {
if (s.startsWith("[") && s.endsWith("]")) {
modids = s.substring(1, s.length() - 1).split("\\|");
} else {
for (String modid : modids) {
if (!Platform.isModLoaded(modid)) {
continue;
}
NON_BLOCKING_MAP.putIfAbsent(modid, new Object2ObjectOpenHashMap<>());
String[] ModItemMeta = s.split(":");
String[] ModItemMeta = s.split(":");
if (ModItemMeta.length < 2 || ModItemMeta.length > 3) {
AELog.error("Invalid non blocking item entry: " + s);
continue;
}
if (ModItemMeta.length < 2 || ModItemMeta.length > 3) {
AELog.error("Invalid non blocking item entry: " + s);
continue;
}
if (ModItemMeta[0].equals("gregtech") && Platform.GTLoaded) {
boolean found = false;
for (MetaItem<?> metaItem : MetaItem.getMetaItems()) {
MetaItem<?>.MetaValueItem metaItem2 = metaItem.getItem(ModItemMeta[1]);
if (metaItem.getItem(ModItemMeta[1]) != null) {
found = true;
ItemStack itemStack = metaItem2.getStackForm();
NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet());
NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) ->
{
intSet.add(itemStack.getItemDamage());
return intSet;
});
} else {
ItemStack itemStack = GameRegistry.makeItemStack(ModItemMeta[0] + ":" + ModItemMeta[1], ModItemMeta.length == 3 ? Integer.parseInt(ModItemMeta[2]) : 0, 1, null);
if (!itemStack.isEmpty()) {
NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet());
NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) ->
{
intSet.add(itemStack.getItemDamage());
return intSet;
});
}
}
}
if (!found) {
AELog.error("Item not found on nonBlocking config: " + s);
}
} else if (ModItemMeta[0].equals("ore")) {
OreDictionary.getOres(ModItemMeta[1]).forEach(itemStack ->
{
NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet());
NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) ->
{
intSet.add(itemStack.getItemDamage());
return intSet;
});
});
} else {
ItemStack itemStack = GameRegistry.makeItemStack(ModItemMeta[0] + ":" + ModItemMeta[1], ModItemMeta.length == 3 ? Integer.parseInt(ModItemMeta[2]) : 0, 1, null);
if (!itemStack.isEmpty()) {
if (ModItemMeta[0].equals("gregtech") && Platform.GTLoaded) {
boolean found = false;
for (MetaItem<?> metaItem : MetaItem.getMetaItems()) {
MetaItem<?>.MetaValueItem metaItem2 = metaItem.getItem(ModItemMeta[1]);
if (metaItem.getItem(ModItemMeta[1]) != null) {
found = true;
ItemStack itemStack = metaItem2.getStackForm();
NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet());
NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) ->
{
@@ -90,9 +54,42 @@ public class NonBlockingItems {
return intSet;
});
} else {
AELog.error("Item not found on nonBlocking config: " + s);
ItemStack itemStack = GameRegistry.makeItemStack(ModItemMeta[0] + ":" + ModItemMeta[1], ModItemMeta.length == 3 ? Integer.parseInt(ModItemMeta[2]) : 0, 1, null);
if (!itemStack.isEmpty()) {
NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet());
NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) ->
{
intSet.add(itemStack.getItemDamage());
return intSet;
});
}
}
}
if (!found) {
AELog.error("Item not found on nonBlocking config: " + s);
}
} else if (ModItemMeta[0].equals("ore")) {
OreDictionary.getOres(ModItemMeta[1]).forEach(itemStack ->
{
NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet());
NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) ->
{
intSet.add(itemStack.getItemDamage());
return intSet;
});
});
} else {
ItemStack itemStack = GameRegistry.makeItemStack(ModItemMeta[0] + ":" + ModItemMeta[1], ModItemMeta.length == 3 ? Integer.parseInt(ModItemMeta[2]) : 0, 1, null);
if (!itemStack.isEmpty()) {
NON_BLOCKING_MAP.get(modid).putIfAbsent(itemStack.getItem(), new IntOpenHashSet());
NON_BLOCKING_MAP.get(modid).computeIfPresent(itemStack.getItem(), (item, intSet) ->
{
intSet.add(itemStack.getItemDamage());
return intSet;
});
} else {
AELog.error("Item not found on nonBlocking config: " + s);
}
}
}
}