diff --git a/build.gradle b/build.gradle index 77511f7..5bed7fa 100644 --- a/build.gradle +++ b/build.gradle @@ -24,11 +24,15 @@ repositories { maven { url "https://repo.oraxen.com/releases" } + maven { + url "https://repo.nexomc.com/releases" + } } dependencies { compileOnly("io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT") compileOnly 'io.th0rgal:oraxen:1.184.1' + compileOnly("com.nexomc:nexo:1.10.0") compileOnly("com.sk89q.worldedit:worldedit-bukkit:7.2.15") implementation('com.google.guava:guava:32.0.1-android') diff --git a/settings.gradle b/settings.gradle index 27cf1f1..81cfb74 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,3 @@ -rootProject.name = 'LotusOffSeasonV2' +rootProject.name = 'FrostAndFallout' diff --git a/src/main/java/dev/lotus/studio/Main.java b/src/main/java/dev/lotus/studio/Main.java index 7489f46..af3ef13 100644 --- a/src/main/java/dev/lotus/studio/Main.java +++ b/src/main/java/dev/lotus/studio/Main.java @@ -78,7 +78,7 @@ public void onDisable() { if (databaseInitializer != null) { databaseInitializer.closeConnection(); } - getLogger().info("LotusOffSeason plugin disabled!"); + getLogger().info("Frost and Fallout plugin disabled!"); HandlerList.unregisterAll(this); } private void metric(){ diff --git a/src/main/java/dev/lotus/studio/event/ArmorEvent.java b/src/main/java/dev/lotus/studio/event/ArmorEvent.java index 1e47f52..472b698 100644 --- a/src/main/java/dev/lotus/studio/event/ArmorEvent.java +++ b/src/main/java/dev/lotus/studio/event/ArmorEvent.java @@ -9,6 +9,8 @@ import dev.lotus.studio.item.CustomItemManager; import dev.lotus.studio.playerdata.PlayerManager; +import static org.bukkit.Bukkit.getLogger; + public class ArmorEvent implements Listener { private final CustomItemManager customItemManager; @@ -33,7 +35,7 @@ public void onArmorChanged(Player player) { // Проверяем, зарегистрирован ли предмет в CustomItemManager CustomItem customItem = customItemManager.getCustomItemByItemStack(armorPiece); - System.out.println("Найдено " + customItem); + getLogger().info("Найдено " + customItem); if (customItem != null) { temperatureResistance += customItem.getTemperatureResistance(); radiationResistance += customItem.getRadiationResistance(); @@ -43,7 +45,7 @@ public void onArmorChanged(Player player) { // Устанавливаем сопротивление для игрока PlayerManager.getInstance().getPlayerData(player).setTemperatureResistance(temperatureResistance); PlayerManager.getInstance().getPlayerData(player).setRadiationResistance(radiationResistance); - System.out.println("Сопротивление температуре: " + temperatureResistance); - System.out.println("Сопротивление радиции: " + radiationResistance); + getLogger().info("Сопротивление температуре: " + temperatureResistance); + getLogger().info("Сопротивление радиции: " + radiationResistance); } } diff --git a/src/main/java/dev/lotus/studio/handlers/TemperatureHandler.java b/src/main/java/dev/lotus/studio/handlers/TemperatureHandler.java index c98cfa5..e72e17b 100644 --- a/src/main/java/dev/lotus/studio/handlers/TemperatureHandler.java +++ b/src/main/java/dev/lotus/studio/handlers/TemperatureHandler.java @@ -172,7 +172,7 @@ public Component createProgressBar(double absoluteValue, String label, boolean s progressBar = progressBar.append(Component.text("]")); if (showValue) { - String numberValue = String.format("%2.1f", absoluteValue); + String numberValue = String.format(" %2.1f", absoluteValue); progressBar = progressBar.append(Component.text(numberValue).color(TextColor.color(0xFFFFFF))); } diff --git a/src/main/java/dev/lotus/studio/item/CustomItemManager.java b/src/main/java/dev/lotus/studio/item/CustomItemManager.java index e3cf505..608b0e2 100644 --- a/src/main/java/dev/lotus/studio/item/CustomItemManager.java +++ b/src/main/java/dev/lotus/studio/item/CustomItemManager.java @@ -1,8 +1,9 @@ package dev.lotus.studio.item; -import org.bukkit.Bukkit; -import org.bukkit.Material; +import dev.lotus.studio.item.armor.CustomItemFactory; +import dev.lotus.studio.item.eat.*; import org.bukkit.NamespacedKey; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.inventory.ItemFlag; @@ -10,34 +11,31 @@ import org.bukkit.persistence.PersistentDataType; import dev.lotus.studio.Main; import dev.lotus.studio.item.armor.CustomItem; -import dev.lotus.studio.item.armor.OraxenCustomItem; -import dev.lotus.studio.item.armor.StandardArmor; -import dev.lotus.studio.item.eat.EatItem; -import dev.lotus.studio.item.eat.OraxenEatItem; -import dev.lotus.studio.item.eat.StandardEatItem; -import dev.lotus.studio.item.view.ViewType; -import dev.lotus.studio.item.view.OraxenViewView; -import dev.lotus.studio.item.view.StandardViewItem; -import dev.lotus.studio.utils.OraxenUtils; +import dev.lotus.studio.item.view.ViewItem; +import dev.lotus.studio.item.view.ViewItemFactory; +import dev.lotus.studio.utils.ResourcePackUtils; +import static org.bukkit.Bukkit.getLogger; import java.io.File; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class CustomItemManager { private final Map items = new HashMap<>(); - private final Map viewItems = new HashMap<>(); - + private final Map viewItems = new HashMap<>(); private final Map eatItems = new HashMap<>(); - private boolean isOraxenEnabled; - public CustomItemManager() { - this.isOraxenEnabled = Bukkit.getPluginManager().getPlugin("Oraxen") != null; + private final boolean isOraxenEnabled; + private final boolean isNexoEnabled; + public CustomItemManager() { + this.isOraxenEnabled = ResourcePackUtils.isOraxenEnable(); if (!isOraxenEnabled) { Main.getInstance().getLogger().warning("Oraxen не найден. Предметы из Oraxen будут пропущены."); } + this.isNexoEnabled = ResourcePackUtils.isNexoEnable(); + if (!isNexoEnabled) { + Main.getInstance().getLogger().warning("Nexo не найден. Предметы из Nexo будут пропущены."); + } } public void loadItems() { @@ -45,278 +43,197 @@ public void loadItems() { if (!file.exists()) { Main.getInstance().saveResource("items.yml", false); } - if (!OraxenUtils.isOraxenEnable()){ - Main.getInstance().getLogger().warning("Oraxen не найден. Предметы из Oraxen будут пропущены."); - } - this.isOraxenEnabled = Bukkit.getPluginManager().getPlugin("Oraxen") != null; FileConfiguration config = YamlConfiguration.loadConfiguration(file); - // Загрузка обычных предметов - loadStandardItems(config); + // Clear existing items before loading new ones + items.clear(); + viewItems.clear(); + eatItems.clear(); - // Загрузка view_item + loadStandardItems(config); loadViewItems(config); - - // Загрузка eat_item loadEatItems(config); } + private void loadStandardItems(FileConfiguration config) { + if (!config.contains("items")) return; + ConfigurationSection itemsSection = config.getConfigurationSection("items"); + if (itemsSection == null) return; + CustomItemFactory factory = new CustomItemFactory(); - private void loadStandardItems(FileConfiguration config) { - for (String key : config.getConfigurationSection("items").getKeys(false)) { + for (String key : itemsSection.getKeys(false)) { String path = "items." + key; - try { - String type = config.getString(path + ".type"); - if (type == null) { - throw new IllegalArgumentException("Не указан тип предмета для ключа '" + key + "'"); + ConfigurationSection itemSection = config.getConfigurationSection(path); + if (itemSection == null) { + throw new IllegalArgumentException("Пустая секция для " + path); } - if ("standard".equalsIgnoreCase(type)) { - Material material = Material.valueOf(config.getString(path + ".material")); - String displayName = config.getString(path + ".displayName"); - List lore = config.getStringList(path + ".lore"); - double temperatureResistance = config.getDouble(path + ".temperatureResistance"); - double radiationResistance = config.getDouble(path + ".radiationResistance"); - - items.put(key, new StandardArmor(material, displayName, lore, temperatureResistance, radiationResistance)); - Main.getInstance().getLogger().info("Успешно загружен стандартный предмет: " + key); - } else if ("oraxen".equalsIgnoreCase(type)) { - if (!isOraxenEnabled) { - Main.getInstance().getLogger().warning("Oraxen-предмет '" + key + "' пропущен, так как Oraxen не активен."); - continue; - } - - String oraxenId = config.getString(path + ".oraxenId"); - if (oraxenId == null || oraxenId.isEmpty()) { - throw new IllegalArgumentException("Отсутствует 'oraxenId' для предмета с ключом '" + key + "'"); - } - double temperatureResistance = config.getDouble(path + ".temperatureResistance"); - double radiationResistance = config.getDouble(path + ".radiationResistance"); - - items.put(key, new OraxenCustomItem(oraxenId, temperatureResistance, radiationResistance)); - Main.getInstance().getLogger().info("Успешно загружен Oraxen-предмет: " + key); - } else { - throw new IllegalArgumentException("Неизвестный тип предмета '" + type + "' для ключа '" + key + "'"); - } - } catch (IllegalArgumentException e) { - Main.getInstance().getLogger().warning("Ошибка при загрузке предмета с ключом '" + key + "': " + e.getMessage()); - } catch (Exception e) { - Main.getInstance().getLogger().severe("Непредвиденная ошибка при загрузке предмета с ключом '" + key + "': " + e.getMessage()); - e.printStackTrace(); + CustomItem item = factory.fromSection(itemSection); + items.put(key, item); + Main.getInstance().getLogger().info("Загружен item: " + key + " (" + item.getCustomItem() + ")"); + + } catch (IllegalArgumentException | IllegalStateException e) { + Main.getInstance().getLogger().warning("Ошибка при загрузке item '" + key + "': " + e.getMessage()); + } catch (Throwable t) { + Main.getInstance().getLogger().severe("Непредвиденная ошибка при загрузке item '" + key + "': " + t.getMessage()); + t.printStackTrace(); } } } private void loadViewItems(FileConfiguration config) { - for (String key : config.getConfigurationSection("view_item").getKeys(false)) { + if (!config.contains("view_item")) return; + var section = config.getConfigurationSection("view_item"); + if (section == null) return; + + ViewItemFactory factory = new ViewItemFactory(); + + for (String key : section.getKeys(false)) { String path = "view_item." + key; try { - String type = config.getString(path + ".type"); - if (type == null) { - throw new IllegalArgumentException("Не указан тип предмета для view_item с ключом '" + key + "'"); + var itemSection = config.getConfigurationSection(path); + if (itemSection == null) { + throw new IllegalArgumentException("Пустая секция для " + path); } - if ("standard".equalsIgnoreCase(type)) { - Material material = Material.valueOf(config.getString(path + ".material")); - String displayName = config.getString(path + ".displayName"); - List lore = config.getStringList(path + ".lore"); - String viewType = config.getString(path + ".view_type"); - - viewItems.put(key, new StandardViewItem(material, displayName, lore, viewType)); - Main.getInstance().getLogger().info("Успешно загружен стандартный view_item: " + key); - } else if ("oraxen".equalsIgnoreCase(type)) { - if (!isOraxenEnabled) { - Main.getInstance().getLogger().warning("Oraxen view_item '" + key + "' пропущен, так как Oraxen не активен."); - continue; - } - - String oraxenId = config.getString(path + ".oraxenId"); - if (oraxenId == null || oraxenId.isEmpty()) { - throw new IllegalArgumentException("Отсутствует 'oraxenId' для view_item с ключом '" + key + "'"); - } - String viewType = config.getString(path + ".view_type"); - - viewItems.put(key, new OraxenViewView(oraxenId, viewType)); - Main.getInstance().getLogger().info("Успешно загружен Oraxen view_item: " + key); - } else { - throw new IllegalArgumentException("Неизвестный тип view_item '" + type + "' для ключа '" + key + "'"); - } - } catch (IllegalArgumentException e) { - Main.getInstance().getLogger().warning("Ошибка при загрузке view_item с ключом '" + key + "': " + e.getMessage()); - } catch (Exception e) { - Main.getInstance().getLogger().severe("Непредвиденная ошибка при загрузке view_item с ключом '" + key + "': " + e.getMessage()); - e.printStackTrace(); + ViewItem viewType = factory.fromSection(itemSection); + viewItems.put(key, viewType); + Main.getInstance().getLogger().info("Загружен view_item: " + key + " (" + viewType.getViewType() + ")"); + + } catch (IllegalArgumentException | IllegalStateException e) { + Main.getInstance().getLogger().warning("Ошибка при загрузке view_item '" + key + "': " + e.getMessage()); + } catch (Throwable t) { + Main.getInstance().getLogger().severe("Непредвиденная ошибка при загрузке view_item '" + key + "': " + t.getMessage()); + t.printStackTrace(); } } } private void loadEatItems(FileConfiguration config) { if (!config.contains("eat_item")) return; + var section = config.getConfigurationSection("eat_item"); + if (section == null) return; - for (String key : config.getConfigurationSection("eat_item").getKeys(false)) { - String path = "eat_item." + key; + EatItemFactory factory = new EatItemFactory(); + for (String key : section.getKeys(false)) { + String path = "eat_item." + key; try { - String type = config.getString(path + ".type"); - if (type == null) { - throw new IllegalArgumentException("Не указан тип предмета для eat_item с ключом '" + key + "'"); + var itemSection = config.getConfigurationSection(path); + if (itemSection == null) { + throw new IllegalArgumentException("Пустая секция для " + path); } - if ("standard".equalsIgnoreCase(type)) { - Material material = Material.valueOf(config.getString(path + ".material")); - String displayName = config.getString(path + ".displayName"); - List lore = config.getStringList(path + ".lore"); - int radiationValue = config.getInt(path + ".foodValue.radiation"); - int temperatureValue = config.getInt(path + ".foodValue.temperature"); - - eatItems.put(key, new StandardEatItem(material, displayName, lore, radiationValue, temperatureValue)); - Main.getInstance().getLogger().info("Успешно загружен стандартный eat_item: " + key); - } else if ("oraxen".equalsIgnoreCase(type)) { - String oraxenId = config.getString(path + ".oraxenId"); - if (oraxenId == null || oraxenId.isEmpty()) { - throw new IllegalArgumentException("Отсутствует 'oraxenId' для eat_item с ключом '" + key + "'"); - } - - int radiationValue = config.getInt(path + ".foodValue.radiation"); - int temperatureValue = config.getInt(path + ".foodValue.temperature"); - - eatItems.put(key, new OraxenEatItem(oraxenId, radiationValue, temperatureValue)); - Main.getInstance().getLogger().info("Успешно загружен Oraxen eat_item: " + key); - } else { - throw new IllegalArgumentException("Неизвестный тип eat_item '" + type + "' для ключа '" + key + "'"); + String providerStr = itemSection.getString("provider", itemSection.getString("type", "standard")); + String providerDbg = providerStr.toLowerCase(Locale.ROOT); + if ("oraxen".equals(providerDbg) && !isOraxenEnabled) { + Main.getInstance().getLogger().warning("Oraxen eat_item '" + key + "' пропущен: Oraxen не активен"); + continue; + } + if ("nexo".equals(providerDbg) && !isNexoEnabled) { + Main.getInstance().getLogger().warning("Nexo eat_item '" + key + "' пропущен: Nexo не активен"); + continue; } - } catch (IllegalArgumentException e) { - Main.getInstance().getLogger().warning("Ошибка при загрузке eat_item с ключом '" + key + "': " + e.getMessage()); - } catch (Exception e) { - Main.getInstance().getLogger().severe("Непредвиденная ошибка при загрузке eat_item с ключом '" + key + "': " + e.getMessage()); - e.printStackTrace(); + + EatItem eatItem = factory.fromSection(itemSection); + eatItems.put(key, eatItem); + Main.getInstance().getLogger().info("Загружен eat_item: " + key + " (src=" + eatItem.getEatItem() + ")"); + + } catch (IllegalArgumentException | IllegalStateException e) { + Main.getInstance().getLogger().warning("Ошибка при загрузке eat_item '" + key + "': " + e.getMessage()); + } catch (Throwable t) { + Main.getInstance().getLogger().severe("Непредвиденная ошибка при загрузке eat_item '" + key + "': " + t.getMessage()); + t.printStackTrace(); } } } - public EatItem getEatItemByItemStack(ItemStack itemStack) { - if (itemStack == null || !itemStack.hasItemMeta()) { - return null; - } - + if (itemStack == null || !itemStack.hasItemMeta()) return null; for (EatItem eatItem : eatItems.values()) { - if (eatItem.getItemStack().isSimilar(itemStack)) { - return eatItem; - } + try { + if (eatItem.getItemStack().isSimilar(itemStack)) return eatItem; + } catch (Exception ignored) {} } - return null; } - public CustomItem getCustomItemByItemStack(ItemStack itemStack) { - if (itemStack == null || !itemStack.hasItemMeta()) { - return null; - } + if (itemStack == null || !itemStack.hasItemMeta()) return null; + var meta = itemStack.getItemMeta(); + String ffId = meta.getPersistentDataContainer().get(ItemKeys.FF_ID, PersistentDataType.STRING); + if (ffId != null) { + for (CustomItem item : items.values()) { + if (ffId.equals(item.getCustomItem())) { + return item; + } + } + getLogger().warning("ffId есть, но не нашли совпадение в items\n" + itemStack); + } - NamespacedKey idKey = new NamespacedKey("oraxen", "id"); - String oraxenId = itemStack.getItemMeta().getPersistentDataContainer().get(idKey, PersistentDataType.STRING); - System.out.println(" "); - System.out.println("Oraxen id: " + oraxenId); - + String oraxenId = meta.getPersistentDataContainer().get(ItemKeys.ORAXEN_ID, PersistentDataType.STRING); if (oraxenId != null) { for (CustomItem item : items.values()) { - - String itemOraxenId = item.getItemStack().getItemMeta().getPersistentDataContainer().get(idKey, PersistentDataType.STRING); - - if (oraxenId.equals(itemOraxenId)) { - System.out.println("FOUNNND WITH ORAXEN ITEM"); + if (oraxenId.equals(item.getCustomItem())) { return item; } + ItemStack itemItemStack = item.getItemStack(); + if (itemItemStack.hasItemMeta()) { + String other = itemItemStack.getItemMeta().getPersistentDataContainer().get(ItemKeys.ORAXEN_ID, PersistentDataType.STRING); + if (oraxenId.equals(other)) return item; + } } + getLogger().warning("oraxenId есть, но не нашли совпадение в items\n" + itemStack); } - - System.out.println("Проверка без praxen"); - - if (!itemStack.getItemMeta().hasItemFlag(ItemFlag.HIDE_ARMOR_TRIM)) { + if (!meta.hasItemFlag(ItemFlag.HIDE_ARMOR_TRIM)) { return null; } - for (CustomItem item : items.values()) { -// System.out.println(itemStack); -// System.out.println(item.getItemStack()); -// System.out.println(" "); -// System.out.println(" "); -// System.out.println(" "); - ItemStack customItemStack = item.getItemStack(); - if (customItemStack == null || !customItemStack.hasItemMeta()) { - continue; - } - - var customMeta = customItemStack.getItemMeta(); - var itemMeta = itemStack.getItemMeta(); - - if (customMeta.displayName() != null && customMeta.displayName().equals(itemMeta.displayName()) && - customMeta.lore() != null && customMeta.lore().equals(itemMeta.lore())) { + ItemStack custom = item.getItemStack(); + if (!custom.hasItemMeta()) continue; + var itemMeta = custom.getItemMeta(); + if (itemMeta.displayName() != null && Objects.equals(itemMeta.displayName(), meta.displayName()) + && itemMeta.lore() != null && Objects.equals(itemMeta.lore(), meta.lore())) { return item; } } - return null; } - - public ViewType getViewItemByItemStack(ItemStack itemStack) { - if (itemStack == null || !itemStack.hasItemMeta()) { - return null; - } - - for (ViewType viewItem : viewItems.values()) { - if (viewItem.getItemStack().isSimilar(itemStack)) { - return viewItem; - } + public ViewItem getViewItemByItemStack(ItemStack itemStack) { + if (itemStack == null || !itemStack.hasItemMeta()) return null; + for (ViewItem viewItem : viewItems.values()) { + try { + if (viewItem.getItemStack().isSimilar(itemStack)) { + return viewItem; + } + } catch (Exception ignored) {} } - return null; } + public Map getItems() { return items; } + public Map getViewItems() { return viewItems; } + public Map getEatItems() { return eatItems; } - - public Map getItems() { - return items; - } - - public Map getViewItems() { - return viewItems; - } - - public Map getEatItems() { - return eatItems; - } - - public EatItem getEatItem(String key) { - return eatItems.get(key); - } - - public CustomItem getItem(String key) { - return items.get(key); - } - - public ViewType getViewItem(String key) { - return viewItems.get(key); - } - + public EatItem getEatItem(String key) { return eatItems.get(key); } + public CustomItem getItem(String key) { return items.get(key); } + public ViewItem getViewItem(String key) { return viewItems.get(key); } public void reloadItemConfig(){ try { Main.getInstance().getLogger().info("Items.yml reloading!"); loadItems(); + Main.getInstance().getLogger().info("Items.yml reloaded"); } catch (Exception e){ - Main.getInstance().getLogger().warning("Items.yml reload failed"); + Main.getInstance().getLogger().warning("Items.yml reload failed: " + e.getMessage()); } - Main.getInstance().getLogger().info("Items.yml reload"); } } diff --git a/src/main/java/dev/lotus/studio/item/ItemKeys.java b/src/main/java/dev/lotus/studio/item/ItemKeys.java new file mode 100644 index 0000000..5215dd5 --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/ItemKeys.java @@ -0,0 +1,9 @@ +package dev.lotus.studio.item; + +import org.bukkit.NamespacedKey; + +public class ItemKeys { + private ItemKeys() {} + public static final NamespacedKey FF_ID = new NamespacedKey("frostandfallout", "id"); + public static final NamespacedKey ORAXEN_ID = new NamespacedKey("oraxen", "id"); +} diff --git a/src/main/java/dev/lotus/studio/item/Provider.java b/src/main/java/dev/lotus/studio/item/Provider.java new file mode 100644 index 0000000..5db94f8 --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/Provider.java @@ -0,0 +1,3 @@ +package dev.lotus.studio.item; + +public enum Provider { STANDARD, ORAXEN, NEXO } diff --git a/src/main/java/dev/lotus/studio/item/armor/CustomItem.java b/src/main/java/dev/lotus/studio/item/armor/CustomItem.java index d7cd025..88a9bd7 100644 --- a/src/main/java/dev/lotus/studio/item/armor/CustomItem.java +++ b/src/main/java/dev/lotus/studio/item/armor/CustomItem.java @@ -1,9 +1,11 @@ package dev.lotus.studio.item.armor; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; public interface CustomItem { - ItemStack getItemStack(); // Отримання предмета - double getTemperatureResistance(); // Отримання теплоємності + @NotNull String getCustomItem(); + @NotNull ItemStack getItemStack(); + double getTemperatureResistance(); double getRadiationResistance(); } diff --git a/src/main/java/dev/lotus/studio/item/armor/CustomItemFactory.java b/src/main/java/dev/lotus/studio/item/armor/CustomItemFactory.java new file mode 100644 index 0000000..2f57f8b --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/armor/CustomItemFactory.java @@ -0,0 +1,54 @@ +package dev.lotus.studio.item.armor; + +import dev.lotus.studio.item.Provider; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.plugin.PluginManager; + +import java.util.List; +import java.util.Locale; +import java.util.Objects; + +public final class CustomItemFactory { + private final PluginManager pluginManager = Bukkit.getPluginManager(); + private final boolean oraxenEnabled = pluginManager.isPluginEnabled("Oraxen"); + private final boolean nexoEnabled = pluginManager.isPluginEnabled("Nexo"); + + public CustomItem fromSection(ConfigurationSection section) { + Objects.requireNonNull(section, "section"); + + String providerStr = section.getString("provider", section.getString("type", "standard")); + Provider provider = Provider.valueOf(providerStr.toUpperCase(Locale.ROOT)); + + String displayName = section.getString("displayName", null); + List lore = section.getStringList("lore"); + double tempRes = section.getDouble("temperatureResistance", 0.0); + double radRes = section.getDouble("radiationResistance", 0.0); + + switch (provider) { + case STANDARD: { + String matName = section.getString("material"); + if (matName == null) throw new IllegalArgumentException("Отсутствует material"); + Material mat = Material.valueOf(matName.toUpperCase(Locale.ROOT)); + return new StandardArmor(mat, displayName, lore, tempRes, radRes); + } + case ORAXEN: { + if (!oraxenEnabled) throw new IllegalStateException("Oraxen недоступен/не загружен"); + String id = section.getString("oraxenId"); + if (id == null || id.isEmpty()) + throw new IllegalArgumentException("Отсутствует oraxenId"); + return new OraxenCustomItem(id, tempRes, radRes, displayName, lore); + } + case NEXO: { + if (!nexoEnabled) throw new IllegalStateException("Nexo недоступен/не загружен"); + String id = section.getString("nexoId"); + if (id == null || id.isEmpty()) + throw new IllegalArgumentException("Отсутствует nexoId"); + return new NexoCustomItem(id, tempRes, radRes, displayName, lore); + } + default: + throw new IllegalArgumentException("Неизвестный provider: " + provider); + } + } +} diff --git a/src/main/java/dev/lotus/studio/item/armor/NexoCustomItem.java b/src/main/java/dev/lotus/studio/item/armor/NexoCustomItem.java new file mode 100644 index 0000000..f18a97b --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/armor/NexoCustomItem.java @@ -0,0 +1,66 @@ +package dev.lotus.studio.item.armor; + +import com.nexomc.nexo.api.NexoItems; +import com.nexomc.nexo.items.ItemBuilder; +import dev.lotus.studio.item.ItemKeys; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Objects; + +public final class NexoCustomItem implements CustomItem { + private final String id; + private final double temperatureResistance; + private final double radiationResistance; + private final String displayName; + private final List lore; + + private ItemStack template; + + public NexoCustomItem(@NotNull String nexoId, + double temperatureResistance, + double radiationResistance, + String displayName, + @NotNull List lore) { + this.id = Objects.requireNonNull(nexoId, "nexoId"); + this.temperatureResistance = temperatureResistance; + this.radiationResistance = radiationResistance; + this.displayName = displayName; + this.lore = List.copyOf(lore); + } + + @Override public @NotNull String getCustomItem() { return id; } + @Override public double getTemperatureResistance() { return temperatureResistance; } + @Override public double getRadiationResistance() { return radiationResistance; } + + @Override + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemBuilder builder = NexoItems.itemFromId(id); + if (builder == null) { + throw new IllegalArgumentException("Nexo item '" + id + "' не найден"); + } + ItemStack itemStack = builder.build().clone(); + ItemMeta meta = itemStack.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream() + .map(Component::text) + .toList(); + meta.lore(components); + } + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, id); + itemStack.setItemMeta(meta); + } + template = itemStack; + } + return template.clone(); + } +} diff --git a/src/main/java/dev/lotus/studio/item/armor/OraxenCustomItem.java b/src/main/java/dev/lotus/studio/item/armor/OraxenCustomItem.java index b20412f..6aca092 100644 --- a/src/main/java/dev/lotus/studio/item/armor/OraxenCustomItem.java +++ b/src/main/java/dev/lotus/studio/item/armor/OraxenCustomItem.java @@ -1,38 +1,64 @@ package dev.lotus.studio.item.armor; +import dev.lotus.studio.item.ItemKeys; import io.th0rgal.oraxen.api.OraxenItems; import io.th0rgal.oraxen.items.ItemBuilder; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; -public class OraxenCustomItem implements CustomItem { - private final String oraxenId; +import java.util.List; +import java.util.Objects; + +public final class OraxenCustomItem implements CustomItem { + private final String id; private final double temperatureResistance; private final double radiationResistance; + private final String displayName; + private final List lore; + + private ItemStack template; - public OraxenCustomItem(String oraxenId, double temperatureResistance, double radiationResistance) { - this.oraxenId = oraxenId; + public OraxenCustomItem(@NotNull String oraxenId, + double temperatureResistance, + double radiationResistance, + String displayName, + @NotNull List lore) { + this.id = Objects.requireNonNull(oraxenId, "oraxenId"); this.temperatureResistance = temperatureResistance; this.radiationResistance = radiationResistance; + this.displayName = displayName; + this.lore = List.copyOf(lore); } - @Override - public ItemStack getItemStack() { - // Получение предмета через Oraxen API - ItemBuilder itemBuilder = OraxenItems.getItemById(oraxenId); - if (itemBuilder == null) { - throw new IllegalArgumentException("Oraxen предмет с ID '" + oraxenId + "' не найден."); - } - return itemBuilder.build(); - } - - @Override - public double getTemperatureResistance() { - return temperatureResistance; - } + @Override public @NotNull String getCustomItem() { return id; } + @Override public double getTemperatureResistance() { return temperatureResistance; } + @Override public double getRadiationResistance() { return radiationResistance; } @Override - public double getRadiationResistance() { - return radiationResistance; + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemBuilder builder = OraxenItems.getItemById(id); + if (builder == null) throw new IllegalArgumentException("Oraxen предмет с ID '" + id + "' не найден"); + ItemStack itemStack = builder.build(); + ItemMeta meta = itemStack.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream() + .map(Component::text) + .toList(); + meta.lore(components); + } + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, id); + itemStack.setItemMeta(meta); + } + template = itemStack; + } + return template.clone(); } } - diff --git a/src/main/java/dev/lotus/studio/item/armor/StandardArmor.java b/src/main/java/dev/lotus/studio/item/armor/StandardArmor.java index 0ac0a04..561b0a2 100644 --- a/src/main/java/dev/lotus/studio/item/armor/StandardArmor.java +++ b/src/main/java/dev/lotus/studio/item/armor/StandardArmor.java @@ -1,42 +1,63 @@ package dev.lotus.studio.item.armor; +import dev.lotus.studio.item.ItemKeys; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; import java.util.List; +import java.util.Objects; -public class StandardArmor implements CustomItem{ - private final ItemStack itemStack; +public final class StandardArmor implements CustomItem { + private final String id; private final double temperatureResistance; private final double radiationResistance; - - public StandardArmor(Material material, String displayName, List lore, double temperatureResistance, double radiationResistance) { - this.itemStack = new ItemStack(material); + private final Material material; + private final String displayName; + private final List lore; + + private ItemStack template; + + public StandardArmor(@NotNull Material material, + String displayName, + @NotNull List lore, + double temperatureResistance, + double radiationResistance) { + this.material = Objects.requireNonNull(material, "material"); + this.displayName = displayName; + this.lore = List.copyOf(lore); this.temperatureResistance = temperatureResistance; this.radiationResistance = radiationResistance; - - ItemMeta meta = this.itemStack.getItemMeta(); - if (meta != null) { - meta.setDisplayName(displayName); - meta.setLore(lore); - this.itemStack.setItemMeta(meta); - } - } - - @Override - public ItemStack getItemStack() { - return itemStack.clone(); + this.id = material.name(); } - @Override - public double getTemperatureResistance() { - return temperatureResistance; - } + @Override public @NotNull String getCustomItem() { return id; } + @Override public double getTemperatureResistance() { return temperatureResistance; } + @Override public double getRadiationResistance() { return radiationResistance; } @Override - public double getRadiationResistance() { - return radiationResistance; + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemStack is = new ItemStack(material); + ItemMeta meta = is.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream() + .map(Component::text) + .toList(); + meta.lore(components); + } + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, id); + is.setItemMeta(meta); + } + template = is; + } + return template.clone(); } - } diff --git a/src/main/java/dev/lotus/studio/item/eat/EatItem.java b/src/main/java/dev/lotus/studio/item/eat/EatItem.java index 28f96da..c914db1 100644 --- a/src/main/java/dev/lotus/studio/item/eat/EatItem.java +++ b/src/main/java/dev/lotus/studio/item/eat/EatItem.java @@ -1,24 +1,11 @@ package dev.lotus.studio.item.eat; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; -public abstract class EatItem { - - private final double radiationValue; - private final double temperatureValue; - - public EatItem(double radiationValue, double temperatureValue) { - this.radiationValue = radiationValue; - this.temperatureValue = temperatureValue; - } - - public double getRadiationValue() { - return radiationValue; - } - - public double getTemperatureValue() { - return temperatureValue; - } - - public abstract ItemStack getItemStack(); +public interface EatItem { + @NotNull String getEatItem(); + @NotNull ItemStack getItemStack(); + int getRadiationValue(); + int getTemperatureValue(); } diff --git a/src/main/java/dev/lotus/studio/item/eat/EatItemFactory.java b/src/main/java/dev/lotus/studio/item/eat/EatItemFactory.java new file mode 100644 index 0000000..68d745d --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/eat/EatItemFactory.java @@ -0,0 +1,64 @@ +package dev.lotus.studio.item.eat; + +import dev.lotus.studio.item.Provider; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.plugin.PluginManager; + +import java.util.List; +import java.util.Locale; +import java.util.Objects; + +public final class EatItemFactory { + private final PluginManager pluginManager = Bukkit.getPluginManager(); + + public EatItem fromSection(ConfigurationSection section) { + Objects.requireNonNull(section, "section"); + + String providerStr = section.getString("provider", section.getString("type", "standard")); + Provider provider = Provider.valueOf(providerStr.toUpperCase(Locale.ROOT)); + + int radiation = section.getInt("foodValue.radiation"); + int temperature = section.getInt("foodValue.temperature"); + + switch (provider) { + case STANDARD: { + String matName = section.getString("material"); + if (matName == null) throw new IllegalArgumentException("Отсутствует material"); + Material mat = Material.valueOf(matName); + + String displayName = section.getString("displayName", null); + List lore = section.getStringList("lore"); + + return new StandardEatItem(mat, displayName, lore, radiation, temperature); + } + case ORAXEN: { + requireEnabled("Oraxen"); + String id = section.getString("oraxenId"); + if (id == null || id.isEmpty()) + throw new IllegalArgumentException("Отсутствует oraxenId"); + return new OraxenEatItem(id, radiation, temperature, + section.getString("displayName", null), + section.getStringList("lore")); + } + case NEXO: { + requireEnabled("Nexo"); + String id = section.getString("id", section.getString("nexoId")); + if (id == null || id.isEmpty()) + throw new IllegalArgumentException("Отсутствует Nexo id"); + return new NexoEatItem(id, radiation, temperature, + section.getString("displayName", null), + section.getStringList("lore")); + } + default: + throw new IllegalArgumentException("Неизвестный provider: " + provider); + } + } + + private void requireEnabled(String pluginName) { + if (!pluginManager.isPluginEnabled(pluginName)) { + throw new IllegalStateException(pluginName + " недоступен/не загружен"); + } + } +} diff --git a/src/main/java/dev/lotus/studio/item/eat/NexoEatItem.java b/src/main/java/dev/lotus/studio/item/eat/NexoEatItem.java new file mode 100644 index 0000000..2587348 --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/eat/NexoEatItem.java @@ -0,0 +1,62 @@ +package dev.lotus.studio.item.eat; + +import com.nexomc.nexo.api.NexoItems; +import com.nexomc.nexo.items.ItemBuilder; +import dev.lotus.studio.item.ItemKeys; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Objects; + +public final class NexoEatItem implements EatItem { + private final String nexoId; + private final int radiationValue; + private final int temperatureValue; + private final String displayName; + private final List lore; + + private ItemStack template; + + public NexoEatItem(@NotNull String nexoId, + int radiationValue, + int temperatureValue, + String displayName, + @NotNull List lore) { + this.nexoId = Objects.requireNonNull(nexoId, "nexoId"); + this.radiationValue = radiationValue; + this.temperatureValue = temperatureValue; + this.displayName = displayName; + this.lore = List.copyOf(lore); + } + + @Override public int getRadiationValue() { return radiationValue; } + @Override public int getTemperatureValue() { return temperatureValue; } + @Override public @NotNull String getEatItem() { return nexoId; } + + @Override + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemBuilder itemBuilder = NexoItems.itemFromId(nexoId); + if (itemBuilder == null) throw new IllegalArgumentException("Nexo item '" + nexoId + "' не найден"); + ItemStack itemStack = itemBuilder.build().clone(); + ItemMeta meta = itemStack.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream().map(Component::text).toList(); + meta.lore(components); + } + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, nexoId); + itemStack.setItemMeta(meta); + } + template = itemStack; + } + return template.clone(); + } +} diff --git a/src/main/java/dev/lotus/studio/item/eat/OraxenEatItem.java b/src/main/java/dev/lotus/studio/item/eat/OraxenEatItem.java index 524f6ee..50f71d6 100644 --- a/src/main/java/dev/lotus/studio/item/eat/OraxenEatItem.java +++ b/src/main/java/dev/lotus/studio/item/eat/OraxenEatItem.java @@ -1,29 +1,64 @@ package dev.lotus.studio.item.eat; +import dev.lotus.studio.item.ItemKeys; import io.th0rgal.oraxen.api.OraxenItems; import io.th0rgal.oraxen.items.ItemBuilder; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; -public class OraxenEatItem extends EatItem { +import java.util.List; +import java.util.Objects; +public final class OraxenEatItem implements EatItem { private final String oraxenId; + private final int radiationValue; + private final int temperatureValue; + private final String displayName; + private final List lore; - public OraxenEatItem(String oraxenId, double radiationValue, double temperatureValue) { - super(radiationValue, temperatureValue); - this.oraxenId = oraxenId; + private ItemStack template; + + public OraxenEatItem(@NotNull String oraxenId, + int radiationValue, + int temperatureValue, + String displayName, + @NotNull List lore) { + this.oraxenId = Objects.requireNonNull(oraxenId, "oraxenId"); + this.radiationValue = radiationValue; + this.temperatureValue = temperatureValue; + this.displayName = displayName; + this.lore = List.copyOf(lore); } + @Override public int getRadiationValue() { return radiationValue; } + @Override public int getTemperatureValue() { return temperatureValue; } + @Override public @NotNull String getEatItem() { return oraxenId; } + @Override - public ItemStack getItemStack() { - // Получение предмета через Oraxen API - ItemBuilder itemBuilder = OraxenItems.getItemById(oraxenId); - if (itemBuilder == null) { - throw new IllegalArgumentException("Oraxen предмет с ID '" + oraxenId + "' не найден."); + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemBuilder itemBuilder = OraxenItems.getItemById(oraxenId); + if (itemBuilder == null) { + throw new IllegalArgumentException("Oraxen предмет с ID '" + oraxenId + "' не найден."); + } + ItemStack itemStack = itemBuilder.build(); + ItemMeta meta = itemStack.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream().map(Component::text).toList(); + meta.lore(components); + } + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, oraxenId); + itemStack.setItemMeta(meta); + } + template = itemStack; } - return itemBuilder.build(); - } - - public String getOraxenId() { - return oraxenId; + return template.clone(); } } diff --git a/src/main/java/dev/lotus/studio/item/eat/StandardEatItem.java b/src/main/java/dev/lotus/studio/item/eat/StandardEatItem.java index 04e4b91..74c6e17 100644 --- a/src/main/java/dev/lotus/studio/item/eat/StandardEatItem.java +++ b/src/main/java/dev/lotus/studio/item/eat/StandardEatItem.java @@ -1,33 +1,62 @@ package dev.lotus.studio.item.eat; +import dev.lotus.studio.item.ItemKeys; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; import java.util.List; +import java.util.Objects; -public class StandardEatItem extends EatItem { +public final class StandardEatItem implements EatItem { private final Material material; + private final String materialName; private final String displayName; private final List lore; + private final int radiationValue; + private final int temperatureValue; - public StandardEatItem(Material material, String displayName, List lore, double radiationValue, double temperatureValue) { - super(radiationValue, temperatureValue); - this.material = material; + private ItemStack template; + + public StandardEatItem(@NotNull Material material, + String displayName, + @NotNull List lore, + int radiationValue, + int temperatureValue) { + this.material = Objects.requireNonNull(material, "material"); this.displayName = displayName; - this.lore = lore; + this.lore = List.copyOf(lore); + this.radiationValue = radiationValue; + this.temperatureValue = temperatureValue; + this.materialName = material.name(); } + @Override public int getRadiationValue() { return radiationValue; } + @Override public int getTemperatureValue() { return temperatureValue; } + + @Override public @NotNull String getEatItem() { return materialName; } + @Override - public ItemStack getItemStack() { - if (material == null) return null; - ItemStack itemStack = new ItemStack(material); - ItemMeta meta = itemStack.getItemMeta(); - if (meta != null) { - meta.setDisplayName(displayName); - meta.setLore(lore); - itemStack.setItemMeta(meta); + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemStack itemStack = new ItemStack(material); + ItemMeta meta = itemStack.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream().map(Component::text).toList(); + meta.lore(components); + } + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, materialName); + itemStack.setItemMeta(meta); + } + template = itemStack; } - return itemStack; + return template.clone(); } } diff --git a/src/main/java/dev/lotus/studio/item/view/NexoViewItem.java b/src/main/java/dev/lotus/studio/item/view/NexoViewItem.java new file mode 100644 index 0000000..6a692ff --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/view/NexoViewItem.java @@ -0,0 +1,60 @@ +package dev.lotus.studio.item.view; + +import dev.lotus.studio.item.ItemKeys; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; +import com.nexomc.nexo.items.ItemBuilder; +import com.nexomc.nexo.api.NexoItems; + +import java.util.List; +import java.util.Objects; + +public final class NexoViewItem implements ViewItem { + private final String viewType; + private final String displayName; + private final List lore; + private final String id; + + private ItemStack template; + + public NexoViewItem(@NotNull String id, String displayName, @NotNull List lore, @NotNull String viewType) { + this.id = Objects.requireNonNull(id, "nexoId"); + this.displayName = displayName; + this.lore = List.copyOf(lore); + this.viewType = Objects.requireNonNull(viewType, "viewType"); + } + + @Override + public @NotNull String getViewType() { + return viewType; + } + + @Override + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemBuilder itemBuilder = NexoItems.itemFromId(id); + if (itemBuilder == null) throw new IllegalArgumentException("Nexo item '" + id + "' не найден"); + ItemStack itemStack = itemBuilder.build().clone(); + ItemMeta meta = itemStack.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream() + .map(Component::text) + .toList(); + meta.lore(components); + } + + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, String.valueOf(id)); + itemStack.setItemMeta(meta); + } + template = itemStack; + } + return template.clone(); + } +} diff --git a/src/main/java/dev/lotus/studio/item/view/OraxenViewItem.java b/src/main/java/dev/lotus/studio/item/view/OraxenViewItem.java new file mode 100644 index 0000000..0e63102 --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/view/OraxenViewItem.java @@ -0,0 +1,59 @@ +package dev.lotus.studio.item.view; + +import dev.lotus.studio.item.ItemKeys; +import io.th0rgal.oraxen.api.OraxenItems; +import io.th0rgal.oraxen.items.ItemBuilder; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Objects; + +public final class OraxenViewItem implements ViewItem { + private final String viewType; + private final String displayName; + private final List lore; + private final String id; + + private ItemStack template; + + public OraxenViewItem(@NotNull String id, String displayName, @NotNull List lore, @NotNull String viewType) { + this.id = Objects.requireNonNull(id, "oraxenId"); + this.displayName = displayName; + this.lore = List.copyOf(lore); + this.viewType = Objects.requireNonNull(viewType, "viewType"); + } + + @Override + public @NotNull String getViewType() { + return viewType; + } + + @Override + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemBuilder itemBuilder = OraxenItems.getItemById(id); + if (itemBuilder == null) { throw new IllegalArgumentException("Oraxen предмет с ID '" + id + "' не найден"); } + ItemStack itemStack = itemBuilder.build(); + ItemMeta meta = itemStack.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream() + .map(Component::text) + .toList(); + meta.lore(components); + } + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, String.valueOf(id)); + itemStack.setItemMeta(meta); + } + template = itemStack; + } + return template.clone(); + } +} diff --git a/src/main/java/dev/lotus/studio/item/view/OraxenViewView.java b/src/main/java/dev/lotus/studio/item/view/OraxenViewView.java deleted file mode 100644 index a8c2b25..0000000 --- a/src/main/java/dev/lotus/studio/item/view/OraxenViewView.java +++ /dev/null @@ -1,28 +0,0 @@ -package dev.lotus.studio.item.view; - -import io.th0rgal.oraxen.api.OraxenItems; -import io.th0rgal.oraxen.items.ItemBuilder; -import org.bukkit.inventory.ItemStack; - -public class OraxenViewView extends ViewType { - private final String oraxenId; - - public OraxenViewView(String oraxenId, String viewType) { - super(viewType); - this.oraxenId = oraxenId; - } - - @Override - public ItemStack getItemStack() { - // Получение предмета через Oraxen API - ItemBuilder itemBuilder = OraxenItems.getItemById(oraxenId); - if (itemBuilder == null) { - throw new IllegalArgumentException("Oraxen предмет с ID '" + oraxenId + "' не найден."); - } - return itemBuilder.build(); - } - - public String getOraxenId() { - return oraxenId; - } -} \ No newline at end of file diff --git a/src/main/java/dev/lotus/studio/item/view/StandardViewItem.java b/src/main/java/dev/lotus/studio/item/view/StandardViewItem.java index d23d712..cf1a88d 100644 --- a/src/main/java/dev/lotus/studio/item/view/StandardViewItem.java +++ b/src/main/java/dev/lotus/studio/item/view/StandardViewItem.java @@ -1,35 +1,59 @@ package dev.lotus.studio.item.view; - +import dev.lotus.studio.item.ItemKeys; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataType; +import org.jetbrains.annotations.NotNull; import java.util.List; +import java.util.Objects; -public class StandardViewItem extends ViewType { +public final class StandardViewItem implements ViewItem { + private final String viewType; private final Material material; private final String displayName; private final List lore; - public StandardViewItem(Material material, String displayName, List lore, String viewType) { - super(viewType); - this.material = material; + private ItemStack template; + + public StandardViewItem(@NotNull Material material, + String displayName, + @NotNull List lore, + @NotNull String viewType) { + this.material = Objects.requireNonNull(material, "material"); this.displayName = displayName; - this.lore = lore; + this.lore = List.copyOf(lore); + this.viewType = Objects.requireNonNull(viewType, "viewType"); } @Override - public ItemStack getItemStack() { - if (material == null) return null; - ItemStack itemStack = new ItemStack(material); - ItemMeta meta = itemStack.getItemMeta(); - if (meta != null) { - meta.setDisplayName(displayName); - meta.setLore(lore); - itemStack.setItemMeta(meta); - } - return itemStack; + public @NotNull String getViewType() { + return viewType; } + @Override + public @NotNull ItemStack getItemStack() { + if (template == null) { + ItemStack itemStack = new ItemStack(material); + ItemMeta meta = itemStack.getItemMeta(); + if (meta != null) { + if (displayName != null) meta.displayName(Component.text(displayName)); + if (!lore.isEmpty()) { + List components = lore.stream() + .map(Component::text) + .toList(); + meta.lore(components); + } + meta.getPersistentDataContainer().set(ItemKeys.FF_ID, PersistentDataType.STRING, String.valueOf(material)); + itemStack.setItemMeta(meta); + } + template = itemStack; + } + return template.clone(); + } } diff --git a/src/main/java/dev/lotus/studio/item/view/ViewItem.java b/src/main/java/dev/lotus/studio/item/view/ViewItem.java new file mode 100644 index 0000000..b0d5813 --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/view/ViewItem.java @@ -0,0 +1,9 @@ +package dev.lotus.studio.item.view; + +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public interface ViewItem { + @NotNull String getViewType(); + @NotNull ItemStack getItemStack(); +} diff --git a/src/main/java/dev/lotus/studio/item/view/ViewItemFactory.java b/src/main/java/dev/lotus/studio/item/view/ViewItemFactory.java new file mode 100644 index 0000000..955fb15 --- /dev/null +++ b/src/main/java/dev/lotus/studio/item/view/ViewItemFactory.java @@ -0,0 +1,57 @@ +package dev.lotus.studio.item.view; + +import dev.lotus.studio.item.Provider; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.plugin.PluginManager; + +import java.util.List; +import java.util.Locale; +import java.util.Objects; + +public final class ViewItemFactory { + private final PluginManager pluginManager = Bukkit.getPluginManager(); + + public ViewItem fromSection(ConfigurationSection section) { + Objects.requireNonNull(section, "section"); + + // Поддерживаем и "provider", и старое "type" + String providerStr = section.getString("provider", section.getString("type", "standard")); + Provider provider = Provider.valueOf(providerStr.toUpperCase(Locale.ROOT)); + String viewType = section.getString("view_type", "default"); + String displayName = section.getString("displayName", null); + List lore = section.getStringList("lore"); + + switch (provider) { + case STANDARD: { + String matName = section.getString("material"); + if (matName == null) throw new IllegalArgumentException("Отсутствует material"); + Material mat = Material.valueOf(matName); + return new StandardViewItem(mat, displayName, lore, viewType); + } + case ORAXEN: { + requireEnabled("Oraxen"); + String id = section.getString("oraxenId"); + if (id == null || id.isEmpty()) + throw new IllegalArgumentException("Отсутствует oraxenId"); + return new OraxenViewItem(id, displayName, lore, viewType); + } + case NEXO: { + requireEnabled("Nexo"); + String id = section.getString("id", section.getString("nexoId")); + if (id == null || id.isEmpty()) + throw new IllegalArgumentException("Отсутствует Nexo id"); + return new NexoViewItem(id, displayName, lore, viewType); + } + default: + throw new IllegalArgumentException("Неизвестный provider: " + provider); + } + } + + private void requireEnabled(String pluginName) { + if (!pluginManager.isPluginEnabled(pluginName)) { + throw new IllegalStateException(pluginName + " недоступен/не загружен"); + } + } +} diff --git a/src/main/java/dev/lotus/studio/item/view/ViewType.java b/src/main/java/dev/lotus/studio/item/view/ViewType.java deleted file mode 100644 index 52caad5..0000000 --- a/src/main/java/dev/lotus/studio/item/view/ViewType.java +++ /dev/null @@ -1,20 +0,0 @@ -package dev.lotus.studio.item.view; - -import org.bukkit.inventory.ItemStack; - - -public abstract class ViewType { - private final String viewType; - - protected ViewType(String viewType) { - this.viewType = viewType; - } - - public String getViewType() { - return viewType; - } - - public abstract ItemStack getItemStack(); -} - - diff --git a/src/main/java/dev/lotus/studio/playerdata/PlayerBar.java b/src/main/java/dev/lotus/studio/playerdata/PlayerBar.java index 3af61c2..3b26f49 100644 --- a/src/main/java/dev/lotus/studio/playerdata/PlayerBar.java +++ b/src/main/java/dev/lotus/studio/playerdata/PlayerBar.java @@ -1,19 +1,24 @@ package dev.lotus.studio.playerdata; -import net.kyori.adventure.text.Component; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.bukkit.scheduler.BukkitRunnable; import dev.lotus.studio.Main; import dev.lotus.studio.handlers.RadiationHandler; import dev.lotus.studio.handlers.TemperatureHandler; import dev.lotus.studio.item.CustomItemManager; -import dev.lotus.studio.item.view.ViewType; +import dev.lotus.studio.item.view.ViewItem; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitTask; +import net.kyori.adventure.text.Component; public class PlayerBar { private final Main plugin; private final CustomItemManager itemManager; + private BukkitTask task; + + private static final Component SEP = Component.text(" "); + private static final String ICON_RADIATION = "☢"; + private static final String ICON_TEMPERATURE = "\uD83C\uDF21"; public PlayerBar(Main plugin, CustomItemManager itemManager) { this.plugin = plugin; @@ -21,51 +26,54 @@ public PlayerBar(Main plugin, CustomItemManager itemManager) { startActionBarTask(); } + public void stop() { + if (task != null) task.cancel(); + } + private void startActionBarTask() { - new BukkitRunnable() { - @Override - public void run() { - for (Player player : Bukkit.getOnlinePlayers()) { + this.task = Bukkit.getScheduler().runTaskTimer(plugin, () -> { + for (Player player : Bukkit.getOnlinePlayers()) { + try { updateActionBar(player); + } catch (Throwable t) { + plugin.getLogger().warning("ActionBar error for " + player.getName() + ": " + t.getMessage()); } } - }.runTaskTimer(plugin, 0, 20); // Выполнять каждую секунду + }, 0L, 20L); } private void updateActionBar(Player player) { - ViewType customItem; - customItem = itemManager.getViewItemByItemStack(player.getInventory().getItemInMainHand()); - if (customItem == null) { - customItem = itemManager.getViewItemByItemStack(player.getInventory().getItemInOffHand()); - } + var mainHand = player.getInventory().getItemInMainHand(); + var offHand = player.getInventory().getItemInOffHand(); - PlayerData playerData = PlayerManager.getInstance().getPlayerData(player); - double radiation = playerData.getRadiationValue(); - double temperature = playerData.getTemperatureValue(); - - Component radiationBar = Component.text(""); - Component temperatureBar = Component.text(""); - - if (customItem != null) { + ViewItem viewItem = itemManager.getViewItemByItemStack(mainHand); + if (viewItem == null) viewItem = itemManager.getViewItemByItemStack(offHand); - if ("RADIATION".equalsIgnoreCase(customItem.getViewType())) { - radiationBar = RadiationHandler.getInstance().createProgressBar(radiation,"☢", true); - } + var playerData = PlayerManager.getInstance().getPlayerData(player); + double radiation = playerData.getRadiationValue(); + double temperature = playerData.getTemperatureValue(); - else if ("TEMPERATURE".equalsIgnoreCase(customItem.getViewType())) { - temperatureBar = TemperatureHandler.getInstance().createProgressBar(temperature, "\uD83C\uDF21",true); + Component radiationBar = Component.empty(); + Component temperatureBar = Component.empty(); + boolean hasRadiationBar = false; + boolean hasTemperatureBar = false; + + if (viewItem != null) { + String viewType = viewItem.getViewType(); + if ("RADIATION".equalsIgnoreCase(viewType)) { + radiationBar = RadiationHandler.getInstance().createProgressBar(radiation, ICON_RADIATION, true); + hasRadiationBar = true; + } else if ("TEMPERATURE".equalsIgnoreCase(viewType)) { + temperatureBar = TemperatureHandler.getInstance().createProgressBar(temperature, ICON_TEMPERATURE, true); + hasTemperatureBar = true; } } - if (radiationBar.equals(Component.empty()) && temperatureBar.equals(Component.empty())) { - temperatureBar = TemperatureHandler.getInstance().createProgressBar(temperature, "\uD83C\uDF21", false); + if (!hasRadiationBar && !hasTemperatureBar) { + temperatureBar = TemperatureHandler.getInstance().createProgressBar(temperature, ICON_TEMPERATURE, false); } - Component actionBarMessage = radiationBar.append(Component.text(" ")).append(temperatureBar); - + Component actionBarMessage = radiationBar.append(SEP).append(temperatureBar); player.sendActionBar(actionBarMessage); } - - - } diff --git a/src/main/java/dev/lotus/studio/utils/OraxenUtils.java b/src/main/java/dev/lotus/studio/utils/OraxenUtils.java deleted file mode 100644 index 93d886a..0000000 --- a/src/main/java/dev/lotus/studio/utils/OraxenUtils.java +++ /dev/null @@ -1,10 +0,0 @@ -package dev.lotus.studio.utils; - -import org.bukkit.Bukkit; - -public class OraxenUtils { - - public static boolean isOraxenEnable(){ - return Bukkit.getPluginManager().getPlugin("Oraxen") != null; - } -} diff --git a/src/main/java/dev/lotus/studio/utils/ResourcePackUtils.java b/src/main/java/dev/lotus/studio/utils/ResourcePackUtils.java new file mode 100644 index 0000000..56a7bbc --- /dev/null +++ b/src/main/java/dev/lotus/studio/utils/ResourcePackUtils.java @@ -0,0 +1,19 @@ +package dev.lotus.studio.utils; + +import org.bukkit.Bukkit; + +public final class ResourcePackUtils { + + private ResourcePackUtils() { + // Запрещаем создание экземпляра + } + + public static boolean isOraxenEnable() { + return Bukkit.getPluginManager().getPlugin("Oraxen") != null; + } + + public static boolean isNexoEnable() { + return Bukkit.getPluginManager().getPlugin("Nexo") != null; + } +} + diff --git a/src/main/resources/items.yml b/src/main/resources/items.yml index c2a7f2b..5698dba 100644 --- a/src/main/resources/items.yml +++ b/src/main/resources/items.yml @@ -1,43 +1,31 @@ eat_item: testeat: - type: standard - material: APPLE - displayName: "Счетчик Гейгера" + type: standard # oraxen / nexo + material: APPLE # Not necessary if you use nexo or oraxen + displayName: "Warm apple" foodValue: radiation: 0 temperature: 10 - # oraxen_viewer: - # type: oraxen - # oraxenId: "oraxen_viewer_id" - # foodValue: - # radiation: 0 - # temperature: 10 +# oraxenId / nexoId: "viewer_id" view_item: winter_viewer: - type: standard - material: CLOCK - displayName: "Счетчик Гейгера" - view_type: RADIATION + type: standard # oraxen / nexo + material: CLOCK # Not necessary if you use nexo or oraxen + displayName: "Geiger counter" + view_type: RADIATION # RADIATION / TEMPERATURE lore: - - "Кажется, эта штука полезная..." - # oraxen_viewer: - # type: oraxen - # oraxenId: "oraxen_viewer_id" - # view_type: RADIATION + - "This thing seems useful..." +# oraxenId / nexoId: "viewer_id" items: snowy_chestplate: - type: standard - material: LEATHER_CHESTPLATE - displayName: "Снежный нагрудник" + type: standard # oraxen / nexo + material: LEATHER_CHESTPLATE # Not necessary if you use nexo or oraxen + displayName: "Snow chestplate" lore: - - "Греет грудь" + - "Warms the chest" temperatureResistance: 1.0 radiationResistance: 0.5 - # oraxen_chestplate: - # type: oraxen - # oraxenId: "oraxen_chestplate_id" - # temperatureResistance: 2.0 - # radiationResistance: 1.0 \ No newline at end of file +# oraxenId / nexoId: "item_id" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index e39a2e7..695bd34 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -11,5 +11,5 @@ commands: lotus: description: Перезагрузка конфигурации плагина. usage: / - permission: lotusoffseason.armor + permission: frostandfallout.armor permission-message: "У вас нет прав для выполнения этой команды." \ No newline at end of file