|
| 1 | +package fr.zcraft.quartzlib.components.commands.exceptions; |
| 2 | + |
| 3 | +import fr.zcraft.quartzlib.components.commands.CommandGroup; |
| 4 | +import fr.zcraft.quartzlib.components.commands.CommandNode; |
| 5 | +import fr.zcraft.quartzlib.components.i18n.I; |
| 6 | +import fr.zcraft.quartzlib.components.rawtext.RawText; |
| 7 | +import fr.zcraft.quartzlib.components.rawtext.RawTextPart; |
| 8 | +import org.bukkit.ChatColor; |
| 9 | +import org.bukkit.command.CommandSender; |
| 10 | + |
| 11 | +public class MissingSubcommandException extends CommandException { |
| 12 | + private final CommandGroup commandGroup; |
| 13 | + |
| 14 | + public MissingSubcommandException(CommandGroup commandGroup) { |
| 15 | + this.commandGroup = commandGroup; |
| 16 | + } |
| 17 | + |
| 18 | + @Override |
| 19 | + public RawText display(CommandSender sender) { |
| 20 | + RawTextPart<?> text = new RawText(I.t("Missing subcommand: ")) |
| 21 | + .color(ChatColor.RED) |
| 22 | + .then("/").color(ChatColor.WHITE) |
| 23 | + .then(getParents()).color(ChatColor.AQUA) |
| 24 | + .then(" <").style(ChatColor.GRAY) |
| 25 | + .then(I.t("sub-command")) |
| 26 | + .style(ChatColor.GRAY, ChatColor.UNDERLINE) |
| 27 | + .hover(appendSubCommandList(new RawText())) |
| 28 | + .then(">").style(ChatColor.GRAY); |
| 29 | + |
| 30 | + return text.build(); |
| 31 | + } |
| 32 | + |
| 33 | + private String getParents() { |
| 34 | + StringBuilder builder = new StringBuilder(); |
| 35 | + |
| 36 | + CommandGroup group = commandGroup; |
| 37 | + |
| 38 | + do { |
| 39 | + if (builder.length() > 0) { |
| 40 | + builder.append(' '); |
| 41 | + } |
| 42 | + builder.append(group.getName()); |
| 43 | + group = group.getParent(); |
| 44 | + } while (group != null); |
| 45 | + |
| 46 | + return builder.toString(); |
| 47 | + } |
| 48 | + |
| 49 | + private RawTextPart<?> appendSubCommandList(RawTextPart<?> text) { |
| 50 | + boolean first = true; |
| 51 | + text = text.then(I.t("One of the following:\n ")); |
| 52 | + for (CommandNode subCommand : commandGroup.getSubCommands()) { |
| 53 | + if (!first) { |
| 54 | + text = text.then(", ").color(ChatColor.GRAY); |
| 55 | + } |
| 56 | + first = false; |
| 57 | + |
| 58 | + text = text.then(subCommand.getName()).color(ChatColor.AQUA); |
| 59 | + } |
| 60 | + |
| 61 | + return text; |
| 62 | + } |
| 63 | +} |
0 commit comments