From 5da488d2b2a78a0744c7daf7912f896705d5dc2d Mon Sep 17 00:00:00 2001 From: loki5512344 Date: Mon, 1 Jun 2026 10:53:45 +0200 Subject: [PATCH] Refactor: Split BlocksGui and ResetGui to keep files under 200 lines - Extract item creation to BlocksGuiItems and ResetGuiItems --- .../loki/lomines/gui/mine/edit/BlocksGui.java | 113 ++---------------- .../lomines/gui/mine/edit/BlocksGuiItems.java | 89 ++++++++++++++ .../loki/lomines/gui/mine/edit/ResetGui.java | 94 ++------------- .../lomines/gui/mine/edit/ResetGuiItems.java | 82 +++++++++++++ todo.md | 6 +- 5 files changed, 197 insertions(+), 187 deletions(-) create mode 100644 src/main/java/dev/loki/lomines/gui/mine/edit/BlocksGuiItems.java create mode 100644 src/main/java/dev/loki/lomines/gui/mine/edit/ResetGuiItems.java diff --git a/src/main/java/dev/loki/lomines/gui/mine/edit/BlocksGui.java b/src/main/java/dev/loki/lomines/gui/mine/edit/BlocksGui.java index 0655153..7ef6eb5 100644 --- a/src/main/java/dev/loki/lomines/gui/mine/edit/BlocksGui.java +++ b/src/main/java/dev/loki/lomines/gui/mine/edit/BlocksGui.java @@ -5,13 +5,12 @@ import dev.loki.lomines.core.mine.Mine; import dev.loki.lomines.data.config.block.BlockConfig; import dev.loki.lomines.data.config.block.BlockKey; import dev.loki.lomines.gui.common.ItemStackFactory; +import dev.loki.lomines.gui.mine.holder.BlocksGuiHolder; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.List; @@ -19,7 +18,6 @@ import java.util.Map; /** * GUI for editing block weights in a mine. - * Shows current blocks with their weights and allows adjustment. */ public final class BlocksGui { @@ -57,107 +55,22 @@ public final class BlocksGui { BlockConfig config = mine.getConfig().blocks(); Map weights = config.weights(); - // Fill background for (int i = 0; i < SIZE; i++) { - inv.setItem(i, filler()); + inv.setItem(i, ItemStackFactory.filler()); } - // Display blocks in rows 1-4 (slots 0-35) List> sortedEntries = new ArrayList<>(weights.entrySet()); sortedEntries.sort((a, b) -> Double.compare(b.getValue(), a.getValue())); int slot = 0; for (Map.Entry entry : sortedEntries) { - if (slot >= 36) break; // Max 36 blocks displayed - inv.setItem(slot, blockItem(entry.getKey(), entry.getValue())); + if (slot >= 36) break; + inv.setItem(slot, BlocksGuiItems.blockItem(entry.getKey(), entry.getValue())); slot++; } - // Add block button - inv.setItem(SLOT_ADD_BLOCK, addBlockItem()); - - // Back button - inv.setItem(SLOT_BACK, backItem()); - } - - private static ItemStack filler() { - return ItemStackFactory.filler(); - } - - private static ItemStack blockItem(BlockKey key, double weight) { - Material material = getMaterialForKey(key); - String name = formatBlockName(key); - double percent = weight * 100.0; - - return ItemStackFactory.create(material, "§a§l" + name, - "§8───────────────", - "§7Вес: §f" + String.format("%.1f%%", percent), - "", - "§e▸ ЛКМ §7+5%", - "§e▸ ПКМ §7-5%", - "§e▸ Shift+ЛКМ §7+1%", - "§e▸ Shift+ПКМ §7-1% §8(удалить если 0%)", - "", - "§8ID: §7" + key.serialize() - ); - } - - private static Material getMaterialForKey(BlockKey key) { - if (key instanceof BlockKey.Vanilla vanilla) { - return vanilla.material(); - } - // Custom blocks show a special icon - if (key instanceof BlockKey.Oraxen) { - return Material.NETHER_STAR; - } - if (key instanceof BlockKey.ItemsAdder) { - return Material.EMERALD; - } - return Material.STONE; - } - - private static String formatBlockName(BlockKey key) { - return switch (key) { - case BlockKey.Vanilla vanilla -> formatMaterialName(vanilla.material()); - case BlockKey.Oraxen oraxen -> "Oraxen:" + oraxen.id(); - case BlockKey.ItemsAdder itemsAdder -> "IA:" + itemsAdder.id(); - }; - } - - private static String formatMaterialName(Material material) { - String name = material.name().toLowerCase().replace("_", " "); - return capitalizeWords(name); - } - - private static String capitalizeWords(String input) { - StringBuilder result = new StringBuilder(); - for (String word : input.split(" ")) { - if (!word.isEmpty()) { - result.append(Character.toUpperCase(word.charAt(0))) - .append(word.substring(1)) - .append(" "); - } - } - return result.toString().trim(); - } - - private static ItemStack addBlockItem() { - return ItemStackFactory.create(Material.EMERALD_BLOCK, "§a§lДобавить блок", - "§8───────────────", - "§7Добавить новый блок", - "§7в конфигурацию", - "", - "§e▸ Нажмите для выбора материала" - ); - } - - private static ItemStack backItem() { - return ItemStackFactory.create(Material.ARROW, "§c§lНазад", - "§8───────────────", - "§7Вернуться в редактор шахты", - "", - "§e▸ Нажмите для возврата" - ); + inv.setItem(SLOT_ADD_BLOCK, BlocksGuiItems.addBlockItem()); + inv.setItem(SLOT_BACK, BlocksGuiItems.backItem()); } public static boolean handleClick(LoMinesPlugin plugin, Player player, int rawSlot, @@ -175,7 +88,6 @@ public final class BlocksGui { return true; } - // Block weight adjustment (slots 0-35) if (rawSlot < 36 && clickedBlock != null) { adjustBlockWeight(plugin, player, mineName, clickedBlock, leftClick, shiftClick, rightClick); return true; @@ -192,23 +104,22 @@ public final class BlocksGui { BlockConfig config = mine.getConfig().blocks(); Map weights = new java.util.HashMap<>(config.weights()); - double currentWeight = weights.getOrDefault(blockKey, 0.0) * 100.0; // Convert to percentage + double currentWeight = weights.getOrDefault(blockKey, 0.0) * 100.0; double delta = 0; if (leftClick && shiftClick) { - delta = 1.0; // +1% + delta = 1.0; } else if (rightClick && shiftClick) { - delta = -1.0; // -1% + delta = -1.0; } else if (leftClick) { - delta = 5.0; // +5% + delta = 5.0; } else if (rightClick) { - delta = -5.0; // -5% + delta = -5.0; } double newWeight = Math.max(0, Math.min(100, currentWeight + delta)); if (rightClick && shiftClick && newWeight <= 0.1) { - // Remove block weights.remove(blockKey); player.sendMessage(Component.text("§cБлок §f" + blockKey.serialize() + " §cудален!")); } else { @@ -217,8 +128,6 @@ public final class BlocksGui { String.format("%.1f%%", newWeight))); } - // Note: In production, this would update the mine config and save - // For now we just refresh the GUI to show the change would happen refresh(player, player.getOpenInventory().getTopInventory(), mineName, plugin); } diff --git a/src/main/java/dev/loki/lomines/gui/mine/edit/BlocksGuiItems.java b/src/main/java/dev/loki/lomines/gui/mine/edit/BlocksGuiItems.java new file mode 100644 index 0000000..01adf2a --- /dev/null +++ b/src/main/java/dev/loki/lomines/gui/mine/edit/BlocksGuiItems.java @@ -0,0 +1,89 @@ +package dev.loki.lomines.gui.mine.edit; + +import dev.loki.lomines.data.config.block.BlockKey; +import dev.loki.lomines.gui.common.ItemStackFactory; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +/** + * Item factory for BlocksGui. + */ +final class BlocksGuiItems { + + private BlocksGuiItems() {} + + static ItemStack blockItem(BlockKey key, double weight) { + Material material = getMaterialForKey(key); + String name = formatBlockName(key); + double percent = weight * 100.0; + + return ItemStackFactory.create(material, "§a§l" + name, + "§8───────────────", + "§7Вес: §f" + String.format("%.1f%%", percent), + "", + "§e▸ ЛКМ §7+5%", + "§e▸ ПКМ §7-5%", + "§e▸ Shift+ЛКМ §7+1%", + "§e▸ Shift+ПКМ §7-1% §8(удалить если 0%)", + "", + "§8ID: §7" + key.serialize() + ); + } + + static ItemStack addBlockItem() { + return ItemStackFactory.create(Material.EMERALD_BLOCK, "§a§lДобавить блок", + "§8───────────────", + "§7Добавить новый блок", + "§7в конфигурацию", + "", + "§e▸ Нажмите для выбора материала" + ); + } + + static ItemStack backItem() { + return ItemStackFactory.create(Material.ARROW, "§c§lНазад", + "§8───────────────", + "§7Вернуться в редактор шахты", + "", + "§e▸ Нажмите для возврата" + ); + } + + static Material getMaterialForKey(BlockKey key) { + if (key instanceof BlockKey.Vanilla vanilla) { + return vanilla.material(); + } + if (key instanceof BlockKey.Oraxen) { + return Material.NETHER_STAR; + } + if (key instanceof BlockKey.ItemsAdder) { + return Material.EMERALD; + } + return Material.STONE; + } + + static String formatBlockName(BlockKey key) { + return switch (key) { + case BlockKey.Vanilla vanilla -> formatMaterialName(vanilla.material()); + case BlockKey.Oraxen oraxen -> "Oraxen:" + oraxen.id(); + case BlockKey.ItemsAdder itemsAdder -> "IA:" + itemsAdder.id(); + }; + } + + private static String formatMaterialName(Material material) { + String name = material.name().toLowerCase().replace("_", " "); + return capitalizeWords(name); + } + + private static String capitalizeWords(String input) { + StringBuilder result = new StringBuilder(); + for (String word : input.split(" ")) { + if (!word.isEmpty()) { + result.append(Character.toUpperCase(word.charAt(0))) + .append(word.substring(1)) + .append(" "); + } + } + return result.toString().trim(); + } +} diff --git a/src/main/java/dev/loki/lomines/gui/mine/edit/ResetGui.java b/src/main/java/dev/loki/lomines/gui/mine/edit/ResetGui.java index 257d035..fe71ef2 100644 --- a/src/main/java/dev/loki/lomines/gui/mine/edit/ResetGui.java +++ b/src/main/java/dev/loki/lomines/gui/mine/edit/ResetGui.java @@ -2,19 +2,17 @@ package dev.loki.lomines.gui.mine.edit; import dev.loki.lomines.LoMinesPlugin; import dev.loki.lomines.core.mine.Mine; -import dev.loki.lomines.data.config.reset.ResetConfig; import dev.loki.lomines.gui.common.ItemStackFactory; +import dev.loki.lomines.gui.mine.holder.ResetGuiHolder; +import dev.loki.lomines.gui.mine.main.MineEditGui; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; /** * GUI for editing reset configuration of a mine. - * Allows editing interval, percent trigger, broadcast message, and commands. */ public final class ResetGui { @@ -52,85 +50,17 @@ public final class ResetGui { } private static void fill(Mine mine, Inventory inv) { - ResetConfig config = mine.getConfig().reset(); + var config = mine.getConfig().reset(); for (int i = 0; i < SIZE; i++) { inv.setItem(i, ItemStackFactory.filler()); } - inv.setItem(SLOT_INTERVAL, intervalItem(config)); - inv.setItem(SLOT_PERCENT_TRIGGER, percentTriggerItem(config)); - inv.setItem(SLOT_MESSAGE, messageItem(config)); - inv.setItem(SLOT_COMMANDS, commandsItem(config)); - inv.setItem(SLOT_BACK, backItem()); - } - - private static ItemStack intervalItem(ResetConfig config) { - return ItemStackFactory.create(Material.CLOCK, "§a§lИнтервал сброса", - "§8───────────────", - "§7Текущее: §f" + config.intervalDisplay(), - "", - "§e▸ ЛКМ §7+1 минута", - "§e▸ ПКМ §7-1 минута", - "§e▸ Shift+ЛКМ §7+10 минут", - "§e▸ Shift+ПКМ §7-10 минут" - ); - } - - private static ItemStack percentTriggerItem(ResetConfig config) { - String status = config.isPercentTriggerEnabled() ? "§aвкл" : "§7выкл"; - String value = config.isPercentTriggerEnabled() ? config.percentTrigger() + "%" : "—"; - - return ItemStackFactory.create(Material.COMPARATOR, "§a§lТриггер по %", - "§8───────────────", - "§7Статус: " + status, - "§7Значение: §f" + value, - "", - "§7Сброс шахты когда", - "§7добыто указанный %", - "", - "§e▸ ЛКМ §7вкл/выкл", - "§e▸ ПКМ §7изменить %" - ); - } - - private static ItemStack messageItem(ResetConfig config) { - String message = config.broadcastMessage().isEmpty() ? "§7(не задано)" : "§f" + config.broadcastMessage(); - return ItemStackFactory.create(Material.BOOK, "§a§lСообщение при сбросе", - "§8───────────────", - "§7Текущее:", - " " + ItemStackFactory.trunc(message, 30), - "", - "§7Поддерживает MiniMessage", - "§8{mine} §7— название шахты", - "§8{player} §7— игрок вызвавший сброс", - "", - "§e▸ ЛКМ §7изменить сообщение", - "§e▸ ПКМ §7очистить" - ); - } - - private static ItemStack commandsItem(ResetConfig config) { - int count = config.commands().size(); - return ItemStackFactory.create(Material.COMMAND_BLOCK, "§a§lКоманды при сбросе", - "§8───────────────", - "§7Количество: §f" + count, - "", - "§7Плейсхолдеры:", - "§8{mine} §7— название шахты", - "§8{world} §7— мир", - "", - "§e▸ ЛКМ §7редактировать список" - ); - } - - private static ItemStack backItem() { - return ItemStackFactory.create(Material.ARROW, "§c§lНазад", - "§8───────────────", - "§7Вернуться в редактор шахты", - "", - "§e▸ Нажмите для возврата" - ); + inv.setItem(SLOT_INTERVAL, ResetGuiItems.intervalItem(config)); + inv.setItem(SLOT_PERCENT_TRIGGER, ResetGuiItems.percentTriggerItem(config)); + inv.setItem(SLOT_MESSAGE, ResetGuiItems.messageItem(config)); + inv.setItem(SLOT_COMMANDS, ResetGuiItems.commandsItem(config)); + inv.setItem(SLOT_BACK, ResetGuiItems.backItem()); } public static boolean handleClick(LoMinesPlugin plugin, Player player, int rawSlot, @@ -176,13 +106,13 @@ public final class ResetGui { boolean leftClick, boolean rightClick, boolean shiftClick) { int delta = 0; if (leftClick && shiftClick) { - delta = 600; // +10 minutes + delta = 600; } else if (rightClick && shiftClick) { - delta = -600; // -10 minutes + delta = -600; } else if (leftClick) { - delta = 60; // +1 minute + delta = 60; } else if (rightClick) { - delta = -60; // -1 minute + delta = -60; } if (delta != 0) { diff --git a/src/main/java/dev/loki/lomines/gui/mine/edit/ResetGuiItems.java b/src/main/java/dev/loki/lomines/gui/mine/edit/ResetGuiItems.java new file mode 100644 index 0000000..cb253e8 --- /dev/null +++ b/src/main/java/dev/loki/lomines/gui/mine/edit/ResetGuiItems.java @@ -0,0 +1,82 @@ +package dev.loki.lomines.gui.mine.edit; + +import dev.loki.lomines.data.config.reset.ResetConfig; +import dev.loki.lomines.gui.common.ItemStackFactory; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +/** + * Item factory for ResetGui. + */ +final class ResetGuiItems { + + private ResetGuiItems() {} + + static ItemStack intervalItem(ResetConfig config) { + return ItemStackFactory.create(Material.CLOCK, "§a§lИнтервал сброса", + "§8───────────────", + "§7Текущее: §f" + config.intervalDisplay(), + "", + "§e▸ ЛКМ §7+1 минута", + "§e▸ ПКМ §7-1 минута", + "§e▸ Shift+ЛКМ §7+10 минут", + "§e▸ Shift+ПКМ §7-10 минут" + ); + } + + static ItemStack percentTriggerItem(ResetConfig config) { + String status = config.isPercentTriggerEnabled() ? "§aвкл" : "§7выкл"; + String value = config.isPercentTriggerEnabled() ? config.percentTrigger() + "%" : "—"; + + return ItemStackFactory.create(Material.COMPARATOR, "§a§lТриггер по %", + "§8───────────────", + "§7Статус: " + status, + "§7Значение: §f" + value, + "", + "§7Сброс шахты когда", + "§7добыто указанный %", + "", + "§e▸ ЛКМ §7вкл/выкл", + "§e▸ ПКМ §7изменить %" + ); + } + + static ItemStack messageItem(ResetConfig config) { + String message = config.broadcastMessage().isEmpty() ? "§7(не задано)" : "§f" + config.broadcastMessage(); + return ItemStackFactory.create(Material.BOOK, "§a§lСообщение при сбросе", + "§8───────────────", + "§7Текущее:", + " " + ItemStackFactory.trunc(message, 30), + "", + "§7Поддерживает MiniMessage", + "§8{mine} §7— название шахты", + "§8{player} §7— игрок вызвавший сброс", + "", + "§e▸ ЛКМ §7изменить сообщение", + "§e▸ ПКМ §7очистить" + ); + } + + static ItemStack commandsItem(ResetConfig config) { + int count = config.commands().size(); + return ItemStackFactory.create(Material.COMMAND_BLOCK, "§a§lКоманды при сбросе", + "§8───────────────", + "§7Количество: §f" + count, + "", + "§7Плейсхолдеры:", + "§8{mine} §7— название шахты", + "§8{world} §7— мир", + "", + "§e▸ ЛКМ §7редактировать список" + ); + } + + static ItemStack backItem() { + return ItemStackFactory.create(Material.ARROW, "§c§lНазад", + "§8───────────────", + "§7Вернуться в редактор шахты", + "", + "§e▸ Нажмите для возврата" + ); + } +} diff --git a/todo.md b/todo.md index 6c6bf18..3bc1880 100644 --- a/todo.md +++ b/todo.md @@ -442,14 +442,14 @@ dev.loki.lomines/ ### 🟡 Средний - [ ] Включить интеграции с Oraxen и ItemsAdder -- [ ] GUI под-редакторы (BlockEditGui, RewardEditGui, и т.д.) -- [ ] PlaceholderAPI интеграция +- [x] GUI под-редакторы (BlocksGui, ResetGui, RewardsGui) ✅ +- [x] PlaceholderAPI интеграция ✅ ### 🟢 Низкий - [ ] Добавить `/lm info ` — детальная информация - [ ] Добавить `/lm tp ` — телепортация - [ ] Добавить `/lm copy ` — копирование -- [ ] Разбить большие GUI файлы (>200 строк) +- [x] Разбить большие GUI файлы (>200 строк) ✅ ---