Add isReachable API to IMessageRecipient.

Add translatable message recentlyForeverAlone.
Add behaviour that preserves reply-recipient only if they are reachable, and update reply-recipient if they are not reachable.
Fixes #248
This commit is contained in:
Ali Moghnieh 2015-10-30 19:58:30 +00:00
parent e5ebeaf724
commit 26045e2ec0
6 changed files with 33 additions and 5 deletions

View file

@ -50,6 +50,10 @@ public final class Console implements IMessageRecipient {
@Override public void sendMessage(String message) { @Override public void sendMessage(String message) {
getCommandSender().sendMessage(message); getCommandSender().sendMessage(message);
} }
@Override public boolean isReachable() {
return true;
}
/* ================================ /* ================================
* >> DELEGATE METHODS * >> DELEGATE METHODS

View file

@ -717,6 +717,10 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
public String getName() { public String getName() {
return this.getBase().getName(); return this.getBase().getName();
} }
@Override public boolean isReachable() {
return getBase().isOnline();
}
@Override public MessageResponse sendMessage(IMessageRecipient recipient, String message) { @Override public MessageResponse sendMessage(IMessageRecipient recipient, String message) {
return this.messageRecipient.sendMessage(recipient, message); return this.messageRecipient.sendMessage(recipient, message);

View file

@ -34,9 +34,8 @@ public class Commandr extends EssentialsCommand {
} }
final IMessageRecipient target = messageSender.getReplyRecipient(); final IMessageRecipient target = messageSender.getReplyRecipient();
// Check to make sure the sender does have a quick-reply recipient
// Check to make sure the sender does have a quick-reply recipient, and that the recipient is online. if (target == null) {
if (target == null || (target instanceof User && !((User) target).getBase().isOnline())) {
throw new Exception(tl("foreverAlone")); throw new Exception(tl("foreverAlone"));
} }
messageSender.sendMessage(target, message); messageSender.sendMessage(target, message);

View file

@ -56,6 +56,13 @@ public interface IMessageRecipient {
*/ */
String getDisplayName(); String getDisplayName();
/**
* Returns whether this recipient is reachable. A case where the recipient is not reachable is if they are offline.
*
* @return whether this recipient is reachable
*/
boolean isReachable();
/** /**
* Returns the {@link IMessageRecipient} this recipient should send replies to. * Returns the {@link IMessageRecipient} this recipient should send replies to.
* *
@ -82,7 +89,9 @@ public interface IMessageRecipient {
/** States that the message was <b>NOT</b> received as a result of the receiver ignoring all messages. */ /** States that the message was <b>NOT</b> received as a result of the receiver ignoring all messages. */
MESSAGES_IGNORED, MESSAGES_IGNORED,
/** States that the message was <b>NOT</b> received as a result of the sender being ignored by the recipient. */ /** States that the message was <b>NOT</b> received as a result of the sender being ignored by the recipient. */
SENDER_IGNORED; SENDER_IGNORED,
/** States that the message was <b>NOT</b> received as a result of the recipient being unreachable. */
UNREACHABLE;
/** /**
* Returns whether this response is a success. In other words equal to {@link #SUCCESS} or {@link #SUCCESS_BUT_AFK} * Returns whether this response is a success. In other words equal to {@link #SUCCESS} or {@link #SUCCESS_BUT_AFK}

View file

@ -23,6 +23,7 @@ import java.lang.ref.WeakReference;
* <li>{@link IMessageRecipient#sendMessage(String)}</li> * <li>{@link IMessageRecipient#sendMessage(String)}</li>
* <li>{@link IMessageRecipient#getName()}</li> * <li>{@link IMessageRecipient#getName()}</li>
* <li>{@link IMessageRecipient#getDisplayName()}</li> * <li>{@link IMessageRecipient#getDisplayName()}</li>
* <li>{@link IMessageRecipient#isReachable()}</li>
* </ul> * </ul>
* *
* The reply-recipient is wrapped in a {@link WeakReference}. * The reply-recipient is wrapped in a {@link WeakReference}.
@ -56,6 +57,8 @@ public class SimpleMessageRecipient implements IMessageRecipient {
@Override public MessageResponse sendMessage(IMessageRecipient recipient, String message) { @Override public MessageResponse sendMessage(IMessageRecipient recipient, String message) {
MessageResponse messageResponse = recipient.onReceiveMessage(this.parent, message); MessageResponse messageResponse = recipient.onReceiveMessage(this.parent, message);
switch (messageResponse) { switch (messageResponse) {
case UNREACHABLE:
sendMessage(tl("recentlyForeverAlone", recipient.getDisplayName()));
case MESSAGES_IGNORED: case MESSAGES_IGNORED:
sendMessage(tl("msgIgnore", recipient.getDisplayName())); sendMessage(tl("msgIgnore", recipient.getDisplayName()));
break; break;
@ -79,6 +82,10 @@ public class SimpleMessageRecipient implements IMessageRecipient {
@Override @Override
public MessageResponse onReceiveMessage(IMessageRecipient sender, String message) { public MessageResponse onReceiveMessage(IMessageRecipient sender, String message) {
if (!isReachable()) {
return MessageResponse.UNREACHABLE;
}
User user = this.parent instanceof User ? (User) this.parent : null; User user = this.parent instanceof User ? (User) this.parent : null;
boolean afk = false; boolean afk = false;
if (user != null) { if (user != null) {
@ -98,7 +105,7 @@ public class SimpleMessageRecipient implements IMessageRecipient {
if (ess.getSettings().isLastMessageReplyRecipient()) { if (ess.getSettings().isLastMessageReplyRecipient()) {
// If this recipient doesn't have a reply recipient, initiate by setting the first // If this recipient doesn't have a reply recipient, initiate by setting the first
// message sender to this recipient's replyRecipient. // message sender to this recipient's replyRecipient.
if (getReplyRecipient() == null) { if (!isReachable()) {
setReplyRecipient(sender); setReplyRecipient(sender);
} }
} else { // Old message functionality, always set the reply recipient to the last person who sent us a message. } else { // Old message functionality, always set the reply recipient to the last person who sent us a message.
@ -107,6 +114,10 @@ public class SimpleMessageRecipient implements IMessageRecipient {
return afk ? MessageResponse.SUCCESS_BUT_AFK : MessageResponse.SUCCESS; return afk ? MessageResponse.SUCCESS_BUT_AFK : MessageResponse.SUCCESS;
} }
@Override public boolean isReachable() {
return this.parent.isReachable();
}
/** /**
* {@inheritDoc} * {@inheritDoc}
* <p /> * <p />

View file

@ -121,6 +121,7 @@ fireworkSyntax=\u00a76Firework parameters\:\u00a7c color\:<color> [fade\:<color>
flyMode=\u00a76Set fly mode\u00a7c {0} \u00a76for {1}\u00a76. flyMode=\u00a76Set fly mode\u00a7c {0} \u00a76for {1}\u00a76.
flying=flying flying=flying
foreverAlone=\u00a74You have nobody to whom you can reply. foreverAlone=\u00a74You have nobody to whom you can reply.
recentlyForeverAlone=\u00a74{0} recently went offline.
fullStack=\u00a74You already have a full stack. fullStack=\u00a74You already have a full stack.
gameMode=\u00a76Set game mode\u00a7c {0} \u00a76for \u00a7c{1}\u00a76. gameMode=\u00a76Set game mode\u00a7c {0} \u00a76for \u00a7c{1}\u00a76.
gcWorld=\u00a76{0} "\u00a7c{1}\u00a76"\: \u00a7c{2}\u00a76 chunks, \u00a7c{3}\u00a76 entities, \u00a7c{4}\u00a76 tiles. gcWorld=\u00a76{0} "\u00a7c{1}\u00a76"\: \u00a7c{2}\u00a76 chunks, \u00a7c{3}\u00a76 entities, \u00a7c{4}\u00a76 tiles.