Skip to content

Commit

Permalink
Add /mail sendtempall (#4561)
Browse files Browse the repository at this point in the history
Implements #4558
  • Loading branch information
JRoy authored Oct 24, 2021
1 parent 9601687 commit e43f06b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,19 @@ public void run(final Server server, final User user, final String commandLabel,
if (!user.isAuthorized("essentials.mail.sendall")) {
throw new Exception(tl("noPerm", "essentials.mail.sendall"));
}
ess.runTaskAsynchronously(new SendAll(user, FormatUtil.formatMessage(user, "essentials.mail", StringUtil.sanitizeString(FormatUtil.stripFormat(getFinalArg(args, 1))))));
ess.runTaskAsynchronously(new SendAll(user,
FormatUtil.formatMessage(user, "essentials.mail",
StringUtil.sanitizeString(FormatUtil.stripFormat(getFinalArg(args, 1)))), 0));
user.sendMessage(tl("mailSent"));
return;
}
if (args.length >= 3 && "sendtempall".equalsIgnoreCase(args[0])) {
if (!user.isAuthorized("essentials.mail.sendtempall")) {
throw new Exception(tl("noPerm", "essentials.mail.sendtempall"));
}
ess.runTaskAsynchronously(new SendAll(user,
FormatUtil.formatMessage(user, "essentials.mail",
StringUtil.sanitizeString(FormatUtil.stripFormat(getFinalArg(args, 2)))), DateUtil.parseDateDiff(args[1], true)));
user.sendMessage(tl("mailSent"));
return;
}
Expand Down Expand Up @@ -213,7 +225,12 @@ protected void run(final Server server, final CommandSource sender, final String
sender.sendMessage(tl("mailSent"));
return;
} else if (args.length >= 2 && "sendall".equalsIgnoreCase(args[0])) {
ess.runTaskAsynchronously(new SendAll(Console.getInstance(), FormatUtil.replaceFormat(getFinalArg(args, 1))));
ess.runTaskAsynchronously(new SendAll(Console.getInstance(), FormatUtil.replaceFormat(getFinalArg(args, 1)), 0));
sender.sendMessage(tl("mailSent"));
return;
} else if (args.length >= 3 && "sendtempall".equalsIgnoreCase(args[0])) {
final long dateDiff = DateUtil.parseDateDiff(args[1], true);
ess.runTaskAsynchronously(new SendAll(Console.getInstance(), FormatUtil.replaceFormat(getFinalArg(args, 2)), dateDiff));
sender.sendMessage(tl("mailSent"));
return;
} else if (args.length >= 2) {
Expand All @@ -232,20 +249,22 @@ protected void run(final Server server, final CommandSource sender, final String
}

private class SendAll implements Runnable {
IMessageRecipient messageRecipient;
String message;
private final IMessageRecipient messageRecipient;
private final String message;
private final long dateDiff;

SendAll(IMessageRecipient messageRecipient, String message) {
SendAll(IMessageRecipient messageRecipient, String message, long dateDiff) {
this.messageRecipient = messageRecipient;
this.message = message;
this.dateDiff = dateDiff;
}

@Override
public void run() {
for (UUID userid : ess.getUserMap().getAllUniqueUsers()) {
final User user = ess.getUserMap().getUser(userid);
if (user != null) {
user.sendMail(messageRecipient, message);
user.sendMail(messageRecipient, message, dateDiff);
}
}
}
Expand All @@ -264,38 +283,47 @@ protected List<String> getTabCompleteOptions(final Server server, final User use
if (user.isAuthorized("essentials.mail.sendall")) {
options.add("sendall");
}
if (user.isAuthorized("essentials.mail.sendtempall")) {
options.add("sendtempall");
}
return options;
} else if (args.length == 2 && ((args[0].equalsIgnoreCase("send") && user.isAuthorized("essentials.mail.send")) || (args[0].equalsIgnoreCase("sendtemp") && user.isAuthorized("essentials.mail.sendtemp")))) {
return getPlayers(server, user);
} else if (args.length == 2 && args[0].equalsIgnoreCase("read")) {
final ArrayList<MailMessage> mail = user.getMailMessages();
final int pages = mail != null ? (mail.size() / 9 + (mail.size() % 9 > 0 ? 1 : 0)) : 0;
if (pages == 0) {
return Lists.newArrayList("0");
} else {
final List<String> options = Lists.newArrayList("1");
if (pages > 1) {
options.add(String.valueOf(pages));
} else if (args.length == 2) {
if ((args[0].equalsIgnoreCase("send") && user.isAuthorized("essentials.mail.send")) || (args[0].equalsIgnoreCase("sendtemp") && user.isAuthorized("essentials.mail.sendtemp"))) {
return getPlayers(server, user);
} else if (args[0].equalsIgnoreCase("sendtempall") && user.isAuthorized("essentials.mail.sendtempall")) {
return COMMON_DATE_DIFFS;
} else if (args[0].equalsIgnoreCase("read")) {
final ArrayList<MailMessage> mail = user.getMailMessages();
final int pages = mail != null ? (mail.size() / 9 + (mail.size() % 9 > 0 ? 1 : 0)) : 0;
if (pages == 0) {
return Lists.newArrayList("0");
} else {
final List<String> options = Lists.newArrayList("1");
if (pages > 1) {
options.add(String.valueOf(pages));
}
return options;
}
return options;
}
} else if (args.length == 3 && args[0].equalsIgnoreCase("sendtemp") && user.isAuthorized("essentials.mail.sendtemp")) {
return COMMON_DATE_DIFFS;
} else {
return Collections.emptyList();
}
return Collections.emptyList();
}

@Override
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
if (args.length == 1) {
return Lists.newArrayList("send", "sendall");
} else if (args.length == 2 && (args[0].equalsIgnoreCase("send") || args[0].equalsIgnoreCase("sendtemp"))) {
return getPlayers(server, sender);
return Lists.newArrayList("send", "sendall", "sendtemp", "sendtempall");
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("send") || args[0].equalsIgnoreCase("sendtemp")) {
return getPlayers(server, sender);
} else if (args[0].equalsIgnoreCase("sendtempall")) {
return COMMON_DATE_DIFFS;
}
} else if (args.length == 3 && args[0].equalsIgnoreCase("sendtemp")) {
return COMMON_DATE_DIFFS;
} else {
return Collections.emptyList();
}
return Collections.emptyList();
}
}
2 changes: 2 additions & 0 deletions Essentials/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ mailCommandUsage4=/<command> sendall <message>
mailCommandUsage4Description=Sends all players the given message
mailCommandUsage5=/<command> sendtemp <player> <expire time> <message>
mailCommandUsage5Description=Sends the specified player the given message which will expire in the specified time
mailCommandUsage6=/<command> sendtempall <expire time> <message>
mailCommandUsage6Description=Sends all players the given message which will expire in the specified time
mailDelay=Too many mails have been sent within the last minute. Maximum\: {0}
mailFormatNew=\u00a76[\u00a7r{0}\u00a76] \u00a76[\u00a7r{1}\u00a76] \u00a7r{2}
mailFormatNewTimed=\u00a76[\u00a7e\u26a0\u00a76] \u00a76[\u00a7r{0}\u00a76] \u00a76[\u00a7r{1}\u00a76] \u00a7r{2}
Expand Down

0 comments on commit e43f06b

Please sign in to comment.