SendMailCommand.java 10.7 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")
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
27
28
29
30
    @Override
    public String description() {
        return translate("commands.sendMail.description");
    }

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

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

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

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

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

Benjamin Elsdon's avatar
Benjamin Elsdon committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        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();
            }
        }
203
204
    }
}