|
| 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 fr.zcraft.quartzlib.tools.text.StringUtils; |
| 9 | +import org.bukkit.ChatColor; |
| 10 | +import org.bukkit.command.CommandSender; |
| 11 | +import org.jetbrains.annotations.Nullable; |
| 12 | + |
| 13 | +public class UnknownSubcommandException extends CommandException { |
| 14 | + public static final String CHAT_PREFIX = "┃ "; |
| 15 | + private final CommandGroup commandGroup; |
| 16 | + private final String attemptedSubcommand; |
| 17 | + |
| 18 | + public UnknownSubcommandException(CommandGroup commandGroup, String attemptedSubcommand) { |
| 19 | + this.commandGroup = commandGroup; |
| 20 | + this.attemptedSubcommand = attemptedSubcommand; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public RawText display(CommandSender sender) { |
| 25 | + RawTextPart<?> text = new RawText(CHAT_PREFIX).color(ChatColor.DARK_RED) |
| 26 | + .then(I.t("Unknown subcommand: ")).color(ChatColor.RED) |
| 27 | + .then("/").color(ChatColor.WHITE) |
| 28 | + .then(getParents() + " ").color(ChatColor.AQUA) |
| 29 | + .then(attemptedSubcommand).style(ChatColor.RED, ChatColor.UNDERLINE, ChatColor.BOLD) |
| 30 | + .hover(appendSubCommandList(new RawText())); |
| 31 | + |
| 32 | + String nearest = getNearestCommand(); |
| 33 | + |
| 34 | + if (nearest != null) { |
| 35 | + text = text.then("\n" + CHAT_PREFIX).color(ChatColor.AQUA) |
| 36 | + .then(" " + I.t("Did you mean") + ": ").color(ChatColor.GRAY) |
| 37 | + .then("/").color(ChatColor.WHITE) |
| 38 | + .then(getParents() + " ").color(ChatColor.AQUA) |
| 39 | + .then(nearest).color(ChatColor.DARK_AQUA); |
| 40 | + } |
| 41 | + |
| 42 | + return text.build(); |
| 43 | + } |
| 44 | + |
| 45 | + private static final int MAX_DISTANCE = 10; |
| 46 | + |
| 47 | + @Nullable |
| 48 | + private String getNearestCommand() { |
| 49 | + String nearest = null; |
| 50 | + int nearestDistance = MAX_DISTANCE; |
| 51 | + |
| 52 | + for (CommandNode subCommand : commandGroup.getSubCommands()) { |
| 53 | + String name = subCommand.getName(); |
| 54 | + int distance = StringUtils.levenshteinDistance(attemptedSubcommand, name); |
| 55 | + |
| 56 | + if (distance < nearestDistance) { |
| 57 | + nearest = name; |
| 58 | + nearestDistance = distance; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return nearest; |
| 63 | + } |
| 64 | + |
| 65 | + private String getParents() { |
| 66 | + StringBuilder builder = new StringBuilder(); |
| 67 | + |
| 68 | + CommandGroup group = commandGroup; |
| 69 | + |
| 70 | + do { |
| 71 | + if (builder.length() > 0) { |
| 72 | + builder.append(' '); |
| 73 | + } |
| 74 | + builder.append(group.getName()); |
| 75 | + group = group.getParent(); |
| 76 | + } while (group != null); |
| 77 | + |
| 78 | + return builder.toString(); |
| 79 | + } |
| 80 | + |
| 81 | + private RawTextPart<?> appendSubCommandList(RawTextPart<?> text) { |
| 82 | + boolean first = true; |
| 83 | + text = text.then(I.t("Should be one of the following:\n ")); |
| 84 | + for (CommandNode subCommand : commandGroup.getSubCommands()) { |
| 85 | + if (!first) { |
| 86 | + text = text.then(", ").color(ChatColor.GRAY); |
| 87 | + } |
| 88 | + first = false; |
| 89 | + |
| 90 | + text = text.then(subCommand.getName()).color(ChatColor.AQUA); |
| 91 | + } |
| 92 | + |
| 93 | + return text; |
| 94 | + } |
| 95 | +} |
0 commit comments