|
| 1 | +package me.flame.menus.builders.items; |
| 2 | + |
| 3 | +import lombok.val; |
| 4 | +import me.flame.menus.items.MenuItem; |
| 5 | +import me.flame.menus.util.VersionHelper; |
| 6 | +import org.bukkit.Material; |
| 7 | +import org.bukkit.enchantments.Enchantment; |
| 8 | +import org.bukkit.entity.Damageable; |
| 9 | +import org.bukkit.event.inventory.InventoryClickEvent; |
| 10 | +import org.bukkit.inventory.ItemFlag; |
| 11 | +import org.bukkit.inventory.ItemStack; |
| 12 | +import org.bukkit.inventory.meta.ItemMeta; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | +import java.util.function.Consumer; |
| 19 | + |
| 20 | +@SuppressWarnings({ "unchecked", "unused" }) |
| 21 | +public class BaseItemBuilder<B extends BaseItemBuilder<B>> { |
| 22 | + final ItemStack item; |
| 23 | + ItemMeta meta; |
| 24 | + private final boolean hasNoItemMeta; |
| 25 | + |
| 26 | + BaseItemBuilder(Material material, int amount) { |
| 27 | + this(new ItemStack(material, amount)); |
| 28 | + } |
| 29 | + |
| 30 | + BaseItemBuilder(@NotNull ItemStack item) { |
| 31 | + this.item = item; |
| 32 | + this.meta = item.getItemMeta(); |
| 33 | + this.hasNoItemMeta = this.meta == null; |
| 34 | + } |
| 35 | + |
| 36 | + public B setGlow(boolean glow) { |
| 37 | + // add enchantment and hide it if "glow" is true |
| 38 | + if (this.hasNoItemMeta) return (B) this; |
| 39 | + if (!glow) { |
| 40 | + this.meta.removeEnchant(Enchantment.DURABILITY); |
| 41 | + this.meta.removeItemFlags(ItemFlag.HIDE_ENCHANTS); |
| 42 | + return (B) this; |
| 43 | + } |
| 44 | + this.meta.addEnchant(Enchantment.DURABILITY, 1, true); |
| 45 | + this.meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); |
| 46 | + return (B) this; |
| 47 | + } |
| 48 | + |
| 49 | + public B setAmount(int amount) { |
| 50 | + this.item.setAmount(amount); |
| 51 | + return (B) this; |
| 52 | + } |
| 53 | + |
| 54 | + public B setName(String name) { |
| 55 | + if (this.hasNoItemMeta) return (B) this; |
| 56 | + this.meta.setDisplayName(name); |
| 57 | + return (B) this; |
| 58 | + } |
| 59 | + |
| 60 | + public B setLore(String... lore) { |
| 61 | + if (this.hasNoItemMeta) return (B) this; |
| 62 | + this.meta.setLore(Arrays.asList(lore)); |
| 63 | + return (B) this; |
| 64 | + } |
| 65 | + |
| 66 | + public B setLore(List<String> lore) { |
| 67 | + if (this.hasNoItemMeta) return (B) this; |
| 68 | + this.meta.setLore(lore); |
| 69 | + return (B) this; |
| 70 | + } |
| 71 | + |
| 72 | + public B addEnchant(Enchantment enchant) { |
| 73 | + if (this.hasNoItemMeta) return (B) this; |
| 74 | + this.meta.addEnchant(enchant, 1, false); |
| 75 | + return (B) this; |
| 76 | + } |
| 77 | + |
| 78 | + public B addEnchant(Enchantment enchant, int level) { |
| 79 | + if (this.hasNoItemMeta) return (B) this; |
| 80 | + this.meta.addEnchant(enchant, level, false); |
| 81 | + return (B) this; |
| 82 | + } |
| 83 | + |
| 84 | + public B addEnchant(Enchantment enchant, int level, boolean ignore) { |
| 85 | + if (this.hasNoItemMeta) return (B) this; |
| 86 | + this.meta.addEnchant(enchant, level, ignore); |
| 87 | + return (B) this; |
| 88 | + } |
| 89 | + |
| 90 | + public B addEnchant(int level, boolean ignore, Enchantment... enchant) { |
| 91 | + if (this.hasNoItemMeta) return (B) this; |
| 92 | + for (Enchantment enchantment : enchant) |
| 93 | + this.meta.addEnchant(enchantment, level, ignore); |
| 94 | + return (B) this; |
| 95 | + } |
| 96 | + |
| 97 | + public B addEnchant(int level, Enchantment... enchant) { |
| 98 | + if (this.hasNoItemMeta) return (B) this; |
| 99 | + for (Enchantment enchantment : enchant) |
| 100 | + this.meta.addEnchant(enchantment, level, false); |
| 101 | + return (B) this; |
| 102 | + } |
| 103 | + |
| 104 | + public B addEnchant(Enchantment... enchant) { |
| 105 | + if (this.hasNoItemMeta) return (B) this; |
| 106 | + for (Enchantment enchantment : enchant) |
| 107 | + this.meta.addEnchant(enchantment, 1, false); |
| 108 | + return (B) this; |
| 109 | + } |
| 110 | + |
| 111 | + public B setUnbreakable() { |
| 112 | + if (this.hasNoItemMeta) return (B) this; |
| 113 | + this.meta.setUnbreakable(true); |
| 114 | + return (B) this; |
| 115 | + } |
| 116 | + |
| 117 | + public B setUnbreakable(boolean breakable) { |
| 118 | + if (this.hasNoItemMeta || VersionHelper.IS_UNBREAKABLE_LEGACY) return (B) this; |
| 119 | + this.meta.setUnbreakable(breakable); |
| 120 | + return (B) this; |
| 121 | + } |
| 122 | + |
| 123 | + public B setDamage(int d) { |
| 124 | + if (this.hasNoItemMeta) return (B) this; |
| 125 | + if (meta instanceof Damageable) { |
| 126 | + val damageable = ((Damageable) meta); |
| 127 | + damageable.damage(d); |
| 128 | + } |
| 129 | + return (B) this; |
| 130 | + } |
| 131 | + |
| 132 | + public ItemStack build() { |
| 133 | + this.item.setItemMeta(meta); |
| 134 | + return item; |
| 135 | + } |
| 136 | + |
| 137 | + public MenuItem buildItem() { |
| 138 | + this.item.setItemMeta(meta); |
| 139 | + return new MenuItem(item, null); |
| 140 | + } |
| 141 | + |
| 142 | + public MenuItem buildItem(@Nullable Consumer<InventoryClickEvent> clickAction) { |
| 143 | + this.item.setItemMeta(meta); |
| 144 | + return new MenuItem(item, clickAction); |
| 145 | + } |
| 146 | +} |
0 commit comments