fix amount item sell
This commit is contained in:
@ -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();
|
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()) {
|
||||||
sendOperationError(operation.get("id").getAsString(), "Игрок не в сети");
|
sendOperationError(operation.get("id").getAsString(), "Игрок не в сети");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerInventory inv = player.getInventory();
|
PlayerInventory inv = player.getInventory();
|
||||||
ItemStack item = null;
|
ItemStack item = null;
|
||||||
|
|
||||||
// Получаем предмет из нужного слота
|
// Получаем предмет из нужного слота
|
||||||
if (slotIndex >= 0 && slotIndex <= 35) {
|
if (slotIndex >= 0 && slotIndex <= 35) {
|
||||||
item = inv.getItem(slotIndex);
|
item = inv.getItem(slotIndex);
|
||||||
@ -702,35 +703,69 @@ public final class popa extends JavaPlugin implements Listener {
|
|||||||
} else if (slotIndex == 40) {
|
} else if (slotIndex == 40) {
|
||||||
item = inv.getItemInOffHand();
|
item = inv.getItemInOffHand();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (item == null || item.getType() == Material.AIR) {
|
if (item == null || item.getType() == Material.AIR) {
|
||||||
sendOperationError(operation.get("id").getAsString(), "Предмет не найден в указанном слоте");
|
sendOperationError(operation.get("id").getAsString(), "Предмет не найден в указанном слоте");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Создаем полную копию предмета с метаданными для отправки на сервер
|
int currentAmount = item.getAmount();
|
||||||
JsonObject itemData = itemStackToDetailedJson(item, slotIndex);
|
|
||||||
|
// Валидация количества
|
||||||
// Очищаем слот
|
if (sellAmount <= 0 || sellAmount > currentAmount) {
|
||||||
if (slotIndex >= 0 && slotIndex <= 35) {
|
sendOperationError(operation.get("id").getAsString(),
|
||||||
inv.setItem(slotIndex, null);
|
"Некорректное количество для продажи: " + sellAmount);
|
||||||
} else if (slotIndex == 36) {
|
return;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Отправляем сообщение игроку
|
// Создаём отдельный 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();
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user