Chemistry Set: look up the recipe once per tick

updateEntity walked the whole recipe list several times per tick, since isRecipeValid, getAmountNeeded and the output merging branch each ran their own lookup, and every comparison allocated a padded copy of the recipe array. The lookup now happens once and the result is reused.

The matching rules are untouched, so which recipe wins for a given set of slots does not change.
This commit is contained in:
Eldrinn-Elantey committed 2026-09-02 19:10:25 +04:00
1 parent 6776676ef2
commit a75378a078
3 files changed
+58 -108

No files matched your search

@@ -6,6 +6,8 @@ import net.minecraftforge.oredict.OreDictionary;
public class AlchemyRecipe {
private static final int MAX_INPUT_SLOTS = 5;
private final ItemStack output;
private final ItemStack[] recipe;
private final int bloodOrbLevel;
@@ -23,35 +25,13 @@ public class AlchemyRecipe {
return false;
}
ItemStack[] recipe;
if (items.length < 5) {
if (items.length < MAX_INPUT_SLOTS) {
return false;
}
if (this.recipe.length != 5) {
ItemStack[] newRecipe = new ItemStack[5];
boolean[] checkList = new boolean[MAX_INPUT_SLOTS];
for (int i = 0; i < 5; i++) {
if (i + 1 > this.recipe.length) {
newRecipe[i] = null;
} else {
newRecipe[i] = this.recipe[i];
}
}
recipe = newRecipe;
} else {
recipe = this.recipe;
}
boolean[] checkList = new boolean[5];
for (int i = 0; i < 5; i++) {
checkList[i] = false;
}
for (int i = 0; i < 5; i++) {
for (int i = 0; i < Math.min(recipe.length, MAX_INPUT_SLOTS); i++) {
ItemStack recipeItemStack = recipe[i];
if (recipeItemStack == null) {
@@ -60,7 +40,7 @@ public class AlchemyRecipe {
boolean test = false;
for (int j = 0; j < 5; j++) {
for (int j = 0; j < MAX_INPUT_SLOTS; j++) {
if (checkList[j]) {
continue;
}
@@ -15,7 +15,7 @@ public class AlchemyRecipeRegistry {
recipes.add(new AlchemyRecipe(output, amountNeeded, recipe, bloodOrbLevel));
}
public static ItemStack getResult(ItemStack[] recipe, ItemStack bloodOrb) {
public static AlchemyRecipe findRecipe(ItemStack[] recipe, ItemStack bloodOrb) {
if (bloodOrb == null) {
return null;
}
@@ -28,31 +28,23 @@ public class AlchemyRecipeRegistry {
for (AlchemyRecipe ar : recipes) {
if (ar.doesRecipeMatch(recipe, bloodOrbLevel)) {
return (ar.getResult());
return ar;
}
}
return null;
}
public static ItemStack getResult(ItemStack[] recipe, ItemStack bloodOrb) {
AlchemyRecipe ar = findRecipe(recipe, bloodOrb);
return ar == null ? null : ar.getResult();
}
public static int getAmountNeeded(ItemStack[] recipe, ItemStack bloodOrb) {
if (bloodOrb == null) {
return 0;
}
AlchemyRecipe ar = findRecipe(recipe, bloodOrb);
if (!(bloodOrb.getItem() instanceof IBloodOrb)) {
return 0;
}
int bloodOrbLevel = ((IBloodOrb) bloodOrb.getItem()).getOrbLevel();
for (AlchemyRecipe ar : recipes) {
if (ar.doesRecipeMatch(recipe, bloodOrbLevel)) {
return (ar.getAmountNeeded());
}
}
return 0;
return ar == null ? 0 : ar.getAmountNeeded();
}
public static ItemStack[] getRecipeForItemStack(ItemStack itemStack) {
@@ -540,14 +540,22 @@ public class TEWritingTable extends TEInventory implements ISidedInventory, IBlo
}
}
} else {
if (!isRecipeValid()) {
ItemStack[] composedRecipe = new ItemStack[5];
System.arraycopy(inv, 1, composedRecipe, 0, 5);
AlchemyRecipe recipe = AlchemyRecipeRegistry.findRecipe(composedRecipe, inv[0]);
if (recipe == null) {
progress = 0;
return;
}
ItemStack result = recipe.getResult();
if (progress <= 0) {
progress = 0;
amountUsed = this.getAmountNeeded(getStackInSlot(0));
amountUsed = recipe.getAmountNeeded();
if (worldObj != null) {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
@@ -580,60 +588,50 @@ public class TEWritingTable extends TEInventory implements ISidedInventory, IBlo
if (progress >= progressNeeded) {
progress = 0;
this.setInventorySlotContents(6, getResultingItemStack());
this.setInventorySlotContents(6, result.copy());
ItemStack[] composedRecipe = new ItemStack[5];
System.arraycopy(inv, 1, composedRecipe, 0, 5);
this.decrementSlots(this.getRecipeForItems(composedRecipe, inv[0]));
this.decrementSlots(recipe.getRecipe());
if (worldObj != null) {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
} else if (getStackInSlot(6).getItem() == getResultingItemStack().getItem()
&& getResultingItemStack().stackSize
<= (getStackInSlot(6).getMaxStackSize() - getStackInSlot(6).stackSize)) {
if (worldTime % 4 == 0) {
SpellHelper.sendIndexedParticleToAllAround(
worldObj,
xCoord,
yCoord,
zCoord,
20,
worldObj.provider.dimensionId,
1,
xCoord,
yCoord,
zCoord);
}
} else if (getStackInSlot(6).getItem() == result.getItem()
&& result.stackSize <= (getStackInSlot(6).getMaxStackSize() - getStackInSlot(6).stackSize)) {
if (worldTime % 4 == 0) {
SpellHelper.sendIndexedParticleToAllAround(
worldObj,
xCoord,
yCoord,
zCoord,
20,
worldObj.provider.dimensionId,
1,
xCoord,
yCoord,
zCoord);
}
if (!SoulNetworkHandler.syphonFromNetworkWhileInContainer(
getStackInSlot(0),
amountUsed * acceleration)) {
return;
}
if (!SoulNetworkHandler
.syphonFromNetworkWhileInContainer(getStackInSlot(0), amountUsed * acceleration)) {
return;
}
progress += acceleration;
progress += acceleration;
if (progress >= progressNeeded) {
progress = 0;
ItemStack result = getResultingItemStack().copy();
result.stackSize += getStackInSlot(6).stackSize;
this.setInventorySlotContents(6, result);
if (progress >= progressNeeded) {
progress = 0;
ItemStack mergedResult = result.copy();
mergedResult.stackSize += getStackInSlot(6).stackSize;
this.setInventorySlotContents(6, mergedResult);
ItemStack[] composedRecipe = new ItemStack[5];
this.decrementSlots(recipe.getRecipe());
System.arraycopy(inv, 1, composedRecipe, 0, 5);
this.decrementSlots(this.getRecipeForItems(composedRecipe, inv[0]));
if (worldObj != null) {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
if (worldObj != null) {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
}
}
}
}
@@ -667,26 +665,6 @@ public class TEWritingTable extends TEInventory implements ISidedInventory, IBlo
}
}
public ItemStack[] getRecipeForItems(ItemStack[] recipe, ItemStack bloodOrb) {
if (bloodOrb == null) {
return null;
}
if (!(bloodOrb.getItem() instanceof IBloodOrb)) {
return null;
}
int bloodOrbLevel = ((IBloodOrb) bloodOrb.getItem()).getOrbLevel();
for (AlchemyRecipe ar : AlchemyRecipeRegistry.recipes) {
if (ar.doesRecipeMatch(recipe, bloodOrbLevel)) {
return ar.getRecipe();
}
}
return null;
}
public int getSpeedIncrease() {
return accelerationTime > 0 ? 5 : 1;
}