From 72c734b138a9f213ba1d1b2ea060c7d1471abd67 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Tue, 21 Dec 2021 03:17:58 +0100 Subject: [PATCH] Added sell message, added selling items --- .../java/com/alttd/GUI/windows/SellGUI.java | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alttd/GUI/windows/SellGUI.java b/src/main/java/com/alttd/GUI/windows/SellGUI.java index 02b7cc6..de93232 100644 --- a/src/main/java/com/alttd/GUI/windows/SellGUI.java +++ b/src/main/java/com/alttd/GUI/windows/SellGUI.java @@ -16,8 +16,12 @@ import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.meta.ItemMeta; +import java.util.Arrays; +import java.util.Objects; + public class SellGUI extends GUIMerchant { private static final MiniMessage miniMessage = MiniMessage.get(); @@ -39,22 +43,52 @@ public class SellGUI extends GUIMerchant { } private void sell(VillagerType villagerType, Player player, Material material, int amount, Price price) { + PlayerInventory inventory = player.getInventory(); + + if (!inventory.containsAtLeast(new ItemStack(material), amount)) + { + player.sendMessage(miniMessage.parse(Config.NOT_ENOUGH_ITEMS, + Template.of("type", material.name()), + Template.of("amount",String.valueOf(amount)))); + return; + } + Economy econ = VillagerUI.getInstance().getEconomy(); EconUser econUser = EconUser.getUser(player.getUniqueId()); - int oldPoints = econUser.getPointsMap().get(villagerType.getName()); + int oldPoints = Objects.requireNonNullElse(econUser.getPointsMap().get(villagerType.getName()), 0); int trans_pts = (int) (Math.floor(price.getPrice(amount)/ WorthConfig.POINT_MOD) * amount); double cost = price.calculatePriceThing(oldPoints, trans_pts); econ.depositPlayer(player, cost); econUser.addPoints(villagerType.getName(), -price.getPoints()); + var ref = new Object() { + int tmpAmount = amount; + }; + Arrays.stream(inventory.getContents()) + .filter(Objects::nonNull) + .filter(itemStack -> itemStack.getType().equals(material)) + .forEach(itemStack -> { + if (ref.tmpAmount == 0) + return; + if (itemStack.getAmount() > ref.tmpAmount) + { + itemStack.setAmount(itemStack.getAmount() - ref.tmpAmount); + ref.tmpAmount = 0; + } else { + ref.tmpAmount -= itemStack.getAmount(); + itemStack.setAmount(0); + } + }); + //TODO remove items from inv player.sendMessage(MiniMessage.get().parse(Config.PURCHASED_ITEM, Template.of("amount", String.valueOf(amount)), Template.of("item", material.toString()), - Template.of("price", String.valueOf(price)))); + Template.of("price", String.valueOf(cost)))); - Bukkit.getServer().getPluginManager() - .callEvent(new SpawnShopEvent(player, amount, cost, material, - oldPoints, econUser.getPointsMap().get(villagerType.getName()), false)); +// Bukkit.getServer().getPluginManager() +// .callEvent(new SpawnShopEvent(player, amount, cost, material, +// oldPoints, econUser.getPointsMap().get(villagerType.getName()), false)); + //TODO FIX LOGGING } private ItemStack getPriceItem(double price) {