2011-03-19 23:39:51 +01:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
2013-10-16 21:59:39 +02:00
|
|
|
import com.earth2me.essentials.CommandSource;
|
2021-07-01 17:23:32 +02:00
|
|
|
import com.earth2me.essentials.Console;
|
2011-03-19 23:39:51 +01:00
|
|
|
import com.earth2me.essentials.User;
|
2021-07-01 17:23:32 +02:00
|
|
|
import com.earth2me.essentials.messaging.IMessageRecipient;
|
2014-08-11 20:03:24 +02:00
|
|
|
import com.earth2me.essentials.textreader.SimpleTextInput;
|
|
|
|
import com.earth2me.essentials.textreader.TextPager;
|
2020-07-06 20:52:51 +02:00
|
|
|
import com.earth2me.essentials.utils.DateUtil;
|
2013-06-08 23:31:19 +02:00
|
|
|
import com.earth2me.essentials.utils.FormatUtil;
|
2021-07-01 17:23:32 +02:00
|
|
|
import com.earth2me.essentials.utils.NumberUtil;
|
2013-10-11 04:44:41 +02:00
|
|
|
import com.earth2me.essentials.utils.StringUtil;
|
2017-06-11 02:17:43 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2021-07-01 17:23:32 +02:00
|
|
|
import net.essentialsx.api.v2.services.mail.MailMessage;
|
2011-11-18 18:42:26 +01:00
|
|
|
import org.bukkit.Server;
|
2011-03-19 23:39:51 +01:00
|
|
|
|
2021-07-01 17:23:32 +02:00
|
|
|
import java.util.ArrayList;
|
2017-06-11 02:17:43 +02:00
|
|
|
import java.util.Collections;
|
2014-03-08 07:21:50 +01:00
|
|
|
import java.util.List;
|
2021-07-01 17:23:32 +02:00
|
|
|
import java.util.ListIterator;
|
2014-04-13 07:53:11 +02:00
|
|
|
import java.util.UUID;
|
2014-03-08 07:21:50 +01:00
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
|
|
|
|
|
|
|
public class Commandmail extends EssentialsCommand {
|
|
|
|
private static int mailsPerMinute = 0;
|
|
|
|
private static long timestamp = 0;
|
|
|
|
|
|
|
|
public Commandmail() {
|
|
|
|
super("mail");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
|
|
|
|
if (args.length >= 1 && "read".equalsIgnoreCase(args[0])) {
|
2021-07-01 17:23:32 +02:00
|
|
|
final ArrayList<MailMessage> mail = user.getMailMessages();
|
|
|
|
if (mail == null || mail.size() == 0) {
|
|
|
|
user.sendMessage(tl("noMail"));
|
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
final SimpleTextInput input = new SimpleTextInput();
|
|
|
|
final ListIterator<MailMessage> iterator = mail.listIterator();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
final MailMessage mailObj = iterator.next();
|
|
|
|
if (mailObj.isExpired()) {
|
|
|
|
iterator.remove();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
input.addLine(ess.getMail().getMailLine(mailObj));
|
|
|
|
iterator.set(new MailMessage(true, mailObj.isLegacy(), mailObj.getSenderUsername(),
|
|
|
|
mailObj.getSenderUUID(), mailObj.getTimeSent(), mailObj.getTimeExpire(), mailObj.getMessage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input.getLines().isEmpty()) {
|
2015-04-15 06:06:16 +02:00
|
|
|
user.sendMessage(tl("noMail"));
|
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
|
|
|
|
|
|
|
final TextPager pager = new TextPager(input);
|
|
|
|
pager.showPage(args.length > 1 ? args[1] : null, null, commandLabel + " " + args[0], user.getSource());
|
|
|
|
|
|
|
|
user.sendMessage(tl("mailClear"));
|
2021-07-01 17:23:32 +02:00
|
|
|
user.setMailList(mail);
|
2015-04-15 06:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (args.length >= 3 && "send".equalsIgnoreCase(args[0])) {
|
|
|
|
if (!user.isAuthorized("essentials.mail.send")) {
|
|
|
|
throw new Exception(tl("noPerm", "essentials.mail.send"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.isMuted()) {
|
2020-10-03 19:46:05 +02:00
|
|
|
final String dateDiff = user.getMuteTimeout() > 0 ? DateUtil.formatDateDiff(user.getMuteTimeout()) : null;
|
2020-07-06 20:52:51 +02:00
|
|
|
if (dateDiff == null) {
|
|
|
|
throw new Exception(user.hasMuteReason() ? tl("voiceSilencedReason", user.getMuteReason()) : tl("voiceSilenced"));
|
|
|
|
}
|
|
|
|
throw new Exception(user.hasMuteReason() ? tl("voiceSilencedReasonTime", dateDiff, user.getMuteReason()) : tl("voiceSilencedTime", dateDiff));
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
2020-10-03 19:46:05 +02:00
|
|
|
final User u;
|
Use playerNeverOnServer message where appropriate (#3489)
### Use `playerNeverOnServer` message where it should be used:
### https://github.com/EssentialsX/Essentials/pull/3489/commits/309e1c470d4bcfe2a2186f9720ce33f26441b802
`playerNeverOnServer=\u00a74Player\u00a7c {0} \u00a74was never on this server.` is currently used in the **Commandmail** class at [Line 61](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L61), [Line 116](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L116) and [Line 129](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L129) however is **not called** as PlayerNotFoundException is thrown by `#getPlayer` breaking current execution (below).
```
Commandmail#run() throws Exception {
...
User u = getPlayer(server, args[1], true, true); // throws PlayerNotFoundException
if (u == null) {
throw new Exception(tl("playerNeverOnServer", args[1]));
}
...
}
```
Before changes:
![bm](https://user-images.githubusercontent.com/24858857/87236993-6b679180-c3e8-11ea-83a7-002194f5c467.png)
After changes:
![mailafter](https://user-images.githubusercontent.com/24858857/87237060-3576dd00-c3e9-11ea-8020-d5a80a958ca0.png)
--------------------------
**Commandseen** currently throws the default PlayerNotFoundException `playerNotFound` message for players that have not logged on to the server where it would be more appropriate to use the `playerNeverOnServer` message.
```
Commandseen#run throws Exception {
...
AsyncRunnable#run() {
User userFromBukkit = ess.getUserMap().getUserFromBukkit(args[0]); <-- ***
try {
if (userFromBukkit != null) { <--- ***
showUserSeen(userFromBukkit);
} else {
showUserSeen(getPlayer(server, sender, args, 0)); <--- ***
}
} catch (Exception e) {
ess.showError(sender, e, commandLabel);
}
}
private void showUserSeen(User user) throws Exception {
if (user == null) { <--- ***
throw new PlayerNotFoundException();
}
showSeenMessage(server, sender, user, showBan, showIp, showLocation);
}
...
}
```
**`<-- ***`:**
`usersFromBukkit` null check is performed before `#showUserSeen` so there is no need for another null check.
`EssentialsCommand#getPlayer` throws **NotEnoughArguementsException** or **PlayerNotFoundException** after [arg checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L88) and [player checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L103). https://github.com/EssentialsX/Essentials/pull/3489#issuecomment-657138524
Before changes:
![sb](https://user-images.githubusercontent.com/24858857/87237038-e9c43380-c3e8-11ea-8294-8f91e8b6f25d.png)
After changes:
![seenafter](https://user-images.githubusercontent.com/24858857/87237067-53444200-c3e9-11ea-92c5-1784b4dcd739.png)
----------------
### 725128e
Catch more specfic exception `PlayerNotFoundException`.
Before changes:
![banbefore](https://user-images.githubusercontent.com/24858857/87237021-c1d4d000-c3e8-11ea-99e4-eb97b5a5ba6d.png)
After changes:
![afterunban](https://user-images.githubusercontent.com/24858857/87237081-8d154880-c3e9-11ea-9d35-a25b8c105969.png)
2020-08-04 16:30:05 +02:00
|
|
|
try {
|
|
|
|
u = getPlayer(server, args[1], true, true);
|
2020-10-03 19:46:05 +02:00
|
|
|
} catch (final PlayerNotFoundException e) {
|
2015-04-15 06:06:16 +02:00
|
|
|
throw new Exception(tl("playerNeverOnServer", args[1]));
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:23:32 +02:00
|
|
|
final String msg = FormatUtil.formatMessage(user, "essentials.mail", StringUtil.sanitizeString(FormatUtil.stripFormat(getFinalArg(args, 2))));
|
|
|
|
if (msg.length() > 1000) {
|
2015-04-15 06:06:16 +02:00
|
|
|
throw new Exception(tl("mailTooLong"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!u.isIgnoredPlayer(user)) {
|
|
|
|
if (Math.abs(System.currentTimeMillis() - timestamp) > 60000) {
|
|
|
|
timestamp = System.currentTimeMillis();
|
|
|
|
mailsPerMinute = 0;
|
|
|
|
}
|
|
|
|
mailsPerMinute++;
|
|
|
|
if (mailsPerMinute > ess.getSettings().getMailsPerMinute()) {
|
|
|
|
throw new Exception(tl("mailDelay", ess.getSettings().getMailsPerMinute()));
|
|
|
|
}
|
2021-07-01 17:23:32 +02:00
|
|
|
u.sendMail(user, msg);
|
2015-04-15 06:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
user.sendMessage(tl("mailSentTo", u.getDisplayName(), u.getName()));
|
2021-07-01 17:23:32 +02:00
|
|
|
user.sendMessage(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (args.length >= 4 && "sendtemp".equalsIgnoreCase(args[0])) {
|
|
|
|
if (!user.isAuthorized("essentials.mail.sendtemp")) {
|
|
|
|
throw new Exception(tl("noPerm", "essentials.mail.sendtemp"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.isMuted()) {
|
|
|
|
final String dateDiff = user.getMuteTimeout() > 0 ? DateUtil.formatDateDiff(user.getMuteTimeout()) : null;
|
|
|
|
if (dateDiff == null) {
|
|
|
|
throw new Exception(user.hasMuteReason() ? tl("voiceSilencedReason", user.getMuteReason()) : tl("voiceSilenced"));
|
|
|
|
}
|
|
|
|
throw new Exception(user.hasMuteReason() ? tl("voiceSilencedReasonTime", dateDiff, user.getMuteReason()) : tl("voiceSilencedTime", dateDiff));
|
|
|
|
}
|
|
|
|
|
|
|
|
final User u;
|
|
|
|
try {
|
|
|
|
u = getPlayer(server, args[1], true, true);
|
|
|
|
} catch (final PlayerNotFoundException e) {
|
|
|
|
throw new Exception(tl("playerNeverOnServer", args[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
final long dateDiff = DateUtil.parseDateDiff(args[2], true);
|
|
|
|
|
|
|
|
final String msg = FormatUtil.formatMessage(user, "essentials.mail", StringUtil.sanitizeString(FormatUtil.stripFormat(getFinalArg(args, 3))));
|
|
|
|
if (msg.length() > 1000) {
|
|
|
|
throw new Exception(tl("mailTooLong"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!u.isIgnoredPlayer(user)) {
|
|
|
|
if (Math.abs(System.currentTimeMillis() - timestamp) > 60000) {
|
|
|
|
timestamp = System.currentTimeMillis();
|
|
|
|
mailsPerMinute = 0;
|
|
|
|
}
|
|
|
|
mailsPerMinute++;
|
|
|
|
if (mailsPerMinute > ess.getSettings().getMailsPerMinute()) {
|
|
|
|
throw new Exception(tl("mailDelay", ess.getSettings().getMailsPerMinute()));
|
|
|
|
}
|
|
|
|
u.sendMail(user, msg, dateDiff);
|
|
|
|
}
|
|
|
|
|
|
|
|
user.sendMessage(tl("mailSentToExpire", u.getDisplayName(), DateUtil.formatDateDiff(dateDiff), u.getName()));
|
|
|
|
user.sendMessage(msg);
|
2015-04-15 06:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (args.length > 1 && "sendall".equalsIgnoreCase(args[0])) {
|
|
|
|
if (!user.isAuthorized("essentials.mail.sendall")) {
|
|
|
|
throw new Exception(tl("noPerm", "essentials.mail.sendall"));
|
|
|
|
}
|
2021-10-24 18:45:01 +02:00
|
|
|
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)));
|
2015-04-15 06:06:16 +02:00
|
|
|
user.sendMessage(tl("mailSent"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (args.length >= 1 && "clear".equalsIgnoreCase(args[0])) {
|
2021-07-01 17:23:32 +02:00
|
|
|
final ArrayList<MailMessage> mails = user.getMailMessages();
|
|
|
|
if (mails == null || mails.size() == 0) {
|
2019-04-18 19:18:42 +02:00
|
|
|
user.sendMessage(tl("noMail"));
|
|
|
|
throw new NoChargeException();
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:23:32 +02:00
|
|
|
if (args.length > 1) {
|
|
|
|
if (!NumberUtil.isPositiveInt(args[1])) {
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
final int toRemove = Integer.parseInt(args[1]);
|
|
|
|
if (toRemove > mails.size()) {
|
|
|
|
user.sendMessage(tl("mailClearIndex", mails.size()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mails.remove(toRemove - 1);
|
|
|
|
user.setMailList(mails);
|
|
|
|
} else {
|
|
|
|
user.setMailList(null);
|
|
|
|
}
|
|
|
|
|
2015-04-15 06:06:16 +02:00
|
|
|
user.sendMessage(tl("mailCleared"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-03 19:46:05 +02:00
|
|
|
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
|
2015-04-15 06:06:16 +02:00
|
|
|
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 >= 3 && "send".equalsIgnoreCase(args[0])) {
|
2020-10-03 19:46:05 +02:00
|
|
|
final User u;
|
Use playerNeverOnServer message where appropriate (#3489)
### Use `playerNeverOnServer` message where it should be used:
### https://github.com/EssentialsX/Essentials/pull/3489/commits/309e1c470d4bcfe2a2186f9720ce33f26441b802
`playerNeverOnServer=\u00a74Player\u00a7c {0} \u00a74was never on this server.` is currently used in the **Commandmail** class at [Line 61](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L61), [Line 116](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L116) and [Line 129](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L129) however is **not called** as PlayerNotFoundException is thrown by `#getPlayer` breaking current execution (below).
```
Commandmail#run() throws Exception {
...
User u = getPlayer(server, args[1], true, true); // throws PlayerNotFoundException
if (u == null) {
throw new Exception(tl("playerNeverOnServer", args[1]));
}
...
}
```
Before changes:
![bm](https://user-images.githubusercontent.com/24858857/87236993-6b679180-c3e8-11ea-83a7-002194f5c467.png)
After changes:
![mailafter](https://user-images.githubusercontent.com/24858857/87237060-3576dd00-c3e9-11ea-8020-d5a80a958ca0.png)
--------------------------
**Commandseen** currently throws the default PlayerNotFoundException `playerNotFound` message for players that have not logged on to the server where it would be more appropriate to use the `playerNeverOnServer` message.
```
Commandseen#run throws Exception {
...
AsyncRunnable#run() {
User userFromBukkit = ess.getUserMap().getUserFromBukkit(args[0]); <-- ***
try {
if (userFromBukkit != null) { <--- ***
showUserSeen(userFromBukkit);
} else {
showUserSeen(getPlayer(server, sender, args, 0)); <--- ***
}
} catch (Exception e) {
ess.showError(sender, e, commandLabel);
}
}
private void showUserSeen(User user) throws Exception {
if (user == null) { <--- ***
throw new PlayerNotFoundException();
}
showSeenMessage(server, sender, user, showBan, showIp, showLocation);
}
...
}
```
**`<-- ***`:**
`usersFromBukkit` null check is performed before `#showUserSeen` so there is no need for another null check.
`EssentialsCommand#getPlayer` throws **NotEnoughArguementsException** or **PlayerNotFoundException** after [arg checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L88) and [player checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L103). https://github.com/EssentialsX/Essentials/pull/3489#issuecomment-657138524
Before changes:
![sb](https://user-images.githubusercontent.com/24858857/87237038-e9c43380-c3e8-11ea-8294-8f91e8b6f25d.png)
After changes:
![seenafter](https://user-images.githubusercontent.com/24858857/87237067-53444200-c3e9-11ea-92c5-1784b4dcd739.png)
----------------
### 725128e
Catch more specfic exception `PlayerNotFoundException`.
Before changes:
![banbefore](https://user-images.githubusercontent.com/24858857/87237021-c1d4d000-c3e8-11ea-99e4-eb97b5a5ba6d.png)
After changes:
![afterunban](https://user-images.githubusercontent.com/24858857/87237081-8d154880-c3e9-11ea-9d35-a25b8c105969.png)
2020-08-04 16:30:05 +02:00
|
|
|
try {
|
|
|
|
u = getPlayer(server, args[1], true, true);
|
2020-10-03 19:46:05 +02:00
|
|
|
} catch (final PlayerNotFoundException e) {
|
2015-04-15 06:06:16 +02:00
|
|
|
throw new Exception(tl("playerNeverOnServer", args[1]));
|
|
|
|
}
|
2021-07-01 17:23:32 +02:00
|
|
|
u.sendMail(Console.getInstance(), FormatUtil.replaceFormat(getFinalArg(args, 2)));
|
|
|
|
sender.sendMessage(tl("mailSent"));
|
|
|
|
return;
|
|
|
|
} else if (args.length >= 4 && "sendtemp".equalsIgnoreCase(args[0])) {
|
|
|
|
final User u;
|
|
|
|
try {
|
|
|
|
u = getPlayer(server, args[1], true, true);
|
|
|
|
} catch (final PlayerNotFoundException e) {
|
|
|
|
throw new Exception(tl("playerNeverOnServer", args[1]));
|
|
|
|
}
|
|
|
|
final long dateDiff = DateUtil.parseDateDiff(args[2], true);
|
|
|
|
u.sendMail(Console.getInstance(), FormatUtil.replaceFormat(getFinalArg(args, 3)), dateDiff);
|
2015-04-15 06:06:16 +02:00
|
|
|
sender.sendMessage(tl("mailSent"));
|
|
|
|
return;
|
|
|
|
} else if (args.length >= 2 && "sendall".equalsIgnoreCase(args[0])) {
|
2021-10-24 18:45:01 +02:00
|
|
|
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));
|
2015-04-15 06:06:16 +02:00
|
|
|
sender.sendMessage(tl("mailSent"));
|
|
|
|
return;
|
|
|
|
} else if (args.length >= 2) {
|
|
|
|
//allow sending from console without "send" argument, since it's the only thing the console can do
|
2020-10-03 19:46:05 +02:00
|
|
|
final User u;
|
Use playerNeverOnServer message where appropriate (#3489)
### Use `playerNeverOnServer` message where it should be used:
### https://github.com/EssentialsX/Essentials/pull/3489/commits/309e1c470d4bcfe2a2186f9720ce33f26441b802
`playerNeverOnServer=\u00a74Player\u00a7c {0} \u00a74was never on this server.` is currently used in the **Commandmail** class at [Line 61](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L61), [Line 116](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L116) and [Line 129](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/Commandmail.java#L129) however is **not called** as PlayerNotFoundException is thrown by `#getPlayer` breaking current execution (below).
```
Commandmail#run() throws Exception {
...
User u = getPlayer(server, args[1], true, true); // throws PlayerNotFoundException
if (u == null) {
throw new Exception(tl("playerNeverOnServer", args[1]));
}
...
}
```
Before changes:
![bm](https://user-images.githubusercontent.com/24858857/87236993-6b679180-c3e8-11ea-83a7-002194f5c467.png)
After changes:
![mailafter](https://user-images.githubusercontent.com/24858857/87237060-3576dd00-c3e9-11ea-8020-d5a80a958ca0.png)
--------------------------
**Commandseen** currently throws the default PlayerNotFoundException `playerNotFound` message for players that have not logged on to the server where it would be more appropriate to use the `playerNeverOnServer` message.
```
Commandseen#run throws Exception {
...
AsyncRunnable#run() {
User userFromBukkit = ess.getUserMap().getUserFromBukkit(args[0]); <-- ***
try {
if (userFromBukkit != null) { <--- ***
showUserSeen(userFromBukkit);
} else {
showUserSeen(getPlayer(server, sender, args, 0)); <--- ***
}
} catch (Exception e) {
ess.showError(sender, e, commandLabel);
}
}
private void showUserSeen(User user) throws Exception {
if (user == null) { <--- ***
throw new PlayerNotFoundException();
}
showSeenMessage(server, sender, user, showBan, showIp, showLocation);
}
...
}
```
**`<-- ***`:**
`usersFromBukkit` null check is performed before `#showUserSeen` so there is no need for another null check.
`EssentialsCommand#getPlayer` throws **NotEnoughArguementsException** or **PlayerNotFoundException** after [arg checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L88) and [player checks](https://github.com/EssentialsX/Essentials/blob/2.x/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java#L103). https://github.com/EssentialsX/Essentials/pull/3489#issuecomment-657138524
Before changes:
![sb](https://user-images.githubusercontent.com/24858857/87237038-e9c43380-c3e8-11ea-8294-8f91e8b6f25d.png)
After changes:
![seenafter](https://user-images.githubusercontent.com/24858857/87237067-53444200-c3e9-11ea-92c5-1784b4dcd739.png)
----------------
### 725128e
Catch more specfic exception `PlayerNotFoundException`.
Before changes:
![banbefore](https://user-images.githubusercontent.com/24858857/87237021-c1d4d000-c3e8-11ea-99e4-eb97b5a5ba6d.png)
After changes:
![afterunban](https://user-images.githubusercontent.com/24858857/87237081-8d154880-c3e9-11ea-9d35-a25b8c105969.png)
2020-08-04 16:30:05 +02:00
|
|
|
try {
|
|
|
|
u = getPlayer(server, args[0], true, true);
|
2020-10-03 19:46:05 +02:00
|
|
|
} catch (final PlayerNotFoundException e) {
|
2015-04-15 06:06:16 +02:00
|
|
|
throw new Exception(tl("playerNeverOnServer", args[0]));
|
|
|
|
}
|
2021-07-01 17:23:32 +02:00
|
|
|
u.sendMail(Console.getInstance(), FormatUtil.replaceFormat(getFinalArg(args, 1)));
|
2015-04-15 06:06:16 +02:00
|
|
|
sender.sendMessage(tl("mailSent"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new NotEnoughArgumentsException();
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:23:32 +02:00
|
|
|
private class SendAll implements Runnable {
|
2021-10-24 18:45:01 +02:00
|
|
|
private final IMessageRecipient messageRecipient;
|
|
|
|
private final String message;
|
|
|
|
private final long dateDiff;
|
2021-07-01 17:23:32 +02:00
|
|
|
|
2021-10-24 18:45:01 +02:00
|
|
|
SendAll(IMessageRecipient messageRecipient, String message, long dateDiff) {
|
2021-07-01 17:23:32 +02:00
|
|
|
this.messageRecipient = messageRecipient;
|
|
|
|
this.message = message;
|
2021-10-24 18:45:01 +02:00
|
|
|
this.dateDiff = dateDiff;
|
2021-07-01 17:23:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (UUID userid : ess.getUserMap().getAllUniqueUsers()) {
|
|
|
|
final User user = ess.getUserMap().getUser(userid);
|
|
|
|
if (user != null) {
|
2021-10-24 18:45:01 +02:00
|
|
|
user.sendMail(messageRecipient, message, dateDiff);
|
2021-07-01 17:23:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 02:17:43 +02:00
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
|
|
|
|
if (args.length == 1) {
|
2020-10-03 19:46:05 +02:00
|
|
|
final List<String> options = Lists.newArrayList("read", "clear");
|
2017-06-11 02:17:43 +02:00
|
|
|
if (user.isAuthorized("essentials.mail.send")) {
|
|
|
|
options.add("send");
|
|
|
|
}
|
2021-07-01 17:23:32 +02:00
|
|
|
if (user.isAuthorized("essentials.mail.sendtemp")) {
|
|
|
|
options.add("sendtemp");
|
|
|
|
}
|
2017-06-11 02:17:43 +02:00
|
|
|
if (user.isAuthorized("essentials.mail.sendall")) {
|
|
|
|
options.add("sendall");
|
|
|
|
}
|
2021-10-24 18:45:01 +02:00
|
|
|
if (user.isAuthorized("essentials.mail.sendtempall")) {
|
|
|
|
options.add("sendtempall");
|
|
|
|
}
|
2017-06-11 02:17:43 +02:00
|
|
|
return options;
|
2021-10-24 18:45:01 +02:00
|
|
|
} 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;
|
2017-06-11 02:17:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-01 17:23:32 +02:00
|
|
|
} else if (args.length == 3 && args[0].equalsIgnoreCase("sendtemp") && user.isAuthorized("essentials.mail.sendtemp")) {
|
|
|
|
return COMMON_DATE_DIFFS;
|
2017-06-11 02:17:43 +02:00
|
|
|
}
|
2021-10-24 18:45:01 +02:00
|
|
|
return Collections.emptyList();
|
2017-06-11 02:17:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
|
|
|
|
if (args.length == 1) {
|
2021-10-24 18:45:01 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-07-01 21:22:48 +02:00
|
|
|
} else if (args.length == 3 && args[0].equalsIgnoreCase("sendtemp")) {
|
2021-07-01 17:23:32 +02:00
|
|
|
return COMMON_DATE_DIFFS;
|
2017-06-11 02:17:43 +02:00
|
|
|
}
|
2021-10-24 18:45:01 +02:00
|
|
|
return Collections.emptyList();
|
2017-06-11 02:17:43 +02:00
|
|
|
}
|
2011-03-19 23:39:51 +01:00
|
|
|
}
|