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,23 @@ 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 {
4244 final @ NotNull Optional <@ NotNull Player > senderPlayer = Optional .ofNullable (this .sender .getPlayer ());
4345 final @ NotNull Optional <@ NotNull Player > recipientPlayer = Optional .ofNullable (this .recipient .getPlayer ());
4446
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 ());
47+ if (!recipient .getUniqueId ().equals (console .getUniqueId ()) && recipientPlayer .isEmpty () || (recipientPlayer .isPresent () && senderPlayer .isPresent () && CloudnodeMSG .isVanished (recipientPlayer .get ()) && !senderPlayer .get ().hasPermission (Permission .SEND_VANISHED ))) {
48+ if (context == Context .CHANNEL ) {
49+ final @ NotNull Player player = Objects .requireNonNull (sender .getPlayer ());
50+ Message .exitChannel (player );
51+ new ChannelOfflineError (sender , recipient ).send (player );
52+ }
4853 else {
49- Message .exitChannel (senderPlayer .get ());
50- new ChannelOfflineError (senderPlayer .get (), recipient ).send (senderPlayer .get ());
54+ final @ NotNull Audience senderAudience = senderPlayer .isPresent () ? senderPlayer .get () : CloudnodeMSG .getInstance ().getServer ().getConsoleSender ();
55+ if (context == Context .REPLY ) new ReplyOfflineError (recipient ).send (senderAudience );
56+ else new PlayerNotFoundError (name (this .recipient )).send (senderAudience );
5157 }
5258 return ;
5359 }
@@ -58,17 +64,16 @@ public void send(final boolean channel) throws InvalidPlayerError {
5864 return ;
5965 }
6066
61- sendMessage (sender , CloudnodeMSG .getInstance ().config ().outgoing (sender , recipient , message ));
62- if (senderPlayer .isPresent () && !Message .hasChannel (senderPlayer .get (), recipient ))
63- setReplyTo (sender , recipient );
64-
6567 sendSpyMessage (sender , recipient , message );
66-
68+ sendMessage ( sender , CloudnodeMSG . getInstance (). config (). outgoing ( sender , recipient , message ));
6769 if ((recipientPlayer .isPresent () && Message .isIgnored (recipientPlayer .get (), sender )) && (senderPlayer .isPresent () && !senderPlayer
6870 .get ().hasPermission (Permission .IGNORE_BYPASS ))) return ;
6971 sendMessage (recipient , CloudnodeMSG .getInstance ().config ()
7072 .incoming (sender , recipient , message ));
71- if (recipientPlayer .isPresent () && !Message .hasChannel (recipientPlayer .get (), sender ))
73+
74+ if (sender .getUniqueId ().equals (console .getUniqueId ()) || (senderPlayer .isPresent () && !Message .hasChannel (senderPlayer .get (), recipient )))
75+ setReplyTo (sender , recipient );
76+ if (recipient .getUniqueId ().equals (console .getUniqueId ()) || (recipientPlayer .isPresent () && !Message .hasChannel (recipientPlayer .get (), sender )))
7277 setReplyTo (recipient , sender );
7378 }
7479
@@ -289,4 +294,24 @@ public static void exitTeamChannel(final @NotNull Player player) {
289294 public static boolean hasTeamChannel (final @ NotNull Player player ) {
290295 return player .getPersistentDataContainer ().getOrDefault (CHANNEL_TEAM , PersistentDataType .BOOLEAN , false );
291296 }
297+
298+ /**
299+ * The context in which this message is sent
300+ */
301+ public static enum Context {
302+ /**
303+ * Message sent via command (i.e. no special context)
304+ */
305+ REGULAR ,
306+
307+ /**
308+ * Message sent via messaging channel
309+ */
310+ CHANNEL ,
311+
312+ /**
313+ * Message sent as a reply
314+ */
315+ REPLY ;
316+ }
292317}
0 commit comments