11package pro .cloudnode .smp .cloudnodemsg ;
22
3+ import net .kyori .adventure .audience .Audience ;
34import net .kyori .adventure .text .Component ;
45import org .bukkit .NamespacedKey ;
56import org .bukkit .OfflinePlayer ;
1213import pro .cloudnode .smp .cloudnodemsg .error .InvalidPlayerError ;
1314import pro .cloudnode .smp .cloudnodemsg .error .PlayerHasIncomingDisabledError ;
1415import pro .cloudnode .smp .cloudnodemsg .error .PlayerNotFoundError ;
16+ import pro .cloudnode .smp .cloudnodemsg .error .ReplyOfflineError ;
1517
1618import java .util .Arrays ;
1719import java .util .HashSet ;
@@ -45,23 +47,27 @@ public Message(@NotNull OfflinePlayer sender, @NotNull OfflinePlayer recipient,
4547 }
4648
4749 public void send () throws InvalidPlayerError {
48- send (false );
50+ send (Context . REGULAR );
4951 }
5052
51- public void send (final boolean channel ) throws InvalidPlayerError {
53+ public void send (final @ NotNull Context context ) throws InvalidPlayerError {
5254 final @ NotNull String senderUsername = playerOrServerUsername (this .sender );
5355 final @ NotNull String recipientUsername = playerOrServerUsername (this .recipient );
5456
5557 final @ NotNull Optional <@ NotNull Player > senderPlayer = Optional .ofNullable (this .sender .getPlayer ());
5658 final @ NotNull Optional <@ NotNull Player > recipientPlayer = Optional .ofNullable (this .recipient .getPlayer ());
5759
58- if (senderPlayer .isPresent () && (recipientPlayer .isEmpty () || (CloudnodeMSG .isVanished (recipientPlayer .get ()) && !senderPlayer
59- .get ().hasPermission (Permission .SEND_VANISHED )))) {
60- if (!channel ) new PlayerNotFoundError (senderPlayer .get ().getName ()).send (senderPlayer .get ());
60+ if (!recipient .getUniqueId ().equals (console .getUniqueId ()) && recipientPlayer .isEmpty () || (recipientPlayer .isPresent () && senderPlayer .isPresent () && CloudnodeMSG .isVanished (recipientPlayer .get ()) && !senderPlayer .get ().hasPermission (Permission .SEND_VANISHED ))) {
61+ if (context == Context .CHANNEL ) {
62+ final @ NotNull Player player = Objects .requireNonNull (sender .getPlayer ());
63+ Message .exitChannel (player );
64+ new ChannelOfflineError (player .getName (), Optional .ofNullable (recipient .getName ())
65+ .orElse ("Unknown Player" )).send (player );
66+ }
6167 else {
62- Message . exitChannel ( senderPlayer .get ());
63- new ChannelOfflineError ( senderPlayer . get (). getName (), Optional . ofNullable ( recipient . getName ())
64- . orElse ( "Unknown Player" )) .send (senderPlayer . get () );
68+ final @ NotNull Audience senderAudience = senderPlayer . isPresent () ? senderPlayer .get () : CloudnodeMSG . getInstance (). getServer (). getConsoleSender ( );
69+ if ( context == Context . REPLY ) new ReplyOfflineError ( recipientUsername ). send ( senderAudience );
70+ else new PlayerNotFoundError ( recipientUsername ) .send (senderAudience );
6571 }
6672 return ;
6773 }
@@ -72,17 +78,16 @@ public void send(final boolean channel) throws InvalidPlayerError {
7278 return ;
7379 }
7480
75- sendMessage (sender , CloudnodeMSG .getInstance ().config ().outgoing (senderUsername , recipientUsername , message ));
76- if (senderPlayer .isPresent () && !Message .hasChannel (senderPlayer .get (), recipient ))
77- setReplyTo (sender , recipient );
78-
7981 sendSpyMessage (sender , recipient , message );
80-
82+ sendMessage ( sender , CloudnodeMSG . getInstance (). config (). outgoing ( senderUsername , recipientUsername , message ));
8183 if ((recipientPlayer .isPresent () && Message .isIgnored (recipientPlayer .get (), sender )) && (senderPlayer .isPresent () && !senderPlayer
8284 .get ().hasPermission (Permission .IGNORE_BYPASS ))) return ;
8385 sendMessage (recipient , CloudnodeMSG .getInstance ().config ()
8486 .incoming (senderUsername , recipientUsername , message ));
85- if (recipientPlayer .isPresent () && !Message .hasChannel (recipientPlayer .get (), sender ))
87+
88+ if (sender .getUniqueId ().equals (console .getUniqueId ()) || (senderPlayer .isPresent () && !Message .hasChannel (senderPlayer .get (), recipient )))
89+ setReplyTo (sender , recipient );
90+ if (recipient .getUniqueId ().equals (console .getUniqueId ()) || (recipientPlayer .isPresent () && !Message .hasChannel (recipientPlayer .get (), sender )))
8691 setReplyTo (recipient , sender );
8792 }
8893
@@ -306,4 +311,24 @@ public static void exitTeamChannel(final @NotNull Player player) {
306311 public static boolean hasTeamChannel (final @ NotNull Player player ) {
307312 return player .getPersistentDataContainer ().getOrDefault (CHANNEL_TEAM , PersistentDataType .BOOLEAN , false );
308313 }
314+
315+ /**
316+ * The context in which this message is sent
317+ */
318+ public static enum Context {
319+ /**
320+ * Message sent via command (i.e. no special context)
321+ */
322+ REGULAR ,
323+
324+ /**
325+ * Message sent via messaging channel
326+ */
327+ CHANNEL ,
328+
329+ /**
330+ * Message sent as a reply
331+ */
332+ REPLY ;
333+ }
309334}
0 commit comments