All source projects

MrUltimateLibrary Source

Standalone source project / 68 files

MrUltimateLibrary is the backbone of all my plugins; it offers features that facilitate plugin creation, maintenance, and updates.

java

MrUltimateLibrary/src/main/java/eu/mrneznamy/mrultimatelibrary/gui/MrLibItemBuilder.java

JAVA · Read-only source

package eu.mrneznamy.mrultimatelibrary.gui;

import eu.mrneznamy.mrultimatelibrary.utils.MrLibColors;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Item builder for GUI items
 */
public class MrLibItemBuilder {
    
    private final ItemStack item;
    private final ItemMeta meta;
    
    public MrLibItemBuilder(Material material) {
        this.item = new ItemStack(material);
        this.meta = item.getItemMeta();
    }
    
    public MrLibItemBuilder(ItemStack item) {
        this.item = item.clone();
        this.meta = this.item.getItemMeta();
    }
    
    /**
     * Set display name
     */
    public MrLibItemBuilder setName(String name) {
        if (meta != null) {
            meta.setDisplayName(MrLibColors.colorize(name));
        }
        return this;
    }
    
    /**
     * Set display name with player placeholders
     */
    public MrLibItemBuilder setName(String name, Player player) {
        if (meta != null) {
            meta.setDisplayName(MrLibColors.colorize(name, player));
        }
        return this;
    }
    
    /**
     * Set lore
     */
    public MrLibItemBuilder setLore(String... lore) {
        if (meta != null) {
            List<String> coloredLore = new ArrayList<>();
            for (String line : lore) {
                coloredLore.add(MrLibColors.colorize(line));
            }
            meta.setLore(coloredLore);
        }
        return this;
    }
    
    /**
     * Set lore with player placeholders
     */
    public MrLibItemBuilder setLore(Player player, String... lore) {
        if (meta != null) {
            List<String> coloredLore = new ArrayList<>();
            for (String line : lore) {
                coloredLore.add(MrLibColors.colorize(line, player));
            }
            meta.setLore(coloredLore);
        }
        return this;
    }
    
    /**
     * Set lore from list
     */
    public MrLibItemBuilder setLore(List<String> lore) {
        if (meta != null) {
            List<String> coloredLore = new ArrayList<>();
            for (String line : lore) {
                coloredLore.add(MrLibColors.colorize(line));
            }
            meta.setLore(coloredLore);
        }
        return this;
    }
    
    /**
     * Add lore lines
     */
    public MrLibItemBuilder addLore(String... lines) {
        if (meta != null) {
            List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
            for (String line : lines) {
                lore.add(MrLibColors.colorize(line));
            }
            meta.setLore(lore);
        }
        return this;
    }
    
    /**
     * Set amount
     */
    public MrLibItemBuilder setAmount(int amount) {
        item.setAmount(Math.max(1, Math.min(64, amount)));
        return this;
    }
    
    /**
     * Add enchantment
     */
    public MrLibItemBuilder addEnchant(Enchantment enchant, int level) {
        if (meta != null) {
            meta.addEnchant(enchant, level, true);
        }
        return this;
    }
    
    /**
     * Add glow effect (fake enchant)
     */
    public MrLibItemBuilder addGlow() {
        if (meta != null) {
            meta.addEnchant(Enchantment.DURABILITY, 1, true);
            meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
        }
        return this;
    }
    
    /**
     * Add item flags
     */
    public MrLibItemBuilder addFlags(ItemFlag... flags) {
        if (meta != null) {
            meta.addItemFlags(flags);
        }
        return this;
    }
    
    /**
     * Hide all flags
     */
    public MrLibItemBuilder hideAllFlags() {
        if (meta != null) {
            meta.addItemFlags(ItemFlag.values());
        }
        return this;
    }
    
    /**
     * Set skull owner
     */
    public MrLibItemBuilder setSkullOwner(String owner) {
        if (meta instanceof SkullMeta) {
            ((SkullMeta) meta).setOwner(owner);
        }
        return this;
    }
    
    /**
     * Set skull owner by player
     */
    public MrLibItemBuilder setSkullOwner(Player player) {
        if (meta instanceof SkullMeta) {
            ((SkullMeta) meta).setOwningPlayer(player);
        }
        return this;
    }
    
    /**
     * Set custom model data (1.14+)
     */
    public MrLibItemBuilder setCustomModelData(int data) {
        if (meta != null) {
            meta.setCustomModelData(data);
        }
        return this;
    }
    
    /**
     * Build final ItemStack
     */
    public ItemStack build() {
        if (meta != null) {
            item.setItemMeta(meta);
        }
        return item;
    }
}

200 linesGNU GENERAL PUBLIC LICENSE · Version 3, 29 June 2007 · UTF-8 · Read only