9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-19 15:09:24 +00:00

Improve locales

This commit is contained in:
XiaoMoMi
2024-07-30 15:07:04 +08:00
parent abeccdf870
commit 5b9c65caf7
6 changed files with 42 additions and 17 deletions

View File

@@ -24,6 +24,7 @@ import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
/** /**
@@ -40,7 +41,7 @@ public class FishingBagHolder implements InventoryHolder {
* @param owner the UUID of the player who owns this fishing bag. * @param owner the UUID of the player who owns this fishing bag.
*/ */
public FishingBagHolder(UUID owner) { public FishingBagHolder(UUID owner) {
this.owner = owner; this.owner = Objects.requireNonNull(owner, "uuid should be nonnull");
} }
/** /**

View File

@@ -37,7 +37,8 @@ import java.util.stream.Stream;
public class TranslationManager { public class TranslationManager {
public static final Locale DEFAULT_LOCALE = Locale.ENGLISH; private static final Locale DEFAULT_LOCALE = Locale.ENGLISH;
private static Locale FORCE_LOCALE = null;
private static final List<String> locales = List.of("en", "zh_cn"); private static final List<String> locales = List.of("en", "zh_cn");
private static TranslationManager instance; private static TranslationManager instance;
@@ -52,6 +53,10 @@ public class TranslationManager {
instance = this; instance = this;
} }
public static void forceLocale(Locale locale) {
FORCE_LOCALE = locale;
}
public void reload() { public void reload() {
// remove any previous registry // remove any previous registry
if (this.registry != null) { if (this.registry != null) {
@@ -73,6 +78,9 @@ public class TranslationManager {
} }
public static String miniMessageTranslation(String key, @Nullable Locale locale) { public static String miniMessageTranslation(String key, @Nullable Locale locale) {
if (FORCE_LOCALE != null) {
return instance.registry.miniMessageTranslation(key, FORCE_LOCALE);
}
if (locale == null) { if (locale == null) {
locale = Locale.getDefault(); locale = Locale.getDefault();
if (locale == null) { if (locale == null) {
@@ -87,6 +95,9 @@ public class TranslationManager {
} }
public static Component render(Component component, @Nullable Locale locale) { public static Component render(Component component, @Nullable Locale locale) {
if (FORCE_LOCALE != null) {
return MiniMessageTranslator.render(component, FORCE_LOCALE);
}
if (locale == null) { if (locale == null) {
locale = Locale.getDefault(); locale = Locale.getDefault();
if (locale == null) { if (locale == null) {
@@ -134,7 +145,7 @@ public class TranslationManager {
Locale localLocale = Locale.getDefault(); Locale localLocale = Locale.getDefault();
if (!this.installed.contains(localLocale)) { if (!this.installed.contains(localLocale)) {
plugin.getPluginLogger().warn(localLocale.toString().toLowerCase(Locale.ENGLISH) + ".yml not exists, using en.yml as default locale."); plugin.getPluginLogger().warn(localLocale.toString().toLowerCase(Locale.ENGLISH) + ".yml not exists, using " + DEFAULT_LOCALE.toString().toLowerCase(Locale.ENGLISH) + ".yml as default locale.");
} }
} }
@@ -185,6 +196,6 @@ public class TranslationManager {
} }
public static @Nullable Locale parseLocale(@Nullable String locale) { public static @Nullable Locale parseLocale(@Nullable String locale) {
return locale == null ? null : Translator.parseLocale(locale); return locale == null || locale.isEmpty() ? null : Translator.parseLocale(locale);
} }
} }

View File

@@ -22,6 +22,7 @@ import dev.dejvokep.boostedyaml.YamlDocument;
import dev.dejvokep.boostedyaml.block.implementation.Section; import dev.dejvokep.boostedyaml.block.implementation.Section;
import dev.dejvokep.boostedyaml.dvs.versioning.BasicVersioning; import dev.dejvokep.boostedyaml.dvs.versioning.BasicVersioning;
import dev.dejvokep.boostedyaml.libs.org.snakeyaml.engine.v2.common.ScalarStyle; import dev.dejvokep.boostedyaml.libs.org.snakeyaml.engine.v2.common.ScalarStyle;
import dev.dejvokep.boostedyaml.libs.org.snakeyaml.engine.v2.exceptions.ConstructorException;
import dev.dejvokep.boostedyaml.libs.org.snakeyaml.engine.v2.nodes.Tag; import dev.dejvokep.boostedyaml.libs.org.snakeyaml.engine.v2.nodes.Tag;
import dev.dejvokep.boostedyaml.settings.dumper.DumperSettings; import dev.dejvokep.boostedyaml.settings.dumper.DumperSettings;
import dev.dejvokep.boostedyaml.settings.general.GeneralSettings; import dev.dejvokep.boostedyaml.settings.general.GeneralSettings;
@@ -71,6 +72,7 @@ import net.momirealms.customfishing.common.helper.AdventureHelper;
import net.momirealms.customfishing.common.helper.VersionHelper; import net.momirealms.customfishing.common.helper.VersionHelper;
import net.momirealms.customfishing.common.item.AbstractItem; import net.momirealms.customfishing.common.item.AbstractItem;
import net.momirealms.customfishing.common.item.Item; import net.momirealms.customfishing.common.item.Item;
import net.momirealms.customfishing.common.locale.TranslationManager;
import net.momirealms.customfishing.common.util.*; import net.momirealms.customfishing.common.util.*;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
@@ -259,6 +261,8 @@ public class BukkitConfigManager extends ConfigManager {
} }
} }
} }
TranslationManager.forceLocale(TranslationManager.parseLocale(config.getString("force-locale", "")));
} }
private void loadConfigs() { private void loadConfigs() {
@@ -279,11 +283,15 @@ public class BukkitConfigManager extends ConfigManager {
if (subFile.isDirectory()) { if (subFile.isDirectory()) {
fileDeque.push(subFile); fileDeque.push(subFile);
} else if (subFile.isFile() && subFile.getName().endsWith(".yml")) { } else if (subFile.isFile() && subFile.getName().endsWith(".yml")) {
YamlDocument document = plugin.getConfigManager().loadData(subFile); try {
for (Map.Entry<String, Object> entry : document.getStringRouteMappedValues(false).entrySet()) { YamlDocument document = plugin.getConfigManager().loadData(subFile);
if (entry.getValue() instanceof Section section) { for (Map.Entry<String, Object> entry : document.getStringRouteMappedValues(false).entrySet()) {
type.parse(entry.getKey(), section, nodes); if (entry.getValue() instanceof Section section) {
type.parse(entry.getKey(), section, nodes);
}
} }
} catch (ConstructorException e) {
plugin.getPluginLogger().warn("Could not load config file: " + subFile.getAbsolutePath() + ". Is it a corrupted file?");
} }
} }
} }

View File

@@ -90,7 +90,7 @@ public class BukkitIntegrationManager implements IntegrationManager {
if (isHooked("NeigeItems")) { if (isHooked("NeigeItems")) {
registerItemProvider(new NeigeItemsItemProvider()); registerItemProvider(new NeigeItemsItemProvider());
} }
if (isHooked("MythicMobs")) { if (isHooked("MythicMobs", "5")) {
registerItemProvider(new MythicMobsItemProvider()); registerItemProvider(new MythicMobsItemProvider());
registerEntityProvider(new MythicEntityProvider()); registerEntityProvider(new MythicEntityProvider());
} }
@@ -127,7 +127,7 @@ public class BukkitIntegrationManager implements IntegrationManager {
registerSeasonProvider(new RealisticSeasonsProvider()); registerSeasonProvider(new RealisticSeasonsProvider());
} else if (isHooked("AdvancedSeasons")) { } else if (isHooked("AdvancedSeasons")) {
registerSeasonProvider(new AdvancedSeasonsProvider()); registerSeasonProvider(new AdvancedSeasonsProvider());
} else if (isHooked("CustomCrops")) { } else if (isHooked("CustomCrops", "3.4", "3.5", "3.6")) {
registerSeasonProvider(new CustomCropsSeasonProvider()); registerSeasonProvider(new CustomCropsSeasonProvider());
} }
if (isHooked("Vault")) { if (isHooked("Vault")) {
@@ -162,12 +162,15 @@ public class BukkitIntegrationManager implements IntegrationManager {
return false; return false;
} }
private boolean isHooked(String hooked, String versionPrefix) { private boolean isHooked(String hooked, String... versionPrefix) {
Plugin p = Bukkit.getPluginManager().getPlugin(hooked); Plugin p = Bukkit.getPluginManager().getPlugin(hooked);
if (p != null) { if (p != null) {
if (p.getDescription().getVersion().startsWith(versionPrefix)) { String ver = p.getDescription().getVersion();
plugin.getPluginLogger().info(hooked + " hooked!"); for (String prefix : versionPrefix) {
return true; if (ver.startsWith(prefix)) {
plugin.getPluginLogger().info(hooked + " hooked!");
return true;
}
} }
} }
return false; return false;

View File

@@ -7,6 +7,8 @@ debug: false
metrics: true metrics: true
# Check updates # Check updates
update-checker: true update-checker: true
# Force locale, for instance zh_cn
force-locale: ''
# Mechanic settings # Mechanic settings
mechanics: mechanics:

View File

@@ -1,7 +1,7 @@
# Project settings # Project settings
# Rule: [major update].[feature update].[bug fix] # Rule: [major update].[feature update].[bug fix]
project_version=2.2.10 project_version=2.2.11
config_version=34 config_version=35
project_group=net.momirealms project_group=net.momirealms
# Dependency settings # Dependency settings
@@ -17,7 +17,7 @@ h2_driver_version=2.2.224
sqlite_driver_version=3.46.0.0 sqlite_driver_version=3.46.0.0
adventure_bundle_version=4.17.0 adventure_bundle_version=4.17.0
adventure_platform_version=4.3.3 adventure_platform_version=4.3.3
sparrow_heart_version=0.33 sparrow_heart_version=0.34
cloud_core_version=2.0.0-rc.2 cloud_core_version=2.0.0-rc.2
cloud_services_version=2.0.0-rc.2 cloud_services_version=2.0.0-rc.2
cloud_brigadier_version=2.0.0-beta.9 cloud_brigadier_version=2.0.0-beta.9