Newer
Older
ChallengeSystem / ChallengeUtils / src / main / java / de / fanta / challengeutils / ItemUtils.java
@fanta fanta on 10 Aug 2024 2 KB fixes
package de.fanta.challengeutils;

import de.iani.cubesideutils.bukkit.MinecraftVersion;
import de.iani.cubesideutils.bukkit.items.ItemBuilder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;

public class ItemUtils {

    public static final ItemStack EMPTY_ICON = createGuiItem(Material.GRAY_STAINED_GLASS_PANE, Component.empty(), true, false);

    public static ItemStack createGuiItem(Material material, Component name, Component... lore) {
        return createGuiItem(material, name, false, lore);
    }

    public static ItemStack createGuiItem(Material material, Component name, boolean glowing, boolean showTooltip, Component... lore) {
        return createGuiItem(new ItemStack(material), name, glowing, showTooltip, lore);
    }

    public static ItemStack createGuiItem(ItemStack stack, Component name, boolean glowing, boolean showTooltip, Component... lore) {
        if (!name.style().hasDecoration(TextDecoration.ITALIC)) {
            name = name.decoration(TextDecoration.ITALIC, TextDecoration.State.FALSE);
        }

        for (int i = 0; i < lore.length; i++) {
            Component component = lore[i];
            if (!component.style().hasDecoration(TextDecoration.ITALIC)) {
                component = component.decoration(TextDecoration.ITALIC, TextDecoration.State.FALSE);
            }
            lore[i] = component;
        }

        ItemBuilder builder = ItemBuilder.fromItem(stack).displayName(name).lore(lore);
        if (glowing) {
            builder.enchantment(Enchantment.MENDING, 1, true).flag(ItemFlag.HIDE_ENCHANTS);
        }
        if (!showTooltip) {
            if (MinecraftVersion.isAboveOrEqual(1, 20, 6)) {
                builder.hideTooltip(true);
            }
        }
        return builder.build();
    }

    public static ItemStack createGuiItem(Material material, Component name, boolean glowing, Component... lore) {
        return createGuiItem(material, name, glowing, true, lore);
    }
}