|
| 1 | +package me.flame.menus.builders.items; |
| 2 | + |
| 3 | +import me.flame.menus.items.MenuItem; |
| 4 | +import me.flame.menus.util.ItemResponse; |
| 5 | +import me.flame.menus.util.VersionHelper; |
| 6 | + |
| 7 | +import org.bukkit.Material; |
| 8 | +import org.bukkit.enchantments.Enchantment; |
| 9 | +import org.bukkit.entity.Damageable; |
| 10 | +import org.bukkit.inventory.ItemFlag; |
| 11 | +import org.bukkit.inventory.ItemStack; |
| 12 | +import org.bukkit.inventory.meta.ItemMeta; |
| 13 | + |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import static org.bukkit.ChatColor.translateAlternateColorCodes; |
| 20 | + |
| 21 | +@SuppressWarnings({ "unchecked", "unused" }) |
| 22 | +public abstract class BaseItemBuilder<B extends BaseItemBuilder<B>> { |
| 23 | + final ItemStack item; |
| 24 | + |
| 25 | + ItemMeta meta; |
| 26 | + |
| 27 | + private final boolean hasNoItemMeta; |
| 28 | + |
| 29 | + BaseItemBuilder(Material material, int amount) { |
| 30 | + this(new ItemStack(material, amount)); |
| 31 | + } |
| 32 | + |
| 33 | + BaseItemBuilder(@NotNull ItemStack item) { |
| 34 | + this.item = item; |
| 35 | + this.meta = item.getItemMeta(); |
| 36 | + this.hasNoItemMeta = this.meta == null; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Sets the glow effect on the item. |
| 41 | + * |
| 42 | + * @param glow true to add enchantment and hide it, false to remove enchantment and show it |
| 43 | + * @apiNote Will hide the enchantments by default. |
| 44 | + * @return the builder for chaining |
| 45 | + */ |
| 46 | + public B setGlow(boolean glow) { |
| 47 | + // add enchantment and hide it if "glow" is true |
| 48 | + if (this.hasNoItemMeta) return (B) this; |
| 49 | + if (!glow) { |
| 50 | + this.meta.removeEnchant(Enchantment.DURABILITY); |
| 51 | + this.meta.removeItemFlags(ItemFlag.HIDE_ENCHANTS); |
| 52 | + return (B) this; |
| 53 | + } |
| 54 | + this.meta.addEnchant(Enchantment.DURABILITY, 1, true); |
| 55 | + this.meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); |
| 56 | + return (B) this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Sets the amount of the item. |
| 61 | + * @param amount the amount to set |
| 62 | + * @return the builder for chaining |
| 63 | + */ |
| 64 | + public B setAmount(int amount) { |
| 65 | + this.item.setAmount(amount); |
| 66 | + return (B) this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Sets the name of the itemStack to whatever the provided name is. |
| 71 | + * @param name the new name |
| 72 | + * @return the builder for chaining |
| 73 | + */ |
| 74 | + public B setName(String name) { |
| 75 | + if (this.hasNoItemMeta) return (B) this; |
| 76 | + this.meta.setDisplayName(translateAlternateColorCodes('&', name)); |
| 77 | + return (B) this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets the lore of the itemStack to whatever the provided lore is. |
| 82 | + * @param lore the new lore |
| 83 | + * @return the builder for chaining |
| 84 | + */ |
| 85 | + public B setLore(String... lore) { |
| 86 | + if (this.hasNoItemMeta) return (B) this; |
| 87 | + this.meta.setLore(Arrays.asList(lore)); |
| 88 | + return (B) this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Sets the lore of the itemStack to whatever the provided lore is. |
| 93 | + * @param lore the new lore |
| 94 | + * @return the builder for chaining |
| 95 | + */ |
| 96 | + public B setLore(List<String> lore) { |
| 97 | + if (this.hasNoItemMeta) return (B) this; |
| 98 | + this.meta.setLore(lore); |
| 99 | + return (B) this; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Enchant the itemStack with the provided enchantment |
| 104 | + * @param enchant the enchantment to enchant the itemStack with |
| 105 | + * @return the builder for chaining |
| 106 | + */ |
| 107 | + public B addEnchant(Enchantment enchant) { |
| 108 | + if (this.hasNoItemMeta) return (B) this; |
| 109 | + this.meta.addEnchant(enchant, 1, false); |
| 110 | + return (B) this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Enchant the itemStack with the provided enchantment |
| 115 | + * @param enchant the enchantment to enchant the itemStack with |
| 116 | + * @param level the level of the enchantment |
| 117 | + * @return the builder for chaining |
| 118 | + */ |
| 119 | + public B addEnchant(Enchantment enchant, int level) { |
| 120 | + if (this.hasNoItemMeta) return (B) this; |
| 121 | + this.meta.addEnchant(enchant, level, false); |
| 122 | + return (B) this; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Enchant the itemStack with the provided enchantment |
| 127 | + * @param enchant the enchantment to enchant the itemStack with |
| 128 | + * @param level the level of the enchantment |
| 129 | + * @param ignore whether to ignore the enchantment restrictions |
| 130 | + * @return the builder for chaining |
| 131 | + */ |
| 132 | + public B addEnchant(Enchantment enchant, int level, boolean ignore) { |
| 133 | + if (this.hasNoItemMeta) return (B) this; |
| 134 | + this.meta.addEnchant(enchant, level, ignore); |
| 135 | + return (B) this; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Apply all the enchantments to the itemStack with the same level & ignore restrictions |
| 140 | + * @param level the level of the enchantment |
| 141 | + * @param ignore whether to ignore the enchantment restrictions |
| 142 | + * @param enchant the enchantments to apply |
| 143 | + * @return the builder for chaining |
| 144 | + */ |
| 145 | + public B addEnchant(int level, boolean ignore, Enchantment... enchant) { |
| 146 | + if (this.hasNoItemMeta) return (B) this; |
| 147 | + for (Enchantment enchantment : enchant) |
| 148 | + this.meta.addEnchant(enchantment, level, ignore); |
| 149 | + return (B) this; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Apply all the enchantments to the itemStack with the same level |
| 154 | + * @param level the level of the enchantment |
| 155 | + * @param enchant the enchantments to apply |
| 156 | + * @return the builder for chaining |
| 157 | + */ |
| 158 | + public B addEnchant(int level, Enchantment... enchant) { |
| 159 | + if (this.hasNoItemMeta) return (B) this; |
| 160 | + for (Enchantment enchantment : enchant) |
| 161 | + this.meta.addEnchant(enchantment, level, false); |
| 162 | + return (B) this; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Apply all the enchantments to the itemStack (level 1) |
| 167 | + * @param enchant the enchantments to apply |
| 168 | + * @return the builder for chaining |
| 169 | + */ |
| 170 | + public B addEnchant(Enchantment... enchant) { |
| 171 | + if (this.hasNoItemMeta) return (B) this; |
| 172 | + for (Enchantment enchantment : enchant) |
| 173 | + this.meta.addEnchant(enchantment, 1, false); |
| 174 | + return (B) this; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Set the itemStack to be unbreakable |
| 179 | + * @return the builder for chaining |
| 180 | + */ |
| 181 | + public B setUnbreakable() { |
| 182 | + if (this.hasNoItemMeta || VersionHelper.IS_UNBREAKABLE_LEGACY) return (B) this; |
| 183 | + this.meta.setUnbreakable(true); |
| 184 | + return (B) this; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Set the itemStack to be unbreakable or not |
| 189 | + * @param breakable whether the itemStack is unbreakable |
| 190 | + * @return the builder for chaining |
| 191 | + */ |
| 192 | + public B setUnbreakable(boolean breakable) { |
| 193 | + if (this.hasNoItemMeta || VersionHelper.IS_UNBREAKABLE_LEGACY) return (B) this; |
| 194 | + this.meta.setUnbreakable(breakable); |
| 195 | + return (B) this; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Set the damage to the itemStack |
| 200 | + * @param d the damage |
| 201 | + * @return the builder for chaining |
| 202 | + */ |
| 203 | + public B setDamage(int d) { |
| 204 | + if (this.hasNoItemMeta || !(meta instanceof Damageable)) return (B) this; |
| 205 | + ((Damageable) meta).damage(d); |
| 206 | + return (B) this; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Build the item into a new ItemStack. |
| 211 | + * @return the new ItemStack |
| 212 | + */ |
| 213 | + public ItemStack build() { |
| 214 | + this.item.setItemMeta(meta); |
| 215 | + return item; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Build the item into a new MenuItem. |
| 220 | + * @return the new MenuItem |
| 221 | + */ |
| 222 | + public MenuItem buildItem() { |
| 223 | + return MenuItem.of(build()); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Build the item into a new MenuItem with the provided Click Event. |
| 228 | + * @param event the event |
| 229 | + * @return the new MenuItem |
| 230 | + */ |
| 231 | + public MenuItem buildItem(ItemResponse event) { |
| 232 | + return MenuItem.of(build(), event); |
| 233 | + } |
| 234 | +} |
0 commit comments