fix language loader

This commit is contained in:
xGinko 2023-12-12 02:27:16 +01:00
parent 19d223a16c
commit d66155ec87
13 changed files with 292 additions and 304 deletions

View File

@ -7,7 +7,7 @@
<parent>
<groupId>me.xginko.VillagerOptimizer</groupId>
<artifactId>VillagerOptimizer</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</parent>
<artifactId>1.16.5</artifactId>

View File

@ -126,7 +126,7 @@ public final class VillagerOptimizer extends JavaPlugin {
Component.text("").style(plugin_style)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style)));
else logger.info(String.format("Found language file for %s", localeString));
else logger.info("Found language file for " + localeString);
LanguageCache langCache = new LanguageCache(localeString);
languageCacheMap.put(localeString, langCache);
}
@ -140,7 +140,7 @@ public final class VillagerOptimizer extends JavaPlugin {
Component.text("").style(plugin_style)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style)));
else logger.info(String.format("Found language file for %s", localeString));
else logger.info("Found language file for " + localeString);
LanguageCache langCache = new LanguageCache(localeString);
languageCacheMap.put(localeString, langCache);
}

View File

@ -56,7 +56,11 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
return true;
}
if (sender.hasPermission(Permissions.Commands.OPTIMIZE_RADIUS.get())) {
if (!sender.hasPermission(Permissions.Commands.OPTIMIZE_RADIUS.get())) {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
return true;
}
if (args.length != 1) {
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
return true;
@ -132,9 +136,6 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
} catch (NumberFormatException e) {
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
}
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}
return true;
}

View File

@ -50,7 +50,11 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
return true;
}
if (sender.hasPermission(Permissions.Commands.UNOPTIMIZE_RADIUS.get())) {
if (!sender.hasPermission(Permissions.Commands.UNOPTIMIZE_RADIUS.get())) {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
return true;
}
if (args.length != 1) {
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
return true;
@ -111,9 +115,6 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
} catch (NumberFormatException e) {
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
}
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}
return true;
}

View File

@ -31,11 +31,9 @@ public class Config {
}
private ConfigFile loadConfig(File ymlFile) throws Exception {
File parent = new File(ymlFile.getParent());
File parent = ymlFile.getParentFile();
if (!parent.exists() && !parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create plugin config directory.");
if (!ymlFile.exists())
ymlFile.createNewFile(); // Result can be ignored because this method only returns false if the file already exists
return ConfigFile.loadConfig(ymlFile);
}

View File

@ -12,7 +12,6 @@ import java.util.List;
public class LanguageCache {
private final @NotNull ConfigFile lang;
private final @NotNull MiniMessage miniMessage;
public final @NotNull Component no_permission;
public final @NotNull List<Component> nametag_optimize_success, nametag_on_optimize_cooldown, nametag_unoptimize_success,
@ -22,9 +21,18 @@ public class LanguageCache {
command_specify_radius, command_radius_invalid, command_no_villagers_nearby,
trades_restocked, optimize_for_trading, villager_leveling_up;
public LanguageCache(String lang) throws Exception {
this.lang = loadLang(new File(VillagerOptimizer.getInstance().getDataFolder() + File.separator + "lang", lang + ".yml"));
this.miniMessage = MiniMessage.miniMessage();
public LanguageCache(String locale) throws Exception {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
File langYML = new File(plugin.getDataFolder() + File.separator + "lang", locale + ".yml");
// Check if the lang folder has already been created
File parent = langYML.getParentFile();
if (!parent.exists() && !parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create lang directory.");
// Check if the file already exists and save the one from the plugins resources folder if it does not
if (!langYML.exists())
plugin.saveResource("lang/" + locale + ".yml", false);
// Finally load the lang file with configmaster
this.lang = ConfigFile.loadConfig(langYML);
// General
this.no_permission = getTranslation("messages.no-permission",
@ -72,20 +80,6 @@ public class LanguageCache {
this.command_no_villagers_nearby = getListTranslation("messages.command.no-villagers-nearby",
List.of("<gray>Couldn't find any employed villagers within a radius of %radius%."));
saveLang();
}
private ConfigFile loadLang(File ymlFile) throws Exception {
File parent = new File(ymlFile.getParent());
if (!parent.exists())
if (!parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create lang directory.");
if (!ymlFile.exists())
ymlFile.createNewFile(); // Result can be ignored because this method only returns false if the file already exists
return ConfigFile.loadConfig(ymlFile);
}
private void saveLang() {
try {
lang.save();
} catch (Exception e) {
@ -95,21 +89,21 @@ public class LanguageCache {
public @NotNull Component getTranslation(@NotNull String path, @NotNull String defaultTranslation) {
lang.addDefault(path, defaultTranslation);
return miniMessage.deserialize(lang.getString(path, defaultTranslation));
return MiniMessage.miniMessage().deserialize(lang.getString(path, defaultTranslation));
}
public @NotNull Component getTranslation(@NotNull String path, @NotNull String defaultTranslation, @NotNull String comment) {
lang.addDefault(path, defaultTranslation, comment);
return miniMessage.deserialize(lang.getString(path, defaultTranslation));
return MiniMessage.miniMessage().deserialize(lang.getString(path, defaultTranslation));
}
public @NotNull List<Component> getListTranslation(@NotNull String path, @NotNull List<String> defaultTranslation) {
lang.addDefault(path, defaultTranslation);
return lang.getStringList(path).stream().map(miniMessage::deserialize).toList();
return lang.getStringList(path).stream().map(MiniMessage.miniMessage()::deserialize).toList();
}
public @NotNull List<Component> getListTranslation(@NotNull String path, @NotNull List<String> defaultTranslation, @NotNull String comment) {
lang.addDefault(path, defaultTranslation, comment);
return lang.getStringList(path).stream().map(miniMessage::deserialize).toList();
return lang.getStringList(path).stream().map(MiniMessage.miniMessage()::deserialize).toList();
}
}

View File

@ -7,7 +7,7 @@
<parent>
<groupId>me.xginko.VillagerOptimizer</groupId>
<artifactId>VillagerOptimizer</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</parent>
<artifactId>1.20.2</artifactId>

View File

@ -126,7 +126,7 @@ public final class VillagerOptimizer extends JavaPlugin {
Component.text("").style(plugin_style)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style)));
else logger.info(String.format("Found language file for %s", localeString));
else logger.info("Found language file for " + localeString);
LanguageCache langCache = new LanguageCache(localeString);
languageCacheMap.put(localeString, langCache);
}
@ -140,7 +140,7 @@ public final class VillagerOptimizer extends JavaPlugin {
Component.text("").style(plugin_style)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style)));
else logger.info(String.format("Found language file for %s", localeString));
else logger.info("Found language file for " + localeString);
LanguageCache langCache = new LanguageCache(localeString);
languageCacheMap.put(localeString, langCache);
}

View File

@ -56,7 +56,11 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
return true;
}
if (sender.hasPermission(Permissions.Commands.OPTIMIZE_RADIUS.get())) {
if (!sender.hasPermission(Permissions.Commands.OPTIMIZE_RADIUS.get())) {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
return true;
}
if (args.length != 1) {
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
return true;
@ -132,9 +136,6 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
} catch (NumberFormatException e) {
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
}
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}
return true;
}

View File

@ -50,7 +50,11 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
return true;
}
if (sender.hasPermission(Permissions.Commands.UNOPTIMIZE_RADIUS.get())) {
if (!sender.hasPermission(Permissions.Commands.UNOPTIMIZE_RADIUS.get())) {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
return true;
}
if (args.length != 1) {
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
return true;
@ -111,9 +115,6 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
} catch (NumberFormatException e) {
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
}
} else {
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
}
return true;
}

View File

@ -31,11 +31,9 @@ public class Config {
}
private ConfigFile loadConfig(File ymlFile) throws Exception {
File parent = new File(ymlFile.getParent());
File parent = ymlFile.getParentFile();
if (!parent.exists() && !parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create plugin config directory.");
if (!ymlFile.exists())
ymlFile.createNewFile(); // Result can be ignored because this method only returns false if the file already exists
return ConfigFile.loadConfig(ymlFile);
}

View File

@ -12,7 +12,6 @@ import java.util.List;
public class LanguageCache {
private final @NotNull ConfigFile lang;
private final @NotNull MiniMessage miniMessage;
public final @NotNull Component no_permission;
public final @NotNull List<Component> nametag_optimize_success, nametag_on_optimize_cooldown, nametag_unoptimize_success,
@ -22,9 +21,18 @@ public class LanguageCache {
command_specify_radius, command_radius_invalid, command_no_villagers_nearby,
trades_restocked, optimize_for_trading, villager_leveling_up;
public LanguageCache(String lang) throws Exception {
this.lang = loadLang(new File(VillagerOptimizer.getInstance().getDataFolder() + File.separator + "lang", lang + ".yml"));
this.miniMessage = MiniMessage.miniMessage();
public LanguageCache(String locale) throws Exception {
VillagerOptimizer plugin = VillagerOptimizer.getInstance();
File langYML = new File(plugin.getDataFolder() + File.separator + "lang", locale + ".yml");
// Check if the lang folder has already been created
File parent = langYML.getParentFile();
if (!parent.exists() && !parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create lang directory.");
// Check if the file already exists and save the one from the plugins resources folder if it does not
if (!langYML.exists())
plugin.saveResource("lang/" + locale + ".yml", false);
// Finally load the lang file with configmaster
this.lang = ConfigFile.loadConfig(langYML);
// General
this.no_permission = getTranslation("messages.no-permission",
@ -72,20 +80,6 @@ public class LanguageCache {
this.command_no_villagers_nearby = getListTranslation("messages.command.no-villagers-nearby",
List.of("<gray>Couldn't find any employed villagers within a radius of %radius%."));
saveLang();
}
private ConfigFile loadLang(File ymlFile) throws Exception {
File parent = new File(ymlFile.getParent());
if (!parent.exists())
if (!parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create lang directory.");
if (!ymlFile.exists())
ymlFile.createNewFile(); // Result can be ignored because this method only returns false if the file already exists
return ConfigFile.loadConfig(ymlFile);
}
private void saveLang() {
try {
lang.save();
} catch (Exception e) {
@ -95,21 +89,21 @@ public class LanguageCache {
public @NotNull Component getTranslation(@NotNull String path, @NotNull String defaultTranslation) {
lang.addDefault(path, defaultTranslation);
return miniMessage.deserialize(lang.getString(path, defaultTranslation));
return MiniMessage.miniMessage().deserialize(lang.getString(path, defaultTranslation));
}
public @NotNull Component getTranslation(@NotNull String path, @NotNull String defaultTranslation, @NotNull String comment) {
lang.addDefault(path, defaultTranslation, comment);
return miniMessage.deserialize(lang.getString(path, defaultTranslation));
return MiniMessage.miniMessage().deserialize(lang.getString(path, defaultTranslation));
}
public @NotNull List<Component> getListTranslation(@NotNull String path, @NotNull List<String> defaultTranslation) {
lang.addDefault(path, defaultTranslation);
return lang.getStringList(path).stream().map(miniMessage::deserialize).toList();
return lang.getStringList(path).stream().map(MiniMessage.miniMessage()::deserialize).toList();
}
public @NotNull List<Component> getListTranslation(@NotNull String path, @NotNull List<String> defaultTranslation, @NotNull String comment) {
lang.addDefault(path, defaultTranslation, comment);
return lang.getStringList(path).stream().map(miniMessage::deserialize).toList();
return lang.getStringList(path).stream().map(MiniMessage.miniMessage()::deserialize).toList();
}
}

View File

@ -6,7 +6,7 @@
<groupId>me.xginko.VillagerOptimizer</groupId>
<artifactId>VillagerOptimizer</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<modules>
<module>VillagerOptimizer-1.20.2</module>
<module>VillagerOptimizer-1.16.5</module>