From 3ebb86268a543d154cdf20ac2ffbf7b56bdf9794 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Thu, 28 Oct 2021 23:09:28 -0400 Subject: 1.3.1 build 2 --- .../hideAndSeek/configuration/Config.java | 135 ++++++++---------- .../hideAndSeek/configuration/ConfigManager.java | 156 +++++++++++++++++++++ .../hideAndSeek/configuration/Localization.java | 35 ++--- 3 files changed, 227 insertions(+), 99 deletions(-) create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java (limited to 'src/main/java/net/tylermurphy/hideAndSeek/configuration') diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java index efae5d2..d82f01c 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java @@ -1,15 +1,19 @@ package net.tylermurphy.hideAndSeek.configuration; +import java.io.File; import java.util.Map; import java.util.Map.Entry; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.util.Vector; import net.tylermurphy.hideAndSeek.Main; public class Config { + + static ConfigManager manager; public static String messagePrefix, @@ -46,95 +50,78 @@ public class Config { saveMaxX, saveMaxZ; - public static FileConfiguration getConfig() { - return Main.plugin.getConfig(); - } - - public static void saveConfig() { - Main.plugin.saveConfig(); - } - public static void loadConfig() { - - Main.plugin.reloadConfig(); - + + manager = new ConfigManager("config.yml"); + //Spawn spawnPosition = new Vector( - getConfig().getDouble("spawns.game.x"), - Math.max(0,Math.min(255,getConfig().getDouble("spawns.game.y"))), - getConfig().getDouble("spawns.game.z") - ); - spawnWorld = getConfig().getString("spawns.game.world"); - + manager.getDouble("spawns.game.x"), + Math.max(0, Math.min(255, manager.getDouble("spawns.game.y"))), + manager.getDouble("spawns.game.z") + ); + spawnWorld = manager.getString("spawns.game.world"); + ///Lobby lobbyPosition = new Vector( - getConfig().getDouble("spawns.lobby.x"), - Math.max(0,Math.min(255,getConfig().getDouble("spawns.lobby.y"))), - getConfig().getDouble("spawns.lobby.z") - ); - lobbyWorld = getConfig().getString("spawns.lobby.world"); - - announceMessagesToNonPlayers = getConfig().getBoolean("announceMessagesToNonPlayers"); - + manager.getDouble("spawns.lobby.x"), + Math.max(0, Math.min(255, manager.getDouble("spawns.lobby.y"))), + manager.getDouble("spawns.lobby.z") + ); + lobbyWorld = manager.getString("spawns.lobby.world"); + + announceMessagesToNonPlayers = manager.getBoolean("announceMessagesToNonPlayers"); + exitPosition = new Vector( - getConfig().getDouble("spawns.exit.x"), - Math.max(0,Math.min(255,getConfig().getDouble("spawns.exit.y"))), - getConfig().getDouble("spawns.exit.z") - ); - exitWorld = getConfig().getString("spawns.exit.world"); - + manager.getDouble("spawns.exit.x"), + Math.max(0, Math.min(255, manager.getDouble("spawns.exit.y"))), + manager.getDouble("spawns.exit.z") + ); + exitWorld = manager.getString("spawns.exit.world"); + //World border worldborderPosition = new Vector( - getConfig().getInt("worldBorder.x"), - 0, - getConfig().getInt("worldBorder.z") - ); - worldborderSize = Math.max(100,getConfig().getInt("worldBorder.size")); - worldborderDelay = Math.max(1,getConfig().getInt("worldBorder.delay")); - worldborderEnabled = getConfig().getBoolean("worldBorder.enabled"); - + manager.getInt("worldBorder.x"), + 0, + manager.getInt("worldBorder.z") + ); + worldborderSize = Math.max(100, manager.getInt("worldBorder.size")); + worldborderDelay = Math.max(1, manager.getInt("worldBorder.delay")); + worldborderEnabled = manager.getBoolean("worldBorder.enabled"); + //Prefix char SYMBOLE = '\u00A7'; - String SYMBOLE_STRING = new String(new char[] {SYMBOLE}); - - messagePrefix = getConfig().getString("prefix.default").replace("&", SYMBOLE_STRING); - errorPrefix = getConfig().getString("prefix.error").replace("&", SYMBOLE_STRING); - tauntPrefix = getConfig().getString("prefix.taunt").replace("&", SYMBOLE_STRING); - worldborderPrefix = getConfig().getString("prefix.border").replace("&", SYMBOLE_STRING); - abortPrefix = getConfig().getString("prefix.abort").replace("&", SYMBOLE_STRING); - gameoverPrefix = getConfig().getString("prefix.gameover").replace("&", SYMBOLE_STRING); - warningPrefix = getConfig().getString("prefix.warning").replace("&", SYMBOLE_STRING); - + String SYMBOLE_STRING = new String(new char[]{SYMBOLE}); + + messagePrefix = manager.getString("prefix.default").replace("&", SYMBOLE_STRING); + errorPrefix = manager.getString("prefix.error").replace("&", SYMBOLE_STRING); + tauntPrefix = manager.getString("prefix.taunt").replace("&", SYMBOLE_STRING); + worldborderPrefix = manager.getString("prefix.border").replace("&", SYMBOLE_STRING); + abortPrefix = manager.getString("prefix.abort").replace("&", SYMBOLE_STRING); + gameoverPrefix = manager.getString("prefix.gameover").replace("&", SYMBOLE_STRING); + warningPrefix = manager.getString("prefix.warning").replace("&", SYMBOLE_STRING); + //Map Bounds - saveMinX = getConfig().getInt("bounds.min.x"); - saveMinZ = getConfig().getInt("bounds.min.z"); - saveMaxX = getConfig().getInt("bounds.max.x"); - saveMaxZ = getConfig().getInt("bounds.max.z"); - + saveMinX = manager.getInt("bounds.min.x"); + saveMinZ = manager.getInt("bounds.min.z"); + saveMaxX = manager.getInt("bounds.max.x"); + saveMaxZ = manager.getInt("bounds.max.z"); + //Other - nametagsVisible = getConfig().getBoolean("nametagsVisible"); - permissionsRequired = getConfig().getBoolean("permissionsRequired"); - minPlayers = Math.max(2,getConfig().getInt("minPlayers")); - gameLength = getConfig().getInt("gameLength"); - - getConfig().options().copyDefaults(true); - saveConfig(); - - } - - public static void addToSection(String sectionName, Map values) { - ConfigurationSection section = getConfig().getConfigurationSection(sectionName); - if(section == null) section = getConfig().createSection(sectionName); - Map sectionValues = section.getValues(true); - for(Entry entry : values.entrySet()) { - sectionValues.put(entry.getKey(), entry.getValue()); - } - getConfig().createSection(sectionName, sectionValues); - saveConfig(); + nametagsVisible = manager.getBoolean("nametagsVisible"); + permissionsRequired = manager.getBoolean("permissionsRequired"); + minPlayers = Math.max(2, manager.getInt("minPlayers")); + gameLength = manager.getInt("gameLength"); + + manager.saveConfig(); } public static void addToConfig(String path, Object value) { - getConfig().set(path, value); + manager.set(path, value); + } + + public static void saveConfig() { + manager.saveConfig(); } } \ No newline at end of file diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java new file mode 100644 index 0000000..7ea38db --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java @@ -0,0 +1,156 @@ +package net.tylermurphy.hideAndSeek.configuration; + +import net.tylermurphy.hideAndSeek.Main; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.*; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +public class ConfigManager { + + private File file; + private YamlConfiguration config,defaultConfig; + + public ConfigManager(String filename){ + this.file = new File(Main.plugin.getDataFolder(), filename); + + if(!file.exists()){ + saveDefaultConfiguration(); + } + + this.config = YamlConfiguration.loadConfiguration(file); + + InputStream input = Main.plugin.getResource(file.getName()); + InputStreamReader reader = new InputStreamReader(input); + this.defaultConfig = YamlConfiguration.loadConfiguration(reader); + try{ + input.close(); + reader.close(); + } catch (IOException e){} + + } + + private void saveDefaultConfiguration(){ + try{ + InputStream input = Main.plugin.getResource(file.getName()); + java.nio.file.Files.copy(input, file.toPath()); + input.close(); + } catch(IOException e){ + e.printStackTrace(); + } + } + + public void addToSection(String sectionName, Map values) { + ConfigurationSection section = config.getConfigurationSection(sectionName); + if(section == null) section = config.createSection(sectionName); + Map sectionValues = section.getValues(true); + for(Map.Entry entry : values.entrySet()) { + sectionValues.put(entry.getKey(), entry.getValue()); + } + config.createSection(sectionName, sectionValues); + } + + public void addToConfig(String path, Object value) { + config.set(path, value); + } + + public double getDouble(String path){ + double value = config.getDouble(path); + if(value == 0.0D){ + return defaultConfig.getDouble(path); + } else { + return value; + } + } + + public int getInt(String path){ + int value = config.getInt(path); + if(value == 0){ + return defaultConfig.getInt(path); + } else { + return value; + } + } + + public String getString(String path){ + String value = config.getString(path); + if(value == null){ + return defaultConfig.getString(path); + } else { + return value; + } + } + + public boolean getBoolean(String path){ + boolean value = config.getBoolean(path); + if(value == false){ + return defaultConfig.getBoolean(path); + } else { + return value; + } + } + + public ConfigurationSection getConfigurationSection(String path){ + ConfigurationSection section = config.getConfigurationSection(path); + if(section == null){ + return defaultConfig.getConfigurationSection(path); + } else { + return section; + } + } + + public void set(String path, Object value){ + config.set(path, value); + } + + public void saveConfig(){ + try { + InputStream is = Main.plugin.getResource(file.getName()); + StringBuilder textBuilder = new StringBuilder(); + Reader reader = new BufferedReader(new InputStreamReader(is, Charset.forName(StandardCharsets.UTF_8.name()))); + int c = 0; + while((c = reader.read()) != -1){ + textBuilder.append((char) c); + } + String yamlString = textBuilder.toString(); + Map temp = config.getValues(true); + for(Map.Entry entry: temp.entrySet()){ + System.out.println(entry.getKey() + " " + entry.getValue().getClass().getName()); + if(entry.getValue() instanceof Integer || entry.getValue() instanceof Double || entry.getValue() instanceof String || entry.getValue() instanceof Boolean){ + String[] parts = entry.getKey().split("\\."); + int index = 0; + int i = 0; + for(String part : parts) { + if(i == 0) { + index = yamlString.indexOf(part, index); + } else { + index = yamlString.indexOf(" " + part, index); + index++; + } + i++; + if(index == -1) break; + } + if(index == -1) continue;; + int start = yamlString.indexOf(' ', index); + int end = yamlString.indexOf('\n', index); + String replace = entry.getValue().toString(); + if(entry.getValue() instanceof String){ + replace = "\"" + replace + "\""; + } + StringBuilder builder = new StringBuilder(yamlString); + builder.replace(start+1, end, replace); + yamlString = builder.toString(); + } + } + PrintWriter out = new PrintWriter(file); + out.print(yamlString); + out.close(); + } catch (IOException e){ + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java index ee34d4a..82d19d4 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java @@ -16,44 +16,29 @@ public class Localization { public static final Map LOCAL = new HashMap(); - static YamlConfiguration config, defaultConfig; - static File location; + private static ConfigManager manager; - public static boolean init() { - - Main.plugin.saveResource("localization.yml", false); - String path = Main.data.getAbsolutePath()+File.separator + "localization.yml"; - location = new File(path); - config = YamlConfiguration.loadConfiguration(location); - - InputStream is = Main.plugin.getResource("localization.yml"); - InputStreamReader isr = new InputStreamReader(is); - defaultConfig = YamlConfiguration.loadConfiguration(isr); + public static boolean loadLocalization() { + + manager = new ConfigManager("localization.yml"); - for(String key : config.getConfigurationSection("Localization").getKeys(false)) { + for(String key : manager.getConfigurationSection("Localization").getKeys(false)) { LOCAL.put( key, - new LocalizationString( ChatColor.translateAlternateColorCodes('&', config.getString("Localization."+key) ) ) + new LocalizationString( ChatColor.translateAlternateColorCodes('&', manager.getString("Localization."+key) ) ) ); } + + manager.saveConfig(); + return true; } public static LocalizationString message(String key) { LocalizationString temp = LOCAL.get(key); if(temp == null) { - config.set("Localization."+key, defaultConfig.getString("Localization."+key)); - try { - config.save(location); - } catch (IOException e) { - Main.plugin.getLogger().severe(e.getMessage()); - } - LOCAL.put(key, - new LocalizationString( ChatColor.translateAlternateColorCodes('&', defaultConfig.getString("Localization."+key) ) ) - ); - return new LocalizationString(LOCAL.get(key).toString()); + return new LocalizationString(ChatColor.RED + "" + ChatColor.ITALIC + key + "is not found in localization.yml. This is a plugin issue, please report it."); } return new LocalizationString(temp.toString()); - } } -- cgit v1.2.3-freya