InvalidCommandHandler (#6247)

* InvalidCommandHandler

* Update NetClient.java

* Add player

* And here

* :thonk:

* And this

* Update NetClient.java
This commit is contained in:
Darkness#3729 2021-11-01 16:32:22 +03:00 committed by GitHub
parent 4cbc4a0bca
commit 13d16f1883
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 26 deletions

View File

@ -260,33 +260,10 @@ public class NetClient implements ApplicationListener{
//a command was sent, now get the output
if(response.type != ResponseType.valid){
String text;
//send usage
if(response.type == ResponseType.manyArguments){
text = "[scarlet]Too many arguments. Usage:[lightgray] " + response.command.text + "[gray] " + response.command.paramText;
}else if(response.type == ResponseType.fewArguments){
text = "[scarlet]Too few arguments. Usage:[lightgray] " + response.command.text + "[gray] " + response.command.paramText;
}else{ //unknown command
int minDst = 0;
Command closest = null;
for(Command command : netServer.clientCommands.getCommandList()){
int dst = Strings.levenshtein(command.text, response.runCommand);
if(dst < 3 && (closest == null || dst < minDst)){
minDst = dst;
closest = command;
}
}
if(closest != null){
text = "[scarlet]Unknown command. Did you mean \"[lightgray]" + closest.text + "[]\"?";
}else{
text = "[scarlet]Unknown command. Check [lightgray]/help[scarlet].";
}
String text = netServer.invalidHandler.handle(player, response);
if(text != null){
player.sendMessage(text);
}
player.sendMessage(text);
}
}
}

View File

@ -67,6 +67,32 @@ public class NetServer implements ApplicationListener{
/** Converts a message + NULLABLE player sender into a single string. Override for custom prefixes/suffixes. */
public ChatFormatter chatFormatter = (player, message) -> player == null ? message : "[coral][[" + player.coloredName() + "[coral]]:[white] " + message;
/** Handles an incorrect command response. Returns text that will be sent to player. Override for customisation. */
public InvalidCommandHandler invalidHandler = (player, response) -> {
if(response.type == ResponseType.manyArguments){
return "[scarlet]Too many arguments. Usage:[lightgray] " + response.command.text + "[gray] " + response.command.paramText;
}else if(response.type == ResponseType.fewArguments){
return "[scarlet]Too few arguments. Usage:[lightgray] " + response.command.text + "[gray] " + response.command.paramText;
}else{ //unknown command
int minDst = 0;
Command closest = null;
for(Command command : netServer.clientCommands.getCommandList()){
int dst = Strings.levenshtein(command.text, response.runCommand);
if(dst < 3 && (closest == null || dst < minDst)){
minDst = dst;
closest = command;
}
}
if(closest != null){
return "[scarlet]Unknown command. Did you mean \"[lightgray]" + closest.text + "[]\"?";
}else{
return "[scarlet]Unknown command. Check [lightgray]/help[scarlet].";
}
}
};
private boolean closing = false;
private Interval timer = new Interval();
@ -994,4 +1020,8 @@ public class NetServer implements ApplicationListener{
/** @return text to be placed before player name */
String format(@Nullable Player player, String message);
}
public interface InvalidCommandHandler{
String handle(Player player, CommandResponse response);
}
}