overall improvements

This commit is contained in:
xGinko 2023-12-31 23:06:05 +01:00
parent d66155ec87
commit f45ac252c6
11 changed files with 168 additions and 180 deletions

View File

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

View File

@ -39,7 +39,7 @@ public final class VillagerCache {
} }
public @NotNull WrappedVillager add(@NotNull Villager villager) { public @NotNull WrappedVillager add(@NotNull Villager villager) {
return add(new WrappedVillager(villager)); return this.add(new WrappedVillager(villager));
} }
public boolean contains(@NotNull UUID uuid) { public boolean contains(@NotNull UUID uuid) {

View File

@ -19,14 +19,13 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
public final class VillagerOptimizer extends JavaPlugin { public final class VillagerOptimizer extends JavaPlugin {
@ -118,56 +117,51 @@ public final class VillagerOptimizer extends JavaPlugin {
languageCacheMap = new HashMap<>(); languageCacheMap = new HashMap<>();
ConsoleCommandSender console = getServer().getConsoleSender(); ConsoleCommandSender console = getServer().getConsoleSender();
try { try {
File langDirectory = new File(getDataFolder() + "/lang"); File langDirectory = new File(getDataFolder() + File.separator + "lang");
Files.createDirectories(langDirectory.toPath()); Files.createDirectories(langDirectory.toPath());
for (String fileName : getDefaultLanguageFiles()) { for (String fileName : getDefaultLanguageFiles()) {
String localeString = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.')); final String localeString = fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.lastIndexOf('.'));
if (startup) console.sendMessage( if (startup) console.sendMessage(
Component.text("").style(plugin_style) Component.text("").style(plugin_style)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD)) .append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style))); .append(Component.text("").style(plugin_style)));
else logger.info("Found language file for " + localeString); else logger.info(String.format("Found language file for %s", localeString));
LanguageCache langCache = new LanguageCache(localeString); languageCacheMap.put(localeString, new LanguageCache(localeString));
languageCacheMap.put(localeString, langCache);
} }
Pattern langPattern = Pattern.compile("([a-z]{1,3}_[a-z]{1,3})(\\.yml)", Pattern.CASE_INSENSITIVE); final Pattern langPattern = Pattern.compile("([a-z]{1,3}_[a-z]{1,3})(\\.yml)", Pattern.CASE_INSENSITIVE);
for (File langFile : langDirectory.listFiles()) { for (File langFile : langDirectory.listFiles()) {
Matcher langMatcher = langPattern.matcher(langFile.getName()); final Matcher langMatcher = langPattern.matcher(langFile.getName());
if (langMatcher.find()) { if (langMatcher.find()) {
String localeString = langMatcher.group(1).toLowerCase(); String localeString = langMatcher.group(1).toLowerCase();
if (!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded if (!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded
if (startup) console.sendMessage( if (startup) console.sendMessage(
Component.text("").style(plugin_style) Component.text("").style(plugin_style)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD)) .append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style))); .append(Component.text("").style(plugin_style)));
else logger.info("Found language file for " + localeString); else logger.info(String.format("Found language file for %s", localeString));
LanguageCache langCache = new LanguageCache(localeString); languageCacheMap.put(localeString, new LanguageCache(localeString));
languageCacheMap.put(localeString, langCache);
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
if (startup) console.sendMessage( if (startup) console.sendMessage(
Component.text("").style(plugin_style) Component.text("").style(plugin_style)
.append(Component.text("LANG ERROR").color(NamedTextColor.RED).decorate(TextDecoration.BOLD)) .append(Component.text("LANG ERROR").color(NamedTextColor.RED).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style))); .append(Component.text("").style(plugin_style)));
else logger.severe("Error loading language files! Language files will not reload to avoid errors, make sure to correct this before restarting the server!"); else logger.severe("Error loading language files! Language files will not reload to avoid errors, make sure to correct this before restarting the server!");
e.printStackTrace(); e.printStackTrace();
} }
} }
private Set<String> getDefaultLanguageFiles() { private Set<String> getDefaultLanguageFiles() {
Set<String> languageFiles = new HashSet<>(); try (final JarFile pluginJarFile = new JarFile(this.getFile())) {
try (JarFile jarFile = new JarFile(this.getFile())) { return pluginJarFile.stream()
jarFile.entries().asIterator().forEachRemaining(jarFileEntry -> { .map(ZipEntry::getName)
final String path = jarFileEntry.getName(); .filter(name -> name.startsWith("lang" + File.separator) && name.endsWith(".yml"))
if (path.startsWith("lang/") && path.endsWith(".yml")) .collect(Collectors.toSet());
languageFiles.add(path);
});
} catch (IOException e) { } catch (IOException e) {
logger.severe("Error while getting default language files! - " + e.getLocalizedMessage()); logger.severe("Failed getting default lang files! - "+e.getLocalizedMessage());
e.printStackTrace(); return Collections.emptySet();
} }
return languageFiles;
} }
} }

View File

@ -18,58 +18,58 @@ public class Config {
public final long cache_keep_time_seconds; public final long cache_keep_time_seconds;
public Config() throws Exception { public Config() throws Exception {
this.config = loadConfig(new File(VillagerOptimizer.getInstance().getDataFolder(), "config.yml")); // Create plugin folder first if it does not exist yet
File pluginFolder = VillagerOptimizer.getInstance().getDataFolder();
if (!pluginFolder.exists() && !pluginFolder.mkdir())
VillagerOptimizer.getLog().severe("Failed to create plugin directory.");
// Load config.yml with ConfigMaster
this.config = ConfigFile.loadConfig(new File(pluginFolder, "config.yml"));
structureConfig(); structureConfig();
this.default_lang = Locale.forLanguageTag( this.default_lang = Locale.forLanguageTag(
getString("general.default-language", "en_us", getString("general.default-language", "en_us",
"The default language that will be used if auto-language is false or no matching language file was found.") "The default language that will be used if auto-language is false or no matching language file was found.")
.replace("_", "-")); .replace("_", "-"));
this.auto_lang = getBoolean("general.auto-language", true, this.auto_lang = getBoolean("general.auto-language", true,
"If set to true, will display messages based on client language"); "If set to true, will display messages based on client language");
this.cache_keep_time_seconds = getInt("general.cache-keep-time-seconds", 30, this.cache_keep_time_seconds = getInt("general.cache-keep-time-seconds", 30,
"The amount of time in seconds a villager will be kept in the plugin's cache."); "The amount of time in seconds a villager will be kept in the plugin's cache.");
} }
private ConfigFile loadConfig(File ymlFile) throws Exception {
File parent = ymlFile.getParentFile();
if (!parent.exists() && !parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create plugin config directory.");
return ConfigFile.loadConfig(ymlFile);
}
public void saveConfig() { public void saveConfig() {
try { try {
config.save(); this.config.save();
} catch (Exception e) { } catch (Exception e) {
VillagerOptimizer.getLog().severe("Failed to save config file! - " + e.getLocalizedMessage()); VillagerOptimizer.getLog().severe("Failed to save config file! - " + e.getLocalizedMessage());
} }
} }
private void structureConfig() { private void structureConfig() {
config.addDefault("config-version", 1.00); this.config.addDefault("config-version", 1.00);
createTitledSection("General", "general"); this.createTitledSection("General", "general");
createTitledSection("Optimization", "optimization-methods"); this.createTitledSection("Optimization", "optimization-methods");
config.addDefault("optimization-methods.commands.unoptimizevillagers", null); this.config.addDefault("optimization-methods.commands.unoptimizevillagers", null);
config.addComment("optimization-methods.commands", """ this.config.addComment("optimization-methods.commands", """
If you want to disable commands, negate the following permissions:\s If you want to disable commands, negate the following permissions:\s
villageroptimizer.cmd.optimize\s villageroptimizer.cmd.optimize\s
villageroptimizer.cmd.unoptimize villageroptimizer.cmd.unoptimize
"""); """);
config.addDefault("optimization-methods.nametag-optimization.enable", true); this.config.addDefault("optimization-methods.nametag-optimization.enable", true);
createTitledSection("Villager Chunk Limit", "villager-chunk-limit"); this.createTitledSection("Villager Chunk Limit", "villager-chunk-limit");
createTitledSection("Gameplay", "gameplay"); this.createTitledSection("Gameplay", "gameplay");
config.addDefault("gameplay.restock-optimized-trades", null); this.config.addDefault("gameplay.restock-optimized-trades", null);
config.addDefault("gameplay.level-optimized-profession", null); this.config.addDefault("gameplay.level-optimized-profession", null);
config.addDefault("gameplay.rename-optimized-villagers.enable", true); this.config.addDefault("gameplay.rename-optimized-villagers.enable", true);
config.addDefault("gameplay.villagers-spawn-as-adults.enable", false); this.config.addDefault("gameplay.villagers-spawn-as-adults.enable", false);
config.addDefault("gameplay.prevent-trading-with-unoptimized.enable", false); this.config.addDefault("gameplay.prevent-trading-with-unoptimized.enable", false);
config.addDefault("gameplay.prevent-entities-from-targeting-optimized.enable", true); this.config.addDefault("gameplay.prevent-entities-from-targeting-optimized.enable", true);
config.addDefault("gameplay.prevent-damage-to-optimized.enable", true); this.config.addDefault("gameplay.prevent-damage-to-optimized.enable", true);
} }
public void createTitledSection(@NotNull String title, @NotNull String path) { public void createTitledSection(@NotNull String title, @NotNull String path) {
config.addSection(title); this.config.addSection(title);
config.addDefault(path, null); this.config.addDefault(path, null);
} }
public @NotNull ConfigFile master() { public @NotNull ConfigFile master() {
@ -77,70 +77,70 @@ public class Config {
} }
public boolean getBoolean(@NotNull String path, boolean def, @NotNull String comment) { public boolean getBoolean(@NotNull String path, boolean def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getBoolean(path, def); return this.config.getBoolean(path, def);
} }
public boolean getBoolean(@NotNull String path, boolean def) { public boolean getBoolean(@NotNull String path, boolean def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getBoolean(path, def); return this.config.getBoolean(path, def);
} }
public @NotNull String getString(@NotNull String path, @NotNull String def, @NotNull String comment) { public @NotNull String getString(@NotNull String path, @NotNull String def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getString(path, def); return this.config.getString(path, def);
} }
public @NotNull String getString(@NotNull String path, @NotNull String def) { public @NotNull String getString(@NotNull String path, @NotNull String def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getString(path, def); return this.config.getString(path, def);
} }
public double getDouble(@NotNull String path, @NotNull Double def, @NotNull String comment) { public double getDouble(@NotNull String path, @NotNull Double def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getDouble(path, def); return this.config.getDouble(path, def);
} }
public double getDouble(@NotNull String path, @NotNull Double def) { public double getDouble(@NotNull String path, @NotNull Double def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getDouble(path, def); return this.config.getDouble(path, def);
} }
public int getInt(@NotNull String path, int def, @NotNull String comment) { public int getInt(@NotNull String path, int def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getInteger(path, def); return this.config.getInteger(path, def);
} }
public int getInt(@NotNull String path, int def) { public int getInt(@NotNull String path, int def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getInteger(path, def); return this.config.getInteger(path, def);
} }
public @NotNull List<String> getList(@NotNull String path, @NotNull List<String> def, @NotNull String comment) { public @NotNull List<String> getList(@NotNull String path, @NotNull List<String> def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getStringList(path); return this.config.getStringList(path);
} }
public @NotNull List<String> getList(@NotNull String path, @NotNull List<String> def) { public @NotNull List<String> getList(@NotNull String path, @NotNull List<String> def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getStringList(path); return this.config.getStringList(path);
} }
public @NotNull ConfigSection getConfigSection(@NotNull String path, @NotNull Map<String, Object> defaultKeyValue) { public @NotNull ConfigSection getConfigSection(@NotNull String path, @NotNull Map<String, Object> defaultKeyValue) {
config.addDefault(path, null); this.config.addDefault(path, null);
config.makeSectionLenient(path); this.config.makeSectionLenient(path);
defaultKeyValue.forEach((string, object) -> config.addExample(path+"."+string, object)); defaultKeyValue.forEach((string, object) -> this.config.addExample(path+"."+string, object));
return config.getConfigSection(path); return this.config.getConfigSection(path);
} }
public @NotNull ConfigSection getConfigSection(@NotNull String path, @NotNull Map<String, Object> defaultKeyValue, @NotNull String comment) { public @NotNull ConfigSection getConfigSection(@NotNull String path, @NotNull Map<String, Object> defaultKeyValue, @NotNull String comment) {
config.addDefault(path, null, comment); this.config.addDefault(path, null, comment);
config.makeSectionLenient(path); this.config.makeSectionLenient(path);
defaultKeyValue.forEach((string, object) -> config.addExample(path+"."+string, object)); defaultKeyValue.forEach((string, object) -> this.config.addExample(path+"."+string, object));
return config.getConfigSection(path); return this.config.getConfigSection(path);
} }
public void addComment(@NotNull String path, @NotNull String comment) { public void addComment(@NotNull String path, @NotNull String comment) {
config.addComment(path, comment); this.config.addComment(path, comment);
} }
} }

View File

@ -27,7 +27,7 @@ public class LanguageCache {
// Check if the lang folder has already been created // Check if the lang folder has already been created
File parent = langYML.getParentFile(); File parent = langYML.getParentFile();
if (!parent.exists() && !parent.mkdir()) if (!parent.exists() && !parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create lang directory."); VillagerOptimizer.getLog().severe("Failed to create lang directory.");
// Check if the file already exists and save the one from the plugins resources folder if it does not // Check if the file already exists and save the one from the plugins resources folder if it does not
if (!langYML.exists()) if (!langYML.exists())
plugin.saveResource("lang/" + locale + ".yml", false); plugin.saveResource("lang/" + locale + ".yml", false);

View File

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

View File

@ -39,7 +39,7 @@ public final class VillagerCache {
} }
public @NotNull WrappedVillager add(@NotNull Villager villager) { public @NotNull WrappedVillager add(@NotNull Villager villager) {
return add(new WrappedVillager(villager)); return this.add(new WrappedVillager(villager));
} }
public boolean contains(@NotNull UUID uuid) { public boolean contains(@NotNull UUID uuid) {

View File

@ -19,14 +19,13 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
public final class VillagerOptimizer extends JavaPlugin { public final class VillagerOptimizer extends JavaPlugin {
@ -118,56 +117,51 @@ public final class VillagerOptimizer extends JavaPlugin {
languageCacheMap = new HashMap<>(); languageCacheMap = new HashMap<>();
ConsoleCommandSender console = getServer().getConsoleSender(); ConsoleCommandSender console = getServer().getConsoleSender();
try { try {
File langDirectory = new File(getDataFolder() + "/lang"); File langDirectory = new File(getDataFolder() + File.separator + "lang");
Files.createDirectories(langDirectory.toPath()); Files.createDirectories(langDirectory.toPath());
for (String fileName : getDefaultLanguageFiles()) { for (String fileName : getDefaultLanguageFiles()) {
String localeString = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.lastIndexOf('.')); final String localeString = fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.lastIndexOf('.'));
if (startup) console.sendMessage( if (startup) console.sendMessage(
Component.text("").style(plugin_style) Component.text("").style(plugin_style)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD)) .append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style))); .append(Component.text("").style(plugin_style)));
else logger.info("Found language file for " + localeString); else logger.info(String.format("Found language file for %s", localeString));
LanguageCache langCache = new LanguageCache(localeString); languageCacheMap.put(localeString, new LanguageCache(localeString));
languageCacheMap.put(localeString, langCache);
} }
Pattern langPattern = Pattern.compile("([a-z]{1,3}_[a-z]{1,3})(\\.yml)", Pattern.CASE_INSENSITIVE); final Pattern langPattern = Pattern.compile("([a-z]{1,3}_[a-z]{1,3})(\\.yml)", Pattern.CASE_INSENSITIVE);
for (File langFile : langDirectory.listFiles()) { for (File langFile : langDirectory.listFiles()) {
Matcher langMatcher = langPattern.matcher(langFile.getName()); final Matcher langMatcher = langPattern.matcher(langFile.getName());
if (langMatcher.find()) { if (langMatcher.find()) {
String localeString = langMatcher.group(1).toLowerCase(); String localeString = langMatcher.group(1).toLowerCase();
if (!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded if (!languageCacheMap.containsKey(localeString)) { // make sure it wasn't a default file that we already loaded
if (startup) console.sendMessage( if (startup) console.sendMessage(
Component.text("").style(plugin_style) Component.text("").style(plugin_style)
.append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD)) .append(Component.text(" "+localeString).color(NamedTextColor.WHITE).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style))); .append(Component.text("").style(plugin_style)));
else logger.info("Found language file for " + localeString); else logger.info(String.format("Found language file for %s", localeString));
LanguageCache langCache = new LanguageCache(localeString); languageCacheMap.put(localeString, new LanguageCache(localeString));
languageCacheMap.put(localeString, langCache);
} }
} }
} }
} catch (Exception e) { } catch (Exception e) {
if (startup) console.sendMessage( if (startup) console.sendMessage(
Component.text("").style(plugin_style) Component.text("").style(plugin_style)
.append(Component.text("LANG ERROR").color(NamedTextColor.RED).decorate(TextDecoration.BOLD)) .append(Component.text("LANG ERROR").color(NamedTextColor.RED).decorate(TextDecoration.BOLD))
.append(Component.text("").style(plugin_style))); .append(Component.text("").style(plugin_style)));
else logger.severe("Error loading language files! Language files will not reload to avoid errors, make sure to correct this before restarting the server!"); else logger.severe("Error loading language files! Language files will not reload to avoid errors, make sure to correct this before restarting the server!");
e.printStackTrace(); e.printStackTrace();
} }
} }
private Set<String> getDefaultLanguageFiles() { private Set<String> getDefaultLanguageFiles() {
Set<String> languageFiles = new HashSet<>(); try (final JarFile pluginJarFile = new JarFile(this.getFile())) {
try (JarFile jarFile = new JarFile(this.getFile())) { return pluginJarFile.stream()
jarFile.entries().asIterator().forEachRemaining(jarFileEntry -> { .map(ZipEntry::getName)
final String path = jarFileEntry.getName(); .filter(name -> name.startsWith("lang" + File.separator) && name.endsWith(".yml"))
if (path.startsWith("lang/") && path.endsWith(".yml")) .collect(Collectors.toSet());
languageFiles.add(path);
});
} catch (IOException e) { } catch (IOException e) {
logger.severe("Error while getting default language files! - " + e.getLocalizedMessage()); logger.severe("Failed getting default lang files! - "+e.getLocalizedMessage());
e.printStackTrace(); return Collections.emptySet();
} }
return languageFiles;
} }
} }

View File

@ -18,58 +18,58 @@ public class Config {
public final long cache_keep_time_seconds; public final long cache_keep_time_seconds;
public Config() throws Exception { public Config() throws Exception {
this.config = loadConfig(new File(VillagerOptimizer.getInstance().getDataFolder(), "config.yml")); // Create plugin folder first if it does not exist yet
File pluginFolder = VillagerOptimizer.getInstance().getDataFolder();
if (!pluginFolder.exists() && !pluginFolder.mkdir())
VillagerOptimizer.getLog().severe("Failed to create plugin directory.");
// Load config.yml with ConfigMaster
this.config = ConfigFile.loadConfig(new File(pluginFolder, "config.yml"));
structureConfig(); structureConfig();
this.default_lang = Locale.forLanguageTag( this.default_lang = Locale.forLanguageTag(
getString("general.default-language", "en_us", getString("general.default-language", "en_us",
"The default language that will be used if auto-language is false or no matching language file was found.") "The default language that will be used if auto-language is false or no matching language file was found.")
.replace("_", "-")); .replace("_", "-"));
this.auto_lang = getBoolean("general.auto-language", true, this.auto_lang = getBoolean("general.auto-language", true,
"If set to true, will display messages based on client language"); "If set to true, will display messages based on client language");
this.cache_keep_time_seconds = getInt("general.cache-keep-time-seconds", 30, this.cache_keep_time_seconds = getInt("general.cache-keep-time-seconds", 30,
"The amount of time in seconds a villager will be kept in the plugin's cache."); "The amount of time in seconds a villager will be kept in the plugin's cache.");
} }
private ConfigFile loadConfig(File ymlFile) throws Exception {
File parent = ymlFile.getParentFile();
if (!parent.exists() && !parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create plugin config directory.");
return ConfigFile.loadConfig(ymlFile);
}
public void saveConfig() { public void saveConfig() {
try { try {
config.save(); this.config.save();
} catch (Exception e) { } catch (Exception e) {
VillagerOptimizer.getLog().severe("Failed to save config file! - " + e.getLocalizedMessage()); VillagerOptimizer.getLog().severe("Failed to save config file! - " + e.getLocalizedMessage());
} }
} }
private void structureConfig() { private void structureConfig() {
config.addDefault("config-version", 1.00); this.config.addDefault("config-version", 1.00);
createTitledSection("General", "general"); this.createTitledSection("General", "general");
createTitledSection("Optimization", "optimization-methods"); this.createTitledSection("Optimization", "optimization-methods");
config.addDefault("optimization-methods.commands.unoptimizevillagers", null); this.config.addDefault("optimization-methods.commands.unoptimizevillagers", null);
config.addComment("optimization-methods.commands", """ this.config.addComment("optimization-methods.commands", """
If you want to disable commands, negate the following permissions:\s If you want to disable commands, negate the following permissions:\s
villageroptimizer.cmd.optimize\s villageroptimizer.cmd.optimize\s
villageroptimizer.cmd.unoptimize villageroptimizer.cmd.unoptimize
"""); """);
config.addDefault("optimization-methods.nametag-optimization.enable", true); this.config.addDefault("optimization-methods.nametag-optimization.enable", true);
createTitledSection("Villager Chunk Limit", "villager-chunk-limit"); this.createTitledSection("Villager Chunk Limit", "villager-chunk-limit");
createTitledSection("Gameplay", "gameplay"); this.createTitledSection("Gameplay", "gameplay");
config.addDefault("gameplay.restock-optimized-trades", null); this.config.addDefault("gameplay.restock-optimized-trades", null);
config.addDefault("gameplay.level-optimized-profession", null); this.config.addDefault("gameplay.level-optimized-profession", null);
config.addDefault("gameplay.rename-optimized-villagers.enable", true); this.config.addDefault("gameplay.rename-optimized-villagers.enable", true);
config.addDefault("gameplay.villagers-spawn-as-adults.enable", false); this.config.addDefault("gameplay.villagers-spawn-as-adults.enable", false);
config.addDefault("gameplay.prevent-trading-with-unoptimized.enable", false); this.config.addDefault("gameplay.prevent-trading-with-unoptimized.enable", false);
config.addDefault("gameplay.prevent-entities-from-targeting-optimized.enable", true); this.config.addDefault("gameplay.prevent-entities-from-targeting-optimized.enable", true);
config.addDefault("gameplay.prevent-damage-to-optimized.enable", true); this.config.addDefault("gameplay.prevent-damage-to-optimized.enable", true);
} }
public void createTitledSection(@NotNull String title, @NotNull String path) { public void createTitledSection(@NotNull String title, @NotNull String path) {
config.addSection(title); this.config.addSection(title);
config.addDefault(path, null); this.config.addDefault(path, null);
} }
public @NotNull ConfigFile master() { public @NotNull ConfigFile master() {
@ -77,70 +77,70 @@ public class Config {
} }
public boolean getBoolean(@NotNull String path, boolean def, @NotNull String comment) { public boolean getBoolean(@NotNull String path, boolean def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getBoolean(path, def); return this.config.getBoolean(path, def);
} }
public boolean getBoolean(@NotNull String path, boolean def) { public boolean getBoolean(@NotNull String path, boolean def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getBoolean(path, def); return this.config.getBoolean(path, def);
} }
public @NotNull String getString(@NotNull String path, @NotNull String def, @NotNull String comment) { public @NotNull String getString(@NotNull String path, @NotNull String def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getString(path, def); return this.config.getString(path, def);
} }
public @NotNull String getString(@NotNull String path, @NotNull String def) { public @NotNull String getString(@NotNull String path, @NotNull String def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getString(path, def); return this.config.getString(path, def);
} }
public double getDouble(@NotNull String path, @NotNull Double def, @NotNull String comment) { public double getDouble(@NotNull String path, @NotNull Double def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getDouble(path, def); return this.config.getDouble(path, def);
} }
public double getDouble(@NotNull String path, @NotNull Double def) { public double getDouble(@NotNull String path, @NotNull Double def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getDouble(path, def); return this.config.getDouble(path, def);
} }
public int getInt(@NotNull String path, int def, @NotNull String comment) { public int getInt(@NotNull String path, int def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getInteger(path, def); return this.config.getInteger(path, def);
} }
public int getInt(@NotNull String path, int def) { public int getInt(@NotNull String path, int def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getInteger(path, def); return this.config.getInteger(path, def);
} }
public @NotNull List<String> getList(@NotNull String path, @NotNull List<String> def, @NotNull String comment) { public @NotNull List<String> getList(@NotNull String path, @NotNull List<String> def, @NotNull String comment) {
config.addDefault(path, def, comment); this.config.addDefault(path, def, comment);
return config.getStringList(path); return this.config.getStringList(path);
} }
public @NotNull List<String> getList(@NotNull String path, @NotNull List<String> def) { public @NotNull List<String> getList(@NotNull String path, @NotNull List<String> def) {
config.addDefault(path, def); this.config.addDefault(path, def);
return config.getStringList(path); return this.config.getStringList(path);
} }
public @NotNull ConfigSection getConfigSection(@NotNull String path, @NotNull Map<String, Object> defaultKeyValue) { public @NotNull ConfigSection getConfigSection(@NotNull String path, @NotNull Map<String, Object> defaultKeyValue) {
config.addDefault(path, null); this.config.addDefault(path, null);
config.makeSectionLenient(path); this.config.makeSectionLenient(path);
defaultKeyValue.forEach((string, object) -> config.addExample(path+"."+string, object)); defaultKeyValue.forEach((string, object) -> this.config.addExample(path+"."+string, object));
return config.getConfigSection(path); return this.config.getConfigSection(path);
} }
public @NotNull ConfigSection getConfigSection(@NotNull String path, @NotNull Map<String, Object> defaultKeyValue, @NotNull String comment) { public @NotNull ConfigSection getConfigSection(@NotNull String path, @NotNull Map<String, Object> defaultKeyValue, @NotNull String comment) {
config.addDefault(path, null, comment); this.config.addDefault(path, null, comment);
config.makeSectionLenient(path); this.config.makeSectionLenient(path);
defaultKeyValue.forEach((string, object) -> config.addExample(path+"."+string, object)); defaultKeyValue.forEach((string, object) -> this.config.addExample(path+"."+string, object));
return config.getConfigSection(path); return this.config.getConfigSection(path);
} }
public void addComment(@NotNull String path, @NotNull String comment) { public void addComment(@NotNull String path, @NotNull String comment) {
config.addComment(path, comment); this.config.addComment(path, comment);
} }
} }

View File

@ -27,7 +27,7 @@ public class LanguageCache {
// Check if the lang folder has already been created // Check if the lang folder has already been created
File parent = langYML.getParentFile(); File parent = langYML.getParentFile();
if (!parent.exists() && !parent.mkdir()) if (!parent.exists() && !parent.mkdir())
VillagerOptimizer.getLog().severe("Unable to create lang directory."); VillagerOptimizer.getLog().severe("Failed to create lang directory.");
// Check if the file already exists and save the one from the plugins resources folder if it does not // Check if the file already exists and save the one from the plugins resources folder if it does not
if (!langYML.exists()) if (!langYML.exists())
plugin.saveResource("lang/" + locale + ".yml", false); plugin.saveResource("lang/" + locale + ".yml", false);

View File

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