Improve the performance of MechanicalPressTileEntity#canCompress

This commit is contained in:
mezz 2022-03-07 17:22:15 -10:00
parent fa4a0e7261
commit fcec6566fa
No known key found for this signature in database
GPG Key ID: 7513C6CA2F0F7687
2 changed files with 25 additions and 13 deletions

View File

@ -3,8 +3,9 @@ package com.simibubi.create.content.contraptions.components.press;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet;
import com.simibubi.create.AllBlocks; import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllRecipeTypes; import com.simibubi.create.AllRecipeTypes;
import com.simibubi.create.AllSoundEvents; import com.simibubi.create.AllSoundEvents;
@ -344,22 +345,19 @@ public class MechanicalPressTileEntity extends BasinOperatingTileEntity {
return AllRecipeTypes.PRESSING.find(pressingInv, level); return AllRecipeTypes.PRESSING.find(pressingInv, level);
} }
private static final List<ResourceLocation> RECIPE_DENY_LIST = private static final Set<ResourceLocation> RECIPE_DENY_SET =
ImmutableList.of(new ResourceLocation("occultism", "spirit_trade"), new ResourceLocation("occultism", "ritual")); ImmutableSet.of(new ResourceLocation("occultism", "spirit_trade"), new ResourceLocation("occultism", "ritual"));
public static <C extends Container> boolean canCompress(Recipe<C> recipe) { public static <C extends Container> boolean canCompress(Recipe<C> recipe) {
NonNullList<Ingredient> ingredients = recipe.getIngredients(); if (!(recipe instanceof CraftingRecipe) || !AllConfigs.SERVER.recipes.allowShapedSquareInPress.get())
if (!(recipe instanceof CraftingRecipe))
return false; return false;
RecipeSerializer<?> serializer = recipe.getSerializer(); RecipeSerializer<?> serializer = recipe.getSerializer();
for (ResourceLocation denied : RECIPE_DENY_LIST) if (serializer != null && RECIPE_DENY_SET.contains(serializer.getRegistryName()))
if (serializer != null && denied.equals(serializer.getRegistryName())) return false;
return false;
NonNullList<Ingredient> ingredients = recipe.getIngredients();
return AllConfigs.SERVER.recipes.allowShapedSquareInPress.get() return (ingredients.size() == 4 || ingredients.size() == 9) && ItemHelper.matchAllIngredients(ingredients);
&& (ingredients.size() == 4 || ingredients.size() == 9) && ItemHelper.condenseIngredients(ingredients)
.size() == 1;
} }
@Override @Override

View File

@ -118,8 +118,12 @@ public class ItemHelper {
} }
public static boolean matchIngredients(Ingredient i1, Ingredient i2) { public static boolean matchIngredients(Ingredient i1, Ingredient i2) {
if (i1 == i2)
return true;
ItemStack[] stacks1 = i1.getItems(); ItemStack[] stacks1 = i1.getItems();
ItemStack[] stacks2 = i2.getItems(); ItemStack[] stacks2 = i2.getItems();
if (stacks1 == stacks2)
return true;
if (stacks1.length == stacks2.length) { if (stacks1.length == stacks2.length) {
for (int i = 0; i < stacks1.length; i++) for (int i = 0; i < stacks1.length; i++)
if (!ItemStack.isSame(stacks1[i], stacks2[i])) if (!ItemStack.isSame(stacks1[i], stacks2[i]))
@ -129,6 +133,16 @@ public class ItemHelper {
return false; return false;
} }
public static boolean matchAllIngredients(NonNullList<Ingredient> ingredients) {
if (ingredients.size() <= 1)
return true;
Ingredient firstIngredient = ingredients.get(0);
for (int i = 1; i < ingredients.size(); i++)
if (!matchIngredients(firstIngredient, ingredients.get(i)))
return false;
return true;
}
public static enum ExtractionCountMode { public static enum ExtractionCountMode {
EXACTLY, UPTO EXACTLY, UPTO
} }