2015-10-27 18:34:59 +01:00
package com.earth2me.essentials.messaging ;
import com.earth2me.essentials.IEssentials ;
import com.earth2me.essentials.IUser ;
import com.earth2me.essentials.User ;
2024-02-03 21:38:14 +01:00
import com.earth2me.essentials.utils.AdventureUtil ;
2020-05-13 09:03:28 +02:00
import net.ess3.api.events.PrivateMessagePreSendEvent ;
2020-07-06 20:34:13 +02:00
import net.ess3.api.events.PrivateMessageSentEvent ;
2020-11-09 13:25:16 +01:00
import org.bukkit.entity.Player ;
2015-10-27 18:34:59 +01:00
import java.lang.ref.WeakReference ;
2021-07-01 17:23:32 +02:00
import java.util.UUID ;
2015-10-27 18:34:59 +01:00
2024-02-03 21:38:14 +01:00
import static com.earth2me.essentials.I18n.tlLiteral ;
2019-03-10 00:39:45 +01:00
2015-10-27 18:34:59 +01: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 18:34:59 +01: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 20:58:30 +01:00
* < li > { @link IMessageRecipient # isReachable ( ) } < / li >
2015-10-27 18:34:59 +01:00
* < / ul >
2020-10-03 19:46:05 +02:00
* < p >
2015-10-27 18:34:59 +01: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 23:09:59 +01:00
private long lastMessageMs ;
2015-10-27 18:34:59 +01:00
private WeakReference < IMessageRecipient > replyRecipient ;
2016-07-25 14:10:06 +02:00
2020-10-03 19:46:05 +02:00
public SimpleMessageRecipient ( final IEssentials ess , final IMessageRecipient parent ) {
this . ess = ess ;
this . parent = parent ;
}
protected static User getUser ( final IMessageRecipient recipient ) {
2016-07-25 14:10:06 +02:00
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 18:34:59 +01:00
@Override
2020-10-03 19:46:05 +02:00
public void sendMessage ( final String message ) {
2015-10-27 18:34:59 +01:00
this . parent . sendMessage ( message ) ;
}
2024-02-03 21:38:14 +01:00
@Override
public void sendTl ( String tlKey , Object . . . args ) {
this . parent . sendTl ( tlKey , args ) ;
}
@Override
public String tlSender ( String tlKey , Object . . . args ) {
return this . parent . tlSender ( tlKey , args ) ;
}
2015-10-27 18:34:59 +01:00
@Override
public String getName ( ) {
return this . parent . getName ( ) ;
}
2021-07-01 17:23:32 +02:00
@Override
public UUID getUUID ( ) {
return this . parent . getUUID ( ) ;
}
2020-10-03 19:46:05 +02:00
@Override
public String getDisplayName ( ) {
2015-10-27 18:34:59 +01:00
return this . parent . getDisplayName ( ) ;
}
2020-10-03 19:46:05 +02:00
@Override
public MessageResponse sendMessage ( final IMessageRecipient recipient , String message ) {
2020-07-06 20:34:13 +02:00
final PrivateMessagePreSendEvent preSendEvent = new PrivateMessagePreSendEvent ( parent , recipient , message ) ;
ess . getServer ( ) . getPluginManager ( ) . callEvent ( preSendEvent ) ;
if ( preSendEvent . isCancelled ( ) ) {
2020-05-13 09:03:28 +02:00
return MessageResponse . EVENT_CANCELLED ;
}
2020-07-06 20:34:13 +02:00
message = preSendEvent . getMessage ( ) ;
2020-10-03 19:46:05 +02:00
final MessageResponse messageResponse = recipient . onReceiveMessage ( this . parent , message ) ;
2015-10-27 18:34:59 +01:00
switch ( messageResponse ) {
2015-10-30 20:58:30 +01:00
case UNREACHABLE :
2024-02-04 02:47:20 +01:00
sendTl ( " recentlyForeverAlone " , recipient . getDisplayName ( ) ) ;
2015-11-04 00:39:18 +01:00
break ;
2015-10-27 18:34:59 +01:00
case MESSAGES_IGNORED :
2024-02-04 02:47:20 +01:00
sendTl ( " msgIgnore " , recipient . getDisplayName ( ) ) ;
2015-10-27 18:34:59 +01:00
break ;
case SENDER_IGNORED :
break ;
// When this recipient is AFK, notify the sender. Then, proceed to send the message.
2015-10-30 20:22:39 +01:00
case SUCCESS_BUT_AFK :
2016-06-18 18:44:17 +02:00
// Currently, only IUser can be afk, so we unsafely cast to get the afk message.
if ( ( ( IUser ) recipient ) . getAfkMessage ( ) ! = null ) {
2024-02-04 02:47:20 +01:00
sendTl ( " userAFKWithMessage " , recipient . getDisplayName ( ) , ( ( IUser ) recipient ) . getAfkMessage ( ) ) ;
2016-06-18 18:44:17 +02:00
} else {
2024-02-04 02:47:20 +01:00
sendTl ( " userAFK " , recipient . getDisplayName ( ) ) ;
2016-06-18 18:44:17 +02:00
}
2020-10-03 19:46:05 +02:00
// fall through
2015-10-27 18:34:59 +01:00
default :
2024-02-04 02:47:20 +01:00
sendTl ( " msgFormat " , AdventureUtil . parsed ( tlSender ( " meSender " ) ) , recipient . getDisplayName ( ) , message ) ;
2016-07-22 16:52:53 +02:00
// Better Social Spy
2021-02-05 20:02:26 +01:00
if ( ess . getSettings ( ) . isSocialSpyMessages ( ) ) {
final User senderUser = getUser ( this ) ;
final User recipientUser = getUser ( recipient ) ;
if ( senderUser ! = null // not null if player.
// Dont spy on chats involving socialspy exempt players
& & ! senderUser . isAuthorized ( " essentials.chat.spy.exempt " )
& & recipientUser ! = null & & ! recipientUser . isAuthorized ( " essentials.chat.spy.exempt " ) ) {
2024-02-26 21:25:30 +01:00
final String senderName = ess . getSettings ( ) . isSocialSpyDisplayNames ( ) ? getDisplayName ( ) : getName ( ) ;
final String recipientName = ess . getSettings ( ) . isSocialSpyDisplayNames ( ) ? recipient . getDisplayName ( ) : recipient . getName ( ) ;
2021-02-05 20:02:26 +01:00
for ( final User onlineUser : ess . getOnlineUsers ( ) ) {
if ( onlineUser . isSocialSpyEnabled ( )
// Don't send socialspy messages to message sender/receiver to prevent spam
& & ! onlineUser . equals ( senderUser )
& & ! onlineUser . equals ( recipient ) ) {
if ( senderUser . isMuted ( ) & & ess . getSettings ( ) . getSocialSpyListenMutedPlayers ( ) ) {
2024-02-26 21:25:30 +01:00
onlineUser . sendComponent ( AdventureUtil . miniMessage ( ) . deserialize ( tlSender ( " socialSpyMutedPrefix " ) + tlLiteral ( " socialSpyMsgFormat " , senderName , recipientName , message ) ) ) ;
2021-02-05 20:02:26 +01:00
} else {
2024-02-26 21:25:30 +01:00
onlineUser . sendComponent ( AdventureUtil . miniMessage ( ) . deserialize ( tlLiteral ( " socialSpyPrefix " ) + tlLiteral ( " socialSpyMsgFormat " , senderName , recipientName , message ) ) ) ;
2021-02-05 20:02:26 +01:00
}
2017-08-04 01:04:42 +02:00
}
2016-07-22 16:52:53 +02:00
}
}
}
2020-10-03 19:46:05 +02:00
break ;
2015-10-27 18:34:59 +01:00
}
2015-11-03 01:40:56 +01: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 18:34:59 +01:00
}
2020-07-06 20:34:13 +02:00
final PrivateMessageSentEvent sentEvent = new PrivateMessageSentEvent ( parent , recipient , message , messageResponse ) ;
ess . getServer ( ) . getPluginManager ( ) . callEvent ( sentEvent ) ;
2015-10-27 18:34:59 +01:00
return messageResponse ;
}
@Override
2020-10-03 19:46:05 +02:00
public MessageResponse onReceiveMessage ( final IMessageRecipient sender , final String message ) {
2015-10-30 20:58:30 +01:00
if ( ! isReachable ( ) ) {
return MessageResponse . UNREACHABLE ;
}
2018-05-26 21:43:31 +02:00
2020-10-03 19:46:05 +02:00
final User user = getUser ( this ) ;
2015-10-27 18:34:59 +01:00
boolean afk = false ;
2019-03-10 00:39:45 +01:00
boolean isLastMessageReplyRecipient = ess . getSettings ( ) . isLastMessageReplyRecipient ( ) ;
2015-10-27 18:34:59 +01:00
if ( user ! = null ) {
2019-03-10 00:39:45 +01: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 18:34:59 +01:00
}
afk = user . isAfk ( ) ;
2019-03-10 00:39:45 +01:00
isLastMessageReplyRecipient = user . isLastMessageReplyRecipient ( ) ;
2015-10-27 18:34:59 +01: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.
2024-02-04 02:47:20 +01:00
sendTl ( " msgFormat " , sender . getDisplayName ( ) , AdventureUtil . parsed ( tlSender ( " meRecipient " ) ) , message ) ;
2015-10-27 18:34:59 +01:00
2019-03-10 00:39:45 +01:00
if ( isLastMessageReplyRecipient ) {
2015-10-27 18:34:59 +01:00
// If this recipient doesn't have a reply recipient, initiate by setting the first
// message sender to this recipient's replyRecipient.
2020-10-03 19:46:05 +02:00
final long timeout = ess . getSettings ( ) . getLastMessageReplyRecipientTimeout ( ) * 1000 ;
2018-05-26 21:43:31 +02:00
if ( getReplyRecipient ( ) = = null | | ! getReplyRecipient ( ) . isReachable ( )
2015-11-24 23:09:59 +01:00
| | System . currentTimeMillis ( ) - this . lastMessageMs > timeout ) {
2015-10-27 18:34:59 +01: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 23:09:59 +01:00
this . lastMessageMs = System . currentTimeMillis ( ) ;
2015-10-27 18:34:59 +01:00
return afk ? MessageResponse . SUCCESS_BUT_AFK : MessageResponse . SUCCESS ;
}
2020-10-03 19:46:05 +02:00
@Override
public boolean isReachable ( ) {
2015-10-30 20:58:30 +01:00
return this . parent . isReachable ( ) ;
}
2015-10-27 18:34:59 +01:00
/ * *
* { @inheritDoc }
2020-11-25 21:24:24 +01:00
* < p >
2015-10-27 18:34:59 +01:00
* < 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 }
2020-11-25 21:24:24 +01:00
* < p >
2015-10-27 18:34:59 +01:00
* < 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 ) ;
}
2020-11-09 13:25:16 +01:00
@Override
public boolean isHiddenFrom ( Player player ) {
return parent . isHiddenFrom ( player ) ;
}
2015-10-27 18:34:59 +01:00
}