diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandmail.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandmail.java index 59d8b2775..762a3cc14 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandmail.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandmail.java @@ -169,31 +169,50 @@ public class Commandmail extends EssentialsCommand { return; } if (args.length >= 1 && "clear".equalsIgnoreCase(args[0])) { - final ArrayList 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 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 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 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); + } + } + } + } } diff --git a/Essentials/src/main/resources/messages.properties b/Essentials/src/main/resources/messages.properties index 2a8408423..0892fbbaa 100644 --- a/Essentials/src/main/resources/messages.properties +++ b/Essentials/src/main/resources/messages.properties @@ -711,21 +711,26 @@ loomCommandDescription=Opens up a loom. loomCommandUsage=/ 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=/ [read|clear|clear [number]|send [to] [message]|sendtemp [to] [expire time] [message]|sendall [message]] +mailCommandUsage=/ [read|clear|clear [number]|clear [number]|send [to] [message]|sendtemp [to] [expire time] [message]|sendall [message]] mailCommandUsage1=/ read [page] mailCommandUsage1Description=Reads the first (or specified) page of your mail mailCommandUsage2=/ clear [number] mailCommandUsage2Description=Clears either all or the specified mail(s) -mailCommandUsage3=/ send -mailCommandUsage3Description=Sends the specified player the given message -mailCommandUsage4=/ sendall -mailCommandUsage4Description=Sends all players the given message -mailCommandUsage5=/ sendtemp -mailCommandUsage5Description=Sends the specified player the given message which will expire in the specified time -mailCommandUsage6=/ sendtempall -mailCommandUsage6Description=Sends all players the given message which will expire in the specified time +mailCommandUsage3=/ clear [number] +mailCommandUsage3Description=Clears either all or the specified mail(s) for the given player +mailCommandUsage4=/ clearall +mailCommandUsage4Description=Clears all mail for the all players +mailCommandUsage5=/ send +mailCommandUsage5Description=Sends the specified player the given message +mailCommandUsage6=/ sendall +mailCommandUsage6Description=Sends all players the given message +mailCommandUsage7=/ sendtemp +mailCommandUsage7Description=Sends the specified player the given message which will expire in the specified time +mailCommandUsage8=/ sendtempall +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. diff --git a/Essentials/src/main/resources/plugin.yml b/Essentials/src/main/resources/plugin.yml index 0ec11754b..f9a8c44d7 100644 --- a/Essentials/src/main/resources/plugin.yml +++ b/Essentials/src/main/resources/plugin.yml @@ -290,7 +290,7 @@ commands: aliases: [eloom] mail: description: Manages inter-player, intra-server mail. - usage: / [read|clear|clear [number]|send [to] [message]|sendtemp [to] [expire time] [message]|sendall [message]] + usage: / [read|clear|clear [number]|clear [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.