CommandHandler.java 3.1 KB
Newer Older
Jaida Wu's avatar
Jaida Wu committed
1
2
3
package emu.grasscutter.command;

import emu.grasscutter.Grasscutter;
Melledy's avatar
Melledy committed
4
import emu.grasscutter.game.player.Player;
5
import emu.grasscutter.server.event.game.ReceiveCommandFeedbackEvent;
6
import static emu.grasscutter.utils.Language.translate;
Jaida Wu's avatar
Jaida Wu committed
7
8

import java.util.List;
9
import java.util.StringJoiner;
Jaida Wu's avatar
Jaida Wu committed
10
11

public interface CommandHandler {
12

Jaida Wu's avatar
Jaida Wu committed
13
14
15
16
17
18
    /**
     * 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.
     */
19
    static void sendMessage(Player player, String message) {
20
21
22
23
24
25
26
27
        // 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.
Jaida Wu's avatar
Jaida Wu committed
28
        if (player == null) {
29
            Grasscutter.getLogger().info(event.getMessage());
Jaida Wu's avatar
Jaida Wu committed
30
        } else {
31
            player.dropMessage(event.getMessage().replace("\n\t", "\n\n"));
Jaida Wu's avatar
Jaida Wu committed
32
33
        }
    }
34

35
36
37
    static void sendTranslatedMessage(Player player, String messageKey, Object... args) {
        sendMessage(player, translate(player, messageKey, args));
    }
Jaida Wu's avatar
Jaida Wu committed
38

39
40
41
42
43
44
45
46
    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;
        }
47
48
49
        if (player != null) {
            command = "/" + command;
        }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        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);
    }

KingRainbow44's avatar
KingRainbow44 committed
77
78
79
80
81
    /**
     * Called when a player/console invokes a command.
     * @param sender The player/console that invoked the command.
     * @param args The arguments to the command.
     */
AnimeGitB's avatar
AnimeGitB committed
82
    default void execute(Player sender, Player targetPlayer, List<String> args) {
Jaida Wu's avatar
Jaida Wu committed
83
84
    }
}