package me.totalfreedom.totalfreedommod; import me.totalfreedom.totalfreedommod.player.FPlayer; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FSync; import net.pravian.aero.component.service.AbstractService; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.player.AsyncPlayerChatEvent; public class ChatManager extends AbstractService { public ChatManager(TotalFreedomMod plugin) { super(plugin); } @Override protected void onStart() { } @Override protected void onStop() { } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onPlayerChatFormat(AsyncPlayerChatEvent event) { try { handleChatEvent(event); } catch (Exception ex) { FLog.severe(ex); } } private void handleChatEvent(AsyncPlayerChatEvent event) { final Player player = event.getPlayer(); String message = event.getMessage().trim(); final FPlayer playerdata = plugin.pl.getPlayerSync(player); // Strip color from messages message = ChatColor.stripColor(message); // Truncate messages that are too long - 100 characters is vanilla client max if (message.length() > 100) { message = message.substring(0, 100); FSync.playerMsg(player, "Message was shortened because it was too long to send."); } // Check for caps if (message.length() >= 6) { int caps = 0; for (char c : message.toCharArray()) { if (Character.isUpperCase(c)) { caps++; } } if (((float) caps / (float) message.length()) > 0.65) //Compute a ratio so that longer sentences can have more caps. { message = message.toLowerCase(); } } // Check for adminchat if (playerdata.inAdminChat()) { FSync.adminChatMessage(player, message, false); event.setCancelled(true); return; } // Finally, set message event.setMessage(message); // Set the tag if (playerdata.getTag() != null) { event.setFormat("<" + playerdata.getTag().replaceAll("%", "%%") + " %1$s> %2$s"); } } }