CommandHandler.java 3.2 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
7
import emu.grasscutter.utils.Language;

8
import static emu.grasscutter.utils.Language.translate;
Jaida Wu's avatar
Jaida Wu committed
9
10

import java.util.List;
11
import java.util.StringJoiner;
Jaida Wu's avatar
Jaida Wu committed
12
13

public interface CommandHandler {
14

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

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

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

73
    default String getDescriptionKey() {
74
        Command annotation = this.getClass().getAnnotation(Command.class);
75
76
77
78
79
        return "commands.%s.description".formatted(annotation.label());
    }

    default String getDescriptionString(Player player) {
        return translate(player, getDescriptionKey());
80
81
    }

KingRainbow44's avatar
KingRainbow44 committed
82
83
84
85
86
    /**
     * 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
87
    default void execute(Player sender, Player targetPlayer, List<String> args) {
Jaida Wu's avatar
Jaida Wu committed
88
89
    }
}