|
| 1 | +package me.xapu1337.recodes.trollgui.Handlers; |
| 2 | + |
| 3 | +import com.cryptomorin.xseries.XEnchantment; |
| 4 | +import com.cryptomorin.xseries.XMaterial; |
| 5 | +import me.xapu1337.recodes.trollgui.Cores.Core; |
| 6 | +import org.bukkit.Bukkit; |
| 7 | +import org.bukkit.inventory.ItemFlag; |
| 8 | +import org.bukkit.inventory.ItemStack; |
| 9 | +import org.bukkit.inventory.meta.ItemMeta; |
| 10 | + |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.function.Supplier; |
| 13 | + |
| 14 | +public class TrollItemMetaData { |
| 15 | + |
| 16 | + public boolean isToggable = false; |
| 17 | + |
| 18 | + public boolean isToggled = false; |
| 19 | + |
| 20 | + public String displayName = " - PLACEHOLDER - "; |
| 21 | + |
| 22 | + public String[] lore = { " - PLACEHOLDER - " }; |
| 23 | + |
| 24 | + private XMaterial _item; |
| 25 | + |
| 26 | + private ItemStack _itemStack; |
| 27 | + |
| 28 | + private ItemMeta _itemMeta; |
| 29 | + |
| 30 | + public TrollItemMetaData setToggable(boolean toggable) { |
| 31 | + isToggable = toggable; |
| 32 | + return this; |
| 33 | + } |
| 34 | + |
| 35 | + public TrollItemMetaData setToggled(Supplier<Boolean> toggledCallback){ |
| 36 | + isToggled = toggledCallback.get(); |
| 37 | + |
| 38 | + if (isToggled) |
| 39 | + _itemMeta.addEnchant(XEnchantment.DURABILITY.getEnchant(), 1, true); |
| 40 | + else |
| 41 | + _itemMeta.removeEnchant(XEnchantment.DURABILITY.getEnchant()); |
| 42 | + |
| 43 | + // Append either disabled or enabled text to current lore |
| 44 | + |
| 45 | + lore = Arrays.copyOf(lore, lore.length + 1); |
| 46 | + lore[lore.length - 1] = isToggled ? Core.instance.utils.getConfigPath("MenuItems.trollMenu.extras.isEnabled") : Core.instance.utils.getConfigPath("MenuItems.trollMenu.extras.isDisabled"); |
| 47 | + _itemMeta.setLore(Arrays.asList(lore)); |
| 48 | + |
| 49 | + _itemStack.setItemMeta(_itemMeta); |
| 50 | + |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public TrollItemMetaData setToggled(){ |
| 55 | + isToggled = !isToggled; |
| 56 | + |
| 57 | + if (isToggled) |
| 58 | + _itemMeta.addEnchant(XEnchantment.DURABILITY.getEnchant(), 1, true); |
| 59 | + else |
| 60 | + _itemMeta.removeEnchant(XEnchantment.DURABILITY.getEnchant()); |
| 61 | + |
| 62 | + lore = Arrays.copyOf(lore, lore.length + 1); |
| 63 | + lore[lore.length - 1] = isToggled ? Core.instance.utils.getConfigPath("MenuItems.trollMenu.extras.isEnabled") : Core.instance.utils.getConfigPath("MenuItems.trollMenu.extras.isDisabled"); |
| 64 | + _itemMeta.setLore(Arrays.asList(lore)); |
| 65 | + |
| 66 | + _itemStack.setItemMeta(_itemMeta); |
| 67 | + |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + public TrollItemMetaData setDisplayName(String displayName){ |
| 72 | + this.displayName = displayName; |
| 73 | + |
| 74 | + this._itemMeta.setDisplayName(displayName); |
| 75 | + |
| 76 | + _itemStack.setItemMeta(_itemMeta); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + public TrollItemMetaData setLore(String ...lore){ |
| 81 | + this.lore = lore; |
| 82 | + |
| 83 | + _itemMeta.setLore(Arrays |
| 84 | + .asList( |
| 85 | + |
| 86 | + lore |
| 87 | + |
| 88 | + ) |
| 89 | + ); |
| 90 | + |
| 91 | + _itemStack.setItemMeta(_itemMeta); |
| 92 | + |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public TrollItemMetaData setConfigData(String itemConfigName){ |
| 97 | + try { |
| 98 | + this.setDisplayName(Core.instance.utils.getConfigPath("MenuItems.trollMenu." + itemConfigName + ".name")); |
| 99 | + this.setLore(Core.instance.utils.getConfigPath("MenuItems.trollMenu." + itemConfigName + ".lore").split("\\|")); |
| 100 | + } catch (NullPointerException e) { |
| 101 | + Bukkit.getLogger().warning("[TrollGUI] Could not find config data for item: " + itemConfigName + " (either .name or .lore is missing)"); |
| 102 | + e.printStackTrace(); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + public TrollItemMetaData setItem(XMaterial item){ |
| 111 | + _item = item; |
| 112 | + |
| 113 | + _itemStack = new ItemStack(item.parseMaterial(), 1); |
| 114 | + |
| 115 | + _itemMeta = _itemStack.getItemMeta(); |
| 116 | + |
| 117 | + _itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); |
| 118 | + _itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS); |
| 119 | + _itemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS); |
| 120 | + |
| 121 | + _itemStack.setItemMeta(_itemMeta); |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + // A function to replace placeholders in the current lore with values |
| 126 | + public TrollItemMetaData formatPlaceholders(String placeholders, String ...values){ |
| 127 | + String[] lore = this.lore; |
| 128 | + for (int i = 0; i < lore.length; i++) { |
| 129 | + lore[i] = lore[i].replace(placeholders, values[i]); |
| 130 | + } |
| 131 | + _itemMeta.setLore(Arrays.asList(lore)); |
| 132 | + _itemStack.setItemMeta(_itemMeta); |
| 133 | + |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + // Getters |
| 139 | + public XMaterial getItem(){ |
| 140 | + return _item; |
| 141 | + } |
| 142 | + |
| 143 | + public ItemStack getItemStack(){ |
| 144 | + return _itemStack; |
| 145 | + } |
| 146 | + |
| 147 | + public ItemMeta getItemMeta(){ |
| 148 | + return _itemMeta; |
| 149 | + } |
| 150 | +} |
0 commit comments