From 6bef5ca8e7c97fb86c552c72fae9b8f505e91fe0 Mon Sep 17 00:00:00 2001 From: BlitzOffline <52609756+BlitzOffline@users.noreply.github.com> Date: Sun, 28 Aug 2022 20:19:29 +0300 Subject: [PATCH 1/4] Added a User#hasPermission method --- api/src/main/java/at/helpch/chatchat/api/User.java | 2 ++ .../at/helpch/chatchat/channel/AbstractChannel.java | 2 +- .../java/at/helpch/chatchat/channel/ChatChannel.java | 5 ++--- .../java/at/helpch/chatchat/command/DumpCommand.java | 3 +-- .../at/helpch/chatchat/command/WhisperCommand.java | 10 +++++----- .../java/at/helpch/chatchat/user/ChatUserImpl.java | 5 +++++ .../main/java/at/helpch/chatchat/user/ConsoleUser.java | 6 ++++++ .../java/at/helpch/chatchat/util/ChannelUtils.java | 7 ++----- .../java/at/helpch/chatchat/util/MentionUtils.java | 8 ++++---- .../java/at/helpch/chatchat/util/MessageProcessor.java | 10 +++++----- 10 files changed, 33 insertions(+), 25 deletions(-) diff --git a/api/src/main/java/at/helpch/chatchat/api/User.java b/api/src/main/java/at/helpch/chatchat/api/User.java index 8acd359e..5800d0df 100644 --- a/api/src/main/java/at/helpch/chatchat/api/User.java +++ b/api/src/main/java/at/helpch/chatchat/api/User.java @@ -19,6 +19,8 @@ public interface User extends ForwardingAudience.Single, Identified { @NotNull UUID uuid(); + boolean hasPermission(@NotNull final String node); + boolean canSee(@NotNull final User target); @NotNull Set ignoredUsers(); diff --git a/plugin/src/main/java/at/helpch/chatchat/channel/AbstractChannel.java b/plugin/src/main/java/at/helpch/chatchat/channel/AbstractChannel.java index 42844d91..f0248cf4 100644 --- a/plugin/src/main/java/at/helpch/chatchat/channel/AbstractChannel.java +++ b/plugin/src/main/java/at/helpch/chatchat/channel/AbstractChannel.java @@ -60,7 +60,7 @@ public int radius() { @Override public boolean isUseableBy(@NotNull final ChatUser user) { - return user.player().hasPermission(ChannelUtils.USE_CHANNEL_PERMISSION + name()); + return user.hasPermission(ChannelUtils.USE_CHANNEL_PERMISSION + name()); } @Override diff --git a/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java b/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java index 6be20e22..2fe4fa3f 100644 --- a/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java +++ b/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java @@ -53,7 +53,7 @@ public String toString() { public Set targets(final @NotNull User source) { final Predicate filterIgnores = user -> !user.ignoredUsers().contains(source.uuid()) || - (source instanceof ChatUser && ((ChatUser) source).player().hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION)); + source.hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION); if (plugin.configManager().channels().defaultChannel().equals(this.name())) return plugin.usersHolder().users().stream() @@ -62,8 +62,7 @@ public Set targets(final @NotNull User source) { .collect(Collectors.toSet()); return plugin.usersHolder().users().stream().filter(user -> - !(user instanceof ChatUser) || - ((ChatUser) user).player().hasPermission(ChannelUtils.SEE_CHANNEL_PERMISSION + name())) + user.hasPermission(ChannelUtils.SEE_CHANNEL_PERMISSION + name())) .filter(user -> ChannelUtils.isTargetWithinRadius(source, user, radius())) .filter(filterIgnores) .collect(Collectors.toSet()); diff --git a/plugin/src/main/java/at/helpch/chatchat/command/DumpCommand.java b/plugin/src/main/java/at/helpch/chatchat/command/DumpCommand.java index 4c19fc2f..0b0c6650 100644 --- a/plugin/src/main/java/at/helpch/chatchat/command/DumpCommand.java +++ b/plugin/src/main/java/at/helpch/chatchat/command/DumpCommand.java @@ -26,8 +26,7 @@ public DumpCommand(@NotNull final ChatChatPlugin plugin) { @SubCommand("dump") public void dump(final User user, final @Suggestion("files") @Optional String file) { - // TODO: 8/26/22 Implement a User#hasPermission method - if (user instanceof ChatUser && !((ChatUser )user).player().hasPermission(DUMP_PERMISSION)) { + if (!user.hasPermission(DUMP_PERMISSION)) { user.sendMessage(plugin.configManager().messages().noPermission()); return; } diff --git a/plugin/src/main/java/at/helpch/chatchat/command/WhisperCommand.java b/plugin/src/main/java/at/helpch/chatchat/command/WhisperCommand.java index 51ccea9c..61e713ee 100644 --- a/plugin/src/main/java/at/helpch/chatchat/command/WhisperCommand.java +++ b/plugin/src/main/java/at/helpch/chatchat/command/WhisperCommand.java @@ -64,14 +64,14 @@ public void whisperCommand( return; } - final var userHasBypassPerm = user.player().hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION); - if (recipient.ignoredUsers().contains(user.uuid()) && !userHasBypassPerm) { + if (recipient.ignoredUsers().contains(user.uuid()) && + !user.hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION)) { user.sendMessage(plugin.configManager().messages().cantMessageGeneral()); return; } - final var recipientHasBypassPerm = recipient.player().hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION); - if (user.ignoredUsers().contains(recipient.uuid()) && !recipientHasBypassPerm) { + if (user.ignoredUsers().contains(recipient.uuid()) && + !recipient.hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION)) { user.sendMessage(plugin.configManager().messages().cantMessageIgnoredPlayer()); return; } @@ -81,7 +81,7 @@ public void whisperCommand( return; } - if (StringUtils.containsIllegalChars(message) && !user.player().hasPermission(UTF_PERMISSION)) { + if (StringUtils.containsIllegalChars(message) && !user.hasPermission(UTF_PERMISSION)) { user.sendMessage(plugin.configManager().messages().specialCharactersNoPermission()); return; } diff --git a/plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java b/plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java index 7efea835..ec35edbe 100644 --- a/plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java +++ b/plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java @@ -65,6 +65,11 @@ public void format(@NotNull final Format format) { return uuid; } + @Override + public boolean hasPermission(@NotNull final String node) { + return player().hasPermission(node); + } + @Override public @NotNull Optional lastMessagedUser() { return lastMessagedUser.get(); diff --git a/plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java b/plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java index bf1878d0..1456ecd0 100644 --- a/plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java +++ b/plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java @@ -8,6 +8,7 @@ import at.helpch.chatchat.format.ChatFormat; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.identity.Identity; +import org.bukkit.Bukkit; import org.jetbrains.annotations.NotNull; import java.util.Set; @@ -43,6 +44,11 @@ public void format(@NotNull final Format format) { return Identity.nil().uuid(); } + @Override + public boolean hasPermission(@NotNull final String node) { + return Bukkit.getConsoleSender().hasPermission(node); + } + @Override public boolean canSee(@NotNull User target) { return true; diff --git a/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java b/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java index 8e869aab..ebbc2309 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java @@ -5,7 +5,6 @@ import at.helpch.chatchat.api.User; import at.helpch.chatchat.channel.ChatChannel; import org.bukkit.Location; -import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -47,15 +46,13 @@ public static boolean isTargetWithinRadius( return true; } - final Player playerTarget = ((ChatUser) target).player(); - - if (playerTarget.hasPermission(BYPASS_RADIUS_CHANNEL_PERMISSION)) { + if (target.hasPermission(BYPASS_RADIUS_CHANNEL_PERMISSION)) { return true; } if (radius != -1 && source instanceof ChatUser) { final Location sourceLocation = ((ChatUser) source).player().getLocation(); - final Location targetLocation = playerTarget.getLocation(); + final Location targetLocation = ((ChatUser) target).player().getLocation(); final int relativeX = targetLocation.getBlockX() - sourceLocation.getBlockX(); final int relativeZ = targetLocation.getBlockZ() - sourceLocation.getBlockZ(); diff --git a/plugin/src/main/java/at/helpch/chatchat/util/MentionUtils.java b/plugin/src/main/java/at/helpch/chatchat/util/MentionUtils.java index e92b2051..51977b0f 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/MentionUtils.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/MentionUtils.java @@ -93,14 +93,14 @@ public static MentionReplaceResult replaceMention( @NotNull final User target, @NotNull final Component message ) { - if (!user.player().hasPermission(MENTION_CHANNEL_PERMISSION)) { + if (!user.hasPermission(MENTION_CHANNEL_PERMISSION)) { return Map.entry(false, message); } if (target instanceof ChatUser) { final var targetChatUser = (ChatUser) target; - if (!targetChatUser.channelMentions() && !user.player().hasPermission(MENTION_CHANNEL_BLOCK_OVERRIDE_PERMISSION)) { + if (!targetChatUser.channelMentions() && !user.hasPermission(MENTION_CHANNEL_BLOCK_OVERRIDE_PERMISSION)) { return Map.entry(false, message); } } @@ -120,8 +120,8 @@ public static MentionReplaceResult replaceMention( @NotNull final ChatUser target, @NotNull final Component message ) { - if (!user.player().hasPermission(MENTION_PERSONAL_PERMISSION) || - (!target.personalMentions() && !user.player().hasPermission(MENTION_PERSONAL_BLOCK_OVERRIDE_PERMISSION)) + if (!user.hasPermission(MENTION_PERSONAL_PERMISSION) || + (!target.personalMentions() && !user.hasPermission(MENTION_PERSONAL_BLOCK_OVERRIDE_PERMISSION)) ) { return Map.entry(false, message); } diff --git a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java index a179a528..5e1709fc 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java @@ -66,7 +66,7 @@ public static boolean process( @NotNull final String message, final boolean async ) { - if (StringUtils.containsIllegalChars(message) && !user.player().hasPermission(UTF_PERMISSION)) { + if (StringUtils.containsIllegalChars(message) && !user.hasPermission(UTF_PERMISSION)) { user.sendMessage(plugin.configManager().messages().specialCharactersNoPermission()); return false; } @@ -319,7 +319,7 @@ public static boolean process( final var resolver = TagResolver.builder(); for (final var entry : PERMISSION_TAGS.entrySet()) { - if (!user.player().hasPermission(TAG_BASE_PERMISSION + entry.getKey())) { + if (!user.hasPermission(TAG_BASE_PERMISSION + entry.getKey())) { continue; } @@ -327,14 +327,14 @@ public static boolean process( } for (final var tag : TextDecoration.values()) { - if (!user.player().hasPermission(TAG_BASE_PERMISSION + tag.toString())) { + if (!user.hasPermission(TAG_BASE_PERMISSION + tag.toString())) { continue; } resolver.resolver(StandardTags.decorations(tag)); } - if (user.player().hasPermission(ITEM_TAG_PERMISSION)) { + if (user.hasPermission(ITEM_TAG_PERMISSION)) { resolver.resolver( ItemUtils.createItemPlaceholder( plugin.configManager().settings().itemFormat(), @@ -344,7 +344,7 @@ public static boolean process( ); } - return !user.player().hasPermission(URL_PERMISSION) + return !user.hasPermission(URL_PERMISSION) ? USER_MESSAGE_MINI_MESSAGE.deserialize(message, resolver.build()) : USER_MESSAGE_MINI_MESSAGE.deserialize(message, resolver.build()).replaceText(URL_REPLACER_CONFIG); } From e7075d562dd7ea0d3b43040e63f0b52f6efd12e6 Mon Sep 17 00:00:00 2001 From: BlitzOffline <52609756+BlitzOffline@users.noreply.github.com> Date: Mon, 29 Aug 2022 17:56:08 +0300 Subject: [PATCH 2/4] Console always has permission --- plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java b/plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java index 1456ecd0..6abf8a04 100644 --- a/plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java +++ b/plugin/src/main/java/at/helpch/chatchat/user/ConsoleUser.java @@ -46,7 +46,7 @@ public void format(@NotNull final Format format) { @Override public boolean hasPermission(@NotNull final String node) { - return Bukkit.getConsoleSender().hasPermission(node); + return true; } @Override From 0a1379b2abd79c550b9b25f8bde5357573a9f93d Mon Sep 17 00:00:00 2001 From: BlitzOffline <52609756+BlitzOffline@users.noreply.github.com> Date: Fri, 2 Sep 2022 16:11:25 +0300 Subject: [PATCH 3/4] Fixed console users being considered a target --- .../src/main/java/at/helpch/chatchat/channel/ChatChannel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java b/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java index 2fe4fa3f..e8584965 100644 --- a/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java +++ b/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java @@ -53,7 +53,7 @@ public String toString() { public Set targets(final @NotNull User source) { final Predicate filterIgnores = user -> !user.ignoredUsers().contains(source.uuid()) || - source.hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION); + source instanceof ChatUser && source.hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION); if (plugin.configManager().channels().defaultChannel().equals(this.name())) return plugin.usersHolder().users().stream() From a3c2badf68614bfbf705d73445291f86672be7ca Mon Sep 17 00:00:00 2001 From: BlitzOffline <52609756+BlitzOffline@users.noreply.github.com> Date: Fri, 2 Sep 2022 16:16:16 +0300 Subject: [PATCH 4/4] Actually fixed targets this time --- .../src/main/java/at/helpch/chatchat/channel/ChatChannel.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java b/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java index e8584965..4839289e 100644 --- a/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java +++ b/plugin/src/main/java/at/helpch/chatchat/channel/ChatChannel.java @@ -52,8 +52,8 @@ public String toString() { @Override public Set targets(final @NotNull User source) { - final Predicate filterIgnores = user -> !user.ignoredUsers().contains(source.uuid()) || - source instanceof ChatUser && source.hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION); + final Predicate filterIgnores = user -> user instanceof ChatUser && + (!user.ignoredUsers().contains(source.uuid()) || source.hasPermission(IgnoreCommand.IGNORE_BYPASS_PERMISSION)); if (plugin.configManager().channels().defaultChannel().equals(this.name())) return plugin.usersHolder().users().stream()