Refactor Auto Chisel logic to not require caching the result, only the inputs

Now baseCache and inputCache are only used to detect changes to the inputs, which resets the progress
This commit is contained in:
Kynake committed 2026-11-28 16:23:11 -03:00
1 parent 66789b3476
commit 1afc214356
1 file changed
+19 -31
@@ -82,7 +82,6 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory
private ItemStack lastBase;
// Crafting caches to avoid creating a new ItemStack result each tick
private ItemStack baseCache = null;
private ItemStack targetCache = null;
private ItemStack reversionTargetCache = null;
@@ -150,71 +149,60 @@ public class TileEntityAutoChisel extends TileEntity implements ISidedInventory
ItemStack base = inventory[BASE];
ItemStack target = getTarget();
if (base == null || target == null || !hasChisel()) {
baseCache = targetCache = resultCache = null;
baseCache = targetCache = null;
return;
}
// Something has changed about the recipe, regenerate caches
if (resultCache == null || baseCache != base || targetCache != target) {
// Unfiltered chisel result. Actual crafting might fail or be of a lower stack size than this.
resultCache = CarvingUtils.tryChiselingInto(base, target);
if (resultCache == null) {
// Not a valid chisel recipe
baseCache = targetCache = null;
return;
}
if (baseCache != base && targetCache != target) {
baseCache = base;
targetCache = target;
progress = 0;
}
if (progress < (hasUpgrade(Upgrade.SPEED) ? FAST_SPEED : SLOW_SPEED)) {
if (worldObj.getTotalWorldTime() % 10 == 0) {
progress++;
}
// Update progress and return
return;
}
// Crafting finished
progress = 0;
// Use the template's stack size as the max possible for this craft
int canBeMade = target.getMaxStackSize();
ItemStack result = CarvingUtils.tryChiselingInto(base, target);
if (result == null) {
baseCache = targetCache = null;
return;
}
int maxCraftSize = result.getMaxStackSize();
// If there are items in the output, they count towards the max we can make
ItemStack output = inventory[OUTPUT];
if (output != null) {
canBeMade -= output.stackSize;
maxCraftSize -= output.stackSize;
}
// Can't make more than we have
canBeMade = Math.min(base.stackSize, canBeMade);
// If we can't make any, forget it
if (canBeMade <= 0) {
maxCraftSize = Math.min(base.stackSize, maxCraftSize);
if (maxCraftSize <= 0) {
return;
}
// If we have the stack upgrade, boost the stack size to the max possible, otherwise just one
resultCache.stackSize = hasUpgrade(Upgrade.STACK) ? canBeMade : 1;
result.stackSize = hasUpgrade(Upgrade.STACK) ? maxCraftSize : 1;
if (canChisel(resultCache)) {
if (canChisel(result)) {
// Use (and possibly break) the chisel
chiselItem(resultCache.stackSize, target);
chiselItem(result.stackSize, target);
// Remove what we made from the input stack and add it to the output stack
base.stackSize -= resultCache.stackSize;
base.stackSize -= result.stackSize;
if (base.stackSize <= 0) {
setInventorySlotContents(BASE, null);
baseCache = null;
}
if (output == null) {
setInventorySlotContents(OUTPUT, resultCache.copy());
setInventorySlotContents(OUTPUT, result);
} else {
inventory[OUTPUT].stackSize += resultCache.stackSize;
inventory[OUTPUT].stackSize += result.stackSize;
slotChanged(OUTPUT);
}
}