diff --git a/quickshop-api/src/main/java/com/ghostchu/quickshop/api/shop/inventory/ShopInventory.java b/quickshop-api/src/main/java/com/ghostchu/quickshop/api/shop/inventory/ShopInventory.java index 41f85309eb..202123c58b 100644 --- a/quickshop-api/src/main/java/com/ghostchu/quickshop/api/shop/inventory/ShopInventory.java +++ b/quickshop-api/src/main/java/com/ghostchu/quickshop/api/shop/inventory/ShopInventory.java @@ -75,6 +75,8 @@ public interface ShopInventory { */ int getRemainingSpace(); + CompletableFuture getRemainingSpaceAsync(); + /** * Get shop remaining stock. * diff --git a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/menu/browse/MarketUtils.java b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/menu/browse/MarketUtils.java index 42ed2f8b28..d9b84be505 100644 --- a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/menu/browse/MarketUtils.java +++ b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/menu/browse/MarketUtils.java @@ -33,6 +33,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; /** * MarketUtils - Utility class for market/browse operations Handles grouping shops by item, @@ -63,7 +64,7 @@ public static List groupShopsByItem(@NotNull final List s for(final Shop shop : shops) { MarketItemGroup matchingGroup = null; - List matGroups = groupsByMat.computeIfAbsent(shop.getItem().getType(), k->new ArrayList<>()); + final List matGroups = groupsByMat.computeIfAbsent(shop.getItem().getType(), k->new ArrayList<>()); // Find existing group that matches this shop's item for(final MarketItemGroup group : matGroups) { if(matcher.matches(group.getRepresentativeItem(), shop.getItem())) { @@ -406,16 +407,18 @@ public static List processGroups(@NotNull final List shop public static int getStockFromCache(@NotNull final Shop shop) { if(shop.isUnlimited()) { + return -1; } try { - final ShopInventoryCountCache cache = QuickShop.getInstance().getShopManager() - .queryShopInventoryCacheInDatabase(shop).join(); - final int stock = cache.getStock(); + //final ShopInventoryCountCache cache = QuickShop.getInstance().getShopManager().queryShopInventoryCacheInDatabase(shop).join(); + //final int stock = cache.getStock(); + final int stock = shop.getRemainingStockAsync().join(); // Return stock if available, otherwise return 0 for uninitialized cache - return stock >= 0? stock : 0; + return Math.max(stock, 0); } catch(final Exception e) { // Fallback to 0 if cache query fails + System.out.println("Error getting stock: " + e.getMessage()); return 0; } } @@ -434,11 +437,10 @@ public static int getSpaceFromCache(@NotNull final Shop shop) { return -1; } try { - final ShopInventoryCountCache cache = QuickShop.getInstance().getShopManager() - .queryShopInventoryCacheInDatabase(shop).join(); - final int space = cache.getSpace(); + //final ShopInventoryCountCache cache = QuickShop.getInstance().getShopManager().queryShopInventoryCacheInDatabase(shop).join(); + final int space = shop.getRemainingSpaceAsync().join(); // Return space if available, otherwise return 0 for uninitialized cache - return space >= 0? space : 0; + return Math.max(space, 0); } catch(final Exception e) { // Fallback to 0 if cache query fails return 0; diff --git a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/menu/trade/MainPage.java b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/menu/trade/MainPage.java index 92787bcd72..4b9bd73d07 100644 --- a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/menu/trade/MainPage.java +++ b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/menu/trade/MainPage.java @@ -99,7 +99,7 @@ public void handle(final PageOpenCallback open) { final ItemStack shopItem = shop.get().getItem(); final int amount = shopItem.getAmount(); // Use cache to avoid Folia cross-region block access issues - final int stock = (shop.get().isBuying())? -1 : MarketUtils.getStockFromCache(shop.get()); + final int stock = (shop.get().isBuying())? MarketUtils.getSpaceFromCache(shop.get()) : MarketUtils.getStockFromCache(shop.get()); final String stockString = (shop.get().isUnlimited())? "Unlimited" : stock + ""; final String priceFormatted = shop.get().format(shop.get().bukkitLocation().getWorld().getName(), shop.get().getCurrency()); diff --git a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/ContainerShop.java b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/ContainerShop.java index 41c53c23d8..61c31ff0aa 100644 --- a/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/ContainerShop.java +++ b/quickshop-bukkit/src/main/java/com/ghostchu/quickshop/shop/ContainerShop.java @@ -818,6 +818,34 @@ public int getRemainingSpace() { } } + @Override + public CompletableFuture getRemainingSpaceAsync() { + if(this.unlimited) { + return CompletableFuture.completedFuture(-1); + } + + if(Bukkit.getServer().isOwnedByCurrentRegion(location)) { + + return CompletableFuture.completedFuture(getRemainingSpace()); + } + + final CompletableFuture future = new CompletableFuture<>(); + + try { + QuickShop.folia().getScheduler().runAtLocation(this.location, task->{ + try { + future.complete(getRemainingSpace()); + } catch(final Throwable throwable) { + future.completeExceptionally(throwable); + } + }); + } catch(final Throwable throwable) { + future.completeExceptionally(throwable); + } + + return future; + } + /** * Returns the number of items this shop has in stock. * @@ -857,6 +885,7 @@ public CompletableFuture getRemainingStockAsync() { } return future; + } private int calculateRemainingStock() {