|
1 | 1 | package org.broken.arrow.library.command; |
2 | 2 |
|
3 | 3 |
|
| 4 | +import net.md_5.bungee.api.ChatColor; |
| 5 | +import org.broken.arrow.library.color.TextTranslator; |
4 | 6 | import org.broken.arrow.library.command.command.CommandProperty; |
5 | 7 | import org.broken.arrow.library.command.commandhandler.CommandExecutor; |
6 | 8 | import org.broken.arrow.library.command.commandhandler.CommandRegistering; |
7 | 9 | import org.broken.arrow.library.command.commandhandler.MainCommandHandler; |
8 | 10 | import org.broken.arrow.library.command.builers.CommandBuilder; |
9 | 11 | import org.broken.arrow.library.logging.Logging; |
10 | 12 | import org.bukkit.Bukkit; |
| 13 | +import org.bukkit.Color; |
11 | 14 | import org.bukkit.command.Command; |
12 | 15 | import org.bukkit.command.CommandMap; |
13 | 16 | import org.bukkit.plugin.Plugin; |
14 | 17 |
|
15 | 18 | import javax.annotation.Nonnull; |
| 19 | +import javax.print.attribute.standard.ColorSupported; |
16 | 20 | import java.lang.reflect.Field; |
17 | 21 | import java.util.Arrays; |
18 | 22 | import java.util.Collection; |
|
30 | 34 | public class CommandRegister implements CommandRegistering { |
31 | 35 | private final Logging log = new Logging(CommandRegister.class); |
32 | 36 | private final Map<String, MainCommandHandler> commands = new ConcurrentHashMap<>(); |
| 37 | + private static boolean hasColorLib; |
33 | 38 | private boolean registeredMainCommand; |
34 | 39 |
|
| 40 | + public CommandRegister() { |
| 41 | + try { |
| 42 | + TextTranslator.getInstance(); |
| 43 | + hasColorLib = true; |
| 44 | + } catch (NoClassDefFoundError | NoSuchMethodError exception) { |
| 45 | + hasColorLib = false; |
| 46 | + } |
| 47 | + } |
| 48 | + |
35 | 49 | @Override |
36 | 50 | public CommandBuilder registerCommand(final Plugin plugin, final String mainCommand) { |
37 | 51 | final CommandBuilder commandBuilder = new CommandBuilder(); |
@@ -84,6 +98,19 @@ public void setRegisteredMainCommand(final boolean registeredMainCommand) { |
84 | 98 | this.registeredMainCommand = registeredMainCommand; |
85 | 99 | } |
86 | 100 |
|
| 101 | + /** |
| 102 | + * Translate colors on a text. |
| 103 | + * |
| 104 | + * @param message the message to translate the color codes. |
| 105 | + * @return Array of strings that has formated colors. |
| 106 | + */ |
| 107 | + public static String translateColors(final String message) { |
| 108 | + if (message == null) return ""; |
| 109 | + if (hasColorLib) |
| 110 | + return TextTranslator.toSpigotFormat(message); |
| 111 | + return ChatColor.translateAlternateColorCodes('&', message); |
| 112 | + } |
| 113 | + |
87 | 114 | /** |
88 | 115 | * Registers the main command with the specified fallback prefix, command, description, usage message, and aliases. |
89 | 116 | * If the main command has already been registered, this method does nothing. |
|
0 commit comments