mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-07-09 15:27:45 +07:00
InvalidCommandHandler (#6247)
* InvalidCommandHandler * Update NetClient.java * Add player * And here * :thonk: * And this * Update NetClient.java
This commit is contained in:
@ -260,33 +260,10 @@ public class NetClient implements ApplicationListener{
|
|||||||
|
|
||||||
//a command was sent, now get the output
|
//a command was sent, now get the output
|
||||||
if(response.type != ResponseType.valid){
|
if(response.type != ResponseType.valid){
|
||||||
String text;
|
String text = netServer.invalidHandler.handle(player, response);
|
||||||
|
if(text != null){
|
||||||
//send usage
|
player.sendMessage(text);
|
||||||
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].";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
player.sendMessage(text);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,6 +67,32 @@ public class NetServer implements ApplicationListener{
|
|||||||
/** Converts a message + NULLABLE player sender into a single string. Override for custom prefixes/suffixes. */
|
/** 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;
|
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 boolean closing = false;
|
||||||
private Interval timer = new Interval();
|
private Interval timer = new Interval();
|
||||||
|
|
||||||
@ -994,4 +1020,8 @@ public class NetServer implements ApplicationListener{
|
|||||||
/** @return text to be placed before player name */
|
/** @return text to be placed before player name */
|
||||||
String format(@Nullable Player player, String message);
|
String format(@Nullable Player player, String message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface InvalidCommandHandler{
|
||||||
|
String handle(Player player, CommandResponse response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user