From e7e08c060eaaf53fbc865940c94156712cd73d53 Mon Sep 17 00:00:00 2001 From: Video Date: Sun, 13 Nov 2022 20:53:50 -0700 Subject: [PATCH] Attempts to resolve FS-311 and mitigates FS-396 --- .../totalfreedommod/ChatManager.java | 47 ++++++++----------- .../totalfreedommod/discord/Discord.java | 18 +++++++ .../totalfreedommod/shop/Shop.java | 20 ++++++++ 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java b/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java index fd8c3d8d..116a4beb 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/ChatManager.java @@ -4,13 +4,11 @@ import com.google.common.base.Strings; import me.totalfreedom.totalfreedommod.admin.Admin; import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.player.FPlayer; -import me.totalfreedom.totalfreedommod.player.PlayerData; import me.totalfreedom.totalfreedommod.rank.Displayable; import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FSync; import me.totalfreedom.totalfreedommod.util.FUtil; import net.md_5.bungee.api.ChatColor; -import org.bukkit.Bukkit; import org.bukkit.Sound; import org.bukkit.SoundCategory; import org.bukkit.command.CommandSender; @@ -54,17 +52,6 @@ public class ChatManager extends FreedomService message = FUtil.colorize(message); 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)) - { - event.setCancelled(true); - PlayerData data = plugin.pl.getData(player); - data.setCoins(data.getCoins() + plugin.sh.coinsPerReactionWin); - plugin.pl.save(data); - plugin.sh.endReaction(player.getName()); - player.sendMessage(ChatColor.GREEN + "You have been given " + ChatColor.GOLD + plugin.sh.coinsPerReactionWin + ChatColor.GREEN + " coins!"); - return; - } - if (!ConfigEntry.TOGGLE_CHAT.getBoolean() && !plugin.al.isAdmin(player)) { event.setCancelled(true); @@ -96,10 +83,11 @@ public class ChatManager extends FreedomService } // Check for 4chan trigger - boolean green = ChatColor.stripColor(message).toLowerCase().startsWith(">"); - boolean orange = ChatColor.stripColor(message).toLowerCase().endsWith("<"); if (ConfigEntry.FOURCHAN_ENABLED.getBoolean()) { + boolean green = ChatColor.stripColor(message).toLowerCase().startsWith(">"); + boolean orange = ChatColor.stripColor(message).toLowerCase().endsWith("<"); + if (green) { message = ChatColor.GREEN + message; @@ -123,23 +111,27 @@ public class ChatManager extends FreedomService } // Check for mentions - boolean mentionEveryone = ChatColor.stripColor(message).toLowerCase().contains("@everyone") && plugin.al.isAdmin(player); - for (Player p : server.getOnlinePlayers()) + String stripped = ChatColor.stripColor(message).toLowerCase(); + + /* There is an issue would have allowed muted players to ping players. The issue is caused by the order in which + * these event handlers are registered when the plugin starts up. Muter is registered after the ChatManager, + * which results in latter being called first (before the former can cancel it). EventPriority does not seem to + * make a difference. As a short-term solution I've added this mute check alongside the usual isCancelled check + * so that the issue is mitigated, but a long-term solution should be to change the order in which things like + * ChatManager and Muter are registered. */ + if (!event.isCancelled() && !plugin.pl.getPlayer(player).isMuted()) { - if (ChatColor.stripColor(message).toLowerCase().contains("@" + p.getName().toLowerCase()) || mentionEveryone) + server.getOnlinePlayers().forEach(pl -> { - p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, SoundCategory.MASTER, 1337F, 0.9F); - } + if (stripped.contains("@" + pl.getName()) || (plugin.al.isAdmin(player) && stripped.contains("@everyone"))) + { + pl.playSound(pl.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, SoundCategory.MASTER, 1337F, 0.9F); + } + }); } // Set format event.setFormat(format); - - // Send to discord - if (!ConfigEntry.ADMIN_ONLY_MODE.getBoolean() && !Bukkit.hasWhitelist() && !plugin.pl.getPlayer(player).isMuted()) - { - plugin.dc.messageChatChannel(player.getName() + " \u00BB " + ChatColor.stripColor(message)); - } } public ChatColor getColor(Displayable display) @@ -162,7 +154,8 @@ public class ChatManager extends FreedomService server.getOnlinePlayers().stream().filter(player -> plugin.al.isAdmin(player)).forEach(player -> { Admin admin = plugin.al.getAdmin(player); - if (!Strings.isNullOrEmpty(admin.getAcFormat())) { + if (!Strings.isNullOrEmpty(admin.getAcFormat())) + { String format = admin.getAcFormat(); ChatColor color = getColor(display); String msg = format.replace("%name%", sender.getName()).replace("%rank%", display.getAbbr()).replace("%rankcolor%", color.toString()).replace("%msg%", message); diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java index 248d6090..87e0130a 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java @@ -39,6 +39,7 @@ import net.dv8tion.jda.api.utils.MemberCachePolicy; import net.dv8tion.jda.internal.utils.concurrent.CountingThreadFactory; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import net.md_5.bungee.api.ChatColor; import org.apache.commons.lang.WordUtils; import org.apache.commons.lang3.RandomStringUtils; import org.bukkit.GameRule; @@ -47,6 +48,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.jetbrains.annotations.NotNull; @@ -195,15 +197,18 @@ public class Discord extends FreedomService catch (LoginException e) { FLog.warning("An invalid token for Discord integration was provided, the bot will not enable."); + enabled = false; } catch (IllegalArgumentException e) { FLog.warning("Discord integration failed to start."); + enabled = false; } catch (NoClassDefFoundError e) { FLog.warning("The JDA plugin is not installed, therefore the discord bot cannot start."); FLog.warning("To resolve this error, please download the latest JDA from: https://github.com/AtlasMediaGroup/Minecraft-JDA/releases"); + enabled = false; } } @@ -534,4 +539,17 @@ public class Discord extends FreedomService messageChatChannel("**Server has started**", true); } } + + @EventHandler(ignoreCancelled = true) + public void onAsyncPlayerChat(AsyncPlayerChatEvent event) + { + Player player = event.getPlayer(); + String message = event.getMessage(); + + if (!ConfigEntry.ADMIN_ONLY_MODE.getBoolean() && !server.hasWhitelist() + && !plugin.pl.getPlayer(player).isMuted() && bot != null) + { + messageChatChannel(player.getName() + " \u00BB " + ChatColor.stripColor(message)); + } + } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java b/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java index 5eebbb2d..f12fb04d 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/shop/Shop.java @@ -21,6 +21,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; @@ -411,6 +412,25 @@ public class Shop extends FreedomService } + @EventHandler + public void onPlayerChat(AsyncPlayerChatEvent event) + { + String message = event.getMessage(); + Player player = event.getPlayer(); + + if (ConfigEntry.SHOP_ENABLED.getBoolean() && ConfigEntry.SHOP_REACTIONS_ENABLED.getBoolean() + && !plugin.sh.reactionString.isEmpty() && message.equals(plugin.sh.reactionString)) + { + event.setCancelled(true); + PlayerData data = plugin.pl.getData(player); + data.setCoins(data.getCoins() + plugin.sh.coinsPerReactionWin); + plugin.pl.save(data); + plugin.sh.endReaction(player.getName()); + player.sendMessage(ChatColor.GREEN + "You have been given " + ChatColor.GOLD + + plugin.sh.coinsPerReactionWin + ChatColor.GREEN + " coins!"); + } + } + public ShopItem getShopItem(int slot) { for (ShopItem shopItem : ShopItem.values())