Hey, found a performance regression in InstantCraft 3.1.0 that crashed our server.Fixed

Hey, found a performance regression in InstantCraft 3.1.0 that crashed our server.

**Issue:**
With "Split crafted stacks": true (the default), OnItemCraft calls FinishCrafting() once per craft unit:

```
while (task.amount > 0)
owner.inventory.crafting.FinishCrafting(task);
```

A big batch craft like 5000 gunpowder = 500 FinishCrafting calls in a single tick. Each one creates and gives an item, sends note.craft_done and fires OnItemCraftFinished in every other plugin. We saw OnItemCraft at ~600ms per call (4s+ average) and the server froze/crashed when a player queued gunpowder. 3.0.0 used one FinishCrafting call per craft and had no problem.

**Repro:** default config, instantcraft.use, craft a few max batches of gunpowder back to back.

**Suggested fix:**
1. For items without condition, call FinishCrafting once per full stack instead of once per unit (amountToCreate = n * blueprintAmount, task.amount -= n - 1, with n = stackable / amountToCreate). Only items with condition need the per-unit loop.
2. Add a guard that breaks the loop if task.amount doesn't go down after FinishCrafting, so it can never hang the server.
3. Wrap the amountToCreate change in try/finally. It modifies the shared ItemBlueprint, so if a hook throws, the blueprint stays wrong for every player until restart.

With this patched locally, a 15 x 999 gunpowder craft doesn't even show up in hook timings. Happy to share the patched OnItemCraft if useful.

Thanks for the plugin!

i can confirm that this plugin has issues, but its not causing crashes, it caused players to be kicked for packet flooding. repeatedly

cGxBOWi3FT5mYBC.jpg Tacman

i can confirm that this plugin has issues, but its not causing crashes, it caused players to be kicked for packet flooding. repeatedly

yeah, not really crashing, but 15 people on a server, 15 kicked for packet flooding., we almost there . but yeah, its broken 

Can confirm what the OP found, and this one's partly on me. The per-craft loop in 3.1.0 is the one I suggested in my 3.0.0 report, where I said the loop would be short. I missed that one craft task can be a whole stack of crafts (craftingStackable is the item's stack size, so up to 1,000 gunpowder crafts in a single click even on vanilla stacks), and that every FinishCrafting call sends the player two client commands in the same frame. We didn't see it on our own server because gunpowder, explosives and the other big bulk crafts were already on our Normal Speed list, so they never went through the instant loop.

The OP's suggested fix is right: one FinishCrafting per full stack for items without condition, a guard on the loop, and try/finally around the amountToCreate change. We've done the same, with two additions that matter:

1. FinishCrafting only uses one craft's worth of the ingredients in takenItems. With a stack per call, use the remaining n - 1 crafts' worth yourself (same matching as the game's loop), or they're left in the task and never consumed.
2. Workbench bonus rolls happen once per FinishCrafting call. Efficiency copies the crafted item, so one roll on the stack averages out the same. Surplus and the Recycle Bin give a fixed amount per roll, so without one extra roll per craft, big batches quietly get fewer bonuses than crafting one at a time. Merge the bonus items into whole stacks before giving them, or a big batch creates thousands of items.

Smaller one: the free place check counts empty slots only, so it refuses crafts that would fit into stacks the player already has.

If you're on the official 3.1.0 and can't patch the code, a stopgap to prevent kicks: put gunpowder, explosives, ammo and other bulk crafts on the Normal Speed list via the config until a fix is out. Not ideal for players, but it stops the kicks, because those items stay out of the instant loop. With the code fix above, that isn't needed.