Skip to content
Closed
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
56 changes: 50 additions & 6 deletions src/main/java/fr/maxlego08/shop/ZShopManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,19 +433,58 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
LimiterManager limiterManager = this.plugin.getLimiterManager();

/* SERVER LIMIT */
int amountToSell = itemStack.getAmount();
if (optionalServer.isPresent()) {
Limit serverSellLimit = optionalServer.get();
newServerLimitAmount = serverSellLimit.getAmount() + itemStack.getAmount();
if (newServerLimitAmount > serverSellLimit.getLimit()) return;
int currentServerAmount = serverSellLimit.getAmount();
int remainingServerLimit = serverSellLimit.getLimit() - currentServerAmount;

if (remainingServerLimit <= 0) return; // No more can be sold

if (itemStack.getAmount() > remainingServerLimit) {
// Can only sell part of the stack
int overflow = itemStack.getAmount() - remainingServerLimit;
amountToSell = remainingServerLimit;

// Add overflow back to inventory with proper stack sizes
ItemStack overflowStack = itemStack.clone();
int maxStackSize = overflowStack.getType().getMaxStackSize();
while (overflow > 0) {
int stackAmount = Math.min(overflow, maxStackSize);
overflowStack.setAmount(stackAmount);
inventory.addItem(overflowStack.clone());
overflow -= stackAmount;
}
}
newServerLimitAmount = currentServerAmount + amountToSell;
}
/* END SERVER LIMIT */

/* PLAYER LIMIT */
if (optionalPlayer.isPresent()) {
Limit playerSellLimit = optionalPlayer.get();
Optional<PlayerLimit> optional = limiterManager.getLimit(player);
newPlayerLimitAmount = optional.map(e -> e.getSellAmount(material)).orElse(0) + itemStack.getAmount();
if (newPlayerLimitAmount > playerSellLimit.getLimit()) return;
int currentPlayerAmount = optional.map(e -> e.getSellAmount(material)).orElse(0);
int remainingPlayerLimit = playerSellLimit.getLimit() - currentPlayerAmount;

if (remainingPlayerLimit <= 0) return; // No more can be sold

if (amountToSell > remainingPlayerLimit) {
// Can only sell part of the stack
int overflow = amountToSell - remainingPlayerLimit;
amountToSell = remainingPlayerLimit;

// Add overflow back to inventory with proper stack sizes
ItemStack overflowStack = itemStack.clone();
int maxStackSize = overflowStack.getType().getMaxStackSize();
while (overflow > 0) {
int stackAmount = Math.min(overflow, maxStackSize);
overflowStack.setAmount(stackAmount);
inventory.addItem(overflowStack.clone());
overflow -= stackAmount;
}
}
newPlayerLimitAmount = currentPlayerAmount + amountToSell;
}
/* END PLAYER LIMIT */

Expand All @@ -456,9 +495,14 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
/* END LIMIT VALUES */

/* REMOVE ITEMS AND UPDATE MONEY */
prices.put(button.getEconomy(), prices.getOrDefault(button.getEconomy(), 0.0) + action.getPrice());
// Recalculate price based on actual amount being sold
double actualPrice = button.getSellPrice(player, amountToSell);
prices.put(button.getEconomy(), prices.getOrDefault(button.getEconomy(), 0.0) + actualPrice);
// Update the action price for display purposes
action.setPrice(actualPrice);
action.getItemStack().setAmount(amountToSell);
// inventory.remove(itemStack);
InventoryUtils.removeItem(inventory, itemStack, itemStack.getAmount());
InventoryUtils.removeItem(inventory, itemStack, amountToSell);
});

if (prices.isEmpty()) {
Expand Down
Loading