diff --git a/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/configuration/GUIItemConfiguration.java b/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/configuration/GUIItemConfiguration.java index 20ccd9f..bb866b5 100644 --- a/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/configuration/GUIItemConfiguration.java +++ b/base/gui/src/main/java/cc/carm/lib/easyplugin/gui/configuration/GUIItemConfiguration.java @@ -2,11 +2,14 @@ package cc.carm.lib.easyplugin.gui.configuration; import cc.carm.lib.easyplugin.gui.GUI; import cc.carm.lib.easyplugin.gui.GUIItem; +import cc.carm.lib.easyplugin.utils.ColorParser; import cc.carm.lib.easyplugin.utils.ItemStackFactory; import cc.carm.lib.easyplugin.utils.MessageUtils; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -15,6 +18,8 @@ import java.util.stream.Collectors; public class GUIItemConfiguration { + @Nullable ItemStack original; + @NotNull Material type; int amount; int data; @@ -28,6 +33,15 @@ public class GUIItemConfiguration { @Nullable String name, @NotNull List lore, @NotNull List actions, @NotNull List slots) { + this(null, type, amount, data, name, lore, actions, slots); + } + + public GUIItemConfiguration(@Nullable ItemStack original, + @NotNull Material type, int amount, int data, + @Nullable String name, @NotNull List lore, + @NotNull List actions, + @NotNull List slots) { + this.original = original; this.type = type; this.amount = amount; this.data = data; @@ -38,22 +52,58 @@ public class GUIItemConfiguration { } public void setupItems(Player player, GUI gui) { - ItemStackFactory icon = new ItemStackFactory(this.type, this.amount, this.data); - if (this.name != null) icon.setDisplayName(this.name); - icon.setLore(MessageUtils.setPlaceholders(player, this.lore)); + ItemStack itemStack; + if (original != null) { + ItemStack tmp = original.clone(); + ItemMeta originalMeta = original.getItemMeta(); + if (originalMeta != null) { + if (originalMeta.hasDisplayName()) { + originalMeta.setDisplayName(parseText(player, originalMeta.getDisplayName())); + } + if (originalMeta.getLore() != null) { + originalMeta.setLore(parseTexts(player, originalMeta.getLore())); + } - GUIItem item = new GUIItem(icon.toItemStack()); + } + + tmp.setItemMeta(originalMeta); + itemStack = tmp; + } else { + ItemStackFactory icon = new ItemStackFactory(this.type, this.amount, this.data); + if (this.name != null) { + icon.setDisplayName(parseText(player, this.name)); + } + if (!this.lore.isEmpty()) { + icon.setLore(parseTexts(player, this.lore)); + } + itemStack = icon.toItemStack(); + } + + + GUIItem item = new GUIItem(itemStack); this.actions.stream().map(GUIActionConfiguration::toClickAction).forEach(item::addClickAction); this.slots.forEach(slot -> gui.setItem(slot, item)); } + private List parseTexts(Player player, List lore) { + return ColorParser.parse(MessageUtils.setPlaceholders(player, lore)); + } + + @NotNull + private String parseText(Player player, @NotNull String name) { + return ColorParser.parse(MessageUtils.setPlaceholders(player, name)); + } + public @NotNull Map serialize() { LinkedHashMap map = new LinkedHashMap<>(); + if (original != null) map.put("original", original); + else { + map.put("type", this.type.name()); + if (this.data != 0) map.put("data", this.data); + } - map.put("type", this.type.name()); if (this.name != null) map.put("name", this.name); if (this.amount != 1) map.put("amount", this.amount); - if (this.data != 0) map.put("data", this.data); if (!this.lore.isEmpty()) map.put("lore", this.lore); if (this.slots.size() > 1) { map.put("slots", this.slots); @@ -69,6 +119,10 @@ public class GUIItemConfiguration { @Nullable public static GUIItemConfiguration readFrom(@Nullable ConfigurationSection itemSection) { if (itemSection == null) return null; + + ItemStack original = null; + if (itemSection.contains("original")) original = itemSection.getItemStack("original"); + String material = Optional.ofNullable(itemSection.getString("type")).orElse("STONE"); Material type = Optional.ofNullable(Material.matchMaterial(material)).orElse(Material.STONE); int data = itemSection.getInt("data", 0); @@ -88,7 +142,7 @@ public class GUIItemConfiguration { } return new GUIItemConfiguration( - type, amount, data, name, lore, actions, + original, type, amount, data, name, lore, actions, slots.size() > 0 ? slots : Collections.singletonList(slot) ); }