fix language loader
This commit is contained in:
parent
19d223a16c
commit
d66155ec87
@ -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>
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -56,84 +56,85 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sender.hasPermission(Permissions.Commands.OPTIMIZE_RADIUS.get())) {
|
||||
if (args.length != 1) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
int specifiedRadius = Integer.parseInt(args[0]);
|
||||
|
||||
if (specifiedRadius > max_radius) {
|
||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||
.matchLiteral("%distance%")
|
||||
.replacement(Integer.toString(max_radius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
int specifiedRadius = Integer.parseInt(args[0]);
|
||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||
int successCount = 0;
|
||||
int failCount = 0;
|
||||
final boolean player_has_cooldown_bypass = player.hasPermission(Permissions.Bypass.COMMAND_COOLDOWN.get());
|
||||
|
||||
if (specifiedRadius > max_radius) {
|
||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||
.matchLiteral("%distance%")
|
||||
.replacement(Integer.toString(max_radius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
|
||||
return true;
|
||||
}
|
||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||
Villager villager = (Villager) entity;
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
|
||||
|
||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||
int successCount = 0;
|
||||
int failCount = 0;
|
||||
final boolean player_has_cooldown_bypass = player.hasPermission(Permissions.Bypass.COMMAND_COOLDOWN.get());
|
||||
WrappedVillager wVillager = villagerCache.getOrAdd(villager);
|
||||
|
||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||
Villager villager = (Villager) entity;
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
|
||||
|
||||
WrappedVillager wVillager = villagerCache.getOrAdd(villager);
|
||||
|
||||
if (player_has_cooldown_bypass || wVillager.canOptimize(cooldown)) {
|
||||
VillagerOptimizeEvent optimizeEvent = new VillagerOptimizeEvent(wVillager, OptimizationType.COMMAND, player);
|
||||
if (optimizeEvent.callEvent()) {
|
||||
wVillager.setOptimization(optimizeEvent.getOptimizationType());
|
||||
wVillager.saveOptimizeTime();
|
||||
successCount++;
|
||||
}
|
||||
} else {
|
||||
failCount++;
|
||||
if (player_has_cooldown_bypass || wVillager.canOptimize(cooldown)) {
|
||||
VillagerOptimizeEvent optimizeEvent = new VillagerOptimizeEvent(wVillager, OptimizationType.COMMAND, player);
|
||||
if (optimizeEvent.callEvent()) {
|
||||
wVillager.setOptimization(optimizeEvent.getOptimizationType());
|
||||
wVillager.saveOptimizeTime();
|
||||
successCount++;
|
||||
}
|
||||
} else {
|
||||
failCount++;
|
||||
}
|
||||
|
||||
if (successCount <= 0 && failCount <= 0) {
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (successCount > 0) {
|
||||
final TextReplacementConfig success_amount = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(successCount))
|
||||
.build();
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_optimize_success.forEach(line -> player.sendMessage(line
|
||||
.replaceText(success_amount)
|
||||
.replaceText(radius)
|
||||
));
|
||||
}
|
||||
if (failCount > 0) {
|
||||
final TextReplacementConfig alreadyOptimized = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(failCount))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_optimize_fail.forEach(line -> player.sendMessage(line.replaceText(alreadyOptimized)));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
|
||||
|
||||
if (successCount <= 0 && failCount <= 0) {
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (successCount > 0) {
|
||||
final TextReplacementConfig success_amount = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(successCount))
|
||||
.build();
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_optimize_success.forEach(line -> player.sendMessage(line
|
||||
.replaceText(success_amount)
|
||||
.replaceText(radius)
|
||||
));
|
||||
}
|
||||
if (failCount > 0) {
|
||||
final TextReplacementConfig alreadyOptimized = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(failCount))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_optimize_fail.forEach(line -> player.sendMessage(line.replaceText(alreadyOptimized)));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -50,69 +50,70 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sender.hasPermission(Permissions.Commands.UNOPTIMIZE_RADIUS.get())) {
|
||||
if (args.length != 1) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
int specifiedRadius = Integer.parseInt(args[0]);
|
||||
|
||||
if (specifiedRadius > max_radius) {
|
||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||
.matchLiteral("%distance%")
|
||||
.replacement(Integer.toString(max_radius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
int specifiedRadius = Integer.parseInt(args[0]);
|
||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||
int successCount = 0;
|
||||
|
||||
if (specifiedRadius > max_radius) {
|
||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||
.matchLiteral("%distance%")
|
||||
.replacement(Integer.toString(max_radius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
|
||||
return true;
|
||||
}
|
||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||
Villager villager = (Villager) entity;
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
|
||||
|
||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||
int successCount = 0;
|
||||
WrappedVillager wVillager = villagerCache.getOrAdd(villager);
|
||||
|
||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||
Villager villager = (Villager) entity;
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
|
||||
|
||||
WrappedVillager wVillager = villagerCache.getOrAdd(villager);
|
||||
|
||||
if (wVillager.isOptimized()) {
|
||||
VillagerUnoptimizeEvent unOptimizeEvent = new VillagerUnoptimizeEvent(wVillager, player, OptimizationType.COMMAND);
|
||||
if (unOptimizeEvent.callEvent()) {
|
||||
wVillager.setOptimization(OptimizationType.NONE);
|
||||
successCount++;
|
||||
}
|
||||
if (wVillager.isOptimized()) {
|
||||
VillagerUnoptimizeEvent unOptimizeEvent = new VillagerUnoptimizeEvent(wVillager, player, OptimizationType.COMMAND);
|
||||
if (unOptimizeEvent.callEvent()) {
|
||||
wVillager.setOptimization(OptimizationType.NONE);
|
||||
successCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (successCount <= 0) {
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||
} else {
|
||||
final TextReplacementConfig success_amount = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(successCount))
|
||||
.build();
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_unoptimize_success.forEach(line -> player.sendMessage(line
|
||||
.replaceText(success_amount)
|
||||
.replaceText(radius)
|
||||
));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
|
||||
|
||||
if (successCount <= 0) {
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||
} else {
|
||||
final TextReplacementConfig success_amount = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(successCount))
|
||||
.build();
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_unoptimize_success.forEach(line -> player.sendMessage(line
|
||||
.replaceText(success_amount)
|
||||
.replaceText(radius)
|
||||
));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -56,84 +56,85 @@ public class OptVillagersRadius implements VillagerOptimizerCommand, TabComplete
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sender.hasPermission(Permissions.Commands.OPTIMIZE_RADIUS.get())) {
|
||||
if (args.length != 1) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
int specifiedRadius = Integer.parseInt(args[0]);
|
||||
|
||||
if (specifiedRadius > max_radius) {
|
||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||
.matchLiteral("%distance%")
|
||||
.replacement(Integer.toString(max_radius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
int specifiedRadius = Integer.parseInt(args[0]);
|
||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||
int successCount = 0;
|
||||
int failCount = 0;
|
||||
final boolean player_has_cooldown_bypass = player.hasPermission(Permissions.Bypass.COMMAND_COOLDOWN.get());
|
||||
|
||||
if (specifiedRadius > max_radius) {
|
||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||
.matchLiteral("%distance%")
|
||||
.replacement(Integer.toString(max_radius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
|
||||
return true;
|
||||
}
|
||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||
Villager villager = (Villager) entity;
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
|
||||
|
||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||
int successCount = 0;
|
||||
int failCount = 0;
|
||||
final boolean player_has_cooldown_bypass = player.hasPermission(Permissions.Bypass.COMMAND_COOLDOWN.get());
|
||||
WrappedVillager wVillager = villagerCache.getOrAdd(villager);
|
||||
|
||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||
Villager villager = (Villager) entity;
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
|
||||
|
||||
WrappedVillager wVillager = villagerCache.getOrAdd(villager);
|
||||
|
||||
if (player_has_cooldown_bypass || wVillager.canOptimize(cooldown)) {
|
||||
VillagerOptimizeEvent optimizeEvent = new VillagerOptimizeEvent(wVillager, OptimizationType.COMMAND, player);
|
||||
if (optimizeEvent.callEvent()) {
|
||||
wVillager.setOptimization(optimizeEvent.getOptimizationType());
|
||||
wVillager.saveOptimizeTime();
|
||||
successCount++;
|
||||
}
|
||||
} else {
|
||||
failCount++;
|
||||
if (player_has_cooldown_bypass || wVillager.canOptimize(cooldown)) {
|
||||
VillagerOptimizeEvent optimizeEvent = new VillagerOptimizeEvent(wVillager, OptimizationType.COMMAND, player);
|
||||
if (optimizeEvent.callEvent()) {
|
||||
wVillager.setOptimization(optimizeEvent.getOptimizationType());
|
||||
wVillager.saveOptimizeTime();
|
||||
successCount++;
|
||||
}
|
||||
} else {
|
||||
failCount++;
|
||||
}
|
||||
|
||||
if (successCount <= 0 && failCount <= 0) {
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (successCount > 0) {
|
||||
final TextReplacementConfig success_amount = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(successCount))
|
||||
.build();
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_optimize_success.forEach(line -> player.sendMessage(line
|
||||
.replaceText(success_amount)
|
||||
.replaceText(radius)
|
||||
));
|
||||
}
|
||||
if (failCount > 0) {
|
||||
final TextReplacementConfig alreadyOptimized = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(failCount))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_optimize_fail.forEach(line -> player.sendMessage(line.replaceText(alreadyOptimized)));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
|
||||
|
||||
if (successCount <= 0 && failCount <= 0) {
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (successCount > 0) {
|
||||
final TextReplacementConfig success_amount = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(successCount))
|
||||
.build();
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_optimize_success.forEach(line -> player.sendMessage(line
|
||||
.replaceText(success_amount)
|
||||
.replaceText(radius)
|
||||
));
|
||||
}
|
||||
if (failCount > 0) {
|
||||
final TextReplacementConfig alreadyOptimized = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(failCount))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_optimize_fail.forEach(line -> player.sendMessage(line.replaceText(alreadyOptimized)));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -50,69 +50,70 @@ public class UnOptVillagersRadius implements VillagerOptimizerCommand, TabComple
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sender.hasPermission(Permissions.Commands.UNOPTIMIZE_RADIUS.get())) {
|
||||
if (args.length != 1) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_specify_radius.forEach(player::sendMessage);
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
int specifiedRadius = Integer.parseInt(args[0]);
|
||||
|
||||
if (specifiedRadius > max_radius) {
|
||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||
.matchLiteral("%distance%")
|
||||
.replacement(Integer.toString(max_radius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
int specifiedRadius = Integer.parseInt(args[0]);
|
||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||
int successCount = 0;
|
||||
|
||||
if (specifiedRadius > max_radius) {
|
||||
final TextReplacementConfig limit = TextReplacementConfig.builder()
|
||||
.matchLiteral("%distance%")
|
||||
.replacement(Integer.toString(max_radius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_limit_exceed.forEach(line -> player.sendMessage(line.replaceText(limit)));
|
||||
return true;
|
||||
}
|
||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||
Villager villager = (Villager) entity;
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
|
||||
|
||||
VillagerCache villagerCache = VillagerOptimizer.getCache();
|
||||
int successCount = 0;
|
||||
WrappedVillager wVillager = villagerCache.getOrAdd(villager);
|
||||
|
||||
for (Entity entity : player.getNearbyEntities(specifiedRadius, specifiedRadius, specifiedRadius)) {
|
||||
if (!entity.getType().equals(EntityType.VILLAGER)) continue;
|
||||
Villager villager = (Villager) entity;
|
||||
Villager.Profession profession = villager.getProfession();
|
||||
if (profession.equals(Villager.Profession.NITWIT) || profession.equals(Villager.Profession.NONE)) continue;
|
||||
|
||||
WrappedVillager wVillager = villagerCache.getOrAdd(villager);
|
||||
|
||||
if (wVillager.isOptimized()) {
|
||||
VillagerUnoptimizeEvent unOptimizeEvent = new VillagerUnoptimizeEvent(wVillager, player, OptimizationType.COMMAND);
|
||||
if (unOptimizeEvent.callEvent()) {
|
||||
wVillager.setOptimization(OptimizationType.NONE);
|
||||
successCount++;
|
||||
}
|
||||
if (wVillager.isOptimized()) {
|
||||
VillagerUnoptimizeEvent unOptimizeEvent = new VillagerUnoptimizeEvent(wVillager, player, OptimizationType.COMMAND);
|
||||
if (unOptimizeEvent.callEvent()) {
|
||||
wVillager.setOptimization(OptimizationType.NONE);
|
||||
successCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (successCount <= 0) {
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||
} else {
|
||||
final TextReplacementConfig success_amount = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(successCount))
|
||||
.build();
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_unoptimize_success.forEach(line -> player.sendMessage(line
|
||||
.replaceText(success_amount)
|
||||
.replaceText(radius)
|
||||
));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(VillagerOptimizer.getLang(sender).no_permission);
|
||||
|
||||
if (successCount <= 0) {
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_no_villagers_nearby.forEach(line -> player.sendMessage(line.replaceText(radius)));
|
||||
} else {
|
||||
final TextReplacementConfig success_amount = TextReplacementConfig.builder()
|
||||
.matchLiteral("%amount%")
|
||||
.replacement(Integer.toString(successCount))
|
||||
.build();
|
||||
final TextReplacementConfig radius = TextReplacementConfig.builder()
|
||||
.matchLiteral("%radius%")
|
||||
.replacement(Integer.toString(specifiedRadius))
|
||||
.build();
|
||||
VillagerOptimizer.getLang(player.locale()).command_unoptimize_success.forEach(line -> player.sendMessage(line
|
||||
.replaceText(success_amount)
|
||||
.replaceText(radius)
|
||||
));
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
VillagerOptimizer.getLang(player.locale()).command_radius_invalid.forEach(player::sendMessage);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user