From 3f520bfb1b281cc961e62fc0fccbe0925ed4e240 Mon Sep 17 00:00:00 2001 From: M0diis Date: Thu, 29 Feb 2024 22:52:48 +0200 Subject: [PATCH 1/5] Resolve #228, implement Ranged-Chat command. --- .../at/helpch/chatchat/api/user/ChatUser.java | 16 ++++++++++ .../at/helpch/chatchat/ChatChatPlugin.java | 4 ++- .../chatchat/command/RangedChatCommand.java | 32 +++++++++++++++++++ .../config/holder/MessagesHolder.java | 11 +++++++ .../PlaceholderAPIPlaceholders.java | 5 ++- .../at/helpch/chatchat/user/ChatUserImpl.java | 11 +++++++ .../at/helpch/chatchat/util/ChannelUtils.java | 6 ++-- 7 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 plugin/src/main/java/at/helpch/chatchat/command/RangedChatCommand.java diff --git a/api/src/main/java/at/helpch/chatchat/api/user/ChatUser.java b/api/src/main/java/at/helpch/chatchat/api/user/ChatUser.java index 2259ae29..4d732092 100644 --- a/api/src/main/java/at/helpch/chatchat/api/user/ChatUser.java +++ b/api/src/main/java/at/helpch/chatchat/api/user/ChatUser.java @@ -90,4 +90,20 @@ public interface ChatUser extends User { * @param enable True to enable social spy, false to disable. */ void socialSpy(final boolean enable); + + /** + * Checks if the user has ranged chat enabled. + * If it is enabled, they will only see messages from players within a certain range. + * Only applies to the players that have bypass ChannelUtils.BYPASS_RADIUS_CHANNEL_PERMISSION. + * + * @return True if the user has ranged chat enabled, false otherwise. + */ + boolean rangedChat(); + + /** + * Changes the state of the user's ranged chat. + * + * @param enable True to enable ranged chat, false to disable. + */ + void rangedChat(final boolean enable); } diff --git a/plugin/src/main/java/at/helpch/chatchat/ChatChatPlugin.java b/plugin/src/main/java/at/helpch/chatchat/ChatChatPlugin.java index 79f62237..920c5b7e 100644 --- a/plugin/src/main/java/at/helpch/chatchat/ChatChatPlugin.java +++ b/plugin/src/main/java/at/helpch/chatchat/ChatChatPlugin.java @@ -13,6 +13,7 @@ import at.helpch.chatchat.command.IgnoreListCommand; import at.helpch.chatchat.command.MainCommand; import at.helpch.chatchat.command.MentionToggleCommand; +import at.helpch.chatchat.command.RangedChatCommand; import at.helpch.chatchat.command.ReloadCommand; import at.helpch.chatchat.command.ReplyCommand; import at.helpch.chatchat.command.SocialSpyCommand; @@ -274,7 +275,8 @@ private void registerCommands() { new MentionToggleCommand(this), new FormatTestCommand(this), new DumpCommand(this), - new ChatToggleCommand(this) + new ChatToggleCommand(this), + new RangedChatCommand(this) ).forEach(commandManager::registerCommand); if (configManager.settings().privateMessagesSettings().enabled()) { diff --git a/plugin/src/main/java/at/helpch/chatchat/command/RangedChatCommand.java b/plugin/src/main/java/at/helpch/chatchat/command/RangedChatCommand.java new file mode 100644 index 00000000..48832ffb --- /dev/null +++ b/plugin/src/main/java/at/helpch/chatchat/command/RangedChatCommand.java @@ -0,0 +1,32 @@ +package at.helpch.chatchat.command; + +import at.helpch.chatchat.ChatChatPlugin; +import at.helpch.chatchat.api.user.ChatUser; +import dev.triumphteam.cmd.bukkit.annotation.Permission; +import dev.triumphteam.cmd.core.BaseCommand; +import dev.triumphteam.cmd.core.annotation.Command; +import dev.triumphteam.cmd.core.annotation.Default; + +@Command("rangedchat") +public class RangedChatCommand extends BaseCommand { + + private static final String CHAT_TOGGLE_PERMISSION = "chatchat.rangedchat"; + private final ChatChatPlugin plugin; + + public RangedChatCommand(final ChatChatPlugin plugin) { + this.plugin = plugin; + } + + @Default + @Permission(CHAT_TOGGLE_PERMISSION) + public void toggleRangedChat(final ChatUser sender) { + sender.rangedChat(!sender.rangedChat()); + + final var messageHolder = plugin.configManager().messages(); + final var message = sender.rangedChat() ? + messageHolder.rangedChatEnabledSuccessfully() : + messageHolder.rangedChatDisabledSuccessfully(); + + sender.sendMessage(message); + } +} diff --git a/plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java b/plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java index a9fd1f2f..cffb8f5f 100644 --- a/plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java +++ b/plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java @@ -54,6 +54,9 @@ public final class MessagesHolder { private Component channelNoPermissionSwitch = text("You no longer have permission to use this channel so it has been switched to the channel. ", RED); private Component channelSwitched = text("You have switched to the channel", GREEN); + private Component rangedChatEnabledSuccessfully = text("Your ranged chat has been enabled successfully!", GREEN); + private Component rangedChatDisabledSuccessfully = text("Your ranged chat has been disabled successfully!", RED); + // command related private Component commandUnknownCommand = text("Unknown Command.", RED); private Component commandInvalidUsage = text("Invalid usage.", RED); @@ -237,4 +240,12 @@ public final class MessagesHolder { return chatDisabled; } + public @NotNull Component rangedChatEnabledSuccessfully() { + return rangedChatEnabledSuccessfully; + } + + public @NotNull Component rangedChatDisabledSuccessfully() { + return rangedChatDisabledSuccessfully; + } + } diff --git a/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java b/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java index 73b32bb8..e91d0952 100644 --- a/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java +++ b/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java @@ -41,7 +41,8 @@ public PlaceholderAPIPlaceholders(@NotNull final ChatChatPlugin plugin) { "%chatchat_channel_message_prefix%", "%chatchat_social_spy_enabled%", "%chatchat_private_messages_enabled%", - "%chatchat_private_messages_recipient%" + "%chatchat_private_messages_recipient%", + "%chatchat_ranged_chat_enabled%" ); } @@ -86,6 +87,8 @@ public String onRequest(final OfflinePlayer offlinePlayer, @NotNull final String return formatBoolean(chatUser.privateMessages()); case "private_messages_recipient": return chatUser.lastMessagedUser().map(value -> value.player().getName()).orElse(""); + case "ranged_chat_enabled": + return formatBoolean(chatUser.rangedChat()); } return null; 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 e4b5272e..0de9d881 100644 --- a/plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java +++ b/plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java @@ -39,6 +39,7 @@ public ChatUserImpl(@NotNull final UUID uuid) { private boolean channelMentions = true; private boolean socialSpy = false; private boolean chatEnabled = true; + private boolean rangedChat = false; private Set ignoredUsers = new HashSet<>(); @Override @@ -150,6 +151,16 @@ public boolean chatEnabled() { return this.chatEnabled; } + @Override + public boolean rangedChat() { + return rangedChat; + } + + @Override + public void rangedChat(final boolean enabled) { + this.rangedChat = enabled; + } + @Override public boolean canSee(@NotNull final User target) { if (!(target instanceof ChatUser)) { 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 c9e70fa5..735b5aa2 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java @@ -46,13 +46,15 @@ public static boolean isTargetWithinRadius( return true; } - if (target.hasPermission(BYPASS_RADIUS_CHANNEL_PERMISSION)) { + final ChatUser targetChatUser = (ChatUser) target; + + if (target.hasPermission(BYPASS_RADIUS_CHANNEL_PERMISSION) && !targetChatUser.rangedChat()) { return true; } if (radius != -1 && source instanceof ChatUser) { final Location sourceLocation = ((ChatUser) source).player().getLocation(); - final Location targetLocation = ((ChatUser) target).player().getLocation(); + final Location targetLocation = targetChatUser.player().getLocation(); final int relativeX = targetLocation.getBlockX() - sourceLocation.getBlockX(); final int relativeZ = targetLocation.getBlockZ() - sourceLocation.getBlockZ(); From 1848d3cb18f14814fdb764a7477d315a1e9ac459 Mon Sep 17 00:00:00 2001 From: M0diis Date: Thu, 29 Feb 2024 22:54:55 +0200 Subject: [PATCH 2/5] Fix merge issues. --- .../src/main/java/at/helpch/chatchat/util/ChannelUtils.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 16ec5fd8..48dd3e0a 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java @@ -54,7 +54,7 @@ public static boolean isTargetWithinRadius( } if (radius != -1 && source instanceof ChatUser) { - final Location targetLocation = targetChatUser.player().getLocation(); + final Location sourceLocation = targetChatUser.player().getLocation(); final Location targetLocation = ((ChatUser) target).player().getLocation(); final World sourceWorld = sourceLocation.getWorld(); @@ -64,6 +64,9 @@ public static boolean isTargetWithinRadius( return false; } + final int relativeX = targetLocation.getBlockX() - sourceLocation.getBlockX(); + final int relativeZ = targetLocation.getBlockZ() - sourceLocation.getBlockZ(); + return relativeX*relativeX + relativeZ*relativeZ <= radius*radius; } From f97290f48f2e76c34b8d71fe706da9723baaeb32 Mon Sep 17 00:00:00 2001 From: M0diis Date: Thu, 27 Feb 2025 22:13:26 +0200 Subject: [PATCH 3/5] #228 Fix missing bracket --- .../java/at/helpch/chatchat/config/holder/MessagesHolder.java | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java b/plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java index d1a6fd4d..281f747e 100644 --- a/plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java +++ b/plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java @@ -250,6 +250,7 @@ public final class MessagesHolder { public @NotNull Component rangedChatDisabledSuccessfully() { return rangedChatDisabledSuccessfully; + } public @NotNull Component genericError() { return genericError; From e8a0f74eafac2d79564f157a45ed64dfad7038c3 Mon Sep 17 00:00:00 2001 From: M0diis Date: Thu, 27 Feb 2025 22:16:43 +0200 Subject: [PATCH 4/5] Fix duplicate case --- .../chatchat/placeholder/PlaceholderAPIPlaceholders.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java b/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java index 9e0bbeaf..0d3c42be 100644 --- a/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java +++ b/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java @@ -13,7 +13,8 @@ import java.util.List; import java.util.Optional; -public final class PlaceholderAPIPlaceholders extends PlaceholderExpansion { +public final class +PlaceholderAPIPlaceholders extends PlaceholderExpansion { private final ChatChatPlugin plugin; public PlaceholderAPIPlaceholders(@NotNull final ChatChatPlugin plugin) { @@ -87,8 +88,6 @@ public String onRequest(final OfflinePlayer offlinePlayer, @NotNull final String return formatBoolean(chatUser.socialSpy()); case "private_messages_enabled": return formatBoolean(chatUser.privateMessages()); - case "private_messages_recipient": - return chatUser.lastMessagedUser().map(value -> value.player().getName()).orElse(""); case "ranged_chat_enabled": return formatBoolean(chatUser.rangedChat()); case "private_messages_recipient": From 5a3bb0825f63a7627f1dc19b30df9fa989fc57cd Mon Sep 17 00:00:00 2001 From: Ruben <52609756+BlitzOffline@users.noreply.github.com> Date: Thu, 27 Feb 2025 22:21:18 +0200 Subject: [PATCH 5/5] Removed random newline --- .../chatchat/placeholder/PlaceholderAPIPlaceholders.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java b/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java index 0d3c42be..974b5fcf 100644 --- a/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java +++ b/plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java @@ -13,8 +13,7 @@ import java.util.List; import java.util.Optional; -public final class -PlaceholderAPIPlaceholders extends PlaceholderExpansion { +public final class PlaceholderAPIPlaceholders extends PlaceholderExpansion { private final ChatChatPlugin plugin; public PlaceholderAPIPlaceholders(@NotNull final ChatChatPlugin plugin) {