Files
Mekanism-Community-Edition/src/main/java/mekanism/common/OreDictCache.java
T
Richard Freimer d19f1cd7eb Transporter performance improvements (#5488)
* Improve performance of predicted insert. Don't copy stacks nearly as often

* Cache what stack is in the slots and update comments

* Cleanup up pathfinder code some

* Clean up a bit more mainly TileComponentEjector

* Fix crash caused by an earlier commit

* Minor cleanup

* Minor cleanup

* Slightly clean up TileEntityBin and fix creative bin auto eject lowering item count

* Partially cleanup getpaths, and prep work for better handling of AcceptorData with mulitple TransitResponses

* Scratch previous attempted idea of combining responses

* Use EnumMap instead of HashMap for keeping track of side data for Ejector and prep for an eventual change to TransitResponse of inserting into more than one slot at a time if the stack would be best split among multiple

* Change some more HashMaps to EnumMaps

* Make it easier to read component ejector for gas and fluids

* Inline getStacksToDest so that it can bail even earlier in cases when destination is full

* Don't create a copy of the stack if it won't be inserted AND also fix having broken inserting across multiple slots

* Replace a bunch of other HashMap instances with EnumMap where possible

* Make a couple sets more generic

* Use Int2ObjectMap for int based cache & tidy clamping

* ensure EnumMap doesnt throw NPE

* FastUtil map changes
2019-05-28 09:31:33 -04:00

109 lines
4.2 KiB
Java

package mekanism.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import mekanism.api.util.ItemInfo;
import mekanism.common.util.ItemRegistryUtils;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
public final class OreDictCache {
public static Map<ItemInfo, List<String>> cachedKeys = new HashMap<>();
public static Map<String, List<ItemStack>> oreDictStacks = new HashMap<>();
public static Map<String, List<ItemStack>> modIDStacks = new HashMap<>();
public static List<String> getOreDictName(ItemStack check) {
if (check.isEmpty()) {
return new ArrayList<>();
}
ItemInfo info = ItemInfo.get(check);
List<String> cached = cachedKeys.get(info);
if (cached != null) {
return cached;
}
int[] idsFound = OreDictionary.getOreIDs(check);
List<String> ret = new ArrayList<>();
for (Integer id : idsFound) {
ret.add(OreDictionary.getOreName(id));
}
cachedKeys.put(info, ret);
return ret;
}
public static List<ItemStack> getOreDictStacks(String oreName, boolean forceBlock) {
if (oreDictStacks.get(oreName) != null) {
return oreDictStacks.get(oreName);
}
List<String> keys = new ArrayList<>();
for (String s : OreDictionary.getOreNames()) {
if (s == null) {
continue;
}
if (oreName.equals(s) || oreName.equals("*")) {
keys.add(s);
} else if (oreName.endsWith("*") && !oreName.startsWith("*")) {
if (s.startsWith(oreName.substring(0, oreName.length() - 1))) {
keys.add(s);
}
} else if (oreName.startsWith("*") && !oreName.endsWith("*")) {
if (s.endsWith(oreName.substring(1))) {
keys.add(s);
}
} else if (oreName.startsWith("*") && oreName.endsWith("*")) {
if (s.contains(oreName.substring(1, oreName.length() - 1))) {
keys.add(s);
}
}
}
List<ItemStack> stacks = new ArrayList<>();
for (String key : keys) {
for (ItemStack stack : OreDictionary.getOres(key)) {
ItemStack toAdd = stack.copy();
if (!stacks.contains(stack) && (!forceBlock || toAdd.getItem() instanceof ItemBlock)) {
stacks.add(stack.copy());
}
}
}
oreDictStacks.put(oreName, stacks);
return stacks;
}
public static List<ItemStack> getModIDStacks(String modName, boolean forceBlock) {
if (modIDStacks.get(modName) != null) {
return modIDStacks.get(modName);
}
List<ItemStack> stacks = new ArrayList<>();
for (String key : OreDictionary.getOreNames()) {
for (ItemStack stack : OreDictionary.getOres(key)) {
ItemStack toAdd = stack.copy();
String s = ItemRegistryUtils.getMod(toAdd);
if (!stacks.contains(stack) && toAdd.getItem() instanceof ItemBlock) {
if (modName.equals(s) || modName.equals("*")) {
stacks.add(stack.copy());
} else if (modName.endsWith("*") && !modName.startsWith("*")) {
if (s.startsWith(modName.substring(0, modName.length() - 1))) {
stacks.add(stack.copy());
}
} else if (modName.startsWith("*") && !modName.endsWith("*")) {
if (s.endsWith(modName.substring(1))) {
stacks.add(stack.copy());
}
} else if (modName.startsWith("*") && modName.endsWith("*")) {
if (s.contains(modName.substring(1, modName.length() - 1))) {
stacks.add(stack.copy());
}
}
}
}
}
modIDStacks.put(modName, stacks);
return stacks;
}
}