From be3798e7ccb2109e41bb03fec79410a0a50968c5 Mon Sep 17 00:00:00 2001 From: Trixkz Date: Mon, 29 Nov 2021 18:10:34 -0500 Subject: [PATCH] t --- .../menus/shop/weapons/WeaponsButton.java | 138 ++++++++++++++++++ .../menus/shop/weapons/WeaponsMenu.java | 53 +++++++ 2 files changed, 191 insertions(+) create mode 100644 src/main/java/rip/tilly/bedwars/menus/shop/weapons/WeaponsButton.java create mode 100644 src/main/java/rip/tilly/bedwars/menus/shop/weapons/WeaponsMenu.java diff --git a/src/main/java/rip/tilly/bedwars/menus/shop/weapons/WeaponsButton.java b/src/main/java/rip/tilly/bedwars/menus/shop/weapons/WeaponsButton.java new file mode 100644 index 0000000..fa1759b --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/menus/shop/weapons/WeaponsButton.java @@ -0,0 +1,138 @@ +package rip.tilly.bedwars.menus.shop.weapons; + +import lombok.AllArgsConstructor; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.inventory.ItemStack; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.playerdata.PlayerData; +import rip.tilly.bedwars.utils.CC; +import rip.tilly.bedwars.utils.ItemBuilder; +import rip.tilly.bedwars.utils.PlayerUtil; +import rip.tilly.bedwars.utils.menu.Button; + +import java.util.ArrayList; +import java.util.List; + +@AllArgsConstructor +public class WeaponsButton extends Button { + + private String name; + private Material material; + private int data; + private int amount; + private Material costType; + private String costTypeName; + private ChatColor costTypeColor; + private int cost; + private boolean color; + + @Override + public ItemStack getButtonItem(Player player) { + List lore = new ArrayList<>(); + lore.add(""); + lore.add("&9Cost: " + costTypeColor + cost + " " + costTypeName); + lore.add("&9Amount: &e" + amount + "x"); + + int costItems = 0; + for (ItemStack contents : player.getInventory().getContents()) { + if (contents != null) { + if (contents.getType() == costType) { + costItems += contents.getAmount(); + } + } + } + + lore.add(" "); + lore.add(costItems >= cost ? "&aClick to purchase" : "&cYou don't have enough " + costTypeName); + + PlayerData playerData = BedWars.getInstance().getPlayerDataManager().getPlayerData(player.getUniqueId()); + + if (color) { + return new ItemBuilder(material) + .name((costItems >= cost ? "&a" : "&c") + name) + .lore(lore) + .amount(amount) + .durability(playerData.getPlayerTeam().getColorData()) + .hideFlags() + .build(); + } else { + return new ItemBuilder(material) + .name((costItems >= cost ? "&a" : "&c") + name) + .lore(lore) + .amount(amount) + .durability(data) + .hideFlags() + .build(); + } + } + + @Override + public void clicked(Player player, int slot, ClickType clickType, int hotbarButton) { + if (clickType.isShiftClick()) { + return; + } + + int costItems = 0; + + for (ItemStack contents : player.getInventory().getContents()) { + if (contents != null) { + if (contents.getType() == costType) { + costItems += contents.getAmount(); + } + } + } + + if (costItems < cost) { + player.sendMessage(CC.translate("&cYou don't have enough " + costTypeName)); + + playFail(player); + + return; + } + + PlayerData playerData = BedWars.getInstance().getPlayerDataManager().getPlayerData(player.getUniqueId()); + + int finalCost = cost; + + for (ItemStack i : player.getInventory().getContents()) { + if (i == null) { + continue; + } + + if (i.getType() == costType) { + if (i.getAmount() < finalCost) { + finalCost -= i.getAmount(); + + PlayerUtil.minusAmount(player, i, i.getAmount()); + + player.updateInventory(); + } else { + PlayerUtil.minusAmount(player, i, finalCost); + + player.updateInventory(); + + break; + } + } + } + + if (color) { + player.getInventory().addItem(new ItemBuilder(material) + .amount(amount) + .durability(playerData.getPlayerTeam().getColorData()) + .hideFlags() + .build()); + } else { + player.getInventory().addItem(new ItemBuilder(material) + .amount(amount) + .durability(data) + .hideFlags() + .build()); + } + + playNeutral(player); + } +} diff --git a/src/main/java/rip/tilly/bedwars/menus/shop/weapons/WeaponsMenu.java b/src/main/java/rip/tilly/bedwars/menus/shop/weapons/WeaponsMenu.java new file mode 100644 index 0000000..12cb89c --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/menus/shop/weapons/WeaponsMenu.java @@ -0,0 +1,53 @@ +package rip.tilly.bedwars.menus.shop.weapons; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.menus.shop.ShopType; +import rip.tilly.bedwars.menus.shop.ShopTypeButton; +import rip.tilly.bedwars.utils.CC; +import rip.tilly.bedwars.utils.ItemBuilder; +import rip.tilly.bedwars.utils.menu.Button; +import rip.tilly.bedwars.utils.menu.Menu; +import java.util.HashMap; +import java.util.Map; + +public class WeaponsMenu extends Menu { + + private BedWars main = BedWars.getInstance(); + + @Override + public boolean isUpdateAfterClick() { + return true; + } + + @Override + public String getTitle(Player player) { + return CC.translate("&eClick a weapon to purchase..."); + } + + @Override + public Map getButtons(Player player) { + Map buttons = new HashMap<>(); + + for (ShopType types : ShopType.values()) { + buttons.put(types.getSlot(), new ShopTypeButton(types)); + } + + buttons.put(ShopType.WEAPONS.getSlot() + 9, Button.placeholder(Material.STAINED_GLASS_PANE, (byte) 5, "")); + buttons.put(19, new WeaponsButton("Stone Sword", Material.STONE_SWORD, 0, 1, Material.IRON_INGOT, "Iron", ChatColor.WHITE, 10, true)); + buttons.put(20, new WeaponsButton("Iron Sword", Material.IRON_SWORD, 0, 1, Material.GOLD_INGOT, "Gold", ChatColor.GOLD, 7, true)); + buttons.put(21, new WeaponsButton("Diamond Sword", Material.DIAMOND_SWORD, 0, 1, Material.EMERALD, "Emerald", ChatColor.DARK_GREEN, 3, true)); + buttons.put(22, new WeaponsButton("Knockback Stick", Material.STICK, 0, 1, Material.GOLD_INGOT, "Gold", ChatColor.GOLD, 5, true)); + + this.fillEmptySlots(buttons, new ItemBuilder(Material.STAINED_GLASS_PANE).durability(7).name("").build()); + + return buttons; + } + + @Override + public int getSize() { + return 54; + } +}