From 71a099beeb84fd8d99473fc32ba300de859d3498 Mon Sep 17 00:00:00 2001 From: Juniormunk Date: Thu, 21 May 2020 19:29:51 -0400 Subject: [PATCH] Fix bugs #123, #128 --- .../massivecraft/factions/cmd/FCmdRoot.java | 2 + .../factions/shop/ShopGUIFrame.java | 271 +++++++++++++----- .../factions/zcore/file/CustomFile.java | 4 + .../massivecraft/factions/zcore/util/TL.java | 1 + 4 files changed, 202 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java index 0edd6154..3df6087f 100644 --- a/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java +++ b/src/main/java/com/massivecraft/factions/cmd/FCmdRoot.java @@ -26,6 +26,7 @@ import com.massivecraft.factions.discord.CmdInviteBot; import com.massivecraft.factions.discord.CmdSetGuild; import com.massivecraft.factions.missions.CmdMissions; import com.massivecraft.factions.shop.CmdShop; +import com.massivecraft.factions.shop.ShopGUIFrame; import com.massivecraft.factions.zcore.util.TL; import me.lucko.commodore.CommodoreProvider; import org.bukkit.Bukkit; @@ -377,6 +378,7 @@ public class FCmdRoot extends FCommand implements CommandExecutor { } if (FactionsPlugin.getInstance().getConfig().getBoolean("F-Shop.Enabled", false) && !fShopEnabled) { this.addSubCommand(this.cmdShop); + new ShopGUIFrame(null).checkShopConfig(); fShopEnabled = true; } if (FactionsPlugin.getInstance().getConfig().getBoolean("f-inventory-see.Enabled", false) && !invSeeEnabled) { diff --git a/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java b/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java index 5f7667c4..3e79320a 100644 --- a/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java +++ b/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java @@ -11,6 +11,7 @@ import com.massivecraft.factions.util.XMaterial; import com.massivecraft.factions.zcore.util.TL; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; @@ -20,92 +21,210 @@ import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.logging.Level; import java.util.stream.Collectors; -public class ShopGUIFrame { +public class ShopGUIFrame +{ - /** - * @author Driftay - */ + /** + * @author Driftay + */ - private Gui gui; + 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 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))); + 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 = FactionsPlugin.getInstance().getFileManager().getShop().getConfig().getConfigurationSection("items").getKeys(false).size(); - for (int a = 1; a <= items; a++) { - String s = a + ""; - int slot = FactionsPlugin.getInstance().getFileManager().getShop().fetchInt("items." + s + ".slot"); - ItemStack item = XMaterial.matchXMaterial(FactionsPlugin.getInstance().getFileManager().getShop().fetchString("items." + s + ".block")).get().parseItem(); - int cost = FactionsPlugin.getInstance().getFileManager().getShop().fetchInt("items." + s + ".cost"); - String name = FactionsPlugin.getInstance().getFileManager().getShop().fetchString("items." + s + ".name"); - boolean glowing = FactionsPlugin.getInstance().getFileManager().getShop().fetchBoolean("items." + s + ".glowing"); - List lore = FactionsPlugin.getInstance().getFileManager().getShop().fetchStringList("items." + s + ".lore"); + Set items = FactionsPlugin.getInstance().getFileManager().getShop().getConfig().getConfigurationSection("items").getKeys(false); + for (String s : items) + { + if (!checkShopConfig(s)) + { + continue; + } + int slot = FactionsPlugin.getInstance().getFileManager().getShop().fetchInt("items." + s + ".slot"); + ItemStack item = XMaterial.matchXMaterial(FactionsPlugin.getInstance().getFileManager().getShop().fetchString("items." + s + ".block")).get().parseItem(); + int cost = FactionsPlugin.getInstance().getFileManager().getShop().fetchInt("items." + s + ".cost"); + String name = FactionsPlugin.getInstance().getFileManager().getShop().fetchString("items." + s + ".name"); + boolean glowing = FactionsPlugin.getInstance().getFileManager().getShop().fetchBoolean("items." + s + ".glowing"); + List lore = FactionsPlugin.getInstance().getFileManager().getShop().fetchStringList("items." + s + ".lore"); - assert item != null; - ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(FactionsPlugin.instance.color(name)); - meta.addItemFlags(); - if (glowing) { - meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); - meta.addEnchant(Enchantment.DURABILITY, 1, true); - } - if (!glowing) meta.removeEnchant(Enchantment.DURABILITY); - List replacedLore = lore.stream().map(t -> t.replace("{cost}", cost + "")).collect(Collectors.toList()); - meta.setLore(FactionsPlugin.instance.colorList(replacedLore)); - item.setItemMeta(meta); - GUIItems.set(slot, new GuiItem(item, e -> { - e.setCancelled(true); - FPlayer fme = FPlayers.getInstance().getByPlayer((Player) e.getWhoClicked()); - if (fplayer.getFaction().getPoints() >= cost) { - fplayer.getFaction().setPoints(fplayer.getFaction().getPoints() - cost); - runCommands(FactionsPlugin.getInstance().getFileManager().getShop().fetchStringList("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()); - } - } + assert item != null; + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(FactionsPlugin.instance.color(name)); + meta.addItemFlags(); + if (glowing) + { + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + meta.addEnchant(Enchantment.DURABILITY, 1, true); + } + if (!glowing) + meta.removeEnchant(Enchantment.DURABILITY); + List replacedLore = lore.stream().map(t -> t.replace("{cost}", cost + "")).collect(Collectors.toList()); + meta.setLore(FactionsPlugin.instance.colorList(replacedLore)); + item.setItemMeta(meta); + GUIItems.set(slot, new GuiItem(item, e -> + { + e.setCancelled(true); + FPlayer fme = FPlayers.getInstance().getByPlayer((Player) e.getWhoClicked()); + if (fplayer.getFaction().getPoints() >= cost) + { + if (runCommands(FactionsPlugin.getInstance().getFileManager().getShop().fetchStringList("items." + s + ".cmds"), fplayer.getPlayer())) + { + fplayer.getFaction().setPoints(fplayer.getFaction().getPoints() - cost); + 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_ERROR_DURING_PURCHASE); + } + } + else + { + fplayer.msg(TL.SHOP_NOT_ENOUGH_POINTS); + } + })); + pane.populateWithGuiItems(GUIItems); + gui.addPane(pane); + gui.update(); + gui.show(fplayer.getPlayer()); + } + } + private ItemStack buildDummyItem(Faction f) + { + ConfigurationSection config = FactionsPlugin.getInstance().getConfig().getConfigurationSection("F-Shop.GUI.dummy-item"); + ItemStack item = XMaterial.matchXMaterial(config.getString("Type")).get().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; + } - private ItemStack buildDummyItem(Faction f) { - ConfigurationSection config = FactionsPlugin.getInstance().getConfig().getConfigurationSection("F-Shop.GUI.dummy-item"); - ItemStack item = XMaterial.matchXMaterial(config.getString("Type")).get().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 boolean checkShopConfig() + { + boolean ret = true; + Set items = FactionsPlugin.getInstance().getFileManager().getShop().getConfig().getConfigurationSection("items").getKeys(false); + for (String s : items) + { + if (checkShopConfig(s) == false) + { + ret = false; + } + } + return ret; + } - public void runCommands(List list, Player p) { - for (String cmd : list) { - cmd = cmd.replace("%player%", p.getName()); - Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd); - } - } + public boolean checkShopConfig(String s) + { + boolean ret = true; + if (!FactionsPlugin.getInstance().getFileManager().getShop().containsKey("items." + s + ".slot")) + { + FactionsPlugin.getInstance().log(Level.WARNING, "Problem with config item \'" + s + "\' missing slot variable"); + ret = false; + } + if (!FactionsPlugin.getInstance().getFileManager().getShop().containsKey("items." + s + ".block")) + { + FactionsPlugin.getInstance().log(Level.WARNING, "Problem with config item \'" + s + "\' missing block variable"); + ret = false; + } + + if (!FactionsPlugin.getInstance().getFileManager().getShop().containsKey("items." + s + ".cmds")) + { + FactionsPlugin.getInstance().log(Level.WARNING, "Problee with config item \'" + s + "\' missing cmds variable"); + ret = false; + } + + if (!FactionsPlugin.getInstance().getFileManager().getShop().containsKey("items." + s + ".cost")) + { + FactionsPlugin.getInstance().log(Level.WARNING, "Problem with config item \'" + s + "\' missing cost variable this item will cost 0"); + } + if (!FactionsPlugin.getInstance().getFileManager().getShop().containsKey("items." + s + ".name")) + { + FactionsPlugin.getInstance().log(Level.WARNING, "Problem with config item \'" + s + "\' missing name variable"); + ret = false; + } + return ret; + } + + /** + * + * @param list + * The list of commands to be ran. + * @param p + * The player that is using the shop + * @return if all commands are able to be ran or if they did run. + */ + public boolean runCommands(List list, Player p) + { + for (String cmd : list) + { + cmd = cmd.replace("%player%", p.getName()); + if (cmd.toLowerCase().startsWith("give")) + { + String[] args = cmd.split(" "); + if (args.length == 4) + { + Material material = Material.matchMaterial(args[2]); + int amount = Integer.parseInt(args[3]); + Player player = Bukkit.getPlayer(args[1]); + if (!player.isOnline()) + { + return false; + } + // See if the player has this item in their inventory; + if (player.getInventory().contains(material) && player.getInventory().firstEmpty() < 0) + { + int spacesAvailable = 0; + Map contents = player.getInventory().all(material); + for (ItemStack stack : contents.values()) + { + spacesAvailable += stack.getMaxStackSize() - stack.getAmount(); + } + if (spacesAvailable < amount) + { + return false; + } + + } + else + { + if (player.getInventory().firstEmpty() < 0) + { + return false; + } + } + } + } + } + for (String cmd : list) + { + cmd = cmd.replace("%player%", p.getName()); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd); + } + return true; + } } diff --git a/src/main/java/com/massivecraft/factions/zcore/file/CustomFile.java b/src/main/java/com/massivecraft/factions/zcore/file/CustomFile.java index c5c87c17..69debea6 100644 --- a/src/main/java/com/massivecraft/factions/zcore/file/CustomFile.java +++ b/src/main/java/com/massivecraft/factions/zcore/file/CustomFile.java @@ -55,6 +55,10 @@ public class CustomFile { e.printStackTrace(); } } + + public boolean containsKey(String key) { + return getCachedObjects().containsKey(key)|| getConfig().contains(key); + } public String fetchString(String key) { return (String) getObj(key, dataTypes.STRING); 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 67c72b05..08daab0f 100644 --- a/src/main/java/com/massivecraft/factions/zcore/util/TL.java +++ b/src/main/java/com/massivecraft/factions/zcore/util/TL.java @@ -824,6 +824,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_ERROR_DURING_PURCHASE("&c&l[!] &7There was an error while trying to give items please check your inventory! Purchase was not completed!"), SHOP_BOUGHT_BROADCAST_FACTION("\n&c&l[!] &e&lFactionShop ยป &b{player} &7bought &b{item}&7 for &b{cost} &7points!\n"),