From 152eebcfe6d56a252b47e39168b59d9e98750b5e Mon Sep 17 00:00:00 2001 From: jakub Date: Sun, 8 Jun 2025 22:30:06 +0200 Subject: [PATCH] ik --- .../soukup/ecoCraftCore/events/ShopLogic.java | 61 ++++++++++++++++++- .../ecoCraftCore/objects/VirtualChest.java | 2 +- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/soukup/ecoCraftCore/events/ShopLogic.java b/src/main/java/xyz/soukup/ecoCraftCore/events/ShopLogic.java index 00c7906..c4cf277 100644 --- a/src/main/java/xyz/soukup/ecoCraftCore/events/ShopLogic.java +++ b/src/main/java/xyz/soukup/ecoCraftCore/events/ShopLogic.java @@ -44,6 +44,11 @@ public class ShopLogic implements Listener { return; } + if (VirtualChestLogic.openedChests.containsKey(id)){ + event.getPlayer().sendPlainMessage("to by neslo"); + return; + } + Shop shop = Shop.findById(id); if (shop == null){ @@ -102,11 +107,21 @@ public class ShopLogic implements Listener { .name(Converter.toComponent("&eProdat " + amount + "ks za " + String.format("%.2f", sellPrice))) .asGuiItem(); + one.setAction(event -> { + event.setCancelled(true); + ShopLogic.sell(shop, player, 1); + }); + GuiItem eight = ItemBuilder .from(Material.YELLOW_WOOL) .name(Converter.toComponent("&eProdat " + amount*8 + "ks za " + String.format("%.2f", sellPrice*8))) .asGuiItem(); + eight.setAction(event -> { + event.setCancelled(true); + ShopLogic.sell(shop, player, 8); + }); + gui.setItem(7, one); gui.setItem(8, eight); @@ -153,10 +168,54 @@ public class ShopLogic implements Listener { return; } + playerInventory.addItem(itemStack); + + virtualChest.save(); + + Transaction transaction = new Transaction(price, "player", player.getName(), "player", shop.getOwner(), "playerShop", shop.getItemName() ,Integer.toString(amount)); + + transaction.process(); + + } + + public static void sell(Shop shop, Player player, int multiplier){ + int amount = shop.getAmount() * multiplier; + float price = shop.getPriceSell() * multiplier; + ItemStack itemStack = shop.getItemStack(); + itemStack.setAmount(amount); + + Account account = Account.getOrCreate(shop.getOwnerType(), shop.getOwner()); + + if (price > account.getBalance()){ + player.sendPlainMessage("shop nemá mony"); + return; + } + + VirtualChest virtualChest = VirtualChest.findById(shop.getVirtualChestID()); + + if (virtualChest == null){ + return; + } + + Inventory playerInventory = player.getInventory(); + + if (InventoryUtils.getCount(playerInventory, itemStack) < amount){ + player.sendPlainMessage("nemáš prodejní itemy"); + return; + } + + if (!virtualChest.addItem(itemStack)){ + player.sendPlainMessage("není dost místa v chestce"); + return; + } + playerInventory.removeItem(itemStack); - new Transaction(price, "player", player.getName(), "player", shop.getOwner(), "playerShop", shop.getItemName() ,Integer.toString(amount)); + virtualChest.save(); + + Transaction transaction = new Transaction(price, "player", shop.getOwner(), "player", player.getName(), "playerShop", shop.getItemName() ,Integer.toString(amount)); + transaction.process(); } } diff --git a/src/main/java/xyz/soukup/ecoCraftCore/objects/VirtualChest.java b/src/main/java/xyz/soukup/ecoCraftCore/objects/VirtualChest.java index 0d557bb..8cb472b 100644 --- a/src/main/java/xyz/soukup/ecoCraftCore/objects/VirtualChest.java +++ b/src/main/java/xyz/soukup/ecoCraftCore/objects/VirtualChest.java @@ -82,7 +82,7 @@ public class VirtualChest { } public boolean addItem(ItemStack itemStack){ - int spaceLeft = InventoryUtils.getSpaceLeft(this.inventory, itemStack); + int spaceLeft = InventoryUtils.getSpaceLeft(getInventory(), itemStack); if (spaceLeft < itemStack.getAmount()){ return false; }