Skip to content

Commit 3a3823e

Browse files
authored
Prevent messaging vanished players (#12)
2 parents 09dbc09 + a03d117 commit 3a3823e

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/main/java/pro/cloudnode/smp/cloudnodemsg/CloudnodeMSG.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package pro.cloudnode.smp.cloudnodemsg;
22

3+
import org.bukkit.entity.Player;
4+
import org.bukkit.metadata.MetadataValue;
35
import org.bukkit.plugin.java.JavaPlugin;
46
import org.jetbrains.annotations.NotNull;
57
import pro.cloudnode.smp.cloudnodemsg.command.MainCommand;
@@ -33,6 +35,12 @@ public void onDisable() {
3335
// Plugin shutdown logic
3436
}
3537

38+
public static boolean isVanished(final @NotNull Player player) {
39+
for (final @NotNull MetadataValue meta : player.getMetadata("vanished"))
40+
if (meta.asBoolean()) return true;
41+
return false;
42+
}
43+
3644
private final @NotNull PluginConfig config = new PluginConfig(getConfig());
3745

3846
public @NotNull PluginConfig config() {

src/main/java/pro/cloudnode/smp/cloudnodemsg/Permission.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ public final class Permission {
1212
* Allows reloading the plugin
1313
*/
1414
public final static @NotNull String RELOAD = "cloudnodemsg.reload";
15+
16+
/**
17+
* Allows sending messages to vanished players
18+
*/
19+
public final static @NotNull String SEND_VANISHED = "cloudnodemsg.send.vanished";
1520
}

src/main/java/pro/cloudnode/smp/cloudnodemsg/command/MessageCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public boolean onCommand(final @NotNull CommandSender sender, final @NotNull org
2828

2929
final @NotNull Optional<@NotNull Player> recipient = Optional.ofNullable(CloudnodeMSG.getInstance().getServer()
3030
.getPlayer(args[0]));
31-
if (recipient.isEmpty()) return new PlayerNotFoundError(args[0]).send(sender);
31+
if (recipient.isEmpty() || (CloudnodeMSG.isVanished(recipient.get()) && !sender.hasPermission(Permission.SEND_VANISHED))) return new PlayerNotFoundError(args[0]).send(sender);
3232
if (sender instanceof final @NotNull Player player && recipient.get().getUniqueId().equals(player.getUniqueId()))
3333
return new MessageYourselfError().send(sender);
3434

src/main/java/pro/cloudnode/smp/cloudnodemsg/command/ReplyCommand.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.util.ArrayList;
1515
import java.util.List;
16+
import java.util.Objects;
1617
import java.util.Optional;
1718

1819
public final class ReplyCommand extends Command {
@@ -25,7 +26,14 @@ public boolean onCommand(final @NotNull CommandSender sender, final @NotNull org
2526

2627
final @NotNull Optional<@NotNull OfflinePlayer> recipient = Message.getReplyTo(Message.offlinePlayer(sender));
2728
if (recipient.isEmpty()) return new NobodyReplyError().send(sender);
28-
if (!recipient.get().getUniqueId().equals(Message.console.getUniqueId()) && !recipient.get().isOnline()) return new ReplyOfflineError(Optional.ofNullable(recipient.get().getName()).orElse("Unknown Player")).send(sender);
29+
if (
30+
!recipient.get().getUniqueId().equals(Message.console.getUniqueId())
31+
&& (
32+
!recipient.get().isOnline()
33+
|| (CloudnodeMSG.isVanished(Objects.requireNonNull(recipient.get().getPlayer())) && !sender.hasPermission(Permission.SEND_VANISHED))
34+
)
35+
)
36+
return new ReplyOfflineError(Optional.ofNullable(recipient.get().getName()).orElse("Unknown Player")).send(sender);
2937

3038
try {
3139
new Message(Message.offlinePlayer(sender), recipient.get(), String.join(" ", args)).send();

0 commit comments

Comments
 (0)