FS-396: Check if chat event is cancelled before pinging

This commit is contained in:
spacerocket62 2021-07-31 02:40:35 -07:00
parent 62529a6171
commit f8c305a23c

View File

@ -18,35 +18,28 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import static me.totalfreedom.totalfreedommod.util.FUtil.playerMsg; import static me.totalfreedom.totalfreedommod.util.FUtil.playerMsg;
public class ChatManager extends FreedomService public class ChatManager extends FreedomService {
{
@Override @Override
public void onStart() public void onStart() {
{
} }
@Override @Override
public void onStop() public void onStop() {
{
} }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerChatFormat(AsyncPlayerChatEvent event) public void onPlayerChatFormat(AsyncPlayerChatEvent event) {
{ try {
try
{
handleChatEvent(event); handleChatEvent(event);
} } catch (Exception ex) {
catch (Exception ex)
{
FLog.severe(ex); FLog.severe(ex);
} }
} }
private void handleChatEvent(AsyncPlayerChatEvent event) private void handleChatEvent(AsyncPlayerChatEvent event) {
{
final Player player = event.getPlayer(); final Player player = event.getPlayer();
String message = event.getMessage().trim(); String message = event.getMessage().trim();
@ -54,8 +47,7 @@ public class ChatManager extends FreedomService
message = FUtil.colorize(message); message = FUtil.colorize(message);
message = message.replaceAll(ChatColor.MAGIC.toString(), "&k"); message = message.replaceAll(ChatColor.MAGIC.toString(), "&k");
if (ConfigEntry.SHOP_ENABLED.getBoolean() && ConfigEntry.SHOP_REACTIONS_ENABLED.getBoolean() && !plugin.sh.reactionString.isEmpty() && message.equals(plugin.sh.reactionString)) if (ConfigEntry.SHOP_ENABLED.getBoolean() && ConfigEntry.SHOP_REACTIONS_ENABLED.getBoolean() && !plugin.sh.reactionString.isEmpty() && message.equals(plugin.sh.reactionString)) {
{
event.setCancelled(true); event.setCancelled(true);
PlayerData data = plugin.pl.getData(player); PlayerData data = plugin.pl.getData(player);
data.setCoins(data.getCoins() + plugin.sh.coinsPerReactionWin); data.setCoins(data.getCoins() + plugin.sh.coinsPerReactionWin);
@ -65,31 +57,27 @@ public class ChatManager extends FreedomService
return; return;
} }
if (!ConfigEntry.TOGGLE_CHAT.getBoolean() && !plugin.al.isAdmin(player)) if (!ConfigEntry.TOGGLE_CHAT.getBoolean() && !plugin.al.isAdmin(player)) {
{
event.setCancelled(true); event.setCancelled(true);
playerMsg(player, "Chat is currently disabled.", org.bukkit.ChatColor.RED); playerMsg(player, "Chat is currently disabled.", org.bukkit.ChatColor.RED);
return; return;
} }
// Truncate messages that are too long - 256 characters is vanilla client max // Truncate messages that are too long - 256 characters is vanilla client max
if (message.length() > 256) if (message.length() > 256) {
{
message = message.substring(0, 256); message = message.substring(0, 256);
FSync.playerMsg(player, "Message was shortened because it was too long to send."); FSync.playerMsg(player, "Message was shortened because it was too long to send.");
} }
final FPlayer fPlayer = plugin.pl.getPlayerSync(player); final FPlayer fPlayer = plugin.pl.getPlayerSync(player);
if (fPlayer.isLockedUp()) if (fPlayer.isLockedUp()) {
{
FSync.playerMsg(player, "You're locked up and cannot talk."); FSync.playerMsg(player, "You're locked up and cannot talk.");
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
// Check for adminchat // Check for adminchat
if (fPlayer.inAdminChat()) if (fPlayer.inAdminChat()) {
{
FSync.adminChatMessage(player, message); FSync.adminChatMessage(player, message);
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -98,14 +86,10 @@ public class ChatManager extends FreedomService
// Check for 4chan trigger // Check for 4chan trigger
boolean green = ChatColor.stripColor(message).toLowerCase().startsWith(">"); boolean green = ChatColor.stripColor(message).toLowerCase().startsWith(">");
boolean orange = ChatColor.stripColor(message).toLowerCase().endsWith("<"); boolean orange = ChatColor.stripColor(message).toLowerCase().endsWith("<");
if (ConfigEntry.FOURCHAN_ENABLED.getBoolean()) if (ConfigEntry.FOURCHAN_ENABLED.getBoolean()) {
{ if (green) {
if (green)
{
message = ChatColor.GREEN + message; message = ChatColor.GREEN + message;
} } else if (orange) {
else if (orange)
{
message = ChatColor.GOLD + message; message = ChatColor.GOLD + message;
} }
} }
@ -117,18 +101,17 @@ public class ChatManager extends FreedomService
String format = "%1$s §8\u00BB §f%2$s"; String format = "%1$s §8\u00BB §f%2$s";
String tag = fPlayer.getTag(); String tag = fPlayer.getTag();
if (tag != null && !tag.isEmpty()) if (tag != null && !tag.isEmpty()) {
{
format = tag.replace("%", "%%") + " " + format; format = tag.replace("%", "%%") + " " + format;
} }
// Check for mentions // Check for mentions
boolean mentionEveryone = ChatColor.stripColor(message).toLowerCase().contains("@everyone") && plugin.al.isAdmin(player); if (!event.isCancelled()) {
for (Player p : server.getOnlinePlayers()) boolean mentionEveryone = ChatColor.stripColor(message).toLowerCase().contains("@everyone") && plugin.al.isAdmin(player);
{ for (Player p : server.getOnlinePlayers()) {
if (ChatColor.stripColor(message).toLowerCase().contains("@" + p.getName().toLowerCase()) || mentionEveryone) if (ChatColor.stripColor(message).toLowerCase().contains("@" + p.getName().toLowerCase()) || mentionEveryone) {
{ p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, SoundCategory.MASTER, 1337F, 0.9F);
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, SoundCategory.MASTER, 1337F, 0.9F); }
} }
} }
@ -136,25 +119,21 @@ public class ChatManager extends FreedomService
event.setFormat(format); event.setFormat(format);
// Send to discord // Send to discord
if (!ConfigEntry.ADMIN_ONLY_MODE.getBoolean() && !Bukkit.hasWhitelist() && !plugin.pl.getPlayer(player).isMuted() && !plugin.tfg.inGuildChat(player)) if (!ConfigEntry.ADMIN_ONLY_MODE.getBoolean() && !Bukkit.hasWhitelist() && !plugin.pl.getPlayer(player).isMuted() && !plugin.tfg.inGuildChat(player)) {
{
plugin.dc.messageChatChannel(player.getName() + " \u00BB " + ChatColor.stripColor(message)); plugin.dc.messageChatChannel(player.getName() + " \u00BB " + ChatColor.stripColor(message));
} }
} }
public ChatColor getColor(Displayable display) public ChatColor getColor(Displayable display) {
{
return display.getColor(); return display.getColor();
} }
public String getColoredTag(Displayable display) public String getColoredTag(Displayable display) {
{
ChatColor color = display.getColor(); ChatColor color = display.getColor();
return color + display.getAbbr(); return color + display.getAbbr();
} }
public void adminChat(CommandSender sender, String message) public void adminChat(CommandSender sender, String message) {
{
Displayable display = plugin.rm.getDisplay(sender); Displayable display = plugin.rm.getDisplay(sender);
FLog.info("[ADMIN] " + sender.getName() + " " + display.getTag() + ": " + message, true); FLog.info("[ADMIN] " + sender.getName() + " " + display.getTag() + ": " + message, true);
plugin.dc.messageAdminChatChannel(sender.getName() + " \u00BB " + message); plugin.dc.messageAdminChatChannel(sender.getName() + " \u00BB " + message);
@ -167,20 +146,15 @@ public class ChatManager extends FreedomService
ChatColor color = getColor(display); ChatColor color = getColor(display);
String msg = format.replace("%name%", sender.getName()).replace("%rank%", display.getAbbr()).replace("%rankcolor%", color.toString()).replace("%msg%", message); String msg = format.replace("%name%", sender.getName()).replace("%rank%", display.getAbbr()).replace("%rankcolor%", color.toString()).replace("%msg%", message);
player.sendMessage(FUtil.colorize(msg)); player.sendMessage(FUtil.colorize(msg));
} } else {
else
{
player.sendMessage("[" + ChatColor.AQUA + "ADMIN" + ChatColor.WHITE + "] " + ChatColor.DARK_RED + sender.getName() + ChatColor.DARK_GRAY + " [" + getColoredTag(display) + ChatColor.DARK_GRAY + "]" + ChatColor.WHITE + ": " + ChatColor.GOLD + FUtil.colorize(message)); player.sendMessage("[" + ChatColor.AQUA + "ADMIN" + ChatColor.WHITE + "] " + ChatColor.DARK_RED + sender.getName() + ChatColor.DARK_GRAY + " [" + getColoredTag(display) + ChatColor.DARK_GRAY + "]" + ChatColor.WHITE + ": " + ChatColor.GOLD + FUtil.colorize(message));
} }
}); });
} }
public void reportAction(Player reporter, Player reported, String report) public void reportAction(Player reporter, Player reported, String report) {
{ for (Player player : server.getOnlinePlayers()) {
for (Player player : server.getOnlinePlayers()) if (plugin.al.isAdmin(player)) {
{
if (plugin.al.isAdmin(player))
{
playerMsg(player, ChatColor.RED + "[REPORTS] " + ChatColor.GOLD + reporter.getName() + " has reported " + reported.getName() + " for " + report); playerMsg(player, ChatColor.RED + "[REPORTS] " + ChatColor.GOLD + reporter.getName() + " has reported " + reported.getName() + " for " + report);
} }
} }