Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Chat: add disabling local chat
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFaser committed Sep 15, 2023
1 parent 5f64bc4 commit 718d7ca
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
47 changes: 30 additions & 17 deletions src/main/java/net/flectone/listeners/AsyncPlayerChatListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
Expand All @@ -29,7 +30,7 @@ public class AsyncPlayerChatListener implements Listener {

private String noRecipientsMessage = "";

@EventHandler
@EventHandler(priority = EventPriority.LOWEST)
public void chat(@NotNull AsyncPlayerChatEvent event) {
if (event.isCancelled()) return;

Expand All @@ -41,39 +42,51 @@ public void chat(@NotNull AsyncPlayerChatEvent event) {
String globalPrefix = locale.getString("chat.global.prefix");
String message = event.getMessage();

String chatType = config.getBoolean("chat.global.enable")
&& (fPlayerChat.contains("global")
|| message.startsWith(globalPrefix) && !message.equals(globalPrefix) && !fPlayerChat.equals("onlylocal"))
? "global" : "local";
String chatType = fPlayerChat.contains("global")
|| message.startsWith(globalPrefix)
&& !message.equals(globalPrefix)
&& !fPlayerChat.equals("onlylocal")
? "global"
: "local";

String reversedChatType = chatType.equals("global") ? "local" : "global";

if (!config.getBoolean("chat." + chatType + ".enable")) {
if (!config.getBoolean("chat." + reversedChatType + ".enable")) {
player.sendMessage(locale.getFormatString("chat.all-disabled", player));
event.setCancelled(true);
return;
}

String tempString = chatType;
chatType = reversedChatType;
reversedChatType = tempString;
}

Set<Player> recipients = new HashSet<>(event.getRecipients());
removeRecipients(recipients, player, reversedChatType);

if (chatType.equals("local")) {
if(config.getBoolean("chat.global.enable")) {
int localRange = config.getInt("chat.local.range");
recipients.removeIf(recipient -> (player.getWorld() != recipient.getWorld()
|| player.getLocation().distance(recipient.getLocation()) > localRange));

if (config.getBoolean("chat.local.no-recipients.enable") &&
recipients.stream().filter(recipient -> !recipient.getGameMode().equals(GameMode.SPECTATOR)).count() == 1) {
noRecipientsMessage = locale.getFormatString("chat.local.no-recipients", player);
}

} else if(HookManager.enabledInteractiveChat) message = FInteractiveChat.checkMention(event);
int localRange = config.getInt("chat.local.range");
recipients.removeIf(recipient -> (player.getWorld() != recipient.getWorld()
|| player.getLocation().distance(recipient.getLocation()) > localRange));

if (config.getBoolean("chat.local.set-cancelled")) event.setCancelled(true);
if (config.getBoolean("chat.local.no-recipients.enable") &&
recipients.stream().filter(recipient -> !recipient.getGameMode().equals(GameMode.SPECTATOR)).count() == 1) {
noRecipientsMessage = locale.getFormatString("chat.local.no-recipients", player);
}

} else {
if(HookManager.enabledInteractiveChat) message = FInteractiveChat.checkMention(event);

if (config.getBoolean("chat.global.prefix.cleared")) event.setMessage(message.replaceFirst(globalPrefix, ""));
if (config.getBoolean("chat.global.set-cancelled")) event.setCancelled(true);

message = message.replaceFirst(globalPrefix, "").trim();
}

if (config.getBoolean("chat." + chatType + ".set-cancelled")) event.setCancelled(true);

createMessage(recipients, player, message, chatType, null);
event.getRecipients().clear();
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ chat:
read-speed: 800

local:
enable: true
range: 100
set-cancelled: true

no-recipients:
enable: true

set-cancelled: true

global:
enable: true
prefix:
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/language/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ chat:
default:
message: "<player>&&2 » <message>"

all-disabled: "#ff4e4e⁉ Chat is disabled on this server"

tab-complete:
message: "(message)"
reason: "(reason)"
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/language/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ chat:
default:
message: "<player>&&2 » <message>"

all-disabled: "#ff4e4e⁉ На этом сервере отключён чат"

tab-complete:
message: "(сообщение)"
reason: "(причина)"
Expand Down

0 comments on commit 718d7ca

Please sign in to comment.