Skip to content

Commit

Permalink
reduce the amount of itemstack copies during comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsePattern committed Apr 8, 2024
1 parent 79584a1 commit 2e9dbe5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/minetweaker/mc1710/ForgeEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void onLivingDeathDrops(LivingDropsEvent ev) {
for(IItemStack iItemStack : iEntity.getDropsToRemove()) {
for(Iterator<EntityItem> iterator = ev.drops.iterator(); iterator.hasNext(); ) {
EntityItem drop = iterator.next();
if(iItemStack.matches(new MCItemStack(drop.getEntityItem()))) {
if(iItemStack.matches(MCItemStack.shallowWrap(drop.getEntityItem()))) {
iterator.remove();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void remove(IIngredient output, IIngredient input) {
List<ItemStack> toRemove = new ArrayList<ItemStack>();
List<ItemStack> toRemoveValues = new ArrayList<ItemStack>();
for (Map.Entry<ItemStack, ItemStack> entry : smeltingList.entrySet()) {
if (output.matches(new MCItemStack(entry.getValue()))
&& (input == null || input.matches(new MCItemStack(entry.getKey())))) {
if (output.matches(MCItemStack.shallowWrap(entry.getValue()))
&& (input == null || input.matches(MCItemStack.shallowWrap(entry.getKey())))) {
toRemove.add(entry.getKey());
toRemoveValues.add(entry.getValue());
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/minetweaker/mc1710/item/MCItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ public MCItemStack(ItemStack itemStack, boolean wildcardSize) {
this.wildcardSize = wildcardSize;
}

/**
* UNSAFE UNSAFE UNSAFE
* ONLY FOR INTERNAL USE, DOES NOT DEEP COPY THE ITEMSTACK!
*/
public static MCItemStack shallowWrap(ItemStack itemStack) {
return new MCItemStack(itemStack, null);
}

private MCItemStack(ItemStack itemStack, IData tag) {
if (itemStack == null)
throw new IllegalArgumentException("stack cannot be null");
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/minetweaker/mc1710/recipes/MCRecipeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static boolean matches(Object input, IIngredient ingredient) {
return false;
} else if (ingredient != null) {
if (input instanceof ItemStack) {
return ingredient.matches(getIItemStack((ItemStack) input));
return ingredient.matches(MCItemStack.shallowWrap((ItemStack) input));
} else if (input instanceof String) {
return ingredient.contains(getOreDict((String) input));
}
Expand All @@ -102,7 +102,7 @@ public List<ICraftingRecipe> getRecipesFor(IIngredient ingredient) {
List<ICraftingRecipe> results = new ArrayList<>();

for (IRecipe recipe : recipes) {
if (ingredient.matches(MineTweakerMC.getIItemStack(recipe.getRecipeOutput()))) {
if (ingredient.matches(MCItemStack.shallowWrap(recipe.getRecipeOutput()))) {
ICraftingRecipe converted = RecipeConverter.toCraftingRecipe(recipe);
results.add(converted);
}
Expand Down Expand Up @@ -292,15 +292,15 @@ public void clearOutputs() {
@Override
public boolean matches(IRecipe r) {
ItemStack recipeOutput = r.getRecipeOutput();
return recipeOutput != null && matches(getIItemStack(recipeOutput));
return recipeOutput != null && matches(MCItemStack.shallowWrap(recipeOutput));
}

@Override
public Set<IRecipe> find() {
return recipes.parallelStream()
.filter(r -> {
ItemStack recipeOutput = r.getRecipeOutput();
return recipeOutput != null && matches(getIItemStack(recipeOutput));
return recipeOutput != null && matches(MCItemStack.shallowWrap(recipeOutput));
}).collect(Collectors.toSet());
}

Expand Down Expand Up @@ -365,7 +365,7 @@ public ActionRemoveShapelessRecipes(IIngredient output, IIngredient[] ingredient

@Override
public boolean matches(IRecipe recipe) {
if (recipe.getRecipeOutput() == null || !output.matches(new MCItemStack(recipe.getRecipeOutput()))) {
if (recipe.getRecipeOutput() == null || !output.matches(MCItemStack.shallowWrap(recipe.getRecipeOutput()))) {
return false;
}
if (recipe instanceof ShapedRecipes) {
Expand Down Expand Up @@ -498,7 +498,7 @@ public ActionRemoveShapedRecipes(IIngredient output, IIngredient[][] ingredients
@Override
public boolean matches(IRecipe recipe) {
final ItemStack output = recipe.getRecipeOutput();
if (output == null || !this.output.matches(new MCItemStack(output))) {
if (output == null || !this.output.matches(MCItemStack.shallowWrap(output))) {
return false;
}
if (!(recipe instanceof ShapedRecipes || recipe instanceof ShapedOreRecipe))
Expand Down Expand Up @@ -702,8 +702,10 @@ public Object getOverrideKey() {
public static void applyAdditionsAndRemovals() {
System.out.println("MineTweaker: Applying additions and removals");
MineTweakerAPI.apply(MCRecipeManager.actionRemoveRecipesNoIngredients);
if(recipesToUndo.size() > 0) {
recipes.removeIf(recipesToUndo::contains);
if(!recipesToUndo.isEmpty()) {
Set<IRecipe> set = new HashSet<>(recipesToUndo.size() * 10);
set.addAll(recipesToUndo);
recipes.removeIf(set::contains);
}
int commonPoolParallelism = getCommonPoolParallelism();
if (commonPoolParallelism <= 1) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/minetweaker/mc1710/vanilla/MCLootRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import minetweaker.api.minecraft.MineTweakerMC;
import minetweaker.api.vanilla.ILootRegistry;
import minetweaker.api.vanilla.LootEntry;
import minetweaker.mc1710.item.MCItemStack;
import minetweaker.mc1710.util.MineTweakerHacks;
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraftforge.common.ChestGenHooks;
Expand Down Expand Up @@ -160,7 +161,7 @@ public void apply() {
List<WeightedRandomChestContent> contents = MineTweakerHacks.getPrivateObject(recipe, "contents");

for (WeightedRandomChestContent r : contents) {
if (pattern.matches(MineTweakerMC.getIItemStack(r.theItemId))) {
if (pattern.matches(MCItemStack.shallowWrap(r.theItemId))) {
removed.add(r);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/minetweaker/mc1710/vanilla/MCSeedRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import minetweaker.api.item.WeightedItemStack;
import minetweaker.api.minecraft.MineTweakerMC;
import minetweaker.api.vanilla.ISeedRegistry;
import minetweaker.mc1710.item.MCItemStack;
import minetweaker.mc1710.util.MineTweakerHacks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.WeightedRandom;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void apply() {

for (Object entry : SEEDS) {
ItemStack itemStack = MineTweakerHacks.getSeedEntrySeed(entry);
if (pattern.matches(MineTweakerMC.getIItemStack(itemStack))) {
if (pattern.matches(MCItemStack.shallowWrap(itemStack))) {
removed.add(entry);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/minetweaker/mods/ic2/IC2RecipeInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import static minetweaker.api.minecraft.MineTweakerMC.getItemStack;
import minetweaker.api.item.IIngredient;
import minetweaker.api.item.IItemStack;
import minetweaker.mc1710.item.MCItemStack;

import net.minecraft.item.ItemStack;

/**
Expand All @@ -29,7 +31,7 @@ public IC2RecipeInput(IIngredient ingredient) {

@Override
public boolean matches(ItemStack subject) {
return ingredient.matches(getIItemStack(subject));
return ingredient.matches(MCItemStack.shallowWrap(subject));
}

@Override
Expand Down

0 comments on commit 2e9dbe5

Please sign in to comment.