Skip to content

Commit

Permalink
Small fixes in chat config
Browse files Browse the repository at this point in the history
  • Loading branch information
duranaaron committed Jan 5, 2025
1 parent 571e865 commit ae58c11
Showing 1 changed file with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.papermc.paper.event.player.AsyncChatEvent;
import nl.openminetopia.OpenMinetopia;
import nl.openminetopia.api.player.PlayerManager;
import nl.openminetopia.api.player.objects.MinetopiaPlayer;
import nl.openminetopia.configuration.DefaultConfiguration;
import nl.openminetopia.configuration.MessageConfiguration;
import nl.openminetopia.modules.chat.utils.SpyUtils;
Expand All @@ -23,22 +24,25 @@ public class PlayerChatListener implements Listener {
@EventHandler
public void playerChat(AsyncChatEvent event) {
Player source = event.getPlayer();
PlayerManager.getInstance().getMinetopiaPlayer(source).whenComplete((minetopiaPlayer, throwable1) -> {
if (minetopiaPlayer == null) return;
MinetopiaPlayer minetopiaPlayer = PlayerManager.getInstance().getOnlineMinetopiaPlayer(source);

if (!minetopiaPlayer.isInPlace()) return;
if (minetopiaPlayer.isStaffchatEnabled()) return;
if (minetopiaPlayer == null) return;

PoliceModule policeModule = OpenMinetopia.getModuleManager().getModule(PoliceModule.class);
if (policeModule.getWalkieTalkieManager().isPoliceChatEnabled(source)
|| policeModule.getWalkieTalkieManager().isComposingMessage(source)) return;
DefaultConfiguration configuration = OpenMinetopia.getDefaultConfiguration();

List<Player> recipients = new ArrayList<>();
if (!configuration.isChatEnabled()) return;
if (!minetopiaPlayer.isInPlace()) return;
if (minetopiaPlayer.isStaffchatEnabled()) return;

event.setCancelled(true);
PoliceModule policeModule = OpenMinetopia.getModuleManager().getModule(PoliceModule.class);
if (policeModule.getWalkieTalkieManager().isPoliceChatEnabled(source)
|| policeModule.getWalkieTalkieManager().isComposingMessage(source)) return;

DefaultConfiguration configuration = OpenMinetopia.getDefaultConfiguration();
event.setCancelled(true);

List<Player> recipients = new ArrayList<>();

if (configuration.isChatRadiusEnabled()) {
Bukkit.getServer().getOnlinePlayers().forEach(target -> {
if (target.getWorld().equals(source.getWorld())
&& source.getLocation().distance(target.getLocation()) <= configuration.getChatRadiusRange())
Expand All @@ -51,42 +55,44 @@ public void playerChat(AsyncChatEvent event) {
return;
}
recipients.add(source);

// Format the message
String originalMessage = ChatUtils.rawMiniMessage(event.message());
String formattedMessage = configuration.getChatFormat();

SpyUtils.chatSpy(source, originalMessage, recipients);

// Replace <message> placeholder with original message
String finalMessage = formattedMessage.replace("<message>", originalMessage);

// Check if the player is wearing a balaclava and replace placeholders with default values
if (BalaclavaUtils.isBalaclavaItem(source.getInventory().getHelmet())) {
finalMessage = finalMessage
.replace("<level>", configuration.getDefaultLevel() + "")
.replace("<prefix>", configuration.getDefaultPrefix())
.replace("<name_color>", configuration.getDefaultNameColor())
.replace("<level_color>", configuration.getDefaultLevelColor())
.replace("<prefix_color>", configuration.getDefaultPrefixColor())
.replace("<chat_color>", configuration.getDefaultChatColor());
} else {
recipients.addAll(Bukkit.getOnlinePlayers());
}

// Format the message
String originalMessage = ChatUtils.rawMiniMessage(event.message());
String formattedMessage = configuration.getChatFormat();

SpyUtils.chatSpy(source, originalMessage, recipients);

// Replace <message> placeholder with original message
String finalMessage = formattedMessage.replace("<message>", originalMessage);

// Check if the player is wearing a balaclava and replace placeholders with default values
if (BalaclavaUtils.isBalaclavaItem(source.getInventory().getHelmet())) {
finalMessage = finalMessage
.replace("<level>", configuration.getDefaultLevel() + "")
.replace("<prefix>", configuration.getDefaultPrefix())
.replace("<name_color>", configuration.getDefaultNameColor())
.replace("<level_color>", configuration.getDefaultLevelColor())
.replace("<prefix_color>", configuration.getDefaultPrefixColor())
.replace("<chat_color>", configuration.getDefaultChatColor());
}

Bukkit.getConsoleSender().sendMessage(ChatUtils.format(minetopiaPlayer, finalMessage)); // Log the message without potential scrambled name

for (Player target : recipients) {
// Check if the target's name is in the original message and highlight it
if (originalMessage.contains(target.getName())) {
String highlightedMessage = originalMessage.replace(target.getName(), "<green>" + target.getName() + "<white>");
finalMessage = formattedMessage.replace("<message>", highlightedMessage);

// Play sound for the mentioned target
target.playSound(target.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
}

Bukkit.getConsoleSender().sendMessage(ChatUtils.format(minetopiaPlayer, finalMessage)); // Log the message without potential scrambled name

for (Player target : recipients) {
// Check if the target's name is in the original message and highlight it
if (originalMessage.contains(target.getName())) {
String highlightedMessage = originalMessage.replace(target.getName(), "<green>" + target.getName() + "<white>");
finalMessage = formattedMessage.replace("<message>", highlightedMessage);

// Play sound for the mentioned target
target.playSound(target.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
}

// Send the formatted message to the target
target.sendMessage(ChatUtils.format(minetopiaPlayer, finalMessage));
}
});
// Send the formatted message to the target
target.sendMessage(ChatUtils.format(minetopiaPlayer, finalMessage));
}
}
}

0 comments on commit ae58c11

Please sign in to comment.