Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve 228 #229

Closed
wants to merge 10 commits into from
16 changes: 16 additions & 0 deletions api/src/main/java/at/helpch/chatchat/api/user/ChatUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
4 changes: 3 additions & 1 deletion plugin/src/main/java/at/helpch/chatchat/ChatChatPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <default> channel. ", RED);
private Component channelSwitched = text("You have switched to the <channel> 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);
Expand Down Expand Up @@ -237,4 +240,12 @@ public final class MessagesHolder {
return chatDisabled;
}

public @NotNull Component rangedChatEnabledSuccessfully() {
return rangedChatEnabledSuccessfully;
}

public @NotNull Component rangedChatDisabledSuccessfully() {
return rangedChatDisabledSuccessfully;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
));
Expand All @@ -97,6 +102,7 @@ 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()));
user.channel(oldChannel);
}

private static String cleanseMessage(@NotNull final String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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%"
);
}

Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID> ignoredUsers = new HashSet<>();

@Override
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions plugin/src/main/java/at/helpch/chatchat/util/MessageProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ 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 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,
Expand Down
Loading