mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-01-23 05:33:15 +08:00
Fix registering error & implement handling
This commit is contained in:
parent
9cd65046b1
commit
25c414be1c
@ -16,7 +16,6 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import emu.grasscutter.commands.ServerCommands;
|
||||
import emu.grasscutter.data.ResourceLoader;
|
||||
import emu.grasscutter.database.DatabaseManager;
|
||||
import emu.grasscutter.server.dispatch.DispatchServer;
|
||||
@ -103,7 +102,11 @@ public final class Grasscutter {
|
||||
String input;
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
|
||||
while ((input = br.readLine()) != null) {
|
||||
CommandMap.getInstance().invoke(null, input);
|
||||
try {
|
||||
CommandMap.getInstance().invoke(null, input);
|
||||
} catch (Exception e) {
|
||||
Grasscutter.getLogger().error("Command error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Grasscutter.getLogger().error("An error occurred.", e);
|
||||
|
@ -7,7 +7,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
public @interface Command {
|
||||
String label() default "";
|
||||
|
||||
String[] aliases() default "";
|
||||
String[] aliases() default {""};
|
||||
|
||||
int gmLevel() default 1;
|
||||
|
||||
|
@ -21,7 +21,15 @@ public final class CommandMap {
|
||||
* @return Instance chaining.
|
||||
*/
|
||||
public CommandMap registerCommand(String label, CommandHandler command) {
|
||||
this.commands.put(label, command); return this;
|
||||
Grasscutter.getLogger().debug("Registered command: " + label);
|
||||
|
||||
Command annotation = command.getClass().getAnnotation(Command.class);
|
||||
if(annotation.aliases().length > 0) {
|
||||
for (String alias : annotation.aliases())
|
||||
this.commands.put(alias, command);
|
||||
} this.commands.put(label, command);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -30,7 +38,17 @@ public final class CommandMap {
|
||||
* @return Instance chaining.
|
||||
*/
|
||||
public CommandMap unregisterCommand(String label) {
|
||||
this.commands.remove(label); return this;
|
||||
Grasscutter.getLogger().debug("Unregistered command: " + label);
|
||||
CommandHandler handler = this.commands.get(label);
|
||||
if(handler == null) return this;
|
||||
|
||||
Command annotation = handler.getClass().getAnnotation(Command.class);
|
||||
if(annotation.aliases().length > 0) {
|
||||
for (String alias : annotation.aliases())
|
||||
this.commands.remove(alias);
|
||||
} this.commands.remove(label);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,13 +57,18 @@ public final class CommandMap {
|
||||
* @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.");
|
||||
}
|
||||
|
||||
// Remove prefix if present.
|
||||
if(!Character.isLetter(rawMessage.charAt(0)))
|
||||
rawMessage = rawMessage.substring(1);
|
||||
|
||||
// Parse message.
|
||||
String[] split = rawMessage.split(" ");
|
||||
List<String> args = Arrays.asList(split);
|
||||
List<String> args = new LinkedList<>(Arrays.asList(split));
|
||||
String label = args.remove(0);
|
||||
|
||||
// Get command handler.
|
||||
@ -73,15 +96,17 @@ public final class CommandMap {
|
||||
*/
|
||||
private void scan() {
|
||||
Reflections reflector = Grasscutter.reflector;
|
||||
Set<?> classes = reflector.getTypesAnnotatedWith(Command.class);
|
||||
Set<Class<?>> classes = reflector.getTypesAnnotatedWith(Command.class);
|
||||
classes.forEach(annotated -> {
|
||||
try {
|
||||
Class<?> cls = annotated.getClass();
|
||||
Command cmdData = cls.getAnnotation(Command.class);
|
||||
Object object = cls.getDeclaredConstructors()[0].newInstance();
|
||||
Command cmdData = annotated.getAnnotation(Command.class);
|
||||
Object object = annotated.newInstance();
|
||||
if (object instanceof CommandHandler)
|
||||
this.registerCommand(cmdData.label(), (CommandHandler) object);
|
||||
} catch (Exception ignored) { }
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package emu.grasscutter.game.managers;
|
||||
|
||||
import emu.grasscutter.commands.CommandMap;
|
||||
import emu.grasscutter.commands.PlayerCommands;
|
||||
import emu.grasscutter.game.GenshinPlayer;
|
||||
import emu.grasscutter.net.packet.GenshinPacket;
|
||||
import emu.grasscutter.server.game.GameServer;
|
||||
|
Loading…
Reference in New Issue
Block a user