Skip to content

Commit 1f3963f

Browse files
authored
Merge pull request #230 from M0diis/resolve-228
Resolve #228, implement Ranged-Chat command.
2 parents 5f6a815 + 5a3bb08 commit 1f3963f

File tree

7 files changed

+82
-5
lines changed

7 files changed

+82
-5
lines changed

api/src/main/java/at/helpch/chatchat/api/user/ChatUser.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,20 @@ public interface ChatUser extends User {
9090
* @param enable True to enable social spy, false to disable.
9191
*/
9292
void socialSpy(final boolean enable);
93+
94+
/**
95+
* Checks if the user has ranged chat enabled.
96+
* If it is enabled, they will only see messages from players within a certain range.
97+
* Only applies to the players that have bypass ChannelUtils.BYPASS_RADIUS_CHANNEL_PERMISSION.
98+
*
99+
* @return True if the user has ranged chat enabled, false otherwise.
100+
*/
101+
boolean rangedChat();
102+
103+
/**
104+
* Changes the state of the user's ranged chat.
105+
*
106+
* @param enable True to enable ranged chat, false to disable.
107+
*/
108+
void rangedChat(final boolean enable);
93109
}

plugin/src/main/java/at/helpch/chatchat/ChatChatPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import at.helpch.chatchat.command.IgnoreListCommand;
1414
import at.helpch.chatchat.command.MainCommand;
1515
import at.helpch.chatchat.command.MentionToggleCommand;
16+
import at.helpch.chatchat.command.RangedChatCommand;
1617
import at.helpch.chatchat.command.ReloadCommand;
1718
import at.helpch.chatchat.command.ReplyCommand;
1819
import at.helpch.chatchat.command.SocialSpyCommand;
@@ -275,7 +276,8 @@ private void registerCommands() {
275276
new MentionToggleCommand(this),
276277
new FormatTestCommand(this),
277278
new DumpCommand(this),
278-
new ChatToggleCommand(this)
279+
new ChatToggleCommand(this),
280+
new RangedChatCommand(this)
279281
).forEach(commandManager::registerCommand);
280282

281283
if (configManager.settings().privateMessagesSettings().enabled()) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package at.helpch.chatchat.command;
2+
3+
import at.helpch.chatchat.ChatChatPlugin;
4+
import at.helpch.chatchat.api.user.ChatUser;
5+
import dev.triumphteam.cmd.bukkit.annotation.Permission;
6+
import dev.triumphteam.cmd.core.BaseCommand;
7+
import dev.triumphteam.cmd.core.annotation.Command;
8+
import dev.triumphteam.cmd.core.annotation.Default;
9+
10+
@Command("rangedchat")
11+
public class RangedChatCommand extends BaseCommand {
12+
13+
private static final String CHAT_TOGGLE_PERMISSION = "chatchat.rangedchat";
14+
private final ChatChatPlugin plugin;
15+
16+
public RangedChatCommand(final ChatChatPlugin plugin) {
17+
this.plugin = plugin;
18+
}
19+
20+
@Default
21+
@Permission(CHAT_TOGGLE_PERMISSION)
22+
public void toggleRangedChat(final ChatUser sender) {
23+
sender.rangedChat(!sender.rangedChat());
24+
25+
final var messageHolder = plugin.configManager().messages();
26+
final var message = sender.rangedChat() ?
27+
messageHolder.rangedChatEnabledSuccessfully() :
28+
messageHolder.rangedChatDisabledSuccessfully();
29+
30+
sender.sendMessage(message);
31+
}
32+
}

plugin/src/main/java/at/helpch/chatchat/config/holder/MessagesHolder.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public final class MessagesHolder {
5454
private Component channelNoPermissionSwitch = text("You no longer have permission to use this channel so it has been switched to the <default> channel. ", RED);
5555
private Component channelSwitched = text("You have switched to the <channel> channel", GREEN);
5656

57+
private Component rangedChatEnabledSuccessfully = text("Your ranged chat has been enabled successfully!", GREEN);
58+
private Component rangedChatDisabledSuccessfully = text("Your ranged chat has been disabled successfully!", RED);
59+
5760
// command related
5861
private Component commandUnknownCommand = text("Unknown Command.", RED);
5962
private Component commandInvalidUsage = text("Invalid usage.", RED);
@@ -240,8 +243,16 @@ public final class MessagesHolder {
240243
return chatDisabled;
241244
}
242245

246+
247+
public @NotNull Component rangedChatEnabledSuccessfully() {
248+
return rangedChatEnabledSuccessfully;
249+
}
250+
251+
public @NotNull Component rangedChatDisabledSuccessfully() {
252+
return rangedChatDisabledSuccessfully;
253+
}
254+
243255
public @NotNull Component genericError() {
244256
return genericError;
245257
}
246-
247258
}

plugin/src/main/java/at/helpch/chatchat/placeholder/PlaceholderAPIPlaceholders.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public PlaceholderAPIPlaceholders(@NotNull final ChatChatPlugin plugin) {
4343
"%chatchat_channel_message_prefix%",
4444
"%chatchat_social_spy_enabled%",
4545
"%chatchat_private_messages_enabled%",
46-
"%chatchat_private_messages_recipient%"
46+
"%chatchat_private_messages_recipient%",
47+
"%chatchat_ranged_chat_enabled%"
4748
);
4849
}
4950

@@ -86,6 +87,8 @@ public String onRequest(final OfflinePlayer offlinePlayer, @NotNull final String
8687
return formatBoolean(chatUser.socialSpy());
8788
case "private_messages_enabled":
8889
return formatBoolean(chatUser.privateMessages());
90+
case "ranged_chat_enabled":
91+
return formatBoolean(chatUser.rangedChat());
8992
case "private_messages_recipient":
9093
return chatUser.lastMessagedUser().map(value -> value.player().map(Player::getName).orElse("")).orElse("");
9194
}

plugin/src/main/java/at/helpch/chatchat/user/ChatUserImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public ChatUserImpl(@NotNull final UUID uuid) {
3939
private boolean channelMentions = true;
4040
private boolean socialSpy = false;
4141
private boolean chatEnabled = true;
42+
private boolean rangedChat = false;
4243
private Set<UUID> ignoredUsers = new HashSet<>();
4344

4445
@Override
@@ -150,6 +151,16 @@ public boolean chatEnabled() {
150151
return this.chatEnabled;
151152
}
152153

154+
@Override
155+
public boolean rangedChat() {
156+
return rangedChat;
157+
}
158+
159+
@Override
160+
public void rangedChat(final boolean enabled) {
161+
this.rangedChat = enabled;
162+
}
163+
153164
@Override
154165
public boolean canSee(@NotNull final User target) {
155166
if (!(target instanceof ChatUser)) {

plugin/src/main/java/at/helpch/chatchat/util/ChannelUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ public static boolean isTargetWithinRadius(
4747
return true;
4848
}
4949

50-
if (target.hasPermission(BYPASS_RADIUS_CHANNEL_PERMISSION)) {
50+
final ChatUser targetChatUser = (ChatUser) target;
51+
52+
if (target.hasPermission(BYPASS_RADIUS_CHANNEL_PERMISSION) && !targetChatUser.rangedChat()) {
5153
return true;
5254
}
5355

5456
if (radius != -1 && source instanceof ChatUser) {
5557
var sourcePlayer = ((ChatUser) source).player();
56-
var targetPlayer = ((ChatUser) target).player();
58+
var targetPlayer = targetChatUser.player();
5759

5860
if (sourcePlayer.isEmpty() || targetPlayer.isEmpty()) {
5961
return false;

0 commit comments

Comments
 (0)