From c6e2709e6c3abc534e1317f0862c94bfffaea2cc Mon Sep 17 00:00:00 2001 From: Len <40720638+destro174@users.noreply.github.com> Date: Sun, 13 Aug 2023 18:28:27 +0200 Subject: [PATCH] Add GUI option to collect all sales balance --- .../com/alttd/playershops/gui/GuiIcon.java | 1 + .../alttd/playershops/gui/ListShopsGui.java | 42 ++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alttd/playershops/gui/GuiIcon.java b/src/main/java/com/alttd/playershops/gui/GuiIcon.java index 02a9f96..3aeccc2 100644 --- a/src/main/java/com/alttd/playershops/gui/GuiIcon.java +++ b/src/main/java/com/alttd/playershops/gui/GuiIcon.java @@ -29,6 +29,7 @@ public enum GuiIcon { MANAGE_SHOP_TYPE, MANAGE_SHOP_AMOUNT, MANAGE_SHOP_PRICE, + MANAGE_SHOP_COLLECT_SALES, HOME_LIST_OWN_SHOPS, HOME_LIST_PLAYERS, diff --git a/src/main/java/com/alttd/playershops/gui/ListShopsGui.java b/src/main/java/com/alttd/playershops/gui/ListShopsGui.java index e66c6e6..7327cac 100644 --- a/src/main/java/com/alttd/playershops/gui/ListShopsGui.java +++ b/src/main/java/com/alttd/playershops/gui/ListShopsGui.java @@ -3,12 +3,13 @@ package com.alttd.playershops.gui; import com.alttd.playershops.PlayerShops; import com.alttd.playershops.config.MessageConfig; import com.alttd.playershops.shop.PlayerShop; +import com.alttd.playershops.shop.ShopType; +import com.alttd.playershops.utils.EconomyUtils; import com.alttd.playershops.utils.ShopUtil; import com.alttd.playershops.utils.Util; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -19,9 +20,11 @@ import java.util.UUID; public class ListShopsGui extends AbstractGui { List shops; + UUID playerToList; public ListShopsGui(UUID uuid, UUID playerToList) { super(uuid); + this.playerToList = playerToList; this.shops = PlayerShops.getInstance().getShopHandler().getShops(playerToList); OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(playerToList); @@ -29,8 +32,17 @@ public class ListShopsGui extends AbstractGui { Placeholder.unparsed("playername", offlinePlayer.hasPlayedBefore() ? offlinePlayer.getName() : "error") ); this.inventory = Bukkit.createInventory(this, INV_SIZE, Util.parseMiniMessage(MessageConfig.GUI_LIST_SHOPS_TITLE, placeholders)); - initInvContents(); makeMenuBar(); + initInvContents(); + } + + @Override + void makeMenuBar() { + super.makeMenuBar(); + if (!uuid.equals(playerToList) && getPlayer().hasPermission("playershops.shop.collectall")) + return; + + inventory.setItem(GuiIcon.MANAGE_SHOP_COLLECT_SALES.getSlot(), GuiIcon.MANAGE_SHOP_COLLECT_SALES.getItemStack()); } @Override @@ -61,6 +73,32 @@ public class ListShopsGui extends AbstractGui { public void onClick(int slot, ItemStack item) { super.onClick(slot, item); + if (slot == GuiIcon.MANAGE_SHOP_COLLECT_SALES.getSlot() && GuiIcon.MANAGE_SHOP_COLLECT_SALES.getItemStack().equals(item)) { + Player player = getPlayer(); + double d = 0; + int count = 0; + for (PlayerShop playerShop : shops) { + if (!ShopUtil.canManageShop(player, playerShop)) + continue; + if (!playerShop.getType().equals(ShopType.SELL)) + continue; + + double balance = playerShop.getBalance(); + if (balance == 0) + continue; + playerShop.removeBalance(balance); + EconomyUtils.addFunds(player, balance); + d += balance; + count++; + } + TagResolver placeholders = TagResolver.resolver( + Placeholder.parsed("balance", String.valueOf(d)), + Placeholder.parsed("count", String.valueOf(count)) + ); + player.sendMiniMessage(count > 0 ? "You have collected from shops." : "No shops to be collected from.", placeholders); + return; + } + if (slot >= 45) return;