mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-01-09 04:13:00 +08:00
Execution power
This commit is contained in:
parent
25c414be1c
commit
61c0ff36e7
@ -6,10 +6,18 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Command {
|
public @interface Command {
|
||||||
String label() default "";
|
String label() default "";
|
||||||
|
|
||||||
|
String usage() default "";
|
||||||
|
|
||||||
String[] aliases() default {""};
|
String[] aliases() default {""};
|
||||||
|
|
||||||
|
Execution execution() default Execution.ALL;
|
||||||
|
|
||||||
int gmLevel() default 1;
|
int gmLevel() default 1;
|
||||||
|
|
||||||
String usage() default "";
|
enum Execution {
|
||||||
|
ALL,
|
||||||
|
CONSOLE,
|
||||||
|
PLAYER
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,9 @@ import java.util.List;
|
|||||||
|
|
||||||
public interface CommandHandler {
|
public interface CommandHandler {
|
||||||
/* Invoked on player execution. */
|
/* Invoked on player execution. */
|
||||||
void execute(GenshinPlayer player, List<String> args);
|
default void execute(GenshinPlayer player, List<String> args) { }
|
||||||
/* Invoked on server execution. */
|
/* Invoked on server execution. */
|
||||||
void execute(List<String> args);
|
default void execute(List<String> args) { }
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Utilities.
|
* Utilities.
|
||||||
|
@ -13,6 +13,7 @@ public final class CommandMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final Map<String, CommandHandler> commands = new HashMap<>();
|
private final Map<String, CommandHandler> commands = new HashMap<>();
|
||||||
|
private final Map<String, Command.Execution> executionPower = new HashMap<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a command handler.
|
* Register a command handler.
|
||||||
@ -23,13 +24,18 @@ public final class CommandMap {
|
|||||||
public CommandMap registerCommand(String label, CommandHandler command) {
|
public CommandMap registerCommand(String label, CommandHandler command) {
|
||||||
Grasscutter.getLogger().debug("Registered command: " + label);
|
Grasscutter.getLogger().debug("Registered command: " + label);
|
||||||
|
|
||||||
|
// Get command data.
|
||||||
Command annotation = command.getClass().getAnnotation(Command.class);
|
Command annotation = command.getClass().getAnnotation(Command.class);
|
||||||
if(annotation.aliases().length > 0) {
|
this.executionPower.put(label, annotation.execution());
|
||||||
for (String alias : annotation.aliases())
|
this.commands.put(label, command);
|
||||||
this.commands.put(alias, command);
|
|
||||||
} this.commands.put(label, command);
|
|
||||||
|
|
||||||
return this;
|
// Register aliases.
|
||||||
|
if(annotation.aliases().length > 0) {
|
||||||
|
for (String alias : annotation.aliases()) {
|
||||||
|
this.commands.put(alias, command);
|
||||||
|
this.executionPower.put(alias, annotation.execution());
|
||||||
|
}
|
||||||
|
} return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -43,10 +49,16 @@ public final class CommandMap {
|
|||||||
if(handler == null) return this;
|
if(handler == null) return this;
|
||||||
|
|
||||||
Command annotation = handler.getClass().getAnnotation(Command.class);
|
Command annotation = handler.getClass().getAnnotation(Command.class);
|
||||||
|
this.executionPower.remove(label);
|
||||||
|
this.commands.remove(label);
|
||||||
|
|
||||||
|
// Unregister aliases.
|
||||||
if(annotation.aliases().length > 0) {
|
if(annotation.aliases().length > 0) {
|
||||||
for (String alias : annotation.aliases())
|
for (String alias : annotation.aliases()) {
|
||||||
this.commands.remove(alias);
|
this.commands.remove(alias);
|
||||||
} this.commands.remove(label);
|
this.executionPower.remove(alias);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -77,9 +89,16 @@ public final class CommandMap {
|
|||||||
CommandHandler.sendMessage(player, "Unknown command: " + label); return;
|
CommandHandler.sendMessage(player, "Unknown command: " + label); return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execution power check.
|
||||||
|
Command.Execution executionPower = this.executionPower.get(label);
|
||||||
|
if(player == null && executionPower == Command.Execution.PLAYER) {
|
||||||
|
CommandHandler.sendMessage(null, "Run this command in-game."); return;
|
||||||
|
} else if (player != null && executionPower == Command.Execution.CONSOLE) {
|
||||||
|
CommandHandler.sendMessage(player, "This command can only be run from the console."); return;
|
||||||
|
}
|
||||||
|
|
||||||
// Invoke execute method for handler.
|
// Invoke execute method for handler.
|
||||||
if(player == null)
|
if(player == null) handler.execute(args);
|
||||||
handler.execute(args);
|
|
||||||
else handler.execute(player, args);
|
else handler.execute(player, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user