diff --git a/src/main/java/popa/popa.java b/src/main/java/popa/popa.java index ca23217..e272965 100644 --- a/src/main/java/popa/popa.java +++ b/src/main/java/popa/popa.java @@ -675,19 +675,20 @@ public final class popa extends JavaPlugin implements Listener { /** * Обрабатывает операцию продажи предмета */ - private void handleSellOperation(JsonObject operation) { + void handleSellOperation(JsonObject operation) { String playerName = operation.get("player_name").getAsString(); int slotIndex = operation.get("slot_index").getAsInt(); - + int sellAmount = operation.get("amount").getAsInt(); // 👈 берём количество из операции + Player player = Bukkit.getPlayer(playerName); if (player == null || !player.isOnline()) { sendOperationError(operation.get("id").getAsString(), "Игрок не в сети"); return; } - + PlayerInventory inv = player.getInventory(); ItemStack item = null; - + // Получаем предмет из нужного слота if (slotIndex >= 0 && slotIndex <= 35) { item = inv.getItem(slotIndex); @@ -702,35 +703,69 @@ public final class popa extends JavaPlugin implements Listener { } else if (slotIndex == 40) { item = inv.getItemInOffHand(); } - + if (item == null || item.getType() == Material.AIR) { sendOperationError(operation.get("id").getAsString(), "Предмет не найден в указанном слоте"); return; } - - // Создаем полную копию предмета с метаданными для отправки на сервер - JsonObject itemData = itemStackToDetailedJson(item, slotIndex); - - // Очищаем слот - if (slotIndex >= 0 && slotIndex <= 35) { - inv.setItem(slotIndex, null); - } else if (slotIndex == 36) { - inv.setBoots(null); - } else if (slotIndex == 37) { - inv.setLeggings(null); - } else if (slotIndex == 38) { - inv.setChestplate(null); - } else if (slotIndex == 39) { - inv.setHelmet(null); - } else if (slotIndex == 40) { - inv.setItemInOffHand(null); + + int currentAmount = item.getAmount(); + + // Валидация количества + if (sellAmount <= 0 || sellAmount > currentAmount) { + sendOperationError(operation.get("id").getAsString(), + "Некорректное количество для продажи: " + sellAmount); + return; } - - // Отправляем сообщение игроку + + // Создаём отдельный ItemStack для продажи + ItemStack itemToSell = item.clone(); + itemToSell.setAmount(sellAmount); + + // Формируем данные именно продаваемой части + JsonObject itemData = itemStackToDetailedJson(itemToSell, slotIndex); + + // Обновляем инвентарь игрока + if (sellAmount == currentAmount) { + // Продаём весь стак — очищаем слот, как раньше + if (slotIndex >= 0 && slotIndex <= 35) { + inv.setItem(slotIndex, null); + } else if (slotIndex == 36) { + inv.setBoots(null); + } else if (slotIndex == 37) { + inv.setLeggings(null); + } else if (slotIndex == 38) { + inv.setChestplate(null); + } else if (slotIndex == 39) { + inv.setHelmet(null); + } else if (slotIndex == 40) { + inv.setItemInOffHand(null); + } + } else { + // Продаём только часть стака — уменьшаем количество в слоте + int remaining = currentAmount - sellAmount; + item.setAmount(remaining); + + if (slotIndex >= 0 && slotIndex <= 35) { + inv.setItem(slotIndex, item); + } else if (slotIndex == 36) { + inv.setBoots(item); + } else if (slotIndex == 37) { + inv.setLeggings(item); + } else if (slotIndex == 38) { + inv.setChestplate(item); + } else if (slotIndex == 39) { + inv.setHelmet(item); + } else if (slotIndex == 40) { + inv.setItemInOffHand(item); + } + } + + // Сообщение игроку int price = operation.get("price").getAsInt(); - player.sendMessage("§aВы выставили предмет на продажу за " + price + " монет"); - - // Отправляем данные о предмете на сервер + player.sendMessage("§aВы выставили " + sellAmount + " шт. за " + price + " монет"); + + // Отправляем данные о продаваемой части на API sendItemDetails(operation.get("id").getAsString(), itemData); }