diff --git a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java index 049c95804..39d15015f 100755 --- a/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java +++ b/AnarchyExploitFixesFolia/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java @@ -1,5 +1,6 @@ package me.moomoo.anarchyexploitfixes.config; +import com.ibm.icu.text.RuleBasedNumberFormat; import io.github.thatsmusic99.configurationmaster.api.ConfigFile; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import net.kyori.adventure.text.Component; @@ -13,6 +14,8 @@ public class LanguageCache { private final ConfigFile lang; private final Locale locale; + + public final RuleBasedNumberFormat ordinalFormat; public final Component no_permission, invalid_syntax, failed_argument_parse, chat_commandwhitelist_badcommand, misc_joinMessage, misc_leaveMessage, misc_MaskedKickMessage, preventions_witherSpawningDisabledInRadius, misc_enabledConnectionMsgs, misc_disabledConnectionMsgs, elytra_disablePacketElytraFly, elytra_global_YouAreFlyingIn, elytra_global_New, elytra_global_New_UpperCase, elytra_global_New_Color, elytra_global_Old, elytra_global_Old_UpperCase, @@ -24,17 +27,17 @@ public class LanguageCache { elytra_spawn_Speed, elytra_spawn_DisabledLowTPS, elytra_spawn_TooFast, elytra_spawn_TooFastChunkInfo, elytra_spawn_Chunks, lagpreventions_stopSpammingLevers; public final List misc_firstJoinMessage, help_Message; - public LanguageCache(String locale) throws Exception { - this.locale = Locale.forLanguageTag(locale.replace("_", "-")); + public LanguageCache(String lang) throws Exception { + this.locale = Locale.forLanguageTag(lang.replace("_", "-")); AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance(); - File langYML = new File(plugin.getDataFolder() + File.separator + "lang", locale + ".yml"); + File langYML = new File(plugin.getDataFolder() + File.separator + "lang", lang + ".yml"); // Check if the lang folder has already been created File parent = langYML.getParentFile(); if (!parent.exists() && !parent.mkdir()) AnarchyExploitFixes.getPrefixedLogger().error("Unable to create lang directory."); // Check if the file already exists and save the one from the plugin's resources folder if it does not if (!langYML.exists()) - plugin.saveResource("lang/" + locale + ".yml", false); + plugin.saveResource("lang/" + lang + ".yml", false); // Finally, load the lang file with configmaster this.lang = ConfigFile.loadConfig(langYML); @@ -59,6 +62,7 @@ public LanguageCache(String locale) throws Exception { // Chat this.chat_commandwhitelist_badcommand = getTranslation("command-whitelist.bad-command", "Bad command. Type /help for a list of commands.", false); // Misc + this.ordinalFormat = new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.ORDINAL); this.misc_joinMessage = getTranslation("join-leave-messages.join", "%player% joined the game.", false); this.misc_leaveMessage = getTranslation("join-leave-messages.leave", "%player% left the game.", false); this.misc_enabledConnectionMsgs = getTranslation("join-leave-messages.enabled-connection-msgs", "Enabled connection msgs", false); @@ -127,7 +131,7 @@ public LanguageCache(String locale) throws Exception { this.elytra_spawn_Chunks = getTranslation("elytra.elytra-speed.spawn.chunks", "spawn chunks", false); try { - lang.save(); + this.lang.save(); } catch (Exception e) { AnarchyExploitFixes.getPrefixedLogger().error("Failed to save language file: "+ langYML.getName() +"!", e); } 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 6499bb821..fdf25ffbd 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 @@ -1,6 +1,5 @@ package me.moomoo.anarchyexploitfixes.modules.misc; -import com.ibm.icu.text.RuleBasedNumberFormat; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.enums.NamespacedKeys; @@ -16,15 +15,12 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; -import java.util.HashMap; -import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; 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; @@ -38,9 +34,6 @@ public FirstJoinMessages() { 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().stream() - .map(availableLang -> Locale.forLanguageTag(availableLang.replace("_", "-"))) - .forEach(locale -> formats.put(locale, new RuleBasedNumberFormat(locale, RuleBasedNumberFormat.ORDINAL))); } @Override @@ -91,7 +84,8 @@ private void onPlayerJoin(PlayerJoinEvent event) { .build()) .replaceText(TextReplacementConfig.builder() .matchLiteral("%players_num%") - .replacement(formatToOrdinal(onlinePlayer.locale(), joiningPlayersNumber)) + .replacement(AnarchyExploitFixes.getLang(onlinePlayer.locale()).ordinalFormat + .format(joiningPlayersNumber, "%digits-ordinal")) .build())); } } @@ -107,18 +101,11 @@ private void onPlayerJoin(PlayerJoinEvent event) { .build()) .replaceText(TextReplacementConfig.builder() .matchLiteral("%players_num%") - .replacement(formatToOrdinal(joiningPlayer.locale(), joiningPlayersNumber)) + .replacement(AnarchyExploitFixes.getLang(joiningPlayer.locale()).ordinalFormat + .format(joiningPlayersNumber, "%digits-ordinal")) .build()) .append(Component.text(" (" + joiningPlayer.locale() + ")"))); } } } - - private String formatToOrdinal(Locale playerLocale, int number) { - if (config.auto_lang && formats.containsKey(playerLocale)) { - return formats.get(playerLocale).format(number, "%digits-ordinal"); - } else { - return formats.get(config.default_lang).format(number, "%digits-ordinal"); - } - } } diff --git a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java index 27799aba3..06fcb2a57 100755 --- a/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java +++ b/AnarchyExploitFixesLegacy/src/main/java/me/moomoo/anarchyexploitfixes/config/LanguageCache.java @@ -1,5 +1,6 @@ package me.moomoo.anarchyexploitfixes.config; +import com.ibm.icu.text.RuleBasedNumberFormat; import io.github.thatsmusic99.configurationmaster.api.ConfigFile; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import org.bukkit.ChatColor; @@ -8,12 +9,14 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.stream.Collectors; public class LanguageCache { private final ConfigFile lang; - + + public final RuleBasedNumberFormat ordinalFormat; public final String no_permission, invalid_syntax, failed_argument_parse, chat_CommandWhitelist_BadCommand, misc_joinMessage, misc_leaveMessage, misc_MaskedKickMessage, withers_SpawningDisabledInRadius, misc_enabledConnectionMsgs, misc_disabledConnectionMsgs, elytra_disablePacketElytraFly, elytra_global_YouAreFlyingIn, elytra_global_New, elytra_global_New_Color, elytra_global_Old, elytra_global_Old_Color, @@ -59,6 +62,7 @@ public LanguageCache(String locale) throws Exception { // Chat this.chat_CommandWhitelist_BadCommand = getTranslation("command-whitelist.bad-command", "&4Bad command. Type /help for a list of commands."); // Misc + this.ordinalFormat = new RuleBasedNumberFormat(Locale.forLanguageTag(locale.replace("_", "-")), RuleBasedNumberFormat.ORDINAL); this.misc_joinMessage = getTranslation("join-leave-messages.join", "&7%player% joined the game."); this.misc_leaveMessage = getTranslation("join-leave-messages.leave", "&7%player% left the game."); this.misc_enabledConnectionMsgs = getTranslation("join-leave-messages.enabled-connection-msgs", "&7Enabled connection msgs"); 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 f87014a09..2f8b2dfe2 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 @@ -1,6 +1,5 @@ package me.moomoo.anarchyexploitfixes.modules.misc; -import com.ibm.icu.text.RuleBasedNumberFormat; import me.moomoo.anarchyexploitfixes.AnarchyExploitFixes; import me.moomoo.anarchyexploitfixes.config.Config; import me.moomoo.anarchyexploitfixes.modules.AnarchyExploitFixesModule; @@ -10,15 +9,12 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; -import java.util.HashMap; -import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; 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; @@ -30,9 +26,6 @@ public FirstJoinMessages() { // prevent lag by getting total player number once and then counting up manually, as always getting all players is resource intense this.totalPlayers = new AtomicInteger(plugin.getServer().getOfflinePlayers().length); this.logFirstJoin = config.getBoolean("misc.join-leave-messages.first-join-messages.show-in-console", true); - for (String availableLang : AnarchyExploitFixes.getLanguageCacheMap().keySet()) { - formats.put(availableLang, new RuleBasedNumberFormat(Locale.forLanguageTag(availableLang.replace("_", "-")), RuleBasedNumberFormat.ORDINAL)); - } } @Override @@ -56,7 +49,7 @@ public boolean shouldEnable() { } @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - private void onPlayerJoinEvent(PlayerJoinEvent event) { + private void onPlayerJoin(PlayerJoinEvent event) { final Player joiningPlayer = event.getPlayer(); if (joiningPlayer.hasPlayedBefore()) return; if (joiningPlayer.hasPermission("anarchyexploitfixes.silentJoin")) return; @@ -68,7 +61,8 @@ private void onPlayerJoinEvent(PlayerJoinEvent event) { for (String line : AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).misc_firstJoinMessage) { onlinePlayer.sendMessage(line .replace("%player%", joiningPlayer.getName()) - .replace("%players_num%", formatToOrdinal(onlinePlayer.getLocale(), joiningPlayersNumber))); + .replace("%players_num%", AnarchyExploitFixes.getLang(onlinePlayer.getLocale()).ordinalFormat + .format(joiningPlayersNumber, "%digits-ordinal"))); } } } @@ -77,16 +71,9 @@ private void onPlayerJoinEvent(PlayerJoinEvent event) { for (String line : AnarchyExploitFixes.getLang(joiningPlayer.getLocale()).misc_firstJoinMessage) { AnarchyExploitFixes.getUnprefixedLogger().info(line .replace("%player%", joiningPlayer.getName()) - .replace("%players_num%", formatToOrdinal(joiningPlayer.getLocale(), joiningPlayersNumber))); + .replace("%players_num%", AnarchyExploitFixes.getLang(joiningPlayer.getLocale()).ordinalFormat + .format(joiningPlayersNumber, "%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 formats.get(config.default_lang.toString().toLowerCase()).format(number, "%digits-ordinal"); - } - } }