2015-11-15 23:32:04 +00:00
|
|
|
package me.totalfreedom.totalfreedommod;
|
|
|
|
|
|
|
|
import me.totalfreedom.totalfreedommod.player.FPlayer;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
|
|
|
import me.totalfreedom.totalfreedommod.util.FSync;
|
2016-05-12 19:40:39 +00:00
|
|
|
import static me.totalfreedom.totalfreedommod.util.FUtil.playerMsg;
|
2018-03-18 08:32:50 +00:00
|
|
|
import org.bukkit.Sound;
|
|
|
|
import org.bukkit.SoundCategory;
|
2015-11-15 23:32:04 +00:00
|
|
|
import org.bukkit.ChatColor;
|
2016-05-12 19:40:39 +00:00
|
|
|
import org.bukkit.command.CommandSender;
|
2015-11-15 23:32:04 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
|
|
|
|
2016-03-01 16:47:01 +00:00
|
|
|
public class ChatManager extends FreedomService
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Strip color from messages
|
|
|
|
message = ChatColor.stripColor(message);
|
|
|
|
|
2017-06-30 07:41:01 +00:00
|
|
|
// Truncate messages that are too long - 256 characters is vanilla client max
|
|
|
|
if (message.length() > 256)
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2017-06-30 07:41:01 +00:00
|
|
|
message = message.substring(0, 256);
|
2015-11-15 23:32:04 +00:00
|
|
|
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.
|
|
|
|
{
|
2018-03-18 08:32:50 +00:00
|
|
|
if (!plugin.al.isAdmin(player))
|
|
|
|
{
|
|
|
|
message = message.toLowerCase();
|
|
|
|
}
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for adminchat
|
2016-03-07 20:32:05 +00:00
|
|
|
final FPlayer fPlayer = plugin.pl.getPlayerSync(player);
|
|
|
|
if (fPlayer.inAdminChat())
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-03-07 20:32:05 +00:00
|
|
|
FSync.adminChatMessage(player, message);
|
2015-11-15 23:32:04 +00:00
|
|
|
event.setCancelled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-18 08:32:50 +00:00
|
|
|
// Check for mentions
|
|
|
|
checkMentions(message);
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
// Finally, set message
|
|
|
|
event.setMessage(message);
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
// Make format
|
|
|
|
String format = "<%1$s> %2$s";
|
|
|
|
|
|
|
|
String tag = fPlayer.getTag();
|
|
|
|
if (tag != null && !tag.isEmpty())
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
format = tag.replace("%", "%%") + " " + format;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set format
|
|
|
|
event.setFormat(format);
|
|
|
|
}
|
|
|
|
|
2018-03-18 08:32:50 +00:00
|
|
|
public void checkMentions(String message)
|
|
|
|
{
|
|
|
|
checkMentions(message, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkMentions(String message, boolean adminOnly)
|
|
|
|
{
|
|
|
|
for (Player player : server.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
// This is so if admins for some reason mention non-admins in admin chat, they won't be notified
|
|
|
|
if (adminOnly && !plugin.al.isAdmin(player))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ChatColor.stripColor(message).toLowerCase().contains("@" + player.getName().toLowerCase()))
|
|
|
|
{
|
|
|
|
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_PLING, SoundCategory.PLAYERS, 100F, 0.9F);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public void adminChat(CommandSender sender, String message)
|
|
|
|
{
|
|
|
|
String name = sender.getName() + " " + plugin.rm.getDisplay(sender).getColoredTag() + ChatColor.WHITE;
|
|
|
|
FLog.info("[ADMIN] " + name + ": " + message);
|
2018-03-18 08:32:50 +00:00
|
|
|
checkMentions(message, true);
|
2016-05-12 19:40:39 +00:00
|
|
|
|
|
|
|
for (Player player : server.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
if (plugin.al.isAdmin(player))
|
|
|
|
{
|
|
|
|
player.sendMessage("[" + ChatColor.AQUA + "ADMIN" + ChatColor.WHITE + "] " + ChatColor.DARK_RED + name + ": " + ChatColor.GOLD + message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reportAction(Player reporter, Player reported, String report)
|
|
|
|
{
|
|
|
|
for (Player player : server.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
if (plugin.al.isAdmin(player))
|
|
|
|
{
|
|
|
|
playerMsg(player, ChatColor.RED + "[REPORTS] " + ChatColor.GOLD + reporter.getName() + " has reported " + reported.getName() + " for " + report);
|
|
|
|
}
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|