88import org .bukkit .persistence .PersistentDataType ;
99import org .jetbrains .annotations .NotNull ;
1010import org .jetbrains .annotations .Nullable ;
11+ import pro .cloudnode .smp .cloudnodemsg .error .ChannelOfflineError ;
1112import pro .cloudnode .smp .cloudnodemsg .error .InvalidPlayerError ;
13+ import pro .cloudnode .smp .cloudnodemsg .error .PlayerHasIncomingDisabledError ;
14+ import pro .cloudnode .smp .cloudnodemsg .error .PlayerNotFoundError ;
1215
1316import java .util .Arrays ;
1417import java .util .HashSet ;
@@ -32,20 +35,38 @@ public Message(@NotNull OfflinePlayer sender, @NotNull OfflinePlayer recipient,
3235 }
3336
3437 public void send () throws InvalidPlayerError {
38+ send (false );
39+ }
40+
41+ public void send (final boolean channel ) throws InvalidPlayerError {
3542 final @ NotNull Optional <@ NotNull Player > senderPlayer = Optional .ofNullable (this .sender .getPlayer ());
3643 final @ NotNull Optional <@ NotNull Player > recipientPlayer = Optional .ofNullable (this .recipient .getPlayer ());
3744
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 ());
48+ else {
49+ Message .exitChannel (senderPlayer .get ());
50+ new ChannelOfflineError (senderPlayer .get ().getName (), Optional .ofNullable (recipient .getName ())
51+ .orElse ("Unknown Player" )).send (senderPlayer .get ());
52+ }
53+ return ;
54+ }
55+
56+ if (recipientPlayer .isPresent () && senderPlayer .isPresent () && !Message .isIncomingEnabled (recipientPlayer .get ()) && !senderPlayer
57+ .get ().hasPermission (Permission .TOGGLE_BYPASS )) {
58+ new PlayerHasIncomingDisabledError (recipientPlayer .get ().getName ()).send (senderPlayer .get ());
59+ return ;
60+ }
61+
3862 sendMessage (sender , CloudnodeMSG .getInstance ().config ().outgoing (sender , recipient , message ));
3963 if (senderPlayer .isPresent () && !Message .hasChannel (senderPlayer .get (), recipient ))
4064 setReplyTo (sender , recipient );
4165
4266 sendSpyMessage (sender , recipient , message );
4367
44- if (
45- (recipientPlayer .isPresent () && Message .isIgnored (recipientPlayer .get (), sender ))
46- &&
47- (senderPlayer .isPresent () && !senderPlayer .get ().hasPermission (Permission .IGNORE_BYPASS ))
48- ) return ;
68+ if ((recipientPlayer .isPresent () && Message .isIgnored (recipientPlayer .get (), sender )) && (senderPlayer .isPresent () && !senderPlayer
69+ .get ().hasPermission (Permission .IGNORE_BYPASS ))) return ;
4970 sendMessage (recipient , CloudnodeMSG .getInstance ().config ()
5071 .incoming (sender , recipient , message ));
5172 if (recipientPlayer .isPresent () && !Message .hasChannel (recipientPlayer .get (), sender ))
@@ -93,18 +114,17 @@ public static void sendSpyMessage(final @NotNull OfflinePlayer sender, final @No
93114
94115 public static void setReplyTo (final @ NotNull OfflinePlayer sender , final @ NotNull OfflinePlayer recipient ) {
95116 if (sender .getUniqueId ().equals (console .getUniqueId ())) consoleReply = recipient .getUniqueId ();
96- else if (sender .isOnline ())
97- Objects .requireNonNull (sender .getPlayer ()).getPersistentDataContainer ()
98- .set (REPLY_TO , PersistentDataType .STRING , recipient .getUniqueId ().toString ());
117+ else if (sender .isOnline ()) Objects .requireNonNull (sender .getPlayer ()).getPersistentDataContainer ()
118+ .set (REPLY_TO , PersistentDataType .STRING , recipient .getUniqueId ().toString ());
99119 }
100120
101121 public static @ NotNull Optional <@ NotNull OfflinePlayer > getReplyTo (final @ NotNull OfflinePlayer player ) {
102122 if (player .getUniqueId ().equals (console .getUniqueId ())) return Optional .ofNullable (consoleReply )
103123 .map (uuid -> CloudnodeMSG .getInstance ().getServer ().getOfflinePlayer (uuid ));
104- if (player .isOnline ())
105- return Optional .ofNullable (Objects .requireNonNull (player .getPlayer ()).getPersistentDataContainer ()
106- .get (REPLY_TO , PersistentDataType .STRING ))
107- .map (uuid -> CloudnodeMSG .getInstance ().getServer ().getOfflinePlayer (UUID .fromString (uuid )));
124+ if (player .isOnline ()) return Optional
125+ .ofNullable (Objects .requireNonNull (player .getPlayer ()).getPersistentDataContainer ()
126+ .get (REPLY_TO , PersistentDataType .STRING ))
127+ .map (uuid -> CloudnodeMSG .getInstance ().getServer ().getOfflinePlayer (UUID .fromString (uuid )));
108128 return Optional .empty ();
109129 }
110130
@@ -118,15 +138,16 @@ else if (sender.isOnline())
118138 * @param player The player
119139 */
120140 public static @ NotNull HashSet <@ NotNull UUID > getIgnored (final @ NotNull Player player ) {
121- final @ NotNull Optional <@ NotNull String > str = Optional .ofNullable (player .getPersistentDataContainer ().get (IGNORED_PLAYERS , PersistentDataType .STRING ));
122- return str .map (s -> new HashSet <>(Arrays .stream (s .split (";" )).filter (e -> !e .isEmpty ()).map (UUID ::fromString ).toList ()))
123- .orElseGet (HashSet ::new );
141+ final @ NotNull Optional <@ NotNull String > str = Optional .ofNullable (player .getPersistentDataContainer ()
142+ .get (IGNORED_PLAYERS , PersistentDataType .STRING ));
143+ return str .map (s -> new HashSet <>(Arrays .stream (s .split (";" )).filter (e -> !e .isEmpty ()).map (UUID ::fromString )
144+ .toList ())).orElseGet (HashSet ::new );
124145 }
125146
126147 /**
127148 * Check if a player is ignored
128149 *
129- * @param player The player
150+ * @param player The player
130151 * @param ignored The ignored player
131152 */
132153 public static boolean isIgnored (final @ NotNull Player player , final @ NotNull OfflinePlayer ignored ) {
@@ -142,19 +163,23 @@ public static boolean isIgnored(final @NotNull Player player, final @NotNull Off
142163 public static void ignore (final @ NotNull Player player , final @ NotNull OfflinePlayer ignore ) {
143164 final @ NotNull HashSet <@ NotNull UUID > ignoredPlayers = getIgnored (player );
144165 ignoredPlayers .add (ignore .getUniqueId ());
145- player .getPersistentDataContainer ().set (IGNORED_PLAYERS , PersistentDataType .STRING , String .join (";" , ignoredPlayers .stream ().map (UUID ::toString ).toList ()));
166+ player .getPersistentDataContainer ()
167+ .set (IGNORED_PLAYERS , PersistentDataType .STRING , String .join (";" , ignoredPlayers .stream ()
168+ .map (UUID ::toString ).toList ()));
146169 }
147170
148171 /**
149172 * Unignore a player
150173 *
151- * @param player The player
174+ * @param player The player
152175 * @param ignored The player to unignore
153176 */
154177 public static void unignore (final @ NotNull Player player , final @ NotNull OfflinePlayer ignored ) {
155178 final @ NotNull HashSet <@ NotNull UUID > ignoredPlayers = getIgnored (player );
156179 ignoredPlayers .remove (ignored .getUniqueId ());
157- player .getPersistentDataContainer ().set (IGNORED_PLAYERS , PersistentDataType .STRING , String .join (";" , ignoredPlayers .stream ().map (UUID ::toString ).toList ()));
180+ player .getPersistentDataContainer ()
181+ .set (IGNORED_PLAYERS , PersistentDataType .STRING , String .join (";" , ignoredPlayers .stream ()
182+ .map (UUID ::toString ).toList ()));
158183 }
159184
160185 public static final @ NotNull NamespacedKey INCOMING_ENABLED = new NamespacedKey (CloudnodeMSG .getInstance (), "incoming_enabled" );
@@ -193,7 +218,8 @@ public static boolean isIncomingEnabled(final @NotNull Player player) {
193218 * @param recipient The other end of the channel
194219 */
195220 public static void createChannel (final @ NotNull Player player , final @ NotNull OfflinePlayer recipient ) {
196- player .getPersistentDataContainer ().set (CHANNEL_RECIPIENT , PersistentDataType .STRING , recipient .getUniqueId ().toString ());
221+ player .getPersistentDataContainer ()
222+ .set (CHANNEL_RECIPIENT , PersistentDataType .STRING , recipient .getUniqueId ().toString ());
197223 player .getPersistentDataContainer ().remove (CHANNEL_TEAM );
198224 }
199225
@@ -212,21 +238,31 @@ public static void exitChannel(final @NotNull Player player) {
212238 * @param player The player
213239 */
214240 public static @ NotNull Optional <@ NotNull OfflinePlayer > getChannel (final @ NotNull Player player ) {
215- return Optional .ofNullable (player .getPersistentDataContainer ().get (CHANNEL_RECIPIENT , PersistentDataType .STRING ))
241+ return Optional
242+ .ofNullable (player .getPersistentDataContainer ().get (CHANNEL_RECIPIENT , PersistentDataType .STRING ))
216243 .map (uuid -> CloudnodeMSG .getInstance ().getServer ().getOfflinePlayer (UUID .fromString (uuid )));
217244 }
218245
219246 /**
220247 * Check whether player has DM channel with recipient
221248 *
222- * @param player The player
249+ * @param player The player
223250 * @param recipient The recipient
224251 */
225252 public static boolean hasChannel (final @ NotNull Player player , final @ NotNull OfflinePlayer recipient ) {
226253 final @ NotNull Optional <@ NotNull OfflinePlayer > channel = getChannel (player );
227254 return channel .isPresent () && channel .get ().getUniqueId ().equals (recipient .getUniqueId ());
228255 }
229256
257+ /**
258+ * Check whether player has DM channel
259+ *
260+ * @param player The player
261+ */
262+ public static boolean hasChannel (final @ NotNull Player player ) {
263+ return getChannel (player ).isPresent ();
264+ }
265+
230266 /**
231267 * Team message channel
232268 *
0 commit comments