package dev.loki.item; import dev.loki.util.TextUtil; import net.kyori.adventure.text.Component; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.persistence.PersistentDataType; import java.util.List; /** * Central factory for DBD custom items: Medkit and Flashlight. * Items are identified by PersistentDataContainer tag "mdbd:item_type". * Charges are stored as "mdbd:charges". */ public final class DBDItems { public static final NamespacedKey KEY_TYPE = new NamespacedKey("mdbd", "item_type"); public static final NamespacedKey KEY_CHARGES = new NamespacedKey("mdbd", "charges"); private DBDItems() {} // ── Factories ───────────────────────────────────────────────────────────── /** Медкит — лечит одно состояние здоровья. */ public static ItemStack medkit(int charges) { return build( Material.PAPER, "<#00FF7F>Медкит", charges, ItemType.MEDKIT.getDefaultCharges(), List.of( "Лечит одно состояние здоровья.", "ПКМ — начать самолечение" ), "MEDKIT" ); } public static ItemStack medkit() { return medkit(ItemType.MEDKIT.getDefaultCharges()); } /** Фонарик — ослепляет киллера при попадании. */ public static ItemStack flashlight(int charges) { return build( Material.TORCH, "<#FFD700>Фонарик", charges, ItemType.FLASHLIGHT.getDefaultCharges(), List.of( "Ослепляет киллера при попадании.", "ПКМ — направить луч на киллера" ), "FLASHLIGHT" ); } public static ItemStack flashlight() { return flashlight(ItemType.FLASHLIGHT.getDefaultCharges()); } // ── Helpers ─────────────────────────────────────────────────────────────── private static ItemStack build(Material mat, String name, int charges, int maxCharges, List loreLines, String typeId) { var item = new ItemStack(mat); item.editMeta(meta -> { meta.displayName(TextUtil.color(name)); var lore = new java.util.ArrayList(); for (String line : loreLines) { lore.add(TextUtil.color(line)); } lore.add(Component.empty()); lore.add(TextUtil.color(chargesBar(charges, maxCharges))); meta.lore(lore); meta.getPersistentDataContainer().set(KEY_TYPE, PersistentDataType.STRING, typeId); meta.getPersistentDataContainer().set(KEY_CHARGES, PersistentDataType.INTEGER, charges); }); return item; } private static String chargesBar(int charges, int max) { int filled = max > 0 ? (int) (8.0 * charges / max) : 0; int empty = 8 - filled; return "<#FFD700>Заряды: <#00FF00>" + "█".repeat(filled) + "" + "░".repeat(empty) + " " + charges + "/" + max; } // ── Identification ──────────────────────────────────────────────────────── public static String getType(ItemStack item) { if (item == null || !item.hasItemMeta()) return null; return item.getItemMeta().getPersistentDataContainer() .get(KEY_TYPE, PersistentDataType.STRING); } public static int getCharges(ItemStack item) { if (item == null || !item.hasItemMeta()) return 0; Integer c = item.getItemMeta().getPersistentDataContainer() .get(KEY_CHARGES, PersistentDataType.INTEGER); return c != null ? c : 0; } public static ItemStack setCharges(ItemStack item, int newCharges) { if (item == null || !item.hasItemMeta()) return item; String type = getType(item); if (type == null) return item; return switch (type) { case "MEDKIT" -> medkit(newCharges); case "FLASHLIGHT" -> flashlight(newCharges); default -> item; }; } public static boolean isMedkit(ItemStack item) { return "MEDKIT".equals(getType(item)); } public static boolean isFlashlight(ItemStack item) { return "FLASHLIGHT".equals(getType(item)); } public static boolean isDBDItem(ItemStack item) { return getType(item) != null; } }