2015-10-27 17:34:59 +00:00
package com.earth2me.essentials.messaging ;
import com.earth2me.essentials.IEssentials ;
import com.earth2me.essentials.IUser ;
import com.earth2me.essentials.User ;
import java.lang.ref.WeakReference ;
2019-03-09 15:39:45 -08:00
import static com.earth2me.essentials.I18n.tl ;
2015-10-27 17:34:59 +00:00
/ * *
* Represents a simple reusable implementation of { @link IMessageRecipient } . This class provides functionality for the following methods :
* < ul >
* < li > { @link IMessageRecipient # sendMessage ( IMessageRecipient , String ) } < / li >
* < li > { @link IMessageRecipient # onReceiveMessage ( IMessageRecipient , String ) } < / li >
* < li > { @link IMessageRecipient # getReplyRecipient ( ) } < / li >
* < li > { @link IMessageRecipient # setReplyRecipient ( IMessageRecipient ) } < / li >
* < / ul >
2018-05-26 21:43:31 +02:00
*
2015-10-27 17:34:59 +00:00
* < b > The given { @code parent } must implement the following methods to prevent overflow : < / b >
* < ul >
* < li > { @link IMessageRecipient # sendMessage ( String ) } < / li >
* < li > { @link IMessageRecipient # getName ( ) } < / li >
* < li > { @link IMessageRecipient # getDisplayName ( ) } < / li >
2015-10-30 19:58:30 +00:00
* < li > { @link IMessageRecipient # isReachable ( ) } < / li >
2015-10-27 17:34:59 +00:00
* < / ul >
2018-05-26 21:43:31 +02:00
*
2015-10-27 17:34:59 +00:00
* The reply - recipient is wrapped in a { @link WeakReference } .
* /
public class SimpleMessageRecipient implements IMessageRecipient {
private final IEssentials ess ;
private final IMessageRecipient parent ;
2018-05-26 21:43:31 +02:00
2015-11-24 22:09:59 +00:00
private long lastMessageMs ;
2015-10-27 17:34:59 +00:00
private WeakReference < IMessageRecipient > replyRecipient ;
2016-07-25 13:10:06 +01:00
protected static User getUser ( IMessageRecipient recipient ) {
if ( recipient instanceof SimpleMessageRecipient ) {
return ( ( SimpleMessageRecipient ) recipient ) . parent instanceof User ? ( User ) ( ( SimpleMessageRecipient ) recipient ) . parent : null ;
}
return recipient instanceof User ? ( User ) recipient : null ;
}
2018-05-26 21:43:31 +02:00
2015-10-27 17:34:59 +00:00
public SimpleMessageRecipient ( IEssentials ess , IMessageRecipient parent ) {
this . ess = ess ;
this . parent = parent ;
}
@Override
public void sendMessage ( String message ) {
this . parent . sendMessage ( message ) ;
}
@Override
public String getName ( ) {
return this . parent . getName ( ) ;
}
@Override public String getDisplayName ( ) {
return this . parent . getDisplayName ( ) ;
}
@Override public MessageResponse sendMessage ( IMessageRecipient recipient , String message ) {
MessageResponse messageResponse = recipient . onReceiveMessage ( this . parent , message ) ;
switch ( messageResponse ) {
2015-10-30 19:58:30 +00:00
case UNREACHABLE :
sendMessage ( tl ( " recentlyForeverAlone " , recipient . getDisplayName ( ) ) ) ;
2015-11-03 23:39:18 +00:00
break ;
2015-10-27 17:34:59 +00:00
case MESSAGES_IGNORED :
2015-10-30 19:22:39 +00:00
sendMessage ( tl ( " msgIgnore " , recipient . getDisplayName ( ) ) ) ;
2015-10-27 17:34:59 +00:00
break ;
case SENDER_IGNORED :
break ;
// When this recipient is AFK, notify the sender. Then, proceed to send the message.
2015-10-30 19:22:39 +00:00
case SUCCESS_BUT_AFK :
2016-06-18 17:44:17 +01:00
// Currently, only IUser can be afk, so we unsafely cast to get the afk message.
if ( ( ( IUser ) recipient ) . getAfkMessage ( ) ! = null ) {
2016-06-18 17:54:21 +01:00
sendMessage ( tl ( " userAFKWithMessage " , recipient . getDisplayName ( ) , ( ( IUser ) recipient ) . getAfkMessage ( ) ) ) ;
2016-06-18 17:44:17 +01:00
} else {
sendMessage ( tl ( " userAFK " , recipient . getDisplayName ( ) ) ) ;
}
2015-10-27 17:34:59 +00:00
default :
sendMessage ( tl ( " msgFormat " , tl ( " me " ) , recipient . getDisplayName ( ) , message ) ) ;
2016-07-22 15:52:53 +01:00
// Better Social Spy
2016-07-25 13:10:06 +01:00
User senderUser = getUser ( this ) ;
User recipientUser = getUser ( recipient ) ;
2016-07-22 15:52:53 +01:00
if ( senderUser ! = null // not null if player.
2016-07-25 13:10:06 +01:00
// Dont spy on chats involving socialspy exempt players
& & ! senderUser . isAuthorized ( " essentials.chat.spy.exempt " )
& & ( recipientUser ! = null & & ! recipientUser . isAuthorized ( " essentials.chat.spy.exempt " ) ) ) {
2016-07-22 15:52:53 +01:00
for ( User onlineUser : ess . getOnlineUsers ( ) ) {
if ( onlineUser . isSocialSpyEnabled ( )
// Don't send socialspy messages to message sender/receiver to prevent spam
& & ! onlineUser . equals ( senderUser )
2017-08-04 01:04:42 +02:00
& & ! onlineUser . equals ( recipient ) ) {
if ( senderUser . isMuted ( ) & & ess . getSettings ( ) . getSocialSpyListenMutedPlayers ( ) ) {
2017-08-11 09:46:50 -04:00
onlineUser . sendMessage ( tl ( " socialMutedSpyPrefix " ) + tl ( " socialSpyMsgFormat " , getDisplayName ( ) , recipient . getDisplayName ( ) , message ) ) ;
2017-08-04 01:04:42 +02:00
} else {
2017-08-11 09:46:50 -04:00
onlineUser . sendMessage ( tl ( " socialSpyPrefix " ) + tl ( " socialSpyMsgFormat " , getDisplayName ( ) , recipient . getDisplayName ( ) , message ) ) ;
2017-08-04 01:04:42 +02:00
}
2016-07-22 15:52:53 +01:00
}
}
}
2015-10-27 17:34:59 +00:00
}
2015-11-03 00:40:56 +00:00
// If the message was a success, set this sender's reply-recipient to the current recipient.
if ( messageResponse . isSuccess ( ) ) {
setReplyRecipient ( recipient ) ;
2015-10-27 17:34:59 +00:00
}
return messageResponse ;
}
@Override
public MessageResponse onReceiveMessage ( IMessageRecipient sender , String message ) {
2015-10-30 19:58:30 +00:00
if ( ! isReachable ( ) ) {
return MessageResponse . UNREACHABLE ;
}
2018-05-26 21:43:31 +02:00
2016-07-25 13:10:06 +01:00
User user = getUser ( this ) ;
2015-10-27 17:34:59 +00:00
boolean afk = false ;
2019-03-09 15:39:45 -08:00
boolean isLastMessageReplyRecipient = ess . getSettings ( ) . isLastMessageReplyRecipient ( ) ;
2015-10-27 17:34:59 +00:00
if ( user ! = null ) {
2019-03-09 15:39:45 -08:00
if ( user . isIgnoreMsg ( ) & & sender instanceof IUser & & ! ( ( IUser ) sender ) . isAuthorized ( " essentials.msgtoggle.bypass " ) ) { // Don't ignore console and senders with permission
return MessageResponse . MESSAGES_IGNORED ;
2015-10-27 17:34:59 +00:00
}
afk = user . isAfk ( ) ;
2019-03-09 15:39:45 -08:00
isLastMessageReplyRecipient = user . isLastMessageReplyRecipient ( ) ;
2015-10-27 17:34:59 +00:00
// Check whether this recipient ignores the sender, only if the sender is not the console.
if ( sender instanceof IUser & & user . isIgnoredPlayer ( ( IUser ) sender ) ) {
return MessageResponse . SENDER_IGNORED ;
}
}
// Display the formatted message to this recipient.
sendMessage ( tl ( " msgFormat " , sender . getDisplayName ( ) , tl ( " me " ) , message ) ) ;
2019-03-09 15:39:45 -08:00
if ( isLastMessageReplyRecipient ) {
2015-10-27 17:34:59 +00:00
// If this recipient doesn't have a reply recipient, initiate by setting the first
// message sender to this recipient's replyRecipient.
2015-11-24 22:09:59 +00:00
long timeout = ess . getSettings ( ) . getLastMessageReplyRecipientTimeout ( ) * 1000 ;
2018-05-26 21:43:31 +02:00
if ( getReplyRecipient ( ) = = null | | ! getReplyRecipient ( ) . isReachable ( )
2015-11-24 22:09:59 +00:00
| | System . currentTimeMillis ( ) - this . lastMessageMs > timeout ) {
2015-10-27 17:34:59 +00:00
setReplyRecipient ( sender ) ;
}
} else { // Old message functionality, always set the reply recipient to the last person who sent us a message.
setReplyRecipient ( sender ) ;
}
2015-11-24 22:09:59 +00:00
this . lastMessageMs = System . currentTimeMillis ( ) ;
2015-10-27 17:34:59 +00:00
return afk ? MessageResponse . SUCCESS_BUT_AFK : MessageResponse . SUCCESS ;
}
2015-10-30 19:58:30 +00:00
@Override public boolean isReachable ( ) {
return this . parent . isReachable ( ) ;
}
2015-10-27 17:34:59 +00:00
/ * *
* { @inheritDoc }
* < p / >
* < b > This { @link com . earth2me . essentials . messaging . SimpleMessageRecipient } implementation stores the a weak reference to the recipient . < / b >
* /
@Override
public IMessageRecipient getReplyRecipient ( ) {
return replyRecipient = = null ? null : replyRecipient . get ( ) ;
}
/ * *
* { @inheritDoc }
* < p / >
* < b > This { @link com . earth2me . essentials . messaging . SimpleMessageRecipient } implementation stores the a weak reference to the recipient . < / b >
* /
@Override
public void setReplyRecipient ( final IMessageRecipient replyRecipient ) {
this . replyRecipient = new WeakReference < > ( replyRecipient ) ;
}
}