Skip to content

Commit

Permalink
move ordinal format into lang cache
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 6, 2024
1 parent df84353 commit 777b7e3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand All @@ -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<Component> 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);

Expand All @@ -59,6 +62,7 @@ public LanguageCache(String locale) throws Exception {
// Chat
this.chat_commandwhitelist_badcommand = getTranslation("command-whitelist.bad-command", "<dark_red>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", "<gray>%player% joined the game.", false);
this.misc_leaveMessage = getTranslation("join-leave-messages.leave", "<gray>%player% left the game.", false);
this.misc_enabledConnectionMsgs = getTranslation("join-leave-messages.enabled-connection-msgs", "<gray>Enabled connection msgs", false);
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Locale, RuleBasedNumberFormat> formats = new HashMap<>();
private final AtomicInteger uniquePlayerCount = new AtomicInteger(0);
private final boolean logFirstJoin;

Expand All @@ -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
Expand Down Expand Up @@ -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()));
}
}
Expand All @@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String, RuleBasedNumberFormat> formats = new HashMap<>();
private final AtomicInteger totalPlayers;
private final boolean logFirstJoin;

Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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")));
}
}
}
Expand All @@ -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");
}
}
}

0 comments on commit 777b7e3

Please sign in to comment.