From bd4e6cb6a81906c147c3f9e1406b3acd86d366b3 Mon Sep 17 00:00:00 2001 From: Driftay Date: Tue, 17 Sep 2019 19:08:23 -0400 Subject: [PATCH] Shop GUI Cleaned up :D --- .../massivecraft/factions/FactionsPlugin.java | 2 - .../massivecraft/factions/shop/CmdShop.java | 2 +- .../factions/shop/ShopClickPersistence.java | 93 -------------- .../massivecraft/factions/shop/ShopGUI.java | 100 --------------- .../factions/shop/ShopGUIFrame.java | 115 ++++++++++++++++++ .../massivecraft/factions/zcore/util/TL.java | 2 +- src/main/resources/config.yml | 15 +-- 7 files changed, 123 insertions(+), 206 deletions(-) delete mode 100644 src/main/java/com/massivecraft/factions/shop/ShopClickPersistence.java delete mode 100644 src/main/java/com/massivecraft/factions/shop/ShopGUI.java create mode 100644 src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java diff --git a/src/main/java/com/massivecraft/factions/FactionsPlugin.java b/src/main/java/com/massivecraft/factions/FactionsPlugin.java index 637a81b4..bce174a6 100755 --- a/src/main/java/com/massivecraft/factions/FactionsPlugin.java +++ b/src/main/java/com/massivecraft/factions/FactionsPlugin.java @@ -16,7 +16,6 @@ import com.massivecraft.factions.integration.Worldguard; import com.massivecraft.factions.integration.dynmap.EngineDynmap; import com.massivecraft.factions.listeners.*; import com.massivecraft.factions.missions.MissionHandler; -import com.massivecraft.factions.shop.ShopClickPersistence; import com.massivecraft.factions.shop.ShopConfig; import com.massivecraft.factions.struct.ChatMode; import com.massivecraft.factions.struct.Relation; @@ -267,7 +266,6 @@ public class FactionsPlugin extends MPlugin { new FactionsExploitListener(), new FactionsBlockListener(), new FUpgradesGUI(), - new ShopClickPersistence(), new UpgradesListener(), new MissionHandler(this), new ChestLogsHandler() diff --git a/src/main/java/com/massivecraft/factions/shop/CmdShop.java b/src/main/java/com/massivecraft/factions/shop/CmdShop.java index a795308d..7add1c30 100644 --- a/src/main/java/com/massivecraft/factions/shop/CmdShop.java +++ b/src/main/java/com/massivecraft/factions/shop/CmdShop.java @@ -24,7 +24,7 @@ public class CmdShop extends FCommand { if (!FactionsPlugin.getInstance().getConfig().getBoolean("F-Shop.Enabled")) { return; } - ShopGUI.openShop(context.fPlayer); + new ShopGUIFrame(context.faction).buildGUI(context.fPlayer); } @Override diff --git a/src/main/java/com/massivecraft/factions/shop/ShopClickPersistence.java b/src/main/java/com/massivecraft/factions/shop/ShopClickPersistence.java deleted file mode 100644 index 9387b0c9..00000000 --- a/src/main/java/com/massivecraft/factions/shop/ShopClickPersistence.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.massivecraft.factions.shop; - -import com.massivecraft.factions.FPlayer; -import com.massivecraft.factions.FPlayers; -import com.massivecraft.factions.FactionsPlugin; -import com.massivecraft.factions.zcore.util.TL; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - -import java.util.List; - -public class ShopClickPersistence implements Listener { - - public static String color(String line) { - line = ChatColor.translateAlternateColorCodes('&', line); - return line; - } - - public static List colorList(List lore) { - for (int i = 0; i <= lore.size() - 1; i++) { - lore.set(i, color(lore.get(i))); - } - return lore; - } - - public void runCommands(List list, Player p) { - for (String cmd : list) { - cmd = cmd.replace("%player%", p.getName()); - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd); - } - } - - @EventHandler - public void click(InventoryClickEvent e) { - Inventory i = e.getClickedInventory(); - Player p = (Player) e.getWhoClicked(); - FileConfiguration config = FactionsPlugin.getInstance().getConfig(); - FPlayer fplayer = FPlayers.getInstance().getByPlayer(p); - - if (e.getView().getTitle().equalsIgnoreCase(color(config.getString("F-Shop.GUI.Name")))) { - ItemStack item = e.getCurrentItem(); - if (item == null) return; - String name = color(item.getItemMeta().getDisplayName()); - e.setCancelled(true); - int t = e.getSlot(); - int items = ShopConfig.getShop().getConfigurationSection("items").getKeys(false).size(); - for (int a = 1; a <= items; a++) { - String s = a + ""; - int slot = ShopConfig.getShop().getInt("items." + s + ".slot"); - if (t == slot) { - String n = ShopConfig.getShop().getString("items." + s + ".name"); - if (name.contains(color(n))) { - String c = ChatColor.stripColor(color(name)); - c = c.replace(ChatColor.stripColor(color(n)), ""); - c = c.replace(color(" ("), ""); - c = c.replace(color(" Points)"), ""); - int cost = Integer.parseInt(c); - if (fplayer.getFaction().getPoints() >= cost) { - - fplayer.getFaction().setPoints(fplayer.getFaction().getPoints() - cost); - runCommands(ShopConfig.getShop().getStringList("items." + s + ".cmds"), fplayer.getPlayer()); - for (FPlayer fplayerBuy : fplayer.getFaction().getFPlayers()) { - // if (fplayer == fme) { continue; } //Idk if I wanna not send the title to the player - fplayerBuy.getPlayer().sendMessage(TL.SHOP_BOUGHT_BROADCAST_FACTION.toString() - .replace("{player}", fplayer.getPlayer().getName()) - .replace("{item}", name)); - } - fplayer.sendMessage(color(ShopConfig.getShop().getString("prefix").replace("%item%", n).replace("%points%", cost + ""))); - p.closeInventory(); - - } else { - fplayer.sendMessage(TL.SHOP_NOT_ENOUGH_POINTS.toString()); - p.closeInventory(); - } - } else { - e.setCancelled(true); - return; - } - } - } - } - } -} - - - diff --git a/src/main/java/com/massivecraft/factions/shop/ShopGUI.java b/src/main/java/com/massivecraft/factions/shop/ShopGUI.java deleted file mode 100644 index d5db5891..00000000 --- a/src/main/java/com/massivecraft/factions/shop/ShopGUI.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.massivecraft.factions.shop; - -import com.massivecraft.factions.FPlayer; -import com.massivecraft.factions.Faction; -import com.massivecraft.factions.FactionsPlugin; -import com.massivecraft.factions.util.XMaterial; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemFlag; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; - -import java.util.LinkedList; -import java.util.List; - -public class ShopGUI { - /* - TODO: OOP Shop, and Clean it Up. - Made simplistic format for shop for the time being until I get time. - */ - - public static void openShop(FPlayer p) { - FileConfiguration config = FactionsPlugin.getInstance().getConfig(); - Faction fac = p.getFaction(); - - Inventory i = Bukkit.createInventory(null, FactionsPlugin.getInstance().getConfig().getInt("F-Shop.GUI.Size"), color(config.getString("F-Shop.GUI.Name"))); - ItemStack glass = new ItemStack(XMaterial.BLACK_STAINED_GLASS_PANE.parseMaterial(), 1, (short) 7); - ItemMeta glassmeta = glass.getItemMeta(); - glassmeta.setDisplayName(ChatColor.GOLD + " "); - glass.setItemMeta(glassmeta); - - for (int fill = 0; fill < FactionsPlugin.getInstance().getConfig().getInt("F-Shop.GUI.Size"); ++fill) { - i.setItem(fill, glass); - } - - int items = ShopConfig.getShop().getConfigurationSection("items").getKeys(false).size(); - for (int shopitems = 1; shopitems <= items; shopitems++) { - String s = shopitems + ""; - int slot = ShopConfig.getShop().getInt("items." + s + ".slot"); - ItemStack material = XMaterial.matchXMaterial(ShopConfig.getShop().getString("items." + s + ".block")).parseItem(); - // int size = ShopConfig.getShop().getInt("items." + s + ".size"); - int cost = ShopConfig.getShop().getInt("items." + s + ".cost"); - String name = ShopConfig.getShop().getString("items." + s + ".name") + " &f(" + cost + " Points)"; - List lore = ShopConfig.getShop().getStringList("items." + s + ".lore"); - String command = ShopConfig.getShop().getString("items." + s + ".cmd"); - String type = ShopConfig.getShop().getString("items." + s + ".type"); - boolean glowing = ShopConfig.getShop().getBoolean("items." + s + ".glowing"); - - - ItemStack count = new ItemStack(XMaterial.PAPER.parseMaterial(), 1); - ItemMeta countmeta = count.getItemMeta(); - countmeta.setDisplayName(color(config.getString("F-Shop.GUI.Information.name"))); - List PointInfo = new LinkedList<>(); - for (String list : config.getStringList("F-Shop.GUI.Information.lore")) { - PointInfo.add(list.replace("%points%", fac.getPoints() + "")); - } - countmeta.setLore(colorList(PointInfo)); - count.setItemMeta(countmeta); - i.setItem(FactionsPlugin.getInstance().getConfig().getInt("F-Shop.GUI.Information.slot"), count); - - ItemStack item = new ItemStack(material); - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(color(name)); - meta.addItemFlags(); - - if (glowing) { - meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); - meta.addEnchant(Enchantment.DURABILITY, 1, true); - } - if (!glowing) { - meta.removeEnchant(Enchantment.DURABILITY); - } - - if (lore.contains("")) { - meta.setLore(null); - } else { - meta.setLore(FactionsPlugin.getInstance().colorList(lore)); - } - item.setItemMeta(meta); - i.setItem(slot, item); - } - p.getPlayer().openInventory(i); - } - - public static String color(String line) { - line = ChatColor.translateAlternateColorCodes('&', line); - return line; - } - - public static List colorList(List lore) { - for (int i = 0; i <= lore.size() - 1; i++) { - lore.set(i, color(lore.get(i))); - } - return lore; - } - -} diff --git a/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java b/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java new file mode 100644 index 00000000..9e94e814 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java @@ -0,0 +1,115 @@ +package com.massivecraft.factions.shop; + +import com.github.stefvanschie.inventoryframework.Gui; +import com.github.stefvanschie.inventoryframework.GuiItem; +import com.github.stefvanschie.inventoryframework.pane.PaginatedPane; +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.FPlayers; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.FactionsPlugin; +import com.massivecraft.factions.util.XMaterial; +import com.massivecraft.factions.zcore.util.TL; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; + +public class ShopGUIFrame { + + private Gui gui; + + public ShopGUIFrame(Faction f) { + gui = new Gui(FactionsPlugin.getInstance(), + FactionsPlugin.getInstance().getConfig().getInt("F-Shop.GUI.Rows", 4), + FactionsPlugin.getInstance().color(FactionsPlugin.getInstance().getConfig().getString("F-Shop.GUI.Name"))); + } + + public void buildGUI(FPlayer fplayer) { + PaginatedPane pane = new PaginatedPane(0, 0, 9, gui.getRows()); + List GUIItems = new ArrayList<>(); + ItemStack dummy = buildDummyItem(fplayer.getFaction()); + for (int x = 0; x <= (gui.getRows() * 9) - 1; x++) GUIItems.add(new GuiItem(dummy, e -> e.setCancelled(true))); + + int items = ShopConfig.getShop().getConfigurationSection("items").getKeys(false).size(); + for (int shopitems = 1; shopitems <= items; shopitems++) { + String s = shopitems + ""; + String name = ShopConfig.getShop().getString("items." + s + ".name"); + int slot = ShopConfig.getShop().getInt("items." + s + ".slot"); + + GUIItems.set(slot, new GuiItem(buildShopAssets(fplayer.getFaction()), e -> { + e.setCancelled(true); + + FPlayer fme = FPlayers.getInstance().getByPlayer((Player) e.getWhoClicked()); + int cost = ShopConfig.getShop().getInt("items." + s + ".cost"); + if (fplayer.getFaction().getPoints() >= cost) { + fplayer.getFaction().setPoints(fplayer.getFaction().getPoints() - cost); + runCommands(ShopConfig.getShop().getStringList("items." + s + ".cmds"), fplayer.getPlayer()); + for (FPlayer fplayerBuy : fplayer.getFaction().getFPlayers()) { + fplayerBuy.getPlayer().sendMessage(TL.SHOP_BOUGHT_BROADCAST_FACTION.toString() + .replace("{player}", fplayer.getPlayer().getName()) + .replace("{item}", ChatColor.stripColor(FactionsPlugin.getInstance().color(name))).replace("{cost}", cost + "")); + } + buildGUI(fme); + } else { + fplayer.msg(TL.SHOP_NOT_ENOUGH_POINTS); + } + })); + pane.populateWithGuiItems(GUIItems); + gui.addPane(pane); + gui.update(); + gui.show(fplayer.getPlayer()); + } + } + + private ItemStack buildShopAssets(Faction f) { + int items = ShopConfig.getShop().getConfigurationSection("items").getKeys(false).size(); + ItemStack item = null; + for (int shopitems = 1; shopitems <= items; shopitems++) { + String s = shopitems + ""; + ConfigurationSection config = ShopConfig.getShop().getConfigurationSection("items." + s); + boolean glowing = config.getBoolean("glowing"); + item = XMaterial.matchXMaterial(config.getString("block")).parseItem(); + ItemMeta meta = item.getItemMeta(); + if (meta != null) { + meta.setLore(FactionsPlugin.getInstance().colorList(config.getStringList("lore"))); + meta.setDisplayName(FactionsPlugin.getInstance().color(config.getString("name"))); + if (glowing) { + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + meta.addEnchant(Enchantment.DURABILITY, 1, true); + } + if (!glowing) { + meta.removeEnchant(Enchantment.DURABILITY); + } + item.setItemMeta(meta); + } + } + return item; + } + + + private ItemStack buildDummyItem(Faction f) { + ConfigurationSection config = FactionsPlugin.getInstance().getConfig().getConfigurationSection("F-Shop.GUI.dummy-item"); + ItemStack item = XMaterial.matchXMaterial(config.getString("Type")).parseItem(); + ItemMeta meta = item.getItemMeta(); + if (meta != null) { + meta.setLore(FactionsPlugin.getInstance().colorList(config.getStringList("Lore"))); + meta.setDisplayName(FactionsPlugin.getInstance().color(config.getString("Name").replace("{points}", f.getPoints() + ""))); + item.setItemMeta(meta); + } + return item; + } + + public void runCommands(List list, Player p) { + for (String cmd : list) { + cmd = cmd.replace("%player%", p.getName()); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd); + } + } +} diff --git a/src/main/java/com/massivecraft/factions/zcore/util/TL.java b/src/main/java/com/massivecraft/factions/zcore/util/TL.java index abe726a4..f7a33bbb 100644 --- a/src/main/java/com/massivecraft/factions/zcore/util/TL.java +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -721,7 +721,7 @@ public enum TL { COMMAND_STRIKESINFO_DESCRIPTION("Get a faction's strikes"), SHOP_NOT_ENOUGH_POINTS("&c&l[!] &7Your faction does not have enough points to purchase this!"), - SHOP_BOUGHT_BROADCAST_FACTION("\n&c&l[!] &e&lFactionShop » &b{player} &7bought &b{item}&7!\n"), + SHOP_BOUGHT_BROADCAST_FACTION("\n&c&l[!] &e&lFactionShop » &b{player} &7bought &b{item}&7 for &b{cost} &7points!\n"), COMMAND_VIEWCHEST_DESCRIPTION("view a factions chest/pv"), diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3ce2c56c..a452fd1d 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -683,15 +683,12 @@ F-Shop: Enabled: true GUI: Name: '&b&lFaction Shop' - Size: 36 - Information: - name: '&b&lInformation' - lore: - - '&bPoints &7are gained by capturing koths/conquests.' - - '' - - '&b&l[!] &bFaction Points: &f%points%' - slot: 22 - + Rows: 4 + dummy-item: + Type: BLACK_STAINED_GLASS_PANE + Name: '&cFaction Points: &b{points}' + Lore: + - ' ' ############################################################ # +------------------------------------------------------+ # # | Faction Command Cooldowns | #