mirror of https://github.com/Minestom/Minestom.git
Added an unknown command callback
This commit is contained in:
parent
2e0c5a72b7
commit
1c6070a1b2
|
@ -20,6 +20,7 @@ import net.minestom.server.entity.Player;
|
|||
import net.minestom.server.event.player.PlayerCommandEvent;
|
||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||
import net.minestom.server.utils.ArrayUtils;
|
||||
import net.minestom.server.utils.callback.CommandCallback;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
@ -44,6 +45,8 @@ public final class CommandManager {
|
|||
private final CommandDispatcher dispatcher = new CommandDispatcher();
|
||||
private final Map<String, CommandProcessor> commandProcessorMap = new HashMap<>();
|
||||
|
||||
private CommandCallback unknownCommandCallback;
|
||||
|
||||
public CommandManager() {
|
||||
running = true;
|
||||
// Setup console thread
|
||||
|
@ -153,8 +156,12 @@ public final class CommandManager {
|
|||
final String[] splitCommand = command.split(" ");
|
||||
final String commandName = splitCommand[0];
|
||||
final CommandProcessor commandProcessor = commandProcessorMap.get(commandName.toLowerCase());
|
||||
if (commandProcessor == null)
|
||||
if (commandProcessor == null) {
|
||||
if (unknownCommandCallback != null) {
|
||||
this.unknownCommandCallback.apply(sender, command);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Execute the legacy-command
|
||||
final String[] args = command.substring(command.indexOf(" ") + 1).split(" ");
|
||||
|
@ -164,6 +171,26 @@ public final class CommandManager {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the callback executed once an unknown command is run.
|
||||
*
|
||||
* @return the unknown command callback, null if not any
|
||||
*/
|
||||
@Nullable
|
||||
public CommandCallback getUnknownCommandCallback() {
|
||||
return unknownCommandCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the callback executed once an unknown command is run.
|
||||
*
|
||||
* @param unknownCommandCallback the new unknown command callback,
|
||||
* setting it to null mean that nothing will be executed
|
||||
*/
|
||||
public void setUnknownCommandCallback(@Nullable CommandCallback unknownCommandCallback) {
|
||||
this.unknownCommandCallback = unknownCommandCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link ConsoleSender} (which is used as a {@link CommandSender}).
|
||||
*
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package net.minestom.server.utils.callback;
|
||||
|
||||
import net.minestom.server.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Functional interface used by the {@link net.minestom.server.command.CommandManager}
|
||||
* to execute a callback if an unknown command is run.
|
||||
* You can set it with {@link net.minestom.server.command.CommandManager#setUnknownCommandCallback(CommandCallback)}.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface CommandCallback {
|
||||
|
||||
/**
|
||||
* Executed if an unknown command is run.
|
||||
*
|
||||
* @param sender the command sender
|
||||
* @param command the complete command string
|
||||
*/
|
||||
void apply(@NotNull CommandSender sender, @NotNull String command);
|
||||
|
||||
}
|
|
@ -36,6 +36,8 @@ public class Main {
|
|||
commandManager.register(new ShutdownCommand());
|
||||
commandManager.register(new TeleportCommand());
|
||||
|
||||
commandManager.setUnknownCommandCallback((sender, command) -> sender.sendMessage("unknown command"));
|
||||
|
||||
|
||||
StorageManager storageManager = MinecraftServer.getStorageManager();
|
||||
storageManager.defineDefaultStorageSystem(FileStorageSystem::new);
|
||||
|
|
Loading…
Reference in New Issue