AnnounceCommand.java 2.87 KB
Newer Older
Akka's avatar
Akka committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package emu.grasscutter.command.commands;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.packet.send.PacketServerAnnounceNotify;

import java.util.Collections;
import java.util.List;
import java.util.Random;

import static emu.grasscutter.utils.Language.translate;

@Command(label = "announce",
16
    usage = "announce|a <\"tpl\" templateId|\"refresh\"|\"revoke\" templateId|content>",
Akka's avatar
Akka committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
    permission = "server.announce",
    aliases = {"a"},
    description = "commands.announce.description",
    targetRequirement = Command.TargetRequirement.NONE)
public final class AnnounceCommand implements CommandHandler {

    @Override
    public void execute(Player sender, Player targetPlayer, List<String> args) {
        var manager = Grasscutter.getGameServer().getAnnouncementManager();
        if (args.size() < 1) {
            CommandHandler.sendTranslatedMessage(sender, "commands.announce.command_usage");
            return;
        }

        switch (args.get(0)){
            case "tpl":
                if (args.size() < 2) {
                    CommandHandler.sendTranslatedMessage(sender, "commands.announce.command_usage");
                    return;
                }

                var templateId = Integer.parseInt(args.get(1));
                var tpl = manager.getAnnounceConfigItemMap().get(templateId);
                if(tpl == null){
                    CommandHandler.sendMessage(sender, translate(sender, "commands.announce.not_found", templateId));
                    return;
                }

                manager.broadcast(Collections.singletonList(tpl));
                CommandHandler.sendMessage(sender, translate(sender, "commands.announce.send_success", tpl.getTemplateId()));
                break;

            case "refresh":
                var num = manager.refresh();
                CommandHandler.sendMessage(sender, translate(sender, "commands.announce.refresh_success", num));
                break;

            case "revoke":
                if (args.size() < 2) {
                    CommandHandler.sendTranslatedMessage(sender, "commands.announce.command_usage");
                    return;
                }

                var templateId1 = Integer.parseInt(args.get(1));
                manager.revoke(templateId1);
                CommandHandler.sendMessage(sender, translate(sender, "commands.announce.revoke_done", templateId1));
                break;

            default:
                var id = new Random().nextInt(10000, 99999);
                var text = String.join(" ", args);
                manager.getOnlinePlayers().forEach(i -> i.sendPacket(new PacketServerAnnounceNotify(text, id)));

                CommandHandler.sendMessage(sender, translate(sender, "commands.announce.send_success", id));
        }

    }
}