1
|
|
|
/* |
2
|
|
|
* This file is part of Araknemu. |
3
|
|
|
* |
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* Copyright (c) 2017-2021 Vincent Quatrevieux |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.game.admin.exception.handler; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.game.admin.AdminPerformer; |
23
|
|
|
import fr.quatrevieux.araknemu.game.admin.Command; |
24
|
|
|
import fr.quatrevieux.araknemu.game.admin.CommandParser; |
25
|
|
|
import fr.quatrevieux.araknemu.game.admin.exception.CommandNotFoundException; |
26
|
|
|
import fr.quatrevieux.araknemu.game.admin.exception.ExceptionHandler; |
27
|
|
|
import fr.quatrevieux.araknemu.game.admin.formatter.Link; |
28
|
|
|
import org.apache.commons.text.similarity.LevenshteinDistance; |
29
|
|
|
|
30
|
|
|
import java.util.Comparator; |
31
|
|
|
import java.util.Optional; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Handler for command not found |
35
|
|
|
* This handler will try to found the nearest command name on the current context |
36
|
|
|
*/ |
37
|
|
|
public final class CommandNotFoundExceptionHandler implements ExceptionHandler.Function<CommandNotFoundException> { |
38
|
|
|
@Override |
39
|
|
|
public void handle(AdminPerformer performer, CommandNotFoundException error, CommandParser.Arguments arguments) { |
40
|
|
|
performer.error("Command '{}' is not found", error.command()); |
41
|
|
|
|
42
|
|
|
if (arguments == null) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
nearestCommandName(arguments).ifPresent(commandName -> performer.error( |
47
|
|
|
"Did you mean {} ? (See {})", |
48
|
|
|
new Link().text(commandName).command(rebuildCommand(arguments, commandName)), |
49
|
|
|
new Link().text("help").execute(arguments.contextPath() + " help " + commandName) |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Found the nearest command name from the contexts |
55
|
|
|
* |
56
|
|
|
* @param arguments The parsed arguments |
57
|
|
|
* |
58
|
|
|
* @return The command name |
59
|
|
|
*/ |
60
|
|
|
private Optional<String> nearestCommandName(CommandParser.Arguments arguments) { |
61
|
|
|
return arguments.context().commands().stream() |
62
|
|
|
.map(Command::name) |
63
|
|
|
.min(Comparator.comparingInt(name -> LevenshteinDistance.getDefaultInstance().apply(arguments.command(), name))) |
64
|
|
|
; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Change the command name from the current command line |
69
|
|
|
* |
70
|
|
|
* @param arguments The parsed arguments |
71
|
|
|
* @param newName The expected command name |
72
|
|
|
* |
73
|
|
|
* @return The new command line |
74
|
|
|
*/ |
75
|
|
|
private String rebuildCommand(CommandParser.Arguments arguments, String newName) { |
76
|
|
|
final StringBuilder sb = new StringBuilder(); |
77
|
|
|
|
78
|
|
|
sb.append(arguments.contextPath()); |
79
|
|
|
sb.append(' '); |
80
|
|
|
sb.append(newName); |
81
|
|
|
|
82
|
|
|
for (int i = 1; i < arguments.arguments().size(); ++i) { |
83
|
|
|
sb.append(' ').append(arguments.arguments().get(i)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return sb.toString(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|