2022-04-20 20:21:38 +08:00
|
|
|
package emu.grasscutter.command;
|
|
|
|
|
|
|
|
import emu.grasscutter.Grasscutter;
|
2022-04-27 12:24:25 +08:00
|
|
|
import emu.grasscutter.game.player.Player;
|
2022-06-24 13:06:19 +08:00
|
|
|
import emu.grasscutter.server.event.game.ReceiveCommandFeedbackEvent;
|
2022-05-22 16:02:11 +08:00
|
|
|
import static emu.grasscutter.utils.Language.translate;
|
2022-04-20 20:21:38 +08:00
|
|
|
|
|
|
|
import java.util.List;
|
2022-07-18 17:06:17 +08:00
|
|
|
import java.util.StringJoiner;
|
2022-04-20 20:21:38 +08:00
|
|
|
|
|
|
|
public interface CommandHandler {
|
2022-05-08 10:33:53 +08:00
|
|
|
|
2022-04-20 20:21:38 +08:00
|
|
|
/**
|
|
|
|
* Send a message to the target.
|
|
|
|
*
|
|
|
|
* @param player The player to send the message to, or null for the server console.
|
|
|
|
* @param message The message to send.
|
|
|
|
*/
|
2022-04-27 12:21:57 +08:00
|
|
|
static void sendMessage(Player player, String message) {
|
2022-06-24 13:06:19 +08:00
|
|
|
// Call command feedback event.
|
|
|
|
ReceiveCommandFeedbackEvent event = new ReceiveCommandFeedbackEvent(player, message);
|
|
|
|
event.call();
|
|
|
|
if (event.isCanceled()) { // If event is not cancelled, continue.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send message to target.
|
2022-04-20 20:21:38 +08:00
|
|
|
if (player == null) {
|
2022-06-24 13:06:19 +08:00
|
|
|
Grasscutter.getLogger().info(event.getMessage());
|
2022-04-20 20:21:38 +08:00
|
|
|
} else {
|
2022-07-28 10:45:58 +08:00
|
|
|
player.dropMessage(event.getMessage().replace("\n\t", "\n\n"));
|
2022-04-20 20:21:38 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-24 13:06:19 +08:00
|
|
|
|
2022-05-22 16:02:11 +08:00
|
|
|
static void sendTranslatedMessage(Player player, String messageKey, Object... args) {
|
|
|
|
sendMessage(player, translate(player, messageKey, args));
|
|
|
|
}
|
2022-04-20 20:21:38 +08:00
|
|
|
|
2022-07-18 17:06:17 +08:00
|
|
|
default String getUsageString(Player player, String... args) {
|
|
|
|
Command annotation = this.getClass().getAnnotation(Command.class);
|
|
|
|
String usage_prefix = translate(player, "commands.execution.usage_prefix");
|
|
|
|
String command = annotation.label();
|
|
|
|
for (String alias : annotation.aliases()) {
|
|
|
|
if (alias.length() < command.length())
|
|
|
|
command = alias;
|
|
|
|
}
|
2022-07-28 10:45:58 +08:00
|
|
|
if (player != null) {
|
|
|
|
command = "/" + command;
|
|
|
|
}
|
2022-07-18 17:06:17 +08:00
|
|
|
String target = switch (annotation.targetRequirement()) {
|
|
|
|
case NONE -> "";
|
|
|
|
case OFFLINE -> "@<UID> "; // TODO: make translation keys for offline and online players
|
|
|
|
case ONLINE -> (player == null) ? "@<UID> " : "[@<UID>] "; // TODO: make translation keys for offline and online players
|
|
|
|
case PLAYER -> (player == null) ? "@<UID> " : "[@<UID>] ";
|
|
|
|
};
|
|
|
|
String[] usages = annotation.usage();
|
|
|
|
StringJoiner joiner = new StringJoiner("\n\t");
|
|
|
|
for (String usage : usages)
|
|
|
|
joiner.add(usage_prefix + command + " " + target + usage);
|
|
|
|
return joiner.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
default void sendUsageMessage(Player player, String... args) {
|
|
|
|
sendMessage(player, getUsageString(player, args));
|
|
|
|
}
|
|
|
|
|
|
|
|
default String getLabel() {
|
|
|
|
return this.getClass().getAnnotation(Command.class).label();
|
|
|
|
}
|
|
|
|
|
|
|
|
default String getDescriptionString(Player player) {
|
|
|
|
Command annotation = this.getClass().getAnnotation(Command.class);
|
|
|
|
String key = "commands.%s.description".formatted(annotation.label());
|
|
|
|
return translate(player, key);
|
|
|
|
}
|
|
|
|
|
2022-04-21 00:17:56 +08:00
|
|
|
/**
|
|
|
|
* Called when a player/console invokes a command.
|
|
|
|
* @param sender The player/console that invoked the command.
|
|
|
|
* @param args The arguments to the command.
|
|
|
|
*/
|
2022-05-04 14:32:09 +08:00
|
|
|
default void execute(Player sender, Player targetPlayer, List<String> args) {
|
2022-04-20 20:21:38 +08:00
|
|
|
}
|
|
|
|
}
|