SendMailCommand.java 10.9 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
@Command(label = "sendmail", usage = "sendmail <userId|all|help> [templateId]", permission = "server.sendmail", description = "commands.sendMail.description", targetRequirement = Command.TargetRequirement.NONE)
KingRainbow44's avatar
KingRainbow44 committed
17
public final class SendMailCommand implements CommandHandler {
18

Benjamin Elsdon's avatar
Benjamin Elsdon committed
19
20
21
22
23
    // 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
24
    private static final HashMap<Integer, MailBuilder> mailBeingConstructed = new HashMap<Integer, MailBuilder>();
Benjamin Elsdon's avatar
Benjamin Elsdon committed
25
26

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

        if (!mailBeingConstructed.containsKey(senderId)) {
            switch (args.size()) {
38
                case 1 -> {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
39
40
                    MailBuilder mailBuilder;
                    switch (args.get(0).toLowerCase()) {
41
                        case "help" -> {
tester233's avatar
tester233 committed
42
                            CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.usage"));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
43
                            return;
44
45
46
                        }
                        case "all" -> mailBuilder = new MailBuilder(true, new Mail());
                        default -> {
47
                            if (DatabaseHelper.getPlayerByUid(Integer.parseInt(args.get(0))) != null) {
Benjamin Elsdon's avatar
Benjamin Elsdon committed
48
49
                                mailBuilder = new MailBuilder(Integer.parseInt(args.get(0)), new Mail());
                            } else {
Secretboy's avatar
Secretboy committed
50
                                CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.user_not_exist", args.get(0)));
Benjamin Elsdon's avatar
Benjamin Elsdon committed
51
52
                                return;
                            }
53
                        }
Benjamin Elsdon's avatar
Benjamin Elsdon committed
54
55
                    }
                    mailBeingConstructed.put(senderId, mailBuilder);
Secretboy's avatar
Secretboy committed
56
                    CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.start_composition"));
57
                }
Secretboy's avatar
Secretboy committed
58
59
                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
60
61
62
63
64
65
            }
        } else {
            MailBuilder mailBuilder = mailBeingConstructed.get(senderId);

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

Secretboy's avatar
Secretboy committed
164
    private String getConstructionArgs(int stage, Player sender) {
github-actions's avatar
github-actions committed
165
        return switch (stage) {
Secretboy's avatar
Secretboy committed
166
167
168
169
170
            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));
171
        };
Benjamin Elsdon's avatar
Benjamin Elsdon committed
172
173
174
175
176
177
178
    }

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

Benjamin Elsdon's avatar
Benjamin Elsdon committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
        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();
            }
        }
198
199
    }
}