From 601091d7064d7fb5e60ad6e02831bad4a1049eb2 Mon Sep 17 00:00:00 2001 From: Silent Date: Thu, 28 Nov 2024 14:24:26 +0100 Subject: [PATCH] fix crash --- pom.xml | 2 +- .../java/tsp/headdb/core/util/MenuSetup.java | 130 +++++++++--------- 2 files changed, 67 insertions(+), 65 deletions(-) diff --git a/pom.xml b/pom.xml index a057b2b..751fb1e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ tsp.headdb HeadDB - 5.0.0-rc.12 + 5.0.0-rc.13 jar HeadDB diff --git a/src/main/java/tsp/headdb/core/util/MenuSetup.java b/src/main/java/tsp/headdb/core/util/MenuSetup.java index b6cad37..46c7101 100644 --- a/src/main/java/tsp/headdb/core/util/MenuSetup.java +++ b/src/main/java/tsp/headdb/core/util/MenuSetup.java @@ -114,38 +114,39 @@ public final class MenuSetup { localHeadsItemMeta.setDisplayName(ChatColor.GOLD + "Local Heads"); localHeadsItem.setItemMeta(localHeadsItemMeta); builder.button(41, new SimpleButton(localHeadsItem, e -> { - // Local heads must be calculated on every opening since new players can join at any time. - List localHeadsList = new ArrayList<>(HeadAPI.getLocalHeads(e.getWhoClicked().hasPermission("headdb.admin")).join()); // Convert Set to List for indexed access - GUI localGui = new SimpleGUI(); - Player mainPlayer = (Player) e.getWhoClicked(); - - for (int i = 0; i < localHeadsList.size(); i += 45) { - int end = Math.min(i + 45, localHeadsList.size()); - List section = localHeadsList.subList(i, end); // Get the sublist for the current page - - PaginationBuilder localPageBuilder = new PaginationBuilder(localGui) - .parentGui(mainGui) - .name(ChatColor.RED + "Local Heads" + ChatColor.DARK_GRAY + " (" + HeadAPI.getLocalHeads().join().size() + ")"); - - // Iterate over the heads in the current section and add them to the inventory - for (int j = 0; j < section.size(); j++) { - LocalHead localHead = section.get(j); - localPageBuilder.button(j, new SimpleButton(localHead.getItem(), ice -> { - ice.setCancelled(true); - Player player = (Player) ice.getWhoClicked(); - if (categoryPermission && !player.hasPermission("headdb.category.local.*") && !player.hasPermission("headdb.category.local." + localHead.getUniqueId())) { - localization.sendMessage(player, "noPermission"); - return; - } + HeadAPI.getLocalHeads(e.getWhoClicked().hasPermission("headdb.admin")).thenAcceptAsync(heads -> { + GUI localGui = new SimpleGUI(); + Player mainPlayer = (Player) e.getWhoClicked(); + List localHeadsList = new ArrayList<>(heads); + + for (int i = 0; i < localHeadsList.size(); i += 45) { + int end = Math.min(i + 45, localHeadsList.size()); + List section = localHeadsList.subList(i, end); // Get the sublist for the current page + + PaginationBuilder localPageBuilder = new PaginationBuilder(localGui) + .parentGui(mainGui) + .name(ChatColor.RED + "Local Heads" + ChatColor.DARK_GRAY + " (" + heads.size() + ")"); + + // Iterate over the heads in the current section and add them to the inventory + for (int j = 0; j < section.size(); j++) { + LocalHead localHead = section.get(j); + localPageBuilder.button(j, new SimpleButton(localHead.getItem(), ice -> { + ice.setCancelled(true); + Player player = (Player) ice.getWhoClicked(); + if (categoryPermission && !player.hasPermission("headdb.category.local.*") && !player.hasPermission("headdb.category.local." + localHead.getUniqueId())) { + localization.sendMessage(player, "noPermission"); + return; + } + + handleClick(player, localHead, ice); + })); + } - handleClick(player, localHead, ice); - })); + // Build the page and add it to the local GUI + localGui.addPage(localPageBuilder.build()); + localGui.open(mainPlayer); } - - // Build the page and add it to the local GUI - localGui.addPage(localPageBuilder.build()); - localGui.open(mainPlayer); - } + }, Utils.SYNC); })); mainGui.addPage(builder.build()); @@ -154,44 +155,45 @@ public final class MenuSetup { } private static void favorites(Player player, int page) { - Set favorites = HeadAPI.getFavoriteHeads(player.getUniqueId()).join(); - if (!favorites.isEmpty()) { - // Build favorites GUI - GUI favoritesGui = new SimpleGUI(); - List favoriteList = new ArrayList<>(favorites); // Copy to list for consistent indexing - - for (int i = 0; i < favoriteList.size(); i += 45) { - int end = Math.min(i + 45, favoriteList.size()); - List section = favoriteList.subList(i, end); - - PaginationBuilder favoritesPageBuilder = new PaginationBuilder(favoritesGui) - .parentGui(mainGui) - .name(ChatColor.GOLD + "Favorites " + ChatColor.DARK_GRAY + "(" + favoriteList.size() + ")"); - - for (int j = 0; j < section.size(); j++) { - Head head = section.get(j); - favoritesPageBuilder.button(j, new SimpleButton(head.getItem(), ice -> { - handleClick(player, head, ice); - - // Update favorites after removing the head - Set updatedFavorites = HeadAPI.getFavoriteHeads(player.getUniqueId()).join(); + HeadAPI.getFavoriteHeads(player.getUniqueId()).thenAcceptAsync(favorites -> { + if (!favorites.isEmpty()) { + // Build favorites GUI + GUI favoritesGui = new SimpleGUI(); + List favoriteList = new ArrayList<>(favorites); // Copy to list for consistent indexing + + for (int i = 0; i < favoriteList.size(); i += 45) { + int end = Math.min(i + 45, favoriteList.size()); + List section = favoriteList.subList(i, end); + + PaginationBuilder favoritesPageBuilder = new PaginationBuilder(favoritesGui) + .parentGui(mainGui) + .name(ChatColor.GOLD + "Favorites " + ChatColor.DARK_GRAY + "(" + favoriteList.size() + ")"); + + for (int j = 0; j < section.size(); j++) { + Head head = section.get(j); + favoritesPageBuilder.button(j, new SimpleButton(head.getItem(), ice -> { + handleClick(player, head, ice); + + // Update favorites after removing the head + HeadAPI.getFavoriteHeads(player.getUniqueId()).thenAcceptAsync(updated -> { + if (!updated.isEmpty()) { + favorites(player, page); // Refresh the GUI + } else { + mainGui.open(player); + } + }, Utils.SYNC); + })); + } - if (!updatedFavorites.isEmpty()) { - favorites(player, page); // Refresh the GUI - } else { - mainGui.open(player); - } - })); + favoritesGui.addPage(favoritesPageBuilder.build()); } - favoritesGui.addPage(favoritesPageBuilder.build()); + favoritesGui.open(player, !favoritesGui.getPages().isEmpty() ? page : 0); + } else { + localization.sendMessage(player, "noFavorites"); + Sounds.FAIL.play(player); } - - favoritesGui.open(player, !favoritesGui.getPages().isEmpty() ? page : 0); - } else { - localization.sendMessage(player, "noFavorites"); - Sounds.FAIL.play(player); - } + }, Utils.SYNC); } public static void prebuildCategoryGuis() {