@@ -114,38 +114,39 @@ public final class MenuSetup {
114114 localHeadsItemMeta .setDisplayName (ChatColor .GOLD + "Local Heads" );
115115 localHeadsItem .setItemMeta (localHeadsItemMeta );
116116 builder .button (41 , new SimpleButton (localHeadsItem , e -> {
117- // Local heads must be calculated on every opening since new players can join at any time.
118- List <LocalHead > localHeadsList = new ArrayList <>(HeadAPI .getLocalHeads (e .getWhoClicked ().hasPermission ("headdb.admin" )).join ()); // Convert Set to List for indexed access
119- GUI localGui = new SimpleGUI ();
120- Player mainPlayer = (Player ) e .getWhoClicked ();
121-
122- for (int i = 0 ; i < localHeadsList .size (); i += 45 ) {
123- int end = Math .min (i + 45 , localHeadsList .size ());
124- List <LocalHead > section = localHeadsList .subList (i , end ); // Get the sublist for the current page
125-
126- PaginationBuilder localPageBuilder = new PaginationBuilder (localGui )
127- .parentGui (mainGui )
128- .name (ChatColor .RED + "Local Heads" + ChatColor .DARK_GRAY + " (" + HeadAPI .getLocalHeads ().join ().size () + ")" );
129-
130- // Iterate over the heads in the current section and add them to the inventory
131- for (int j = 0 ; j < section .size (); j ++) {
132- LocalHead localHead = section .get (j );
133- localPageBuilder .button (j , new SimpleButton (localHead .getItem (), ice -> {
134- ice .setCancelled (true );
135- Player player = (Player ) ice .getWhoClicked ();
136- if (categoryPermission && !player .hasPermission ("headdb.category.local.*" ) && !player .hasPermission ("headdb.category.local." + localHead .getUniqueId ())) {
137- localization .sendMessage (player , "noPermission" );
138- return ;
139- }
117+ HeadAPI .getLocalHeads (e .getWhoClicked ().hasPermission ("headdb.admin" )).thenAcceptAsync (heads -> {
118+ GUI localGui = new SimpleGUI ();
119+ Player mainPlayer = (Player ) e .getWhoClicked ();
120+ List <LocalHead > localHeadsList = new ArrayList <>(heads );
121+
122+ for (int i = 0 ; i < localHeadsList .size (); i += 45 ) {
123+ int end = Math .min (i + 45 , localHeadsList .size ());
124+ List <LocalHead > section = localHeadsList .subList (i , end ); // Get the sublist for the current page
125+
126+ PaginationBuilder localPageBuilder = new PaginationBuilder (localGui )
127+ .parentGui (mainGui )
128+ .name (ChatColor .RED + "Local Heads" + ChatColor .DARK_GRAY + " (" + heads .size () + ")" );
129+
130+ // Iterate over the heads in the current section and add them to the inventory
131+ for (int j = 0 ; j < section .size (); j ++) {
132+ LocalHead localHead = section .get (j );
133+ localPageBuilder .button (j , new SimpleButton (localHead .getItem (), ice -> {
134+ ice .setCancelled (true );
135+ Player player = (Player ) ice .getWhoClicked ();
136+ if (categoryPermission && !player .hasPermission ("headdb.category.local.*" ) && !player .hasPermission ("headdb.category.local." + localHead .getUniqueId ())) {
137+ localization .sendMessage (player , "noPermission" );
138+ return ;
139+ }
140+
141+ handleClick (player , localHead , ice );
142+ }));
143+ }
140144
141- handleClick (player , localHead , ice );
142- }));
145+ // Build the page and add it to the local GUI
146+ localGui .addPage (localPageBuilder .build ());
147+ localGui .open (mainPlayer );
143148 }
144-
145- // Build the page and add it to the local GUI
146- localGui .addPage (localPageBuilder .build ());
147- localGui .open (mainPlayer );
148- }
149+ }, Utils .SYNC );
149150 }));
150151
151152 mainGui .addPage (builder .build ());
@@ -154,44 +155,45 @@ public final class MenuSetup {
154155 }
155156
156157 private static void favorites (Player player , int page ) {
157- Set <Head > favorites = HeadAPI .getFavoriteHeads (player .getUniqueId ()).join ();
158- if (!favorites .isEmpty ()) {
159- // Build favorites GUI
160- GUI favoritesGui = new SimpleGUI ();
161- List <Head > favoriteList = new ArrayList <>(favorites ); // Copy to list for consistent indexing
162-
163- for (int i = 0 ; i < favoriteList .size (); i += 45 ) {
164- int end = Math .min (i + 45 , favoriteList .size ());
165- List <Head > section = favoriteList .subList (i , end );
166-
167- PaginationBuilder favoritesPageBuilder = new PaginationBuilder (favoritesGui )
168- .parentGui (mainGui )
169- .name (ChatColor .GOLD + "Favorites " + ChatColor .DARK_GRAY + "(" + favoriteList .size () + ")" );
170-
171- for (int j = 0 ; j < section .size (); j ++) {
172- Head head = section .get (j );
173- favoritesPageBuilder .button (j , new SimpleButton (head .getItem (), ice -> {
174- handleClick (player , head , ice );
175-
176- // Update favorites after removing the head
177- Set <Head > updatedFavorites = HeadAPI .getFavoriteHeads (player .getUniqueId ()).join ();
158+ HeadAPI .getFavoriteHeads (player .getUniqueId ()).thenAcceptAsync (favorites -> {
159+ if (!favorites .isEmpty ()) {
160+ // Build favorites GUI
161+ GUI favoritesGui = new SimpleGUI ();
162+ List <Head > favoriteList = new ArrayList <>(favorites ); // Copy to list for consistent indexing
163+
164+ for (int i = 0 ; i < favoriteList .size (); i += 45 ) {
165+ int end = Math .min (i + 45 , favoriteList .size ());
166+ List <Head > section = favoriteList .subList (i , end );
167+
168+ PaginationBuilder favoritesPageBuilder = new PaginationBuilder (favoritesGui )
169+ .parentGui (mainGui )
170+ .name (ChatColor .GOLD + "Favorites " + ChatColor .DARK_GRAY + "(" + favoriteList .size () + ")" );
171+
172+ for (int j = 0 ; j < section .size (); j ++) {
173+ Head head = section .get (j );
174+ favoritesPageBuilder .button (j , new SimpleButton (head .getItem (), ice -> {
175+ handleClick (player , head , ice );
176+
177+ // Update favorites after removing the head
178+ HeadAPI .getFavoriteHeads (player .getUniqueId ()).thenAcceptAsync (updated -> {
179+ if (!updated .isEmpty ()) {
180+ favorites (player , page ); // Refresh the GUI
181+ } else {
182+ mainGui .open (player );
183+ }
184+ }, Utils .SYNC );
185+ }));
186+ }
178187
179- if (!updatedFavorites .isEmpty ()) {
180- favorites (player , page ); // Refresh the GUI
181- } else {
182- mainGui .open (player );
183- }
184- }));
188+ favoritesGui .addPage (favoritesPageBuilder .build ());
185189 }
186190
187- favoritesGui .addPage (favoritesPageBuilder .build ());
191+ favoritesGui .open (player , !favoritesGui .getPages ().isEmpty () ? page : 0 );
192+ } else {
193+ localization .sendMessage (player , "noFavorites" );
194+ Sounds .FAIL .play (player );
188195 }
189-
190- favoritesGui .open (player , !favoritesGui .getPages ().isEmpty () ? page : 0 );
191- } else {
192- localization .sendMessage (player , "noFavorites" );
193- Sounds .FAIL .play (player );
194- }
196+ }, Utils .SYNC );
195197 }
196198
197199 public static void prebuildCategoryGuis () {
0 commit comments