Hey,
habe mal, da ich es brauchte, einen kleinen itemBuilder geschrieben. Klein aber fein
Code:
Java
- import org.bukkit.ChatColor;
- import org.bukkit.Material;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import java.util.Arrays;
- import java.util.List;
- /**
- * ItemBuilder for
- * @see org.bukkit.inventory.ItemStack
- * by SpigotException
- *
- * version 1.0
- */
- public class ItemBuilder {
- private ItemStack itemStack;
- private ItemMeta itemMeta;
- /**
- * Constructor with params:
- * @param itemStack -> ItemStack for item
- */
- public ItemBuilder(ItemStack itemStack) {
- this.itemStack = itemStack;
- this.itemMeta = itemStack.getItemMeta();
- }
- /**
- * Constructor with params:
- * @param material -> Material for item
- */
- public ItemBuilder(Material material) {
- this.itemStack = new ItemStack(material);
- this.itemMeta = itemStack.getItemMeta();
- }
- /**
- * set the DisplayName for the item with params:
- * @param name -> name which will be displayed
- * @return this class
- */
- public ItemBuilder setDisplayName(String name) {
- itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
- return this;
- }
- /**
- * set the subID/metaID for the item (if needed) with params:
- * @param metaID -> sudID/metaID for item
- * @return this class
- */
- public ItemBuilder setMetaID(byte metaID) {
- itemStack.getData().setData(metaID);
- return this;
- }
- /**
- * set the amount of items with params:
- * @param amount -> amount of items
- * @return this class
- */
- public ItemBuilder setAmount(int amount) {
- this.itemStack.setAmount(amount);
- return this;
- }
- /**
- * set the durability of the item with params:
- * @param durability -> durability to set
- * @return this class
- */
- public ItemBuilder setDurability(short durability) {
- this.itemStack.setDurability(durability);
- return this;
- }
- /**
- * add enchantment to item with params:
- * @param enchantment -> enchantment for the item
- * @param lvl -> level of enchantment
- * @return this class
- */
- public ItemBuilder addEnchantment(Enchantment enchantment, int lvl) {
- this.itemMeta.addEnchant(enchantment, lvl, false);
- return this;
- }
- /**
- * remove all enchantments from the item
- * @return this class
- */
- public ItemBuilder clearEnchantments() {
- this.itemMeta.getEnchants().forEach((enchantment, integer) -> this.itemMeta.removeEnchant(enchantment));
- return this;
- }
- /**
- * remove an enchantment from the item with params:
- * @param enchantment -> enchantment which will be removed
- * @return this class
- */
- public ItemBuilder removeEnchantment(Enchantment enchantment) {
- if(this.itemMeta.getEnchants().containsKey(enchantment))
- this.itemMeta.removeEnchant(enchantment);
- return this;
- }
- /**
- * set the lore with an list and params:
- * @param lines -> lines for lore
- * @return this class
- */
- public ItemBuilder setLore(List<String> lines) {
- this.itemMeta.setLore(lines);
- return this;
- }
- /**
- * set the lore with an array and params:
- * @param lines -> lines for lore
- * @return this class
- */
- public ItemBuilder setLore(String... lines) {
- this.itemMeta.setLore(Arrays.asList(lines));
- return this;
- }
- /**
- * clears the lore of the item
- * @return this class
- */
- public ItemBuilder resetLore() {
- this.itemMeta.getLore().clear();
- return this;
- }
- /**
- *
- * @return the final ItemStack
- */
- public ItemStack build() {
- itemStack.setItemMeta(itemMeta);
- return itemStack;
- }
- }
kleines Beispiel:
Java
- //Lange Form:
- ItemStack diamondSword = new ItemStack(Material.DIAMOND_SWORD);
- ItemMeta itemMeta = diamondSword.getItemMeta();
- itemMeta.setDisplayName("§cSchwert");
- diamondSword.setItemMeta(itemMeta);
- //kurze Form:
- ItemStack diamondSword = new ItemBuilder(Material.DIAMOND_SWORD).setDisplayName("§cSchwert").build();
MfG
Nathalie0hneHerz
Damit das Ding wirklich nützlich ist würde das bauen von Köpfen und gefärbten Rüstungen fehlen. Auch unsafe enchantments wären praktisch. Ein zwei Null checks und ne vernünftige Formatierung wären auch nicht falsch. Auch vermisse ich ItemFlags und das bearbeiten der Material data. Auch sollte man das Material setzen können, auch außerhalb des Konstruktors. Desweiteren sollte man auch einzelne Zeilen der Lore hinzufügen können, ohne sie ganz neu zu setzen. Wenn man einen ItemStack übergibt solltest du auch hinzufügen, dass man nur einen Klon bearbeiten kann, nicht zwangweise das Item selber. An sich Cloneable solltest du auch implementieren.