fix amount item sell

This commit is contained in:
2025-12-09 06:38:56 +05:00
parent 4e5e6d1cf7
commit 9b77bbe9a6

View File

@ -675,9 +675,10 @@ public final class popa extends JavaPlugin implements Listener {
/** /**
* Обрабатывает операцию продажи предмета * Обрабатывает операцию продажи предмета
*/ */
private void handleSellOperation(JsonObject operation) { void handleSellOperation(JsonObject operation) {
String playerName = operation.get("player_name").getAsString(); String playerName = operation.get("player_name").getAsString();
int slotIndex = operation.get("slot_index").getAsInt(); int slotIndex = operation.get("slot_index").getAsInt();
int sellAmount = operation.get("amount").getAsInt(); // 👈 берём количество из операции
Player player = Bukkit.getPlayer(playerName); Player player = Bukkit.getPlayer(playerName);
if (player == null || !player.isOnline()) { if (player == null || !player.isOnline()) {
@ -708,10 +709,25 @@ public final class popa extends JavaPlugin implements Listener {
return; return;
} }
// Создаем полную копию предмета с метаданными для отправки на сервер int currentAmount = item.getAmount();
JsonObject itemData = itemStackToDetailedJson(item, slotIndex);
// Очищаем слот // Валидация количества
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) { if (slotIndex >= 0 && slotIndex <= 35) {
inv.setItem(slotIndex, null); inv.setItem(slotIndex, null);
} else if (slotIndex == 36) { } else if (slotIndex == 36) {
@ -725,12 +741,31 @@ public final class popa extends JavaPlugin implements Listener {
} else if (slotIndex == 40) { } else if (slotIndex == 40) {
inv.setItemInOffHand(null); 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(); int price = operation.get("price").getAsInt();
player.sendMessage("§aВы выставили предмет на продажу за " + price + " монет"); player.sendMessage("§aВы выставили " + sellAmount + " шт. за " + price + " монет");
// Отправляем данные о предмете на сервер // Отправляем данные о продаваемой части на API
sendItemDetails(operation.get("id").getAsString(), itemData); sendItemDetails(operation.get("id").getAsString(), itemData);
} }