Add /mail clear <player> and /mail clearall commands (#4878)

Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
This commit is contained in:
Daniel Jensen 2023-08-08 11:03:22 -07:00 committed by GitHub
parent 6fb500d9fb
commit 409af5d2aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 28 deletions

View File

@ -169,31 +169,50 @@ public class Commandmail extends EssentialsCommand {
return;
}
if (args.length >= 1 && "clear".equalsIgnoreCase(args[0])) {
final ArrayList<MailMessage> mails = user.getMailMessages();
if (mails == null || mails.size() == 0) {
user.sendMessage(tl("noMail"));
User mailUser = user;
int toRemove = -1;
if (args.length > 1) {
if (NumberUtil.isPositiveInt(args[1])) {
toRemove = Integer.parseInt(args[1]);
} else if (!user.isAuthorized("essentials.mail.clear.others")) {
throw new Exception(tl("noPerm", "essentials.mail.clear.others"));
} else {
mailUser = getPlayer(ess.getServer(), user, args, 1, true);
if (args.length > 2 && NumberUtil.isPositiveInt(args[2])) {
toRemove = Integer.parseInt(args[2]);
}
}
}
final ArrayList<MailMessage> mails = mailUser.getMailMessages();
if (mails == null || mails.isEmpty()) {
user.sendMessage(tl(mailUser == user ? "noMail" : "noMailOther", mailUser.getDisplayName()));
throw new NoChargeException();
}
if (args.length > 1) {
if (!NumberUtil.isPositiveInt(args[1])) {
throw new NotEnoughArgumentsException();
}
final int toRemove = Integer.parseInt(args[1]);
if (toRemove > 0) {
if (toRemove > mails.size()) {
user.sendMessage(tl("mailClearIndex", mails.size()));
return;
throw new NoChargeException();
}
mails.remove(toRemove - 1);
user.setMailList(mails);
mailUser.setMailList(mails);
} else {
user.setMailList(null);
mailUser.setMailList(null);
}
user.sendMessage(tl("mailCleared"));
return;
}
if (args.length >= 1 && "clearall".equalsIgnoreCase(args[0])){
if (!user.isAuthorized("essentials.mail.clearall")) {
throw new Exception(tl("noPerm", "essentials.mail.clearall"));
}
ess.runTaskAsynchronously(new ClearAll());
user.sendMessage(tl("mailClearedAll"));
return;
}
throw new NotEnoughArgumentsException();
}
@ -201,8 +220,32 @@ public class Commandmail extends EssentialsCommand {
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
if (args.length >= 1 && "read".equalsIgnoreCase(args[0])) {
throw new Exception(tl("onlyPlayers", commandLabel + " read"));
} else if (args.length >= 1 && "clear".equalsIgnoreCase(args[0])) {
throw new Exception(tl("onlyPlayers", commandLabel + " clear"));
} else if (args.length > 1 && "clear".equalsIgnoreCase(args[0])) {
final User mailUser = getPlayer(server, args[1], true, true);
final int toRemove = args.length > 2 ? NumberUtil.isPositiveInt(args[2]) ? Integer.parseInt(args[2]) : -1 : -1;
final ArrayList<MailMessage> mails = mailUser.getMailMessages();
if (mails == null || mails.isEmpty()) {
sender.sendMessage(tl("noMailOther", mailUser.getDisplayName()));
throw new NoChargeException();
}
if (toRemove > 0) {
if (toRemove > mails.size()) {
sender.sendMessage(tl("mailClearIndex", mails.size()));
throw new NoChargeException();
}
mails.remove(toRemove - 1);
mailUser.setMailList(mails);
} else {
mailUser.setMailList(null);
}
sender.sendMessage(tl("mailCleared"));
return;
} else if (args.length >= 1 && "clearall".equalsIgnoreCase(args[0])){
ess.runTaskAsynchronously(new ClearAll());
sender.sendMessage(tl("mailClearedAll"));
return;
} else if (args.length >= 3 && "send".equalsIgnoreCase(args[0])) {
final User u;
try {
@ -286,9 +329,12 @@ public class Commandmail extends EssentialsCommand {
if (user.isAuthorized("essentials.mail.sendtempall")) {
options.add("sendtempall");
}
if (user.isAuthorized("essentials.mail.clearall")){
options.add("clearall");
}
return options;
} else if (args.length == 2) {
if ((args[0].equalsIgnoreCase("send") && user.isAuthorized("essentials.mail.send")) || (args[0].equalsIgnoreCase("sendtemp") && user.isAuthorized("essentials.mail.sendtemp"))) {
if ((args[0].equalsIgnoreCase("send") && user.isAuthorized("essentials.mail.send")) || (args[0].equalsIgnoreCase("sendtemp") && user.isAuthorized("essentials.mail.sendtemp")) || ((args[0].equalsIgnoreCase("clear"))&& user.isAuthorized("essentials.mail.clear.others"))) {
return getPlayers(server, user);
} else if (args[0].equalsIgnoreCase("sendtempall") && user.isAuthorized("essentials.mail.sendtempall")) {
return COMMON_DATE_DIFFS;
@ -326,9 +372,9 @@ public class Commandmail extends EssentialsCommand {
@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", "sendtemp", "sendtempall");
return Lists.newArrayList("send", "sendall", "sendtemp", "sendtempall", "clearall", "clear");
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("send") || args[0].equalsIgnoreCase("sendtemp")) {
if (args[0].equalsIgnoreCase("send") || args[0].equalsIgnoreCase("sendtemp") || args[0].equalsIgnoreCase("clear")) {
return getPlayers(server, sender);
} else if (args[0].equalsIgnoreCase("sendtempall")) {
return COMMON_DATE_DIFFS;
@ -338,4 +384,16 @@ public class Commandmail extends EssentialsCommand {
}
return Collections.emptyList();
}
private class ClearAll implements Runnable {
@Override
public void run() {
for (UUID u : ess.getUsers().getAllUserUUIDs()) {
final User user = ess.getUsers().loadUncachedUser(u);
if (user != null) {
user.setMailList(null);
}
}
}
}
}

View File

@ -711,21 +711,26 @@ loomCommandDescription=Opens up a loom.
loomCommandUsage=/<command>
mailClear=\u00a76To clear your mail, type\u00a7c /mail clear\u00a76.
mailCleared=\u00a76Mail cleared\!
mailClearedAll=\u00a76Mail cleared for all players\!
mailClearIndex=\u00a74You must specify a number between 1-{0}.
mailCommandDescription=Manages inter-player, intra-server mail.
mailCommandUsage=/<command> [read|clear|clear [number]|send [to] [message]|sendtemp [to] [expire time] [message]|sendall [message]]
mailCommandUsage=/<command> [read|clear|clear [number]|clear <player> [number]|send [to] [message]|sendtemp [to] [expire time] [message]|sendall [message]]
mailCommandUsage1=/<command> read [page]
mailCommandUsage1Description=Reads the first (or specified) page of your mail
mailCommandUsage2=/<command> clear [number]
mailCommandUsage2Description=Clears either all or the specified mail(s)
mailCommandUsage3=/<command> send <player> <message>
mailCommandUsage3Description=Sends the specified player the given message
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
mailCommandUsage3=/<command> clear <player> [number]
mailCommandUsage3Description=Clears either all or the specified mail(s) for the given player
mailCommandUsage4=/<command> clearall
mailCommandUsage4Description=Clears all mail for the all players
mailCommandUsage5=/<command> send <player> <message>
mailCommandUsage5Description=Sends the specified player the given message
mailCommandUsage6=/<command> sendall <message>
mailCommandUsage6Description=Sends all players the given message
mailCommandUsage7=/<command> sendtemp <player> <expire time> <message>
mailCommandUsage7Description=Sends the specified player the given message which will expire in the specified time
mailCommandUsage8=/<command> sendtempall <expire time> <message>
mailCommandUsage8Description=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}
@ -853,6 +858,7 @@ noKitPermission=\u00a74You need the \u00a7c{0}\u00a74 permission to use that kit
noKits=\u00a76There are no kits available yet.
noLocationFound=\u00a74No valid location found.
noMail=\u00a76You do not have any mail.
noMailOther=\u00a7c{0} \u00a76does not have any mail.
noMatchingPlayers=\u00a76No matching players found.
noMetaFirework=\u00a74You do not have permission to apply firework meta.
noMetaJson=JSON Metadata is not supported in this version of Bukkit.

View File

@ -290,7 +290,7 @@ commands:
aliases: [eloom]
mail:
description: Manages inter-player, intra-server mail.
usage: /<command> [read|clear|clear [number]|send [to] [message]|sendtemp [to] [expire time] [message]|sendall [message]]
usage: /<command> [read|clear|clear [number]|clear <player> [number]|send [to] [message]|sendtemp [to] [expire time] [message]|sendall [message]]
aliases: [email,eemail,memo,ememo]
me:
description: Describes an action in the context of the player.