From 55f6ace46cd4ca1c57e2957758f930e43e499dbd Mon Sep 17 00:00:00 2001 From: xGinko Date: Tue, 5 Mar 2024 14:42:41 +0100 Subject: [PATCH] improve other modules in misc category --- .../modules/misc/FirstJoinMessages.java | 87 ++++++++++--------- .../modules/misc/PreventMessageKick.java | 13 +-- .../modules/misc/FirstJoinMessages.java | 56 +++++------- .../modules/misc/PreventMessageKick.java | 13 +-- 4 files changed, 87 insertions(+), 82 deletions(-) diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java index 5a5c52ea0..09e70d51c 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java @@ -16,7 +16,7 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; -import java.util.HashSet; +import java.util.HashMap; import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; @@ -24,19 +24,23 @@ public class FirstJoinMessages implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; private final Config config; + private final HashMap formats = new HashMap<>(); + private final AtomicInteger uniquePlayerCount = new AtomicInteger(0); private final boolean logFirstJoin; - private final HashSet availableTranslations = new HashSet<>(); - private final AtomicInteger totalPlayers = new AtomicInteger(0); public FirstJoinMessages() { shouldEnable(); this.config = AnarchyExploitFixes.getConfiguration(); - config.addComment("misc.join-leave-messages.first-join-messages.enable", "Configure message in lang folder."); + config.addComment("misc.join-leave-messages.first-join-messages.enable", + "Configure messages in lang folder."); this.plugin = AnarchyExploitFixes.getInstance(); // prevent lag by getting total player number once and then counting up manually, as always getting all players is resource intense - plugin.getServer().getGlobalRegionScheduler().run(plugin, getTotalPlayers -> totalPlayers.set(plugin.getServer().getOfflinePlayers().length)); + plugin.getServer().getGlobalRegionScheduler().run(plugin, + getCount -> uniquePlayerCount.set(plugin.getServer().getOfflinePlayers().length)); this.logFirstJoin = config.getBoolean("misc.join-leave-messages.first-join-messages.show-in-console", true); - AnarchyExploitFixes.getLanguageCacheMap().keySet().forEach(localeString -> this.availableTranslations.add(Locale.forLanguageTag(localeString.replace("_", "-")))); + AnarchyExploitFixes.getLanguageCacheMap().keySet().stream() + .map(availableLang -> Locale.forLanguageTag(availableLang.replace("_", "-"))) + .forEach(locale -> formats.put(locale, new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.ORDINAL))); } @Override @@ -65,53 +69,56 @@ public void disable() { } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerJoinEvent(PlayerJoinEvent event) { - Player joiningPlayer = event.getPlayer(); + private void onPlayerJoin(PlayerJoinEvent event) { + final Player joiningPlayer = event.getPlayer(); if (joiningPlayer.hasPlayedBefore()) return; if (joiningPlayer.hasPermission("anarchyexploitfixes.silentJoin")) return; - int joiningPlayersNumber = totalPlayers.incrementAndGet(); - - for (Player onlinePlayer : plugin.getServer().getOnlinePlayers()) { - final boolean showConnects; - PersistentDataContainer dataContainer = onlinePlayer.getPersistentDataContainer(); - if (!dataContainer.has(NamespacedKeys.CONNECT_MSG_TOGGLE.key())) { - showConnects = config.connectionMsgsAreOnByDefault; - } else { - showConnects = dataContainer.get(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN); - } - - if (showConnects) { - sendFirstJoinMessage(onlinePlayer, joiningPlayer, joiningPlayersNumber); - } + final int joiningPlayersNumber = uniquePlayerCount.incrementAndGet(); + + for (final Player onlinePlayer : plugin.getServer().getOnlinePlayers()) { + onlinePlayer.getScheduler().execute(plugin, () -> { + final PersistentDataContainer dataContainer = onlinePlayer.getPersistentDataContainer(); + if ( + !dataContainer.has(NamespacedKeys.CONNECT_MSG_TOGGLE.key()) ? config.connectionMsgsAreOnByDefault : + dataContainer.get(NamespacedKeys.CONNECT_MSG_TOGGLE.key(), PersistentDataType.BOOLEAN) + ) { + for (Component line : AnarchyExploitFixes.getLang(onlinePlayer.locale()).misc_firstJoinMessage) { + onlinePlayer.sendMessage(line + .replaceText(TextReplacementConfig.builder() + .matchLiteral("%player%") + .replacement(joiningPlayer.name()) + .build()) + .replaceText(TextReplacementConfig.builder() + .matchLiteral("%players_num%") + .replacement(formatToOrdinal(onlinePlayer.locale(), joiningPlayersNumber)) + .build())); + } + } + }, null, 1L); } if (logFirstJoin) { - // Send to ConsoleSender so we can display colors for (Component line : AnarchyExploitFixes.getLang(joiningPlayer.locale()).misc_firstJoinMessage) { - plugin.getServer().getConsoleSender().sendMessage(line - .replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(joiningPlayer.name()).build()) - .replaceText(TextReplacementConfig.builder().matchLiteral("%players_num%").replacement(formatToOrdinal(joiningPlayer.locale(), joiningPlayersNumber)).build()) - .append(Component.text(" (" + joiningPlayer.locale() + ")")) - ); + plugin.getComponentLogger().info(line + .replaceText(TextReplacementConfig.builder() + .matchLiteral("%player%") + .replacement(joiningPlayer.name()) + .build()) + .replaceText(TextReplacementConfig.builder() + .matchLiteral("%players_num%") + .replacement(formatToOrdinal(joiningPlayer.locale(), joiningPlayersNumber)) + .build()) + .append(Component.text(" (" + joiningPlayer.locale() + ")"))); } } } - private void sendFirstJoinMessage(Player receiver, Player joiningPlayer, int joiningPlayersNumber) { - for (Component line : AnarchyExploitFixes.getLang(receiver.locale()).misc_firstJoinMessage) { - receiver.sendMessage(line - .replaceText(TextReplacementConfig.builder().matchLiteral("%player%").replacement(joiningPlayer.name()).build()) - .replaceText(TextReplacementConfig.builder().matchLiteral("%players_num%").replacement(formatToOrdinal(receiver.locale(), joiningPlayersNumber)).build()) - ); - } - } - private String formatToOrdinal(Locale playerLocale, int number) { - if (config.auto_lang && availableTranslations.contains(playerLocale)) { - return new RuleBasedNumberFormat(playerLocale, RuleBasedNumberFormat.ORDINAL).format(number, "%digits-ordinal"); + if (config.auto_lang && formats.containsKey(playerLocale)) { + return formats.get(playerLocale).format(number, "%digits-ordinal"); } else { - return new RuleBasedNumberFormat(config.default_lang, RuleBasedNumberFormat.ORDINAL).format(number, "%digits-ordinal"); + return formats.get(config.default_lang).format(number, "%digits-ordinal"); } } } diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/PreventMessageKick.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/PreventMessageKick.java index a64ec0e21..07b8aca1b 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/PreventMessageKick.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/PreventMessageKick.java @@ -12,18 +12,21 @@ import java.util.HashSet; import java.util.List; +import java.util.stream.Collectors; public class PreventMessageKick implements AnarchyExploitFixesModule, Listener { - private final HashSet kickMessagesToListenTo = new HashSet<>(); + private final HashSet kickMessagesToListenTo; public PreventMessageKick() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("misc.kicks.prevent-message-kick.enable", "Cancels the kick for specific kick messages."); - config.getList("misc.kicks.prevent-message-kick.kick-messages-to-listen-to", - List.of("Kicked for spamming", "Stop spamming!") - ).forEach(configuredMsg -> this.kickMessagesToListenTo.add(configuredMsg.toLowerCase())); + this.kickMessagesToListenTo = config.getList("misc.kicks.prevent-message-kick.kick-messages-to-listen-to", + List.of("Kicked for spamming", "Stop spamming!")) + .stream() + .map(String::toLowerCase) + .collect(Collectors.toCollection(HashSet::new)); } @Override @@ -52,7 +55,7 @@ public void disable() { HandlerList.unregisterAll(this); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onPlayerKick(PlayerKickEvent event) { if (kickMessagesToListenTo.contains(PlainTextComponentSerializer.plainText().serialize(event.reason()).toLowerCase())) { event.setCancelled(true); diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java index 7b6b226dd..5d4ab5851 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/FirstJoinMessages.java @@ -11,7 +11,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; -import java.util.HashSet; +import java.util.HashMap; import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; @@ -19,19 +19,21 @@ public class FirstJoinMessages implements AnarchyExploitFixesModule, Listener { private final AnarchyExploitFixes plugin; private final Config config; + private final HashMap formats = new HashMap<>(); + private final AtomicInteger totalPlayers; private final boolean logFirstJoin; - private final HashSet availableTranslations = new HashSet<>(); - private final AtomicInteger totalPlayers = new AtomicInteger(0); public FirstJoinMessages() { shouldEnable(); + this.plugin = AnarchyExploitFixes.getInstance(); this.config = AnarchyExploitFixes.getConfiguration(); config.addComment("misc.join-leave-messages.first-join-messages.enable", "Configure message in lang folder."); - this.plugin = AnarchyExploitFixes.getInstance(); // prevent lag by getting total player number once and then counting up manually, as always getting all players is resource intense - plugin.getServer().getScheduler().runTask(plugin, () -> totalPlayers.set(plugin.getServer().getOfflinePlayers().length)); + this.totalPlayers = new AtomicInteger(plugin.getServer().getOfflinePlayers().length); this.logFirstJoin = config.getBoolean("misc.join-leave-messages.first-join-messages.show-in-console", true); - AnarchyExploitFixes.getLanguageCacheMap().keySet().forEach(localeString -> this.availableTranslations.add(Locale.forLanguageTag(localeString.replace("_", "-")))); + for (String availableLang : AnarchyExploitFixes.getLanguageCacheMap().keySet()) { + formats.put(availableLang, new RuleBasedNumberFormat(Locale.forLanguageTag(availableLang.replace("_", "-")), RuleBasedNumberFormat.ORDINAL)); + } } @Override @@ -54,49 +56,39 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("misc.join-leave-messages.first-join-messages.enable", false); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onPlayerJoinEvent(PlayerJoinEvent event) { - Player joiningPlayer = event.getPlayer(); + final Player joiningPlayer = event.getPlayer(); if (joiningPlayer.hasPlayedBefore()) return; if (joiningPlayer.hasPermission("anarchyexploitfixes.silentJoin")) return; - int joiningPlayersNumber = totalPlayers.incrementAndGet(); + final int joiningPlayersNumber = totalPlayers.incrementAndGet(); - for (Player onlinePlayer : plugin.getServer().getOnlinePlayers()) { - if ( - config.connectionMsgsAreOnByDefault && !AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId()) - || !config.connectionMsgsAreOnByDefault && AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId()) - ) { - sendFirstJoinMessage(onlinePlayer, joiningPlayer, joiningPlayersNumber); + for (final Player onlinePlayer : plugin.getServer().getOnlinePlayers()) { + if (config.connectionMsgsAreOnByDefault != AnarchyExploitFixes.CONNECT_MSG_TOGGLE.contains(onlinePlayer.getUniqueId())) { + for (String line : AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_firstJoinMessage) { + onlinePlayer.sendMessage(line + .replace("%player%", joiningPlayer.getName()) + .replace("%players_num%", formatToOrdinal(onlinePlayer.getLocale(), joiningPlayersNumber))); + } } } - // Use Bukkit.getLogger() here to avoid using the plugin's prefix + // Use Bukkit.getLogger() here so we can log without prefix if (logFirstJoin) { for (String line : AnarchyExploitFixes.getLang(joiningPlayer.getLocale()).misc_firstJoinMessage) { Bukkit.getLogger().info(line .replace("%player%", joiningPlayer.getName()) - .replace("%players_num%", formatToOrdinal(joiningPlayer.getLocale(), joiningPlayersNumber)) - ); + .replace("%players_num%", formatToOrdinal(joiningPlayer.getLocale(), joiningPlayersNumber))); } } } - private void sendFirstJoinMessage(Player receiver, Player joiningPlayer, int joiningPlayersNumber) { - for (String line : AnarchyExploitFixes.getLang(receiver.getLocale()).misc_firstJoinMessage) { - receiver.sendMessage(line - .replace("%player%", joiningPlayer.getName()) - .replace("%players_num%", formatToOrdinal(receiver.getLocale(), joiningPlayersNumber)) - ); - } - } - - private String formatToOrdinal(String playerLanguage, int number) { - Locale playerLocale = Locale.forLanguageTag(playerLanguage.replace("-", "_")); - if (config.auto_lang && availableTranslations.contains(playerLocale)) { - return new RuleBasedNumberFormat(playerLocale, RuleBasedNumberFormat.ORDINAL).format(number, "%digits-ordinal"); + private String formatToOrdinal(String playerLocale, int number) { + if (config.auto_lang && formats.containsKey(playerLocale)) { + return formats.get(playerLocale).format(number, "%digits-ordinal"); } else { - return new RuleBasedNumberFormat(config.default_lang, RuleBasedNumberFormat.ORDINAL).format(number, "%digits-ordinal"); + return formats.get(config.default_lang.toString().toLowerCase()).format(number, "%digits-ordinal"); } } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/PreventMessageKick.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/PreventMessageKick.java index c4ef7eddf..b9edd2597 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/PreventMessageKick.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/modules/misc/PreventMessageKick.java @@ -10,18 +10,21 @@ import java.util.Arrays; import java.util.HashSet; +import java.util.stream.Collectors; public class PreventMessageKick implements AnarchyExploitFixesModule, Listener { - private final HashSet kickMessagesToListenTo = new HashSet<>(); + private final HashSet kickMessagesToListenTo; public PreventMessageKick() { shouldEnable(); Config config = AnarchyExploitFixes.getConfiguration(); config.addComment("misc.kicks.prevent-message-kick.enable", "Cancels the kick for specific kick messages."); - config.getList("misc.kicks.prevent-message-kick.kick-messages-to-listen-to", Arrays.asList("Kicked for spamming", "Stop spamming!")).forEach(configuredMsg -> - this.kickMessagesToListenTo.add(configuredMsg.toLowerCase()) - ); + this.kickMessagesToListenTo = config.getList("misc.kicks.prevent-message-kick.kick-messages-to-listen-to", + Arrays.asList("Kicked for spamming", "Stop spamming!")) + .stream() + .map(String::toLowerCase) + .collect(Collectors.toCollection(HashSet::new)); } @Override @@ -45,7 +48,7 @@ public boolean shouldEnable() { return AnarchyExploitFixes.getConfiguration().getBoolean("misc.kicks.prevent-message-kick.enable", false); } - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) private void onPlayerKick(PlayerKickEvent event) { if (kickMessagesToListenTo.contains(event.getReason().toLowerCase())) { event.setCancelled(true);