From d259b7fefbe0a390857e4cb2b159d142d432cc9d Mon Sep 17 00:00:00 2001 From: M0diis Date: Tue, 27 Feb 2024 10:11:52 +0200 Subject: [PATCH 1/5] Resolve issue #223 --- .../java/at/helpch/chatchat/util/ChannelUtils.java | 11 ++++++++++- 1 file changed, 10 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 c9e70fa5..10bb9637 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java @@ -5,6 +5,7 @@ import at.helpch.chatchat.api.user.User; import at.helpch.chatchat.channel.ChatChannel; import org.bukkit.Location; +import org.bukkit.World; import org.jetbrains.annotations.NotNull; import java.util.List; @@ -50,9 +51,17 @@ public static boolean isTargetWithinRadius( return true; } - if (radius != -1 && source instanceof ChatUser) { + if (radius != -1 && source instanceof ChatUser ) { final Location sourceLocation = ((ChatUser) source).player().getLocation(); final Location targetLocation = ((ChatUser) target).player().getLocation(); + + final World sourceWorld = sourceLocation.getWorld(); + final World targetWorld = targetLocation.getWorld(); + + if(sourceWorld != null && targetWorld != null && !sourceWorld.getName().equals(targetWorld.getName())) { + return false; + } + final int relativeX = targetLocation.getBlockX() - sourceLocation.getBlockX(); final int relativeZ = targetLocation.getBlockZ() - sourceLocation.getBlockZ(); From 809deb81813a05d08689f6a9daac4e594729a914 Mon Sep 17 00:00:00 2001 From: M0diis Date: Tue, 27 Feb 2024 10:12:21 +0200 Subject: [PATCH 2/5] Whitespace fix --- plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java | 2 +- 1 file changed, 1 insertion(+), 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 10bb9637..b5081ecd 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java @@ -51,7 +51,7 @@ public static boolean isTargetWithinRadius( return true; } - if (radius != -1 && source instanceof ChatUser ) { + if (radius != -1 && source instanceof ChatUser) { final Location sourceLocation = ((ChatUser) source).player().getLocation(); final Location targetLocation = ((ChatUser) target).player().getLocation(); From b2981c1c1e5119a0442697b9ab5df8164ec6916a Mon Sep 17 00:00:00 2001 From: M0diis Date: Tue, 27 Feb 2024 10:46:16 +0200 Subject: [PATCH 3/5] Resolve #218 --- .../chatchat/command/SwitchChannelCommand.java | 2 +- .../helpch/chatchat/listener/ChatListener.java | 14 +++++++++++++- .../helpch/chatchat/util/MessageProcessor.java | 18 +++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java b/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java index 9d0b038c..4784f6dc 100644 --- a/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java +++ b/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java @@ -57,6 +57,6 @@ public void switchChannel(final ChatUser user, @Join @Optional @NotNull final St return; } - MessageProcessor.process(plugin, user, channel, message, false); + MessageProcessor.process(plugin, user, channel, user.channel(), message, false); } } diff --git a/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java b/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java index 9b096640..7b894a82 100644 --- a/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java +++ b/plugin/src/main/java/at/helpch/chatchat/listener/ChatListener.java @@ -73,6 +73,11 @@ public void onChat(final AsyncPlayerChatEvent event) { final var consoleFormat = plugin.configManager().formats().consoleFormat(); + final var oldChannel = user.channel(); + + // We switch the user to the channel here so that the console can parse the correct channel prefix + user.channel(channel); + event.setMessage(LegacyComponentSerializer.legacySection().serialize( MessageProcessor.processMessage(plugin, user, ConsoleUser.INSTANCE, message) )); @@ -96,7 +101,14 @@ public void onChat(final AsyncPlayerChatEvent event) { // Cancel the event if the message doesn't end up being sent // This only happens if the message contains illegal characters or if the ChatChatEvent is canceled. - event.setCancelled(!MessageProcessor.process(plugin, user, channel, message, event.isAsynchronous())); + boolean cancelled = MessageProcessor.process(plugin, user, channel, oldChannel, message, event.isAsynchronous()); + + event.setCancelled(!cancelled); + + // If the event was cancelled, we need to reset the user back to their old channel + if(cancelled) { + user.channel(oldChannel); + } } private static String cleanseMessage(@NotNull final String 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 3aec1eeb..5fd7d3c9 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java @@ -59,10 +59,23 @@ private MessageProcessor() { throw new AssertionError("Util classes are not to be instantiated!"); } + /** + * Process a message for a user and send it to the recipients. + * @param plugin The plugin instance. + * @param user The user sending the message. + * @param channel The channel the user is sending the message to. + * @param previousChannel The previous channel the user was in. Used to switch back to after sending the message. + * Used to switch back when sending a message to a channel with a prefix. + * @param message The message to send. + * @param async Whether to process the message asynchronously. + * + * @return Whether the message was sent successfully. + */ public static boolean process( @NotNull final ChatChatPlugin plugin, @NotNull final ChatUser user, @NotNull final Channel channel, + @NotNull final Channel previousChannel, @NotNull final String message, final boolean async ) { @@ -103,7 +116,6 @@ public static boolean process( return false; } - final var oldChannel = user.channel(); user.channel(channel); final var parsedMessage = chatEvent.message().compact(); @@ -173,7 +185,7 @@ public static boolean process( } if (!userIsTarget) { - user.channel(oldChannel); + user.channel(previousChannel); return true; } @@ -199,7 +211,7 @@ public static boolean process( user.playSound(mentions.sound()); } - user.channel(oldChannel); + user.channel(previousChannel); return true; } From c357ccf7483cd6d9e339fdf7a6a730e67a152e38 Mon Sep 17 00:00:00 2001 From: M0diis Date: Thu, 29 Feb 2024 22:44:12 +0200 Subject: [PATCH 4/5] Fix SwitchChannelCommand --- .../java/at/helpch/chatchat/command/SwitchChannelCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java b/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java index 4784f6dc..9d0b038c 100644 --- a/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java +++ b/plugin/src/main/java/at/helpch/chatchat/command/SwitchChannelCommand.java @@ -57,6 +57,6 @@ public void switchChannel(final ChatUser user, @Join @Optional @NotNull final St return; } - MessageProcessor.process(plugin, user, channel, user.channel(), message, false); + MessageProcessor.process(plugin, user, channel, message, false); } } From 0a78b6a29c7259b4712aa463078020012f0a6b2d Mon Sep 17 00:00:00 2001 From: M0diis Date: Thu, 29 Feb 2024 22:44:59 +0200 Subject: [PATCH 5/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 5f0b9650..997175a5 100644 --- a/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java +++ b/plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java @@ -47,13 +47,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 World sourceWorld = sourceLocation.getWorld(); final World targetWorld = targetLocation.getWorld();