package ru.rise.fwchat.listeners;
import ru.rise.fwchat.Main;
import org.bukkit.event.player.*;
import net.md_5.bungee.api.*;
import org.bukkit.entity.*;
import org.bukkit.event.*;
import com.nametagedit.plugin.api.NametagAPI;
import me.clip.placeholderapi.*;
public class PlayerListener implements Listener
{
public Main plugin;
public static String getNametag(Player p) {
return getNametag(p);
}
public PlayerListener(final Main plugin) {
this.plugin = plugin;
}
public PlayerListener() {
}
@EventHandler
public void onPlayerjoin(PlayerJoinEvent e) {
Player p = e.getPlayer();
p.setDisplayName(NametagAPI.getNametag(p.getName()));
}
@EventHandler
public void onChat(final AsyncPlayerChatEvent e) {
final Player p = e.getPlayer();
final String g = this.plugin.perms.getPrimaryGroup(p);
if (p.hasPermission("chat.color")) {
e.setMessage(ChatColor.translateAlternateColorCodes('&', e.getMessage()));
}
String format = "";
if (this.plugin.getConfig().getConfigurationSection("groups." + g) != null) {
format = this.plugin.getConfig().getString("groups." + g + ".format");
}
else {
format = this.plugin.getConfig().getString("default_format");
}
format = format.replace("{PLAYER_NAME}", p.getName());
format = format.replace("{DISPLAY_NAME}", p.getDisplayName());
format = this.replacePlaceholderAPI(p, format);
format = this.replaceVault(p, format);
format = ChatColor.translateAlternateColorCodes('&', format);
format = format.replace("%", "%%");
format = format.replace("{MESSAGE}", "%2$s");
e.setFormat(format);
}
public String replaceVault(final Player p, final String message) {
String rank = null;
String prefix = null;
String suffix = null;
prefix = this.plugin.chat.getPlayerPrefix(p);
suffix = this.plugin.chat.getPlayerSuffix(p);
rank = this.plugin.perms.getPrimaryGroup(p);
String holders = message.replace("{PREFIX}", prefix);
holders = holders.replace("{SUFFIX}", suffix);
holders = holders.replace("{RANK}", rank);
return holders;
}
@SuppressWarnings("deprecation")
public String replacePlaceholderAPI(final Player p, final String message) {
String holders = message;
if (this.plugin.placeholders && PlaceholderAPI.containsPlaceholders(holders)) {
holders = PlaceholderAPI.setPlaceholders(p, holders);
}
return holders;
}
}