From 8ec16505c4c313b32acfa53f86e43cccd3309c14 Mon Sep 17 00:00:00 2001 From: Driftay Date: Wed, 18 Sep 2019 07:43:18 -0400 Subject: [PATCH] Fixed Shop Items Sowwy :) --- .../massivecraft/factions/cmd/CmdReload.java | 2 + .../factions/shop/ShopConfig.java | 1 + .../factions/shop/ShopGUIFrame.java | 66 ++++++++++--------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdReload.java b/src/main/java/com/massivecraft/factions/cmd/CmdReload.java index 0ae7517e..9e6c4f3c 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdReload.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdReload.java @@ -2,6 +2,7 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Conf; import com.massivecraft.factions.FactionsPlugin; +import com.massivecraft.factions.shop.ShopConfig; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.zcore.util.TL; @@ -19,6 +20,7 @@ public class CmdReload extends FCommand { long timeInitStart = System.currentTimeMillis(); Conf.load(); Conf.save(); + ShopConfig.loadShop(); FactionsPlugin.getInstance().reloadConfig(); FactionsPlugin.getInstance().loadLang(); diff --git a/src/main/java/com/massivecraft/factions/shop/ShopConfig.java b/src/main/java/com/massivecraft/factions/shop/ShopConfig.java index c867b0a8..492d2cbb 100644 --- a/src/main/java/com/massivecraft/factions/shop/ShopConfig.java +++ b/src/main/java/com/massivecraft/factions/shop/ShopConfig.java @@ -40,6 +40,7 @@ public class ShopConfig { getShop().set("items.1.name", "&aTest Shop"); ArrayList lore = new ArrayList(); lore.add("&cFully Customizable Lore!"); + lore.add("&b&l{cost} &7Points"); getShop().set("items.1.lore", lore); ArrayList t = new ArrayList(); t.add("broadcast %player% bought Test Shop!"); diff --git a/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java b/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java index 9e94e814..fe0fa335 100644 --- a/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java +++ b/src/main/java/com/massivecraft/factions/shop/ShopGUIFrame.java @@ -3,6 +3,7 @@ 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.google.common.collect.ImmutableList; import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.FPlayers; import com.massivecraft.factions.Faction; @@ -11,6 +12,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; @@ -19,16 +21,20 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; public class ShopGUIFrame { private Gui gui; + private String s; 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"))); + this.s = s; } public void buildGUI(FPlayer fplayer) { @@ -38,23 +44,46 @@ public class ShopGUIFrame { 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"); + for (int a = 1; a <= items; a++) { + String s = a + ""; int slot = ShopConfig.getShop().getInt("items." + s + ".slot"); + Material material = XMaterial.matchXMaterial(ShopConfig.getShop().getString("items." + s + ".block")).parseMaterial(); + int cost = ShopConfig.getShop().getInt("items." + s + ".cost"); + String name = ShopConfig.getShop().getString("items." + s + ".name"); + boolean glowing = ShopConfig.getShop().getBoolean("items." + s + ".glowing"); + List lore = ShopConfig.getShop().getStringList("items." + s + ".lore"); - GUIItems.set(slot, new GuiItem(buildShopAssets(fplayer.getFaction()), e -> { + + ItemStack item = new ItemStack(material); + 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()); - 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 + "")); + .replace("{item}", ChatColor.stripColor(FactionsPlugin.getInstance().color(name))) + .replace("{cost}", cost + "")); } buildGUI(fme); } else { @@ -68,31 +97,6 @@ public class ShopGUIFrame { } } - 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");