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 ;
@@ -35,19 +37,27 @@ public Message(@NotNull OfflinePlayer sender, @NotNull OfflinePlayer recipient,
3537 }
3638
3739 public void send () throws InvalidPlayerError {
38- send (false );
40+ send (Context . REGULAR );
3941 }
4042
41- public void send (final boolean channel ) throws InvalidPlayerError {
43+ public void send (final @ NotNull Context context ) throws InvalidPlayerError {
44+ final @ NotNull String senderUsername = playerOrServerUsername (this .sender );
45+ final @ NotNull String recipientUsername = playerOrServerUsername (this .recipient );
46+
4247 final @ NotNull Optional <@ NotNull Player > senderPlayer = Optional .ofNullable (this .sender .getPlayer ());
4348 final @ NotNull Optional <@ NotNull Player > recipientPlayer = Optional .ofNullable (this .recipient .getPlayer ());
4449
45- if (senderPlayer .isPresent () && (recipientPlayer .isEmpty () || (CloudnodeMSG .isVanished (recipientPlayer .get ()) && !senderPlayer
46- .get ().hasPermission (Permission .SEND_VANISHED )))) {
47- if (!channel ) new PlayerNotFoundError (senderPlayer .get ().getName ()).send (senderPlayer .get ());
50+ if (!recipient .getUniqueId ().equals (console .getUniqueId ()) && recipientPlayer .isEmpty () || (recipientPlayer .isPresent () && senderPlayer .isPresent () && CloudnodeMSG .isVanished (recipientPlayer .get ()) && !senderPlayer .get ().hasPermission (Permission .SEND_VANISHED ))) {
51+ if (context == Context .CHANNEL ) {
52+ final @ NotNull Player player = Objects .requireNonNull (sender .getPlayer ());
53+ Message .exitChannel (player );
54+ new ChannelOfflineError (player .getName (), Optional .ofNullable (recipient .getName ())
55+ .orElse ("Unknown Player" )).send (player );
56+ }
4857 else {
49- Message .exitChannel (senderPlayer .get ());
50- new ChannelOfflineError (senderPlayer .get (), recipient ).send (senderPlayer .get ());
58+ final @ NotNull Audience senderAudience = senderPlayer .isPresent () ? senderPlayer .get () : CloudnodeMSG .getInstance ().getServer ().getConsoleSender ();
59+ if (context == Context .REPLY ) new ReplyOfflineError (recipientUsername ).send (senderAudience );
60+ else new PlayerNotFoundError (recipientUsername ).send (senderAudience );
5161 }
5262 return ;
5363 }
@@ -58,17 +68,16 @@ public void send(final boolean channel) throws InvalidPlayerError {
5868 return ;
5969 }
6070
61- sendMessage (sender , CloudnodeMSG .getInstance ().config ().outgoing (sender , recipient , message ));
62- if (senderPlayer .isPresent () && !Message .hasChannel (senderPlayer .get (), recipient ))
63- setReplyTo (sender , recipient );
64-
6571 sendSpyMessage (sender , recipient , message );
66-
72+ sendMessage ( sender , CloudnodeMSG . getInstance (). config (). outgoing ( senderUsername , recipientUsername , message ));
6773 if ((recipientPlayer .isPresent () && Message .isIgnored (recipientPlayer .get (), sender )) && (senderPlayer .isPresent () && !senderPlayer
6874 .get ().hasPermission (Permission .IGNORE_BYPASS ))) return ;
6975 sendMessage (recipient , CloudnodeMSG .getInstance ().config ()
70- .incoming (sender , recipient , message ));
71- if (recipientPlayer .isPresent () && !Message .hasChannel (recipientPlayer .get (), sender ))
76+ .incoming (senderUsername , recipientUsername , message ));
77+
78+ if (sender .getUniqueId ().equals (console .getUniqueId ()) || (senderPlayer .isPresent () && !Message .hasChannel (senderPlayer .get (), recipient )))
79+ setReplyTo (sender , recipient );
80+ if (recipient .getUniqueId ().equals (console .getUniqueId ()) || (recipientPlayer .isPresent () && !Message .hasChannel (recipientPlayer .get (), sender )))
7281 setReplyTo (recipient , sender );
7382 }
7483
@@ -289,4 +298,24 @@ public static void exitTeamChannel(final @NotNull Player player) {
289298 public static boolean hasTeamChannel (final @ NotNull Player player ) {
290299 return player .getPersistentDataContainer ().getOrDefault (CHANNEL_TEAM , PersistentDataType .BOOLEAN , false );
291300 }
301+
302+ /**
303+ * The context in which this message is sent
304+ */
305+ public static enum Context {
306+ /**
307+ * Message sent via command (i.e. no special context)
308+ */
309+ REGULAR ,
310+
311+ /**
312+ * Message sent via messaging channel
313+ */
314+ CHANNEL ,
315+
316+ /**
317+ * Message sent as a reply
318+ */
319+ REPLY ;
320+ }
292321}
0 commit comments