Add GUI option to collect all sales balance

This commit is contained in:
Len 2023-08-13 18:28:27 +02:00
parent 440ab3dae5
commit c6e2709e6c
2 changed files with 41 additions and 2 deletions

View File

@ -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,

View File

@ -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<PlayerShop> 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 ? "<green>You have collected <balance> from <count> shops." : "<red>No shops to be collected from.", placeholders);
return;
}
if (slot >= 45)
return;