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")
Benjamin Elsdon's avatar
Benjamin Elsdon committed
16
17
@Command(label = "sendmail", usage = "sendmail <userId|all|help> [templateId]",
        description = "Sends mail to the specified user. The usage of this command changes based on it's composition state.", permission = "server.sendmail")
KingRainbow44's avatar
KingRainbow44 committed
18
public final class SendMailCommand implements CommandHandler {
19

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

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

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

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

165
    private String getConstructionArgs(int stage) {
166
167
168
169
170
171
172
        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
173
174
175
176
177
178
179
    }

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

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