Grasscutter/src/main/java/emu/grasscutter/command/CommandMap.java

162 lines
5.2 KiB
Java
Raw Normal View History

package emu.grasscutter.command;
2022-04-18 22:24:08 +00:00
import emu.grasscutter.Grasscutter;
2022-04-19 03:46:04 +00:00
import emu.grasscutter.game.Account;
2022-04-18 22:24:08 +00:00
import emu.grasscutter.game.GenshinPlayer;
import org.reflections.Reflections;
import java.util.*;
@SuppressWarnings({"UnusedReturnValue", "unused"})
2022-04-18 22:24:08 +00:00
public final class CommandMap {
private final Map<String, CommandHandler> commands = new HashMap<>();
private final Map<String, Command> annotations = new HashMap<>();
2022-04-21 03:56:27 +00:00
public CommandMap() {
this(false);
}
public CommandMap(boolean scan) {
if (scan) this.scan();
}
2022-04-18 22:24:08 +00:00
public static CommandMap getInstance() {
return Grasscutter.getGameServer().getCommandMap();
}
/**
* Register a command handler.
*
* @param label The command label.
2022-04-18 22:24:08 +00:00
* @param command The command handler.
* @return Instance chaining.
*/
public CommandMap registerCommand(String label, CommandHandler command) {
Grasscutter.getLogger().debug("Registered command: " + label);
2022-04-19 02:09:51 +00:00
// Get command data.
Command annotation = command.getClass().getAnnotation(Command.class);
2022-04-19 03:46:04 +00:00
this.annotations.put(label, annotation);
2022-04-19 02:09:51 +00:00
this.commands.put(label, command);
2022-04-19 02:09:51 +00:00
// Register aliases.
if (annotation.aliases().length > 0) {
2022-04-19 02:09:51 +00:00
for (String alias : annotation.aliases()) {
this.commands.put(alias, command);
2022-04-19 03:46:04 +00:00
this.annotations.put(alias, annotation);
2022-04-19 02:09:51 +00:00
}
}
return this;
2022-04-18 22:24:08 +00:00
}
/**
* Removes a registered command handler.
*
2022-04-18 22:24:08 +00:00
* @param label The command label.
* @return Instance chaining.
*/
public CommandMap unregisterCommand(String label) {
Grasscutter.getLogger().debug("Unregistered command: " + label);
CommandHandler handler = this.commands.get(label);
if (handler == null) return this;
Command annotation = handler.getClass().getAnnotation(Command.class);
2022-04-19 03:46:04 +00:00
this.annotations.remove(label);
2022-04-19 02:09:51 +00:00
this.commands.remove(label);
2022-04-19 02:09:51 +00:00
// Unregister aliases.
if (annotation.aliases().length > 0) {
2022-04-19 02:09:51 +00:00
for (String alias : annotation.aliases()) {
this.commands.remove(alias);
2022-04-19 03:46:04 +00:00
this.annotations.remove(alias);
2022-04-19 02:09:51 +00:00
}
}
return this;
2022-04-18 22:24:08 +00:00
}
2022-04-19 03:06:03 +00:00
/**
* Returns a list of all registered commands.
*
2022-04-19 03:06:03 +00:00
* @return All command handlers as a list.
*/
public List<CommandHandler> getHandlersAsList() {
2022-04-19 03:06:03 +00:00
return new LinkedList<>(this.commands.values());
}
public HashMap<String, CommandHandler> getHandlers() {
return new LinkedHashMap<>(this.commands);
}
2022-04-19 03:06:03 +00:00
/**
* Returns a handler by label/alias.
*
2022-04-19 03:06:03 +00:00
* @param label The command label.
* @return The command handler.
*/
public CommandHandler getHandler(String label) {
return this.commands.get(label);
}
2022-04-18 22:24:08 +00:00
/**
* Invoke a command handler with the given arguments.
*
* @param player The player invoking the command or null for the server console.
2022-04-18 22:24:08 +00:00
* @param rawMessage The messaged used to invoke the command.
*/
public void invoke(GenshinPlayer player, String rawMessage) {
rawMessage = rawMessage.trim();
if(rawMessage.length() == 0) {
CommandHandler.sendMessage(player, "No command specified."); return;
}
2022-04-18 22:24:08 +00:00
// Remove prefix if present.
if (!Character.isLetter(rawMessage.charAt(0)))
2022-04-18 22:24:08 +00:00
rawMessage = rawMessage.substring(1);
2022-04-18 22:24:08 +00:00
// Parse message.
String[] split = rawMessage.split(" ");
List<String> args = new LinkedList<>(Arrays.asList(split));
2022-04-18 22:24:08 +00:00
String label = args.remove(0);
2022-04-18 22:24:08 +00:00
// Get command handler.
CommandHandler handler = this.commands.get(label);
if (handler == null) {
CommandHandler.sendMessage(player, "Unknown command: " + label);
return;
2022-04-18 22:24:08 +00:00
}
2022-04-19 03:46:04 +00:00
// Check for permission.
if (player != null) {
2022-04-19 03:46:04 +00:00
String permissionNode = this.annotations.get(label).permission();
Account account = player.getAccount();
if(!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) {
CommandHandler.sendMessage(player, "You do not have permission to run this command.");
return;
2022-04-19 03:46:04 +00:00
}
}
2022-04-18 22:24:08 +00:00
// Invoke execute method for handler.
2022-04-20 16:17:56 +00:00
handler.execute(player, args);
2022-04-18 22:24:08 +00:00
}
/**
* Scans for all classes annotated with {@link Command} and registers them.
*/
private void scan() {
Reflections reflector = Grasscutter.reflector;
Set<Class<?>> classes = reflector.getTypesAnnotatedWith(Command.class);
2022-04-18 22:24:08 +00:00
classes.forEach(annotated -> {
try {
Command cmdData = annotated.getAnnotation(Command.class);
Object object = annotated.newInstance();
2022-04-18 22:24:08 +00:00
if (object instanceof CommandHandler)
this.registerCommand(cmdData.label(), (CommandHandler) object);
else Grasscutter.getLogger().error("Class " + annotated.getName() + " is not a CommandHandler!");
} catch (Exception exception) {
Grasscutter.getLogger().error("Failed to register command handler for " + annotated.getSimpleName(), exception);
}
2022-04-18 22:24:08 +00:00
});
}
}