|
| 1 | +package dev.felnull.commands; |
| 2 | + |
| 3 | +import net.kyori.adventure.text.format.NamedTextColor; |
| 4 | +import net.md_5.bungee.api.chat.ClickEvent; |
| 5 | +import net.md_5.bungee.api.chat.ComponentBuilder; |
| 6 | +import net.md_5.bungee.api.chat.HoverEvent; |
| 7 | +import net.md_5.bungee.api.chat.TextComponent; |
| 8 | +import org.bukkit.Bukkit; |
| 9 | +import org.bukkit.ChatColor; |
| 10 | +import org.bukkit.command.CommandSender; |
| 11 | +import org.bukkit.entity.Player; |
| 12 | + |
| 13 | +import static net.kyori.adventure.text.format.NamedTextColor.*; |
| 14 | + |
| 15 | +public class ComponentUtil { |
| 16 | + private static final boolean USE_ADVENTURE = isAdventureSupported(); |
| 17 | + |
| 18 | + private static boolean isAdventureSupported() { |
| 19 | + String version = Bukkit.getBukkitVersion(); // 例: 1.16.5-R0.1-SNAPSHOT |
| 20 | + String[] parts = version.split("\\."); |
| 21 | + int major = Integer.parseInt(parts[0]); |
| 22 | + int minor = Integer.parseInt(parts[1]); |
| 23 | + return major > 1 || (major == 1 && minor >= 16); |
| 24 | + } |
| 25 | + |
| 26 | + public static void sendClickableMessage(CommandSender sender, String label, String command, String hoverText) { |
| 27 | + if (USE_ADVENTURE) { |
| 28 | + // Paper 1.16.5以降 |
| 29 | + net.kyori.adventure.text.Component msg = |
| 30 | + net.kyori.adventure.text.Component.text(" - [ ") |
| 31 | + .append(net.kyori.adventure.text.Component.text(label) |
| 32 | + .clickEvent(net.kyori.adventure.text.event.ClickEvent.suggestCommand(command)) |
| 33 | + .hoverEvent(net.kyori.adventure.text.event.HoverEvent.showText( |
| 34 | + net.kyori.adventure.text.Component.text(hoverText)))) |
| 35 | + .append(net.kyori.adventure.text.Component.text(" ]")); |
| 36 | + |
| 37 | + sender.sendMessage(msg); |
| 38 | + } else { |
| 39 | + // Spigot 1.15.2以前 |
| 40 | + if (sender instanceof Player) { |
| 41 | + Player player = (Player) sender; |
| 42 | + TextComponent base = new TextComponent(" - [ "); |
| 43 | + TextComponent clickable = new TextComponent(label); |
| 44 | + clickable.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, command)); |
| 45 | + clickable.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, |
| 46 | + new ComponentBuilder(hoverText).create())); |
| 47 | + base.addExtra(clickable); |
| 48 | + base.addExtra(" ]"); |
| 49 | + player.spigot().sendMessage(base); |
| 50 | + } else { |
| 51 | + sender.sendMessage(" - [ " + label + " ] (" + hoverText + ")"); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static void sendPlainMessage(CommandSender sender, String text, NamedTextColor color) { |
| 57 | + if (USE_ADVENTURE) { |
| 58 | + net.kyori.adventure.text.Component msg = net.kyori.adventure.text.Component.text(text).color(color); |
| 59 | + sender.sendMessage(msg); |
| 60 | + } else { |
| 61 | + sender.sendMessage(namedTextColorToLegacy(color) + text); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static String namedTextColorToLegacy(NamedTextColor color) { |
| 66 | + // 最小限の変換(必要に応じて追加) |
| 67 | + if (color.equals(RED)) { |
| 68 | + return ChatColor.RED.toString(); |
| 69 | + } else if (color.equals(GREEN)) { |
| 70 | + return ChatColor.GREEN.toString(); |
| 71 | + } else if (color.equals(BLUE)) { |
| 72 | + return ChatColor.BLUE.toString(); |
| 73 | + } else if (color.equals(YELLOW)) { |
| 74 | + return ChatColor.YELLOW.toString(); |
| 75 | + } else if (color.equals(AQUA)) { |
| 76 | + return ChatColor.AQUA.toString(); |
| 77 | + } else if (color.equals(GRAY)) { |
| 78 | + return ChatColor.GRAY.toString(); |
| 79 | + } else if (color.equals(DARK_GRAY)) { |
| 80 | + return ChatColor.DARK_GRAY.toString(); |
| 81 | + } else if (color.equals(GOLD)) { |
| 82 | + return ChatColor.GOLD.toString(); |
| 83 | + } else if (color.equals(WHITE)) { |
| 84 | + return ChatColor.WHITE.toString(); |
| 85 | + } else if (color.equals(BLACK)) { |
| 86 | + return ChatColor.BLACK.toString(); |
| 87 | + } else if (color.equals(DARK_RED)) { |
| 88 | + return ChatColor.DARK_RED.toString(); |
| 89 | + } else if (color.equals(DARK_GREEN)) { |
| 90 | + return ChatColor.DARK_GREEN.toString(); |
| 91 | + } else if (color.equals(DARK_BLUE)) { |
| 92 | + return ChatColor.DARK_BLUE.toString(); |
| 93 | + } else if (color.equals(DARK_PURPLE)) { |
| 94 | + return ChatColor.DARK_PURPLE.toString(); |
| 95 | + } else if (color.equals(LIGHT_PURPLE)) { |
| 96 | + return ChatColor.LIGHT_PURPLE.toString(); |
| 97 | + } |
| 98 | + return ChatColor.RESET.toString(); |
| 99 | + } |
| 100 | +} |
0 commit comments