Last active
July 31, 2021 16:11
-
-
Save hamza-cskn/af71812e9235025be348f2600502d6cd to your computer and use it in GitHub Desktop.
My Simple ItemBuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.bukkit.Material; | |
import org.bukkit.enchantments.Enchantment; | |
import org.bukkit.inventory.ItemFlag; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.inventory.meta.EnchantmentStorageMeta; | |
import org.bukkit.inventory.meta.ItemMeta; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Map; | |
public class ItemBuilder { | |
private final ItemStack item; | |
public ItemBuilder(Material material) { | |
item = new ItemStack(material); | |
} | |
public ItemBuilder setDamage(final short dmg) { | |
item.setDurability(dmg); | |
return this; | |
} | |
public ItemBuilder setDamage(final int dmg) { | |
setDamage((short) dmg); | |
return this; | |
} | |
public ItemBuilder setName(final String name) { | |
final ItemMeta meta = item.getItemMeta(); | |
meta.setDisplayName(name); | |
item.setItemMeta(meta); | |
return this; | |
} | |
public ItemBuilder setLore(final List<String> lore) { | |
final ItemMeta meta = item.getItemMeta(); | |
meta.setLore(lore); | |
item.setItemMeta(meta); | |
return this; | |
} | |
public ItemBuilder setLore(final String... lore) { | |
return setLore(new ArrayList<>(Arrays.asList(lore))); | |
} | |
public ItemBuilder appendLore(final List<String> appendLore) { | |
List<String> lore = item.getItemMeta().getLore(); | |
if (lore != null) lore.addAll(appendLore); | |
else lore = appendLore; | |
return setLore(lore); | |
} | |
public ItemBuilder enchant(final Enchantment ench) { | |
return enchant(ench, ench.getStartLevel()); | |
} | |
public ItemBuilder enchant(final Map<Enchantment, Integer> enchantments) { | |
for (Map.Entry<Enchantment, Integer> enchant : enchantments.entrySet()) { | |
enchant(enchant.getKey(), enchant.getValue()); | |
} | |
return this; | |
} | |
public ItemBuilder enchant(final Enchantment ench, final int value) { | |
if (item.getType().equals(Material.ENCHANTED_BOOK)) { | |
final EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta(); | |
if (meta == null) return this; | |
meta.addStoredEnchant(ench, value, true); | |
item.setItemMeta(meta); | |
} else { | |
item.addUnsafeEnchantment(ench, value); | |
} | |
return this; | |
} | |
public ItemBuilder appendLore(final String... appendLore) { | |
return appendLore(new ArrayList<>(Arrays.asList(appendLore))); | |
} | |
public ItemBuilder setAmount(final int amount) { | |
item.setAmount(amount); | |
return this; | |
} | |
public ItemBuilder hideFlags(final ItemFlag itemFlag) { | |
final ItemMeta meta = item.getItemMeta(); | |
meta.addItemFlags(itemFlag); | |
item.setItemMeta(meta); | |
return this; | |
} | |
public ItemBuilder hideFlags() { | |
hideFlags(ItemFlag.HIDE_ATTRIBUTES); | |
return this; | |
} | |
public ItemStack build() { | |
return item; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment