mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-29 19:41:47 +01:00
Prevent command override using CommandManager
This commit is contained in:
parent
669e7ea711
commit
1a8baf36f6
@ -84,7 +84,13 @@ public final class CommandManager {
|
||||
*
|
||||
* @param command the command to register
|
||||
*/
|
||||
public void register(@NotNull Command command) {
|
||||
public synchronized void register(@NotNull Command command) {
|
||||
Check.stateCondition(commandExists(command.getName()),
|
||||
"A command with the name " + command.getName() + " is already registered!");
|
||||
for (String alias : command.getAliases()) {
|
||||
Check.stateCondition(commandExists(alias),
|
||||
"A command with the name " + alias + " is already registered!");
|
||||
}
|
||||
this.dispatcher.register(command);
|
||||
}
|
||||
|
||||
@ -104,12 +110,18 @@ public final class CommandManager {
|
||||
*
|
||||
* @param commandProcessor the command to register
|
||||
*/
|
||||
public void register(@NotNull CommandProcessor commandProcessor) {
|
||||
this.commandProcessorMap.put(commandProcessor.getCommandName().toLowerCase(), commandProcessor);
|
||||
public synchronized void register(@NotNull CommandProcessor commandProcessor) {
|
||||
final String commandName = commandProcessor.getCommandName().toLowerCase();
|
||||
Check.stateCondition(commandExists(commandName),
|
||||
"A command with the name " + commandName + " is already registered!");
|
||||
this.commandProcessorMap.put(commandName, commandProcessor);
|
||||
// Register aliases
|
||||
final String[] aliases = commandProcessor.getAliases();
|
||||
if (aliases != null && aliases.length > 0) {
|
||||
for (String alias : aliases) {
|
||||
Check.stateCondition(commandExists(alias),
|
||||
"A command with the name " + alias + " is already registered!");
|
||||
|
||||
this.commandProcessorMap.put(alias.toLowerCase(), commandProcessor);
|
||||
}
|
||||
}
|
||||
@ -126,6 +138,18 @@ public final class CommandManager {
|
||||
return commandProcessorMap.get(commandName.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets if a command with the name {@code commandName} already exists or name.
|
||||
*
|
||||
* @param commandName the command name to check
|
||||
* @return true if the command does exist
|
||||
*/
|
||||
public boolean commandExists(@NotNull String commandName) {
|
||||
commandName = commandName.toLowerCase();
|
||||
return dispatcher.findCommand(commandName) != null ||
|
||||
commandProcessorMap.get(commandName) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a command for a {@link ConsoleSender}.
|
||||
*
|
||||
|
@ -17,6 +17,12 @@ public class CommandDispatcher {
|
||||
private final Map<String, Command> commandMap = new HashMap<>();
|
||||
private final Set<Command> commands = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Registers a command,
|
||||
* be aware that registering a command name or alias will override the previous entry.
|
||||
*
|
||||
* @param command the command to register
|
||||
*/
|
||||
public void register(@NotNull Command command) {
|
||||
this.commandMap.put(command.getName().toLowerCase(), command);
|
||||
|
||||
|
@ -32,7 +32,6 @@ public class Main {
|
||||
commandManager.register(new EntitySelectorCommand());
|
||||
commandManager.register(new HealthCommand());
|
||||
commandManager.register(new SimpleCommand());
|
||||
commandManager.register(new GamemodeCommand());
|
||||
commandManager.register(new DimensionCommand());
|
||||
commandManager.register(new ShutdownCommand());
|
||||
commandManager.register(new TeleportCommand());
|
||||
|
@ -5,20 +5,14 @@ import net.minestom.server.command.builder.Arguments;
|
||||
import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||
import net.minestom.server.utils.location.RelativeVec;
|
||||
|
||||
public class TestCommand extends Command {
|
||||
|
||||
public TestCommand() {
|
||||
super("testcmd");
|
||||
setDefaultExecutor(this::usage);
|
||||
{
|
||||
//Argument dynamicWord = ArgumentType.DynamicWord("test");
|
||||
|
||||
//addSyntax(this::execute, dynamicWord);
|
||||
}
|
||||
|
||||
Argument test = ArgumentType.RelativeVec2("pos");
|
||||
Argument test = ArgumentType.ItemStack("test");
|
||||
|
||||
test.setCallback((source, value, error) -> {
|
||||
System.out.println("ERROR " + error);
|
||||
@ -30,8 +24,7 @@ public class TestCommand extends Command {
|
||||
});
|
||||
|
||||
addSyntax((source, args) -> {
|
||||
RelativeVec location = args.getRelativeVector("pos");
|
||||
System.out.println("IT WORKS " + location.fromRelativePosition(source.asPlayer()));
|
||||
System.out.println("ARG 1");
|
||||
}, test);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user