Skip to content

Commit d7313f8

Browse files
authored
Merge pull request #279
Improve Crafting Matrix Shrink Logic
2 parents 85b8051 + cb57acd commit d7313f8

1 file changed

Lines changed: 4 additions & 12 deletions

File tree

src/main/java/me/wolfyscript/customcrafting/listeners/crafting/EventBasedCraftRecipeHandler.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,12 @@ public void onCraft(InventoryClickEvent event) {
6767
event.setCancelled(true);
6868
var player = (Player) event.getWhoClicked();
6969
if (event.isShiftClick() || ItemUtils.isAirOrNull(cursor) || cursor.getAmount() + resultItem.getAmount() <= cursor.getMaxStackSize()) {
70-
//Clear Matrix to prevent duplication and buggy behaviour.
71-
//This must not update the inventory yet, as that would call the PrepareItemCraftEvent, invalidating the recipe and preventing consumption of the recipe!
72-
//But clearing it later can cause other issues too!
73-
//So lets just set the items to AIR and amount to 0...
74-
inventory.clear();
70+
// At this point do not change the inventory! Because that would call the PrepareItemCraftEvent, invalidating the recipe and preventing consumption of the recipe!
7571
final int count = craftManager.collectResult(event, craftingData, player);
76-
//...do all the calculations & item replacements...
77-
//...and finally update the inventory.
72+
ItemStack[] matrix = craftingData.getRecipe().shrinkMatrix(player, inventory, count, craftingData, inventory.getMatrix().length == 9 ? 3 : 2);
73+
// Now all calculations are done, so we can update the inventory
74+
inventory.setMatrix(matrix);
7875
player.updateInventory();
79-
//Reset Matrix with the re-calculated items. (1 tick later, to not cause duplication!)
80-
//This will result in a short flicker of the items in the inventory... still better than duplications, so the flickering won't be fixed!
81-
Bukkit.getScheduler().runTaskLater(customCrafting, () -> {
82-
inventory.setMatrix(craftingData.getRecipe().shrinkMatrix(player, inventory, count, craftingData, inventory.getMatrix().length == 9 ? 3 : 2));
83-
}, 1);
8476
}
8577
});
8678
}

0 commit comments

Comments
 (0)