aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormiozune <miozune@gmail.com>2023-05-23 17:16:02 +0900
committerGitHub <noreply@github.com>2023-05-23 10:16:02 +0200
commitf8d9611b449e16c158af1d17a2929a93ced1f804 (patch)
tree8aacb7fb5d7127bf2ef0c7d1cfdd892b94bf66e9 /src
parentcc95112a0f37555b399048000b74321195ecbb76 (diff)
downloadGT5-Unofficial-f8d9611b449e16c158af1d17a2929a93ced1f804.tar.gz
GT5-Unofficial-f8d9611b449e16c158af1d17a2929a93ced1f804.tar.bz2
GT5-Unofficial-f8d9611b449e16c158af1d17a2929a93ced1f804.zip
Speedup Recycler recipe lookup (#2016)
Diffstat (limited to 'src')
-rw-r--r--src/main/java/gregtech/api/util/GT_ModHandler.java54
-rw-r--r--src/main/java/gregtech/api/util/GT_Recipe.java7
-rw-r--r--src/main/java/gregtech/api/util/GT_Utility.java15
3 files changed, 59 insertions, 17 deletions
diff --git a/src/main/java/gregtech/api/util/GT_ModHandler.java b/src/main/java/gregtech/api/util/GT_ModHandler.java
index b87fda289a..f851565bf7 100644
--- a/src/main/java/gregtech/api/util/GT_ModHandler.java
+++ b/src/main/java/gregtech/api/util/GT_ModHandler.java
@@ -9,7 +9,6 @@ import static gregtech.api.enums.GT_Values.M;
import static gregtech.api.enums.GT_Values.RA;
import static gregtech.api.enums.GT_Values.V;
import static gregtech.api.enums.GT_Values.W;
-import static gregtech.api.util.GT_Recipe.GT_Recipe_Map.sMaceratorRecipes;
import java.util.ArrayList;
import java.util.Arrays;
@@ -46,6 +45,7 @@ import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
+import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
@@ -100,6 +100,8 @@ public class GT_ModHandler {
private static final Map<IRecipeInput, RecipeOutput> sOreWashingRecipes = new HashMap<>();
private static final Map<IRecipeInput, RecipeOutput> sThermalCentrifugeRecipes = new HashMap<>();
private static final Map<IRecipeInput, RecipeOutput> sMassfabRecipes = new HashMap<>();
+ private static Set<GT_Utility.ItemId> recyclerWhitelist;
+ private static Set<GT_Utility.ItemId> recyclerBlacklist;
private static boolean sBufferCraftingRecipes = true;
public static List<Integer> sSingleNonBlockDamagableRecipeList_list = new ArrayList<>(100);
@@ -2017,19 +2019,47 @@ public class GT_ModHandler {
*/
public static ItemStack getRecyclerOutput(ItemStack aInput, int aScrapChance) {
if (aInput == null || aScrapChance != 0) return null;
- try {
- if (ic2.api.recipe.Recipes.recyclerWhitelist.isEmpty())
- return ic2.api.recipe.Recipes.recyclerBlacklist.contains(aInput) ? null : ItemList.IC2_Scrap.get(1);
- return ic2.api.recipe.Recipes.recyclerWhitelist.contains(aInput) ? ItemList.IC2_Scrap.get(1) : null;
- } catch (Throwable e) {
- /* Do nothing */
+ if (recyclerWhitelist == null) {
+ generateRecyclerCache();
}
- try {
- return ic2.api.recipe.Recipes.recyclerBlacklist.contains(aInput) ? null : ItemList.IC2_Scrap.get(1);
- } catch (Throwable e) {
- /* Do nothing */
+
+ if (recyclerWhitelist.isEmpty()) {
+ if (searchRecyclerCache(aInput, recyclerBlacklist)) {
+ return null;
+ } else {
+ return ItemList.IC2_Scrap.get(1);
+ }
+ } else {
+ if (searchRecyclerCache(aInput, recyclerWhitelist)) {
+ return ItemList.IC2_Scrap.get(1);
+ } else {
+ return null;
+ }
}
- return null;
+ }
+
+ private static void generateRecyclerCache() {
+ recyclerWhitelist = new HashSet<>();
+ for (IRecipeInput input : ic2.api.recipe.Recipes.recyclerWhitelist) {
+ for (ItemStack stack : input.getInputs()) {
+ recyclerWhitelist.add(GT_Utility.ItemId.create(stack));
+ }
+ }
+ recyclerBlacklist = new HashSet<>();
+ for (IRecipeInput input : ic2.api.recipe.Recipes.recyclerBlacklist) {
+ for (ItemStack stack : input.getInputs()) {
+ recyclerBlacklist.add(GT_Utility.ItemId.create(stack));
+ }
+ }
+ }
+
+ private static boolean searchRecyclerCache(ItemStack stack, Set<GT_Utility.ItemId> set) {
+ if (set.contains(GT_Utility.ItemId.createNoCopy(stack))) {
+ return true;
+ }
+ // ic2.api.recipe.RecipeInputItemStack#matches expects item with wildcard meta to accept arbitrary meta
+ return set.contains(
+ GT_Utility.ItemId.createNoCopy(stack.getItem(), OreDictionary.WILDCARD_VALUE, stack.getTagCompound()));
}
/**
diff --git a/src/main/java/gregtech/api/util/GT_Recipe.java b/src/main/java/gregtech/api/util/GT_Recipe.java
index 6d660c67f6..3d0a731908 100644
--- a/src/main/java/gregtech/api/util/GT_Recipe.java
+++ b/src/main/java/gregtech/api/util/GT_Recipe.java
@@ -5161,13 +5161,12 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
@Override
public GT_Recipe findRecipe(IHasWorldObjectAndCoords aTileEntity, GT_Recipe aRecipe, boolean aNotUnificated,
long aVoltage, FluidStack[] aFluids, ItemStack aSpecialSlot, ItemStack... aInputs) {
- if (aInputs == null || aInputs.length <= 0 || aInputs[0] == null) return null;
+ if (aInputs == null || aInputs.length == 0 || aInputs[0] == null) return null;
if (aRecipe != null && aRecipe.isRecipeInputEqual(false, true, aFluids, aInputs)) return aRecipe;
return new GT_Recipe(
false,
new ItemStack[] { GT_Utility.copyAmount(1, aInputs[0]) },
- GT_ModHandler.getRecyclerOutput(GT_Utility.copyAmount(64, aInputs[0]), 0) == null ? null
- : new ItemStack[] { ItemList.IC2_Scrap.get(1) },
+ new ItemStack[] { GT_ModHandler.getRecyclerOutput(aInputs[0], 0) },
null,
new int[] { 1250 },
null,
@@ -5179,7 +5178,7 @@ public class GT_Recipe implements Comparable<GT_Recipe> {
@Override
public boolean containsInput(ItemStack aStack) {
- return GT_ModHandler.getRecyclerOutput(GT_Utility.copyAmount(64, aStack), 0) != null;
+ return GT_ModHandler.getRecyclerOutput(aStack, 0) != null;
}
}
diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java
index a477f6d59f..f9e7d7fe0c 100644
--- a/src/main/java/gregtech/api/util/GT_Utility.java
+++ b/src/main/java/gregtech/api/util/GT_Utility.java
@@ -4651,7 +4651,7 @@ public class GT_Utility {
@AutoValue
public abstract static class ItemId {
- public static AutoValue_GT_Utility_ItemId create(NBTTagCompound tag) {
+ public static ItemId create(NBTTagCompound tag) {
return new AutoValue_GT_Utility_ItemId(
Item.getItemById(tag.getShort("item")),
tag.getShort("meta"),
@@ -4668,6 +4668,14 @@ public class GT_Utility {
return new AutoValue_GT_Utility_ItemId(itemStack.getItem(), itemStack.getItemDamage(), nbt);
}
+ /** This method copies NBT, as it is mutable. */
+ public static ItemId create(Item item, int metaData, @Nullable NBTTagCompound nbt) {
+ if (nbt != null) {
+ nbt = (NBTTagCompound) nbt.copy();
+ }
+ return new AutoValue_GT_Utility_ItemId(item, metaData, nbt);
+ }
+
/** This method does not copy NBT in order to save time. Make sure not to mutate it! */
public static ItemId createNoCopy(ItemStack itemStack) {
return new AutoValue_GT_Utility_ItemId(
@@ -4676,6 +4684,11 @@ public class GT_Utility {
itemStack.getTagCompound());
}
+ /** This method does not copy NBT in order to save time. Make sure not to mutate it! */
+ public static ItemId createNoCopy(Item item, int metaData, @Nullable NBTTagCompound nbt) {
+ return new AutoValue_GT_Utility_ItemId(item, metaData, nbt);
+ }
+
protected abstract Item item();
protected abstract int metaData();