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();