Untitled

mail@pastecode.io avatar
unknown
java
8 months ago
6.8 kB
7
Indexable
Never
package com.trexmine.chat.listeners;

import com.trexmine.chat.configuration.Configuration;
import com.trexmine.chat.utils.Utils;
import com.trexmine.chat.utils.VanishCheck;
import me.clip.placeholderapi.PlaceholderAPI;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.cacheddata.CachedMetaData;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

import java.util.Collection;

public final class AsyncPlayerChatListener implements Listener {

    private static final LuckPerms LP = Bukkit.getServicesManager().getRegistration(LuckPerms.class).getProvider();
    private final boolean placeholderApiEnabled = Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");

    @EventHandler(priority = EventPriority.LOWEST)
    public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {

        Player player = event.getPlayer();
        String message = event.getMessage();

//        if (player.hasPermission("chat.color")) {
//            message = color(message);
//        }

//        if (player.hasPermission("chat.format")) {
//            message = format(message);
//        }

        if (player.hasPermission("chat.emoji")) {

            String lastColor = ChatColor.getLastColors(message);

            if (lastColor.isEmpty()) {
                lastColor = Configuration.DEFAULT_MESSAGE_COLOR;
            }

            message = Configuration.DEFAULT_MESSAGE_COLOR + message
                    .replace("<3", ChatColor.RED + "❤" + lastColor)
                    .replace("o/", ChatColor.LIGHT_PURPLE + "( ゚◡゚)/" + lastColor)
                    .replace("O_O", ChatColor.BLUE + "◎" + ChatColor.YELLOW + "_" + ChatColor.BLUE + "◎" + lastColor)
                    .replace("h/", ChatColor.GOLD + "ヽ(^◇^*)/" + lastColor)
                    .replace(":gg:", ChatColor.GOLD + "" + ChatColor.BOLD + "GG" + lastColor)
                    .replace("^-^", ChatColor.GREEN + "^-^" + lastColor)
                    .replace(":gimme:", ChatColor.AQUA + "༼つ ◕_◕ ༽つ" + lastColor)
                    .replace(":shrug:", ChatColor.YELLOW + "¯\\_(ツ)_/¯" + lastColor)
                    .replace(":tableflip:", ChatColor.RED + "(╯°□°)╯" + ChatColor.GRAY + "︵ ┻━┻" + lastColor)
                    .replace(":yey:", ChatColor.GREEN + "ヽ (◕◡◕) ノ" + lastColor);
        }

        Collection<? extends Player> players = Bukkit.getOnlinePlayers();

        for (Player eachPlayer : players) {
            if (eachPlayer != player && !VanishCheck.isVanished(eachPlayer)) {

                String name = eachPlayer.getName();

                if (message.contains(name)) {

                    String lastColor = ChatColor.getLastColors(message);

                    if (lastColor.isEmpty()) {
                        lastColor = Configuration.DEFAULT_MESSAGE_COLOR;
                    }

                    message = message.replace(name, ChatColor.GREEN + name + lastColor);
                    eachPlayer.playSound(eachPlayer.getLocation(),
                            Sound.valueOf(Utils.MC_VERSION > 12 ? "BLOCK_NOTE_BLOCK_PLING" : "NOTE_PLING"),
                            1F, 1F);
                }
            }
        }

        if (Configuration.WORLD_WHITELIST_ENABLED && !Configuration.WORLD_WHITELIST_WORLDS.contains(player.getWorld().getName())) {
            event.setMessage(message);
            return;
        }
//
//        String group = loadUser(player).getPrimaryGroup();
//        String format = Configuration.GROUP_FORMATS.getOrDefault(group, Configuration.CHAT_FORMAT)
//                .replace("{name}", player.getName())
//                .replace("{world}", player.getWorld().getName())
//                .replace("{prefix}", getPrefix(player))
//                .replace("{suffix}", getSuffix(player))
//                .replace("{username-color}", getUsernameColor(player, group))
//                .replace("{message-color}", getMessageColor(player, group));
//
//        format = Utils.color(placeholderApiEnabled ? PlaceholderAPI.setPlaceholders(player, format) : format);
//
//        event.setFormat(format.replace("{message}", message).replace("%", "%%"));
    }

    private String color(String text) {

        char[] array = text.toCharArray();

        for (int i = 0; i < array.length - 1; i++) {
            if (array[i] == '&' && "0123456789AaBbCcDdEeFfRr".indexOf(array[i + 1]) != -1) {
                array[i] = 167;
                array[i + 1] = Character.toLowerCase(array[i + 1]);
            }
        }

        return new String(array);
    }

    private String format(String text) {

        char[] array = text.toCharArray();

        for (int i = 0; i < array.length - 1; i++) {
            if (array[i] == '&' && "LlMmNnOoKkRr".indexOf(array[i + 1]) != -1) {
                array[i] = 167;
                array[i + 1] = Character.toLowerCase(array[i + 1]);
            }
        }

        return new String(array);
    }

    private String getPrefix(Player player) {
        String prefix = getPlayerMeta(player).getPrefix();
        return prefix != null ? prefix : "";
    }

    private String getSuffix(Player player) {
        String suffix = getPlayerMeta(player).getSuffix();
        return suffix != null ? suffix : "";
    }

    private String getUsernameColor(Player player, String group) {
        String playerColor = getPlayerMeta(player).getMetaValue("username-color");
        if (playerColor != null) return playerColor;
        String rankColor = getGroupMeta(group).getMetaValue("username-color");
        return rankColor != null ? rankColor : "";
    }

    private String getMessageColor(Player player, String group) {
        String playerColor = getPlayerMeta(player).getMetaValue("message-color");
        if (playerColor != null) return playerColor;
        String rankColor = getGroupMeta(group).getMetaValue("message-color");
        return rankColor != null ? rankColor : "";
    }

    private CachedMetaData getPlayerMeta(Player player) {
        return loadUser(player).getCachedData().getMetaData(LP.getContextManager().getQueryOptions(player));
    }

    private CachedMetaData getGroupMeta(final String group) {
        return loadGroup(group).getCachedData().getMetaData(LP.getContextManager().getStaticQueryOptions());
    }

    private User loadUser(Player player) {
        return LP.getUserManager().getUser(player.getUniqueId());
    }

    private Group loadGroup(String group) {
        return LP.getGroupManager().getGroup(group);
    }
}
Leave a Comment