Skip to content

Commit 694703a

Browse files
committed
chore: optimize crafting gui with cache
1 parent 9c42860 commit 694703a

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

  • type.skyblockgeneric/src/main/java/net/swofty/type/skyblockgeneric/gui/inventories/sbmenu

type.skyblockgeneric/src/main/java/net/swofty/type/skyblockgeneric/gui/inventories/sbmenu/GUICrafting.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,24 @@ public class GUICrafting extends HypixelInventoryGUI implements RefreshingGUI {
3232
private static final ItemStack.Builder RECIPE_REQUIRED = ItemStackCreator.getStack("§cRecipe Required", Material.BARRIER, 1, "§7Add the items for a valid", "§7recipe in the crafting grid", "§7to the left!");
3333
private static final int[] CRAFT_SLOTS = new int[]{10, 11, 12, 19, 20, 21, 28, 29, 30};
3434
private static final int RESULT_SLOT = 23;
35+
private int lastGridHash = 0;
36+
private SkyBlockRecipe<?> lastParsedRecipe = null;
3537

3638
public GUICrafting() {
3739
super("Craft Item", InventoryType.CHEST_6_ROW);
3840
}
3941

42+
private int computeGridHash(Inventory inv) { // TODO: Account for metadata
43+
int h = 1;
44+
for (int slot : CRAFT_SLOTS) {
45+
ItemStack it = inv.getItemStack(slot);
46+
h = 31 * h + it.material().id();
47+
h = 31 * h + it.amount();
48+
}
49+
return h;
50+
}
51+
52+
4053
@Override
4154
public void onOpen(InventoryGUIOpenEvent e) {
4255
fill(ItemStackCreator.createNamedItemStack(Material.BLACK_STAINED_GLASS_PANE), 13, 34);
@@ -76,7 +89,16 @@ public void onBottomClick(InventoryPreClickEvent e) {
7689
@Override
7790
public void refreshItems(HypixelPlayer player) {
7891
Inventory inventory = getInventory();
79-
SkyBlockRecipe<?> recipe = SkyBlockRecipe.parseRecipe(getCurrentRecipe(inventory));
92+
int gridHash = computeGridHash(inventory);
93+
SkyBlockRecipe<?> recipe;
94+
95+
if (gridHash == lastGridHash) {
96+
recipe = lastParsedRecipe;
97+
} else {
98+
lastGridHash = gridHash;
99+
recipe = SkyBlockRecipe.parseRecipe(getCurrentRecipe(inventory));
100+
lastParsedRecipe = recipe;
101+
}
80102

81103
fill(ItemStackCreator.createNamedItemStack(Material.BLACK_STAINED_GLASS_PANE), 13, 34);
82104
border(ItemStackCreator.createNamedItemStack(Material.RED_STAINED_GLASS_PANE));

0 commit comments

Comments
 (0)