SendMailCommand.java 10.8 KB
Newer Older
1
2
package emu.grasscutter.command.commands;

3
import emu.grasscutter.Grasscutter;
4
5
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
Benjamin Elsdon's avatar
Benjamin Elsdon committed
6
import emu.grasscutter.database.DatabaseHelper;
Melledy's avatar
Melledy committed
7
8
import emu.grasscutter.game.mail.Mail;
import emu.grasscutter.game.player.Player;
9

Benjamin Elsdon's avatar
Benjamin Elsdon committed
10
import java.util.HashMap;
11
12
import java.util.List;

13
14
15
import static emu.grasscutter.utils.Language.translate;

@SuppressWarnings("ConstantConditions")
16
17
18
19
20
@Command(
    label = "sendMail",
    usage = {"(<userId>|all) [<templateId>]", "help"},
    permission = "server.sendmail",
    targetRequirement = Command.TargetRequirement.NONE)
KingRainbow44's avatar
KingRainbow44 committed
21
public final class SendMailCommand implements CommandHandler {
22

Benjamin Elsdon's avatar
Benjamin Elsdon committed
23
24
25
26
27
    // TODO: You should be able to do /sendmail and then just send subsequent messages until you finish
    //  However, due to the current nature of the command system, I don't think this is possible without rewriting
    //  the command system (again). For now this will do

    // Key = User that is constructing the mail.
KingRainbow44's avatar
KingRainbow44 committed
28
    private static final HashMap<Integer, MailBuilder> mailBeingConstructed = new HashMap<Integer, MailBuilder>();
Benjamin Elsdon's avatar
Benjamin Elsdon committed
29
30

    // Yes this is awful and I hate it.
31
    @Override
AnimeGitB's avatar
AnimeGitB committed
32
    public void execute(Player sender, Player targetPlayer, List<String> args) {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
33
        int senderId;
github-actions's avatar
github-actions committed
34
        if (sender != null) {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
35
36
37
38
39
40
41
            senderId = sender.getUid();
        } else {
            senderId = -1;
        }

        if (!mailBeingConstructed.containsKey(senderId)) {
            switch (args.size()) {
42
                case 1 -> {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
43
44
                    MailBuilder mailBuilder;
                    switch (args.get(0).toLowerCase()) {
45
                        case "help" -> {
46
                            sendUsageMessage(sender);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
47
                            return;
48
49
50
                        }
                        case "all" -> mailBuilder = new MailBuilder(true, new Mail());
                        default -> {
51
                            if (DatabaseHelper.getPlayerByUid(Integer.parseInt(args.get(0))) != null) {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
52
53
                                mailBuilder = new MailBuilder(Integer.parseInt(args.get(0)), new Mail());
                            } else {
Secretboy's avatar
Secretboy committed
54
                                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.user_not_exist", args.get(0)));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
55
56
                                return;
                            }
57
                        }
Benjamin Elsdon's avatar
Benjamin Elsdon committed
58
59
                    }
                    mailBeingConstructed.put(senderId, mailBuilder);
Secretboy's avatar
Secretboy committed
60
                    CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.start_composition"));
61
                }
Secretboy's avatar
Secretboy committed
62
63
                case 2 -> CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.templates"));
                default -> CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.invalid_arguments"));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
64
65
66
67
68
69
            }
        } else {
            MailBuilder mailBuilder = mailBeingConstructed.get(senderId);

            if (args.size() >= 1) {
                switch (args.get(0).toLowerCase()) {
70
                    case "stop" -> {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
71
                        mailBeingConstructed.remove(senderId);
Luke H-W's avatar
Luke H-W committed
72
                        CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.send_cancel"));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
73
                        return;
74
75
                    }
                    case "finish" -> {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
76
                        if (mailBuilder.constructionStage == 3) {
KingRainbow44's avatar
KingRainbow44 committed
77
                            if (!mailBuilder.sendToAll) {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
78
                                Grasscutter.getGameServer().getPlayerByUid(mailBuilder.recipient, true).sendMail(mailBuilder.mail);
Secretboy's avatar
Secretboy committed
79
                                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.send_done", Integer.toString(mailBuilder.recipient)));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
80
                            } else {
81
                                for (Player player : DatabaseHelper.getAllPlayers()) {
82
                                    Grasscutter.getGameServer().getPlayerByUid(player.getUid(), true).sendMail(mailBuilder.mail);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
83
                                }
Secretboy's avatar
Secretboy committed
84
                                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.send_all_done"));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
85
86
87
                            }
                            mailBeingConstructed.remove(senderId);
                        } else {
Secretboy's avatar
Secretboy committed
88
                            CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.not_composition_end", getConstructionArgs(mailBuilder.constructionStage, sender)));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
89
90
                        }
                        return;
91
92
                    }
                    case "help" -> {
Secretboy's avatar
Secretboy committed
93
                        CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.please_use", getConstructionArgs(mailBuilder.constructionStage, sender)));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
94
                        return;
95
96
                    }
                    default -> {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
97
                        switch (mailBuilder.constructionStage) {
98
                            case 0 -> {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
99
100
                                String title = String.join(" ", args.subList(0, args.size()));
                                mailBuilder.mail.mailContent.title = title;
Secretboy's avatar
Secretboy committed
101
                                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.set_title", title));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
102
                                mailBuilder.constructionStage++;
103
104
                            }
                            case 1 -> {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
105
106
                                String contents = String.join(" ", args.subList(0, args.size()));
                                mailBuilder.mail.mailContent.content = contents;
Secretboy's avatar
Secretboy committed
107
                                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.set_contents", contents));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
108
                                mailBuilder.constructionStage++;
109
110
                            }
                            case 2 -> {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
111
112
                                String msgSender = String.join(" ", args.subList(0, args.size()));
                                mailBuilder.mail.mailContent.sender = msgSender;
Secretboy's avatar
Secretboy committed
113
                                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.set_message_sender", msgSender));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
114
                                mailBuilder.constructionStage++;
115
116
                            }
                            case 3 -> {
AnimeGitB's avatar
AnimeGitB committed
117
118
119
120
                                int item;
                                int lvl = 1;
                                int amount = 1;
                                int refinement = 0;
Benjamin Elsdon's avatar
Benjamin Elsdon committed
121
                                switch (args.size()) {
AnimeGitB's avatar
AnimeGitB committed
122
                                    case 4: // <itemId|itemName> [amount] [level] [refinement] // TODO: this requires Mail support but there's no harm leaving it here for now
Benjamin Elsdon's avatar
Benjamin Elsdon committed
123
                                        try {
AnimeGitB's avatar
AnimeGitB committed
124
                                            refinement = Integer.parseInt(args.get(3));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
125
                                        } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
126
                                            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.itemRefinement"));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
127
                                            return;
AnimeGitB's avatar
AnimeGitB committed
128
129
                                        }  // Fallthrough
                                    case 3: // <itemId|itemName> [amount] [level]
Benjamin Elsdon's avatar
Benjamin Elsdon committed
130
131
                                        try {
                                            lvl = Integer.parseInt(args.get(2));
AnimeGitB's avatar
AnimeGitB committed
132
                                        } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
133
                                            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.itemLevel"));
AnimeGitB's avatar
AnimeGitB committed
134
135
136
137
138
139
                                            return;
                                        }  // Fallthrough
                                    case 2: // <itemId|itemName> [amount]
                                        try {
                                            amount = Integer.parseInt(args.get(1));
                                        } catch (NumberFormatException ignored) {
Secretboy's avatar
Secretboy committed
140
                                            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.amount"));
AnimeGitB's avatar
AnimeGitB committed
141
142
143
144
145
                                            return;
                                        }  // Fallthrough
                                    case 1: // <itemId|itemName>
                                        try {
                                            item = Integer.parseInt(args.get(0));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
146
147
                                        } catch (NumberFormatException ignored) {
                                            // TODO: Parse from item name using GM Handbook.
Secretboy's avatar
Secretboy committed
148
                                            CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.itemId"));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
149
150
                                            return;
                                        }
AnimeGitB's avatar
AnimeGitB committed
151
152
                                        break;
                                    default: // *No args*
153
                                        CommandHandler.sendTranslatedMessage(sender, "commands.sendMail.give_usage");
AnimeGitB's avatar
AnimeGitB committed
154
                                        return;
Benjamin Elsdon's avatar
Benjamin Elsdon committed
155
156
                                }
                                mailBuilder.mail.itemList.add(new Mail.MailItem(item, amount, lvl));
Secretboy's avatar
Secretboy committed
157
                                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.send", Integer.toString(amount), Integer.toString(item), Integer.toString(lvl)));
158
                            }
Benjamin Elsdon's avatar
Benjamin Elsdon committed
159
                        }
160
                    }
Benjamin Elsdon's avatar
Benjamin Elsdon committed
161
162
                }
            } else {
Secretboy's avatar
Secretboy committed
163
                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.invalid_arguments_please_use", getConstructionArgs(mailBuilder.constructionStage, sender)));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
164
165
166
167
            }
        }
    }

Secretboy's avatar
Secretboy committed
168
    private String getConstructionArgs(int stage, Player sender) {
github-actions's avatar
github-actions committed
169
        return switch (stage) {
Secretboy's avatar
Secretboy committed
170
171
172
173
174
            case 0 -> translate(sender, "commands.sendMail.title");
            case 1 -> translate(sender, "commands.sendMail.message");
            case 2 -> translate(sender, "commands.sendMail.sender");
            case 3 -> translate(sender, "commands.sendMail.arguments");
            default -> translate(sender, "commands.sendMail.error", Integer.toString(stage));
175
        };
Benjamin Elsdon's avatar
Benjamin Elsdon committed
176
177
178
179
180
181
182
    }

    public static class MailBuilder {
        public int recipient;
        public boolean sendToAll;
        public int constructionStage;
        public Mail mail;
183

Benjamin Elsdon's avatar
Benjamin Elsdon committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
        public MailBuilder(int recipient, Mail mail) {
            this.recipient = recipient;
            this.sendToAll = false;
            this.constructionStage = 0;
            this.mail = mail;
        }

        public MailBuilder(boolean sendToAll, Mail mail) {
            if (sendToAll) {
                this.recipient = 0;
                this.sendToAll = true;
                this.constructionStage = 0;
                this.mail = mail;
            } else {
                Grasscutter.getLogger().error("Please use MailBuilder(int, mail) when not sending to all");
                Thread.dumpStack();
            }
        }
202
203
    }
}