From 1380c7b84442753f0a856971e1cd22a4b7080454 Mon Sep 17 00:00:00 2001 From: William Date: Tue, 17 Oct 2023 15:46:14 +0100 Subject: [PATCH] Add permission format override for join/quit messages. --- .../bungeecord/getter/BungeePermsDataGetter.java | 7 +++++++ .../java/net/william278/huskchat/config/Locales.java | 8 ++++++-- .../java/net/william278/huskchat/getter/DataGetter.java | 2 ++ .../william278/huskchat/getter/DefaultDataGetter.java | 5 +++++ .../william278/huskchat/getter/LuckPermsDataGetter.java | 7 ++++++- common/src/main/resources/config.yml | 4 ++-- docs/Config-Files.md | 4 ++-- docs/Join-and-Quit-Messages.md | 9 ++++++--- 8 files changed, 36 insertions(+), 10 deletions(-) diff --git a/bungee/src/main/java/net/william278/huskchat/bungeecord/getter/BungeePermsDataGetter.java b/bungee/src/main/java/net/william278/huskchat/bungeecord/getter/BungeePermsDataGetter.java index 31331c9..ee7cb93 100644 --- a/bungee/src/main/java/net/william278/huskchat/bungeecord/getter/BungeePermsDataGetter.java +++ b/bungee/src/main/java/net/william278/huskchat/bungeecord/getter/BungeePermsDataGetter.java @@ -89,6 +89,13 @@ public Optional getPlayerGroupDisplayName(@NotNull Player player) { } } + @Override + public Optional getTextFromNode(@NotNull Player player, @NotNull String nodePrefix) { + final String prefix = nodePrefix.endsWith(".") ? nodePrefix : nodePrefix + "."; + return getUser(player).getPerms().stream().filter(node -> node.startsWith(prefix)).findFirst() + .map(node -> node.length() > prefix.length() ? node.substring(prefix.length()) : ""); + } + private User getUser(Player player) { return permissionsManager.getUser(player.getUuid()); } diff --git a/common/src/main/java/net/william278/huskchat/config/Locales.java b/common/src/main/java/net/william278/huskchat/config/Locales.java index 8972fc3..b5af51b 100644 --- a/common/src/main/java/net/william278/huskchat/config/Locales.java +++ b/common/src/main/java/net/william278/huskchat/config/Locales.java @@ -208,12 +208,16 @@ public void sendFormattedBroadcastMessage(@NotNull Player player, @NotNull Strin } public void sendJoinMessage(@NotNull Player player) { - plugin.replacePlaceholders(player, plugin.getSettings().getJoinMessageFormat()) + plugin.replacePlaceholders(player, + plugin.getDataGetter().getTextFromNode(player, "huskchat.join_message") + .orElse(plugin.getSettings().getJoinMessageFormat())) .thenAccept(replaced -> sendJoinQuitMessage(player, new MineDown(replaced).toComponent())); } public void sendQuitMessage(@NotNull Player player) { - plugin.replacePlaceholders(player, plugin.getSettings().getQuitMessageFormat()) + plugin.replacePlaceholders(player, + plugin.getDataGetter().getTextFromNode(player, "huskchat.quit_message") + .orElse(plugin.getSettings().getQuitMessageFormat())) .thenAccept(replaced -> sendJoinQuitMessage(player, new MineDown(replaced).toComponent())); } diff --git a/common/src/main/java/net/william278/huskchat/getter/DataGetter.java b/common/src/main/java/net/william278/huskchat/getter/DataGetter.java index 38ff3a9..9c87271 100644 --- a/common/src/main/java/net/william278/huskchat/getter/DataGetter.java +++ b/common/src/main/java/net/william278/huskchat/getter/DataGetter.java @@ -41,4 +41,6 @@ public abstract class DataGetter { public abstract Optional getPlayerGroupDisplayName(@NotNull Player player); + public abstract Optional getTextFromNode(@NotNull Player player, @NotNull String nodePrefix); + } diff --git a/common/src/main/java/net/william278/huskchat/getter/DefaultDataGetter.java b/common/src/main/java/net/william278/huskchat/getter/DefaultDataGetter.java index 3a9b54e..926af64 100644 --- a/common/src/main/java/net/william278/huskchat/getter/DefaultDataGetter.java +++ b/common/src/main/java/net/william278/huskchat/getter/DefaultDataGetter.java @@ -63,4 +63,9 @@ public Optional getPlayerGroupDisplayName(@NotNull Player player) { return Optional.empty(); } + @Override + public Optional getTextFromNode(@NotNull Player player, @NotNull String nodePrefix) { + return Optional.empty(); + } + } diff --git a/common/src/main/java/net/william278/huskchat/getter/LuckPermsDataGetter.java b/common/src/main/java/net/william278/huskchat/getter/LuckPermsDataGetter.java index 60262da..bdb2e91 100644 --- a/common/src/main/java/net/william278/huskchat/getter/LuckPermsDataGetter.java +++ b/common/src/main/java/net/william278/huskchat/getter/LuckPermsDataGetter.java @@ -106,8 +106,13 @@ public Optional getPlayerGroupDisplayName(@NotNull Player player) { } return Optional.of(group.getDisplayName()); }); + } - + @Override + public Optional getTextFromNode(@NotNull Player player, @NotNull String nodePrefix) { + return getUser(player.getUuid()).flatMap(user -> Optional.ofNullable( + user.getCachedData().getMetaData().getMetaValue(nodePrefix) + )); } private Optional getUser(@NotNull UUID uuid) { diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index dba9a44..8711072 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -204,11 +204,11 @@ message_replacers: join_and_quit_messages: join: enabled: false - # Use the huskchat.join.[text] permission to override this per-group if needed + # Use the huskchat.join_message.[text] permission to override this per-group if needed format: '&e%name% joined the network' quit: enabled: false - # Use the huskchat.quit.[text] permission to override this per-group if needed + # Use the huskchat.quit_message.[text] permission to override this per-group if needed format: '&e%name% left the network' broadcast_scope: GLOBAL # Note that on Velocity/Bungee, PASSTHROUGH modes won't cancel local join/quit messages diff --git a/docs/Config-Files.md b/docs/Config-Files.md index d6a0a1c..0815f5d 100644 --- a/docs/Config-Files.md +++ b/docs/Config-Files.md @@ -217,11 +217,11 @@ message_replacers: join_and_quit_messages: join: enabled: false - # Use the huskchat.join.[text] permission to override this per-group if needed + # Use the huskchat.join_message.[text] permission to override this per-group if needed format: '&e%name% joined the network' quit: enabled: false - # Use the huskchat.quit.[text] permission to override this per-group if needed + # Use the huskchat.quit_message.[text] permission to override this per-group if needed format: '&e%name% left the network' broadcast_scope: GLOBAL # Note that on Velocity/Bungee, PASSTHROUGH modes won't cancel local join/quit messages diff --git a/docs/Join-and-Quit-Messages.md b/docs/Join-and-Quit-Messages.md index 6af334e..8a228a2 100644 --- a/docs/Join-and-Quit-Messages.md +++ b/docs/Join-and-Quit-Messages.md @@ -16,12 +16,15 @@ Note that global, local and regular PASSTHROUGH scopes are only effective when r join_and_quit_messages: join: enabled: false - # Use the huskchat.join.[text] permission to override this per-group if needed + # Use the huskchat.join_message.[text] permission to override this per-group if needed format: '&e%name% joined the network' quit: enabled: false - # Use the huskchat.quit.[text] permission to override this per-group if needed + # Use the huskchat.quit_message.[text] permission to override this per-group if needed format: '&e%name% left the network' broadcast_scope: GLOBAL # Note that on Velocity/Bungee, PASSTHROUGH modes won't cancel local join/quit messages ``` - \ No newline at end of file + + +## Permission-based formats +You can set specific join/quit messages for specific groups by using the `huskchat.join_message.[text]` and `huskchat.quit_message.[text]` permissions. For example, if you wanted to set a special join message for players with the `vip` group, you could give them the `huskchat.join_message.&a%name% has arrived with style!` permission node to display a different join message. \ No newline at end of file