Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public interface ShopInventory {
*/
int getRemainingSpace();

CompletableFuture<Integer> getRemainingSpaceAsync();

/**
* Get shop remaining stock.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -63,7 +64,7 @@ public static List<MarketItemGroup> groupShopsByItem(@NotNull final List<Shop> s

for(final Shop shop : shops) {
MarketItemGroup matchingGroup = null;
List<MarketItemGroup> matGroups = groupsByMat.computeIfAbsent(shop.getItem().getType(), k->new ArrayList<>());
final List<MarketItemGroup> 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())) {
Expand Down Expand Up @@ -406,16 +407,18 @@ public static List<MarketItemGroup> processGroups(@NotNull final List<Shop> 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;
}
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,34 @@ public int getRemainingSpace() {
}
}

@Override
public CompletableFuture<Integer> getRemainingSpaceAsync() {
if(this.unlimited) {
return CompletableFuture.completedFuture(-1);
}

if(Bukkit.getServer().isOwnedByCurrentRegion(location)) {

return CompletableFuture.completedFuture(getRemainingSpace());
}

final CompletableFuture<Integer> 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.
*
Expand Down Expand Up @@ -857,6 +885,7 @@ public CompletableFuture<Integer> getRemainingStockAsync() {
}

return future;

}

private int calculateRemainingStock() {
Expand Down
Loading