diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2022-06-01 20:03:01 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-01 20:03:01 -0400 |
commit | c506030cbabe938f0afccbf3489ab72e7463e1e7 (patch) | |
tree | 0cafe99d4b314c761d2621457c4a4a824e53350e /src/main/java/net/tylermurphy/hideAndSeek/configuration | |
parent | move code of conduct and contributing guidelines (diff) | |
parent | Update CONTRIBUTING.md (diff) | |
download | kenshinshideandseek-c506030cbabe938f0afccbf3489ab72e7463e1e7.tar.gz kenshinshideandseek-c506030cbabe938f0afccbf3489ab72e7463e1e7.tar.bz2 kenshinshideandseek-c506030cbabe938f0afccbf3489ab72e7463e1e7.zip |
Merge pull request #68 from tylermurphy534/1.5.0
1.5.0
Diffstat (limited to 'src/main/java/net/tylermurphy/hideAndSeek/configuration')
4 files changed, 321 insertions, 192 deletions
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java index c021fce..4aeac68 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java @@ -21,13 +21,16 @@ package net.tylermurphy.hideAndSeek.configuration; import com.cryptomorin.xseries.XItemStack; import com.cryptomorin.xseries.XMaterial; -import net.tylermurphy.hideAndSeek.util.Version; +import com.cryptomorin.xseries.XSound; +import net.tylermurphy.hideAndSeek.Main; +import net.tylermurphy.hideAndSeek.game.util.CountdownDisplay; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; @@ -36,36 +39,45 @@ import java.util.Optional; public class Config { - private static ConfigManager config, leaderboard; - + private static ConfigManager config; + public static String messagePrefix, errorPrefix, tauntPrefix, - worldborderPrefix, + worldBorderPrefix, abortPrefix, - gameoverPrefix, + gameOverPrefix, warningPrefix, spawnWorld, exitWorld, lobbyWorld, locale, - leaveServer; + leaveServer, + placeholderError, + placeholderNoData, + databaseType, + databaseHost, + databasePort, + databaseUser, + databasePass, + databaseName; public static Vector spawnPosition, lobbyPosition, exitPosition, - worldborderPosition; + worldBorderPosition; - public static boolean - nametagsVisible, + public static boolean + nameTagsVisible, permissionsRequired, announceMessagesToNonPlayers, - worldborderEnabled, + worldBorderEnabled, tauntEnabled, tauntCountdown, tauntLast, + alwaysGlow, glowEnabled, glowStackable, pvpEnabled, @@ -74,14 +86,17 @@ public class Config { lobbyCountdownEnabled, seekerPing, bungeeLeave, - lobbyItemStartAdmin; + lobbyItemStartAdmin, + leaveOnEnd, + mapSaveEnabled, + allowNaturalCauses; public static int minPlayers, - worldborderSize, - worldborderDelay, + worldBorderSize, + worldBorderDelay, currentWorldborderSize, - worldborderChange, + worldBorderChange, gameLength, saveMinX, saveMinZ, @@ -97,7 +112,14 @@ public class Config { seekerPingLevel2, seekerPingLevel3, lobbyItemLeavePosition, - lobbyItemStartPosition; + lobbyItemStartPosition, + flightToggleItemPosition, + teleportItemPosition; + + public static float + seekerPingLeadingVolume, + seekerPingVolume, + seekerPingPitch; public static List<String> blockedCommands, @@ -123,18 +145,28 @@ public class Config { public static ItemStack lobbyLeaveItem, - lobbyStartItem; + lobbyStartItem, + glowPowerupItem, + flightToggleItem, + teleportItem; + + public static XSound + ringingSound, + heartbeatSound; + + public static CountdownDisplay + countdownDisplay; public static void loadConfig() { - config = new ConfigManager("config.yml"); + config = ConfigManager.create("config.yml"); config.saveConfig(); - leaderboard = new ConfigManager("leaderboard.yml"); + ConfigManager leaderboard = ConfigManager.create("leaderboard.yml"); //Spawn spawnPosition = new Vector( config.getDouble("spawns.game.x"), - Math.max(Version.atLeast("1.18") ? -64 : 0, Math.min(255, config.getDouble("spawns.game.y"))), + Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.game.y"))), config.getDouble("spawns.game.z") ); spawnWorld = config.getString("spawns.game.world"); @@ -142,7 +174,7 @@ public class Config { ///Lobby lobbyPosition = new Vector( config.getDouble("spawns.lobby.x"), - Math.max(Version.atLeast("1.18") ? -64 : 0, Math.min(255, config.getDouble("spawns.lobby.y"))), + Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.lobby.y"))), config.getDouble("spawns.lobby.z") ); lobbyWorld = config.getString("spawns.lobby.world"); @@ -151,21 +183,21 @@ public class Config { exitPosition = new Vector( config.getDouble("spawns.exit.x"), - Math.max(Version.atLeast("1.18") ? -64 : 0, Math.min(255, config.getDouble("spawns.exit.y"))), + Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.exit.y"))), config.getDouble("spawns.exit.z") ); exitWorld = config.getString("spawns.exit.world"); //World border - worldborderPosition = new Vector( + worldBorderPosition = new Vector( config.getInt("worldBorder.x"), 0, config.getInt("worldBorder.z") ); - worldborderSize = Math.max(100, config.getInt("worldBorder.size")); - worldborderDelay = Math.max(1, config.getInt("worldBorder.delay")); - worldborderEnabled = config.getBoolean("worldBorder.enabled"); - worldborderChange = config.getInt("worldBorder.moveAmount"); + worldBorderSize = Math.max(100, config.getInt("worldBorder.size")); + worldBorderDelay = Math.max(1, config.getInt("worldBorder.delay")); + worldBorderEnabled = config.getBoolean("worldBorder.enabled"); + worldBorderChange = config.getInt("worldBorder.moveAmount"); //Prefix char SYMBOLE = '\u00A7'; @@ -174,9 +206,9 @@ public class Config { messagePrefix = config.getString("prefix.default").replace("&", SYMBOLE_STRING); errorPrefix = config.getString("prefix.error").replace("&", SYMBOLE_STRING); tauntPrefix = config.getString("prefix.taunt").replace("&", SYMBOLE_STRING); - worldborderPrefix = config.getString("prefix.border").replace("&", SYMBOLE_STRING); + worldBorderPrefix = config.getString("prefix.border").replace("&", SYMBOLE_STRING); abortPrefix = config.getString("prefix.abort").replace("&", SYMBOLE_STRING); - gameoverPrefix = config.getString("prefix.gameover").replace("&", SYMBOLE_STRING); + gameOverPrefix = config.getString("prefix.gameover").replace("&", SYMBOLE_STRING); warningPrefix = config.getString("prefix.warning").replace("&", SYMBOLE_STRING); //Map Bounds @@ -184,6 +216,7 @@ public class Config { saveMinZ = config.getInt("bounds.min.z"); saveMaxX = config.getInt("bounds.max.x"); saveMaxZ = config.getInt("bounds.max.z"); + mapSaveEnabled = config.getBoolean("mapSaveEnabled"); //Taunt tauntEnabled = config.getBoolean("taunt.enabled"); @@ -192,9 +225,13 @@ public class Config { tauntLast = config.getBoolean("taunt.whenLastPerson"); //Glow + alwaysGlow = config.getBoolean("alwaysGlow") && Main.getInstance().supports(9); glowLength = Math.max(1, config.getInt("glow.time")); glowStackable = config.getBoolean("glow.stackable"); - glowEnabled = config.getBoolean("glow.enabled") && Version.atLeast("1.9"); + glowEnabled = config.getBoolean("glow.enabled") && Main.getInstance().supports(9) && !alwaysGlow; + if (glowEnabled) { + glowPowerupItem = createItemStack("glow"); + } //Lobby minPlayers = Math.max(2, config.getInt("minPlayers")); @@ -209,23 +246,40 @@ public class Config { seekerPingLevel1 = config.getInt("seekerPing.distances.level1"); seekerPingLevel2 = config.getInt("seekerPing.distances.level2"); seekerPingLevel3 = config.getInt("seekerPing.distances.level3"); + seekerPingLeadingVolume = config.getFloat("seekerPing.sounds.leadingVolume"); + seekerPingVolume = config.getFloat("seekerPing.sounds.volume"); + seekerPingPitch = config.getFloat("seekerPing.sounds.pitch"); + Optional<XSound> heartbeatOptional = XSound.matchXSound(config.getString("seekerPing.sounds.heartbeatNoise")); + heartbeatSound = heartbeatOptional.orElse(XSound.BLOCK_NOTE_BLOCK_BASEDRUM); + Optional<XSound> ringingOptional = XSound.matchXSound(config.getString("seekerPing.sounds.ringingNoise")); + ringingSound = ringingOptional.orElse(XSound.BLOCK_NOTE_BLOCK_PLING); //Other - nametagsVisible = config.getBoolean("nametagsVisible"); + nameTagsVisible = config.getBoolean("nametagsVisible"); permissionsRequired = config.getBoolean("permissionsRequired"); gameLength = config.getInt("gameLength"); pvpEnabled = config.getBoolean("pvp"); + allowNaturalCauses = config.getBoolean("allowNaturalCauses"); autoJoin = config.getBoolean("autoJoin"); teleportToExit = config.getBoolean("teleportToExit"); locale = config.getString("locale", "local"); blockedCommands = config.getStringList("blockedCommands"); + leaveOnEnd = config.getBoolean("leaveOnEnd"); + placeholderError = config.getString("placeholder.incorrect"); + placeholderNoData = config.getString("placeholder.noData"); + try { + countdownDisplay = CountdownDisplay.valueOf(config.getString("hideCountdownDisplay")); + } catch (IllegalArgumentException e) { + countdownDisplay = CountdownDisplay.CHAT; + Main.getInstance().getLogger().warning("hideCountdownDisplay: "+config.getString("hideCountdownDisplay")+" is not a valid configuration option!"); + } blockedInteracts = new ArrayList<>(); List<String> tempInteracts = config.getStringList("blockedInteracts"); - for(String id : tempInteracts){ + for(String id : tempInteracts) { Optional<XMaterial> optional_mat = XMaterial.matchXMaterial(id); - if(optional_mat.isPresent()){ + if (optional_mat.isPresent()) { Material mat = optional_mat.get().parseMaterial(); - if(mat != null){ + if (mat != null) { blockedInteracts.add(mat.name()); } } @@ -252,29 +306,35 @@ public class Config { BORDER_DECREASING = leaderboard.getString("border.decreasing"); //Lobby Items - if(config.getBoolean("lobbyItems.leave.enabled")) { - ConfigurationSection item = new YamlConfiguration().createSection("temp"); - item.set("name", ChatColor.translateAlternateColorCodes('&',config.getString("lobbyItems.leave.name"))); - item.set("material", config.getString("lobbyItems.leave.material")); - List<String> lore = config.getStringList("lobbyItems.leave.lore"); - if (lore != null && !lore.isEmpty()) item.set("lore", lore); - ItemStack temp = null; - try{ temp = XItemStack.deserialize(item); } catch(Exception ignored){} - lobbyLeaveItem = temp; + if (config.getBoolean("lobbyItems.leave.enabled")) { + lobbyLeaveItem = createItemStack("lobbyItems.leave"); lobbyItemLeavePosition = config.getInt("lobbyItems.leave.position"); } - if(config.getBoolean("lobbyItems.start.enabled")) { - ConfigurationSection item = new YamlConfiguration().createSection("temp"); - item.set("name", ChatColor.translateAlternateColorCodes('&',config.getString("lobbyItems.start.name"))); - item.set("material", config.getString("lobbyItems.start.material")); - List<String> lore = config.getStringList("lobbyItems.start.lore"); - if (lore != null && !lore.isEmpty()) item.set("lore", lore); - ItemStack temp = null; - try{ temp = XItemStack.deserialize(item); } catch(Exception ignored){} - lobbyStartItem = temp; + if (config.getBoolean("lobbyItems.start.enabled")) { + lobbyStartItem = createItemStack("lobbyItems.start"); lobbyItemStartAdmin = config.getBoolean("lobbyItems.start.adminOnly"); lobbyItemStartPosition = config.getInt("lobbyItems.start.position"); } + + //Spectator Items + flightToggleItem = createItemStack("spectatorItems.flight"); + flightToggleItemPosition = config.getInt("spectatorItems.flight.position"); + + teleportItem = createItemStack("spectatorItems.teleport"); + teleportItemPosition = config.getInt("spectatorItems.teleport.position"); + + //Database + databaseHost = config.getString("databaseHost"); + databasePort = config.getString("databasePort"); + databaseUser = config.getString("databaseUser"); + databasePass = config.getString("databasePass"); + databaseName = config.getString("databaseName"); + + databaseType = config.getString("databaseType").toUpperCase(); + if(!databaseType.equals("SQLITE") && !databaseType.equals("MYSQL")){ + Main.getInstance().getLogger().warning("databaseType: "+databaseType+" is not a valid configuration option!"); + databaseType = "SQLITE"; + } } public static void addToConfig(String path, Object value) { @@ -284,5 +344,22 @@ public class Config { public static void saveConfig() { config.saveConfig(); } + + @Nullable + private static ItemStack createItemStack(String path){ + ConfigurationSection item = new YamlConfiguration().createSection("temp"); + item.set("name", ChatColor.translateAlternateColorCodes('&',config.getString(path+".name"))); + item.set("material", config.getString(path+".material")); + if (Main.getInstance().supports(14)) { + if (config.contains(path+".model-data") && config.getInt(path+".model-data") != 0) { + item.set("model-data", config.getInt(path+".model-data")); + } + } + List<String> lore = config.getStringList(path+".lore"); + if (lore != null && !lore.isEmpty()) item.set("lore", lore); + ItemStack temp = null; + try{ temp = XItemStack.deserialize(item); } catch(Exception ignored) {} + return temp; + } }
\ 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 index 1684896..5e28258 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java @@ -21,6 +21,7 @@ package net.tylermurphy.hideAndSeek.configuration; import net.tylermurphy.hideAndSeek.Main; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import java.io.*; @@ -35,111 +36,136 @@ public class ConfigManager { private YamlConfiguration config,defaultConfig; private String defaultFilename; - public ConfigManager(String filename){ - this.file = new File(Main.data, filename); - this.defaultFilename = file.getName(); + public static ConfigManager create(String filename) { + return new ConfigManager(filename, filename); + } - File folder = Main.data; - if(!folder.exists()){ - if(!folder.mkdirs()){ - throw new RuntimeException("Failed to make directory: " + file.getPath()); + public static ConfigManager create(String filename, String defaultFilename) { + return new ConfigManager(filename, defaultFilename); + } + + private ConfigManager(String filename, String defaultFilename) { + + File dataFolder = Main.getInstance().getDataFolder(); + File oldDataFolder = new File(Main.getInstance().getDataFolder().getParent() + File.separator + "HideAndSeek"); + + this.defaultFilename = defaultFilename; + this.file = new File(dataFolder, filename); + + if(oldDataFolder.exists()){ + if(!dataFolder.exists()){ + if(!oldDataFolder.renameTo(dataFolder)){ + throw new RuntimeException("Could not rename folder: " + oldDataFolder.getPath()); + } + } else { + throw new RuntimeException("Plugin folders for HideAndSeek & KenshinsHideAndSeek both exists. There can only be one!"); } + } - if(!file.exists()){ - saveDefaultConfiguration(); + if (!dataFolder.exists()) { + if (!dataFolder.mkdirs()) { + throw new RuntimeException("Failed to make directory: " + file.getPath()); + } } - this.config = YamlConfiguration.loadConfiguration(file); + if (!file.exists()) { + try{ + InputStream input = Main.getInstance().getResource(defaultFilename); + if (input == null) { + throw new RuntimeException("Could not create input stream for "+defaultFilename); + } + java.nio.file.Files.copy(input, file.toPath()); + input.close(); + } catch(IOException e) { + e.printStackTrace(); + } + } - InputStream input = Main.plugin.getResource(file.getName()); - if(input == null){ + FileInputStream fileInputStream; + try { + fileInputStream = new FileInputStream(file); + } catch (FileNotFoundException e) { throw new RuntimeException("Could not create input stream for "+file.getPath()); } - InputStreamReader reader = new InputStreamReader(input); - this.defaultConfig = YamlConfiguration.loadConfiguration(reader); - try{ - input.close(); - reader.close(); - } catch (IOException ignored){} - } - - public ConfigManager(String filename, String defaultFilename){ - - this.defaultFilename = defaultFilename; - this.file = new File(Main.data, filename); - - if(!file.exists()){ - saveDefaultConfiguration(); + InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8); + this.config = new YamlConfiguration(); + try { + this.config.load(reader); + } catch(InvalidConfigurationException e) { + throw new RuntimeException("Invalid configuration in config file: "+file.getPath()); + } catch(IOException e) { + throw new RuntimeException("Could not access file: "+file.getPath()); } - this.config = YamlConfiguration.loadConfiguration(file); - - InputStream input = Main.plugin.getResource(defaultFilename); - if(input == null){ + InputStream input = this.getClass().getClassLoader().getResourceAsStream(defaultFilename); + if (input == null) { throw new RuntimeException("Could not create input stream for "+defaultFilename); } - InputStreamReader reader = new InputStreamReader(input); - this.defaultConfig = YamlConfiguration.loadConfiguration(reader); - try{ - input.close(); - reader.close(); - } catch (IOException e){ - Main.plugin.getLogger().severe("Couldn't find "+defaultFilename+" internally. Did you set an incorrect local?"); - Main.plugin.getServer().getPluginManager().disablePlugin(Main.plugin); - throw new RuntimeException(); + InputStreamReader default_reader = new InputStreamReader(input, StandardCharsets.UTF_8); + this.defaultConfig = new YamlConfiguration(); + try { + this.defaultConfig.load(default_reader); + } catch(InvalidConfigurationException e) { + throw new RuntimeException("Invalid configuration in config file: "+file.getPath()); + } catch(IOException e) { + throw new RuntimeException("Could not access file: "+file.getPath()); } - } - private void saveDefaultConfiguration(){ try{ - InputStream input = Main.plugin.getResource(defaultFilename); - if(input == null){ - throw new RuntimeException("Could not create input stream for "+defaultFilename); - } - java.nio.file.Files.copy(input, file.toPath()); - input.close(); - } catch(IOException e){ - e.printStackTrace(); + fileInputStream.close(); + default_reader.close(); + } catch (IOException e) { + throw new RuntimeException("Unable to finalize loading of config files."); } } - public double getDouble(String path){ - double value = config.getDouble(path); - if(value == 0.0D){ + public boolean contains(String path) { + return config.contains(path); + } + + public double getDouble(String path) { + if (!config.contains(path)) { return defaultConfig.getDouble(path); } else { - return value; + return config.getDouble(path); } } - public int getInt(String path){ - int value = config.getInt(path); - if(value == 0){ + public int getInt(String path) { + if (!config.contains(path)) { return defaultConfig.getInt(path); } else { - return value; + return config.getInt(path); } } - public int getDefaultInt(String path){ + public int getDefaultInt(String path) { return defaultConfig.getInt(path); } - public String getString(String path){ + public float getFloat(String path) { + if (!config.contains(path)) { + return (float) defaultConfig.getDouble(path); + } else { + return (float) config.getDouble(path); + } + } + + public String getString(String path) { String value = config.getString(path); - if(value == null){ + if (value == null) { return defaultConfig.getString(path); } else { return value; } } - public String getString(String path, String oldPath){ + public String getString(String path, String oldPath) { String value = config.getString(path); - if(value == null){ + if (value == null) { String oldValue = config.getString(oldPath); - if(oldValue == null){ + if (oldValue == null) { return defaultConfig.getString(path); } else { return oldValue; @@ -149,24 +175,24 @@ public class ConfigManager { } } - public List<String> getStringList(String path){ + public List<String> getStringList(String path) { List<String> value = config.getStringList(path); - if(value == null){ + if (value == null) { return defaultConfig.getStringList(path); } else { return value; } } - public void reset(String path){ + public void reset(String path) { config.set(path, defaultConfig.get(path)); } - public void resetFile(String newDefaultFilename){ + public void resetFile(String newDefaultFilename) { this.defaultFilename = newDefaultFilename; - InputStream input = Main.plugin.getResource(defaultFilename); - if(input == null){ + InputStream input = Main.getInstance().getResource(defaultFilename); + if (input == null) { throw new RuntimeException("Could not create input stream for "+defaultFilename); } InputStreamReader reader = new InputStreamReader(input); @@ -175,85 +201,94 @@ public class ConfigManager { } - public boolean getBoolean(String path){ - boolean value = config.getBoolean(path); - if(!value){ + public boolean getBoolean(String path) { + if (!config.contains(path)) { return defaultConfig.getBoolean(path); } else { - return true; + return config.getBoolean(path); } } - public ConfigurationSection getConfigurationSection(String path){ + public ConfigurationSection getConfigurationSection(String path) { ConfigurationSection section = config.getConfigurationSection(path); - if(section == null){ + if (section == null) { return defaultConfig.getConfigurationSection(path); } else { return section; } } - public void set(String path, Object value){ + public ConfigurationSection getDefaultConfigurationSection(String path) { + return defaultConfig.getConfigurationSection(path); + } + + public void set(String path, Object value) { config.set(path, value); } - public void saveConfig(){ + public void saveConfig() { try { - InputStream is = Main.plugin.getResource(defaultFilename); - if(is == null){ + InputStream is = Main.getInstance().getResource(defaultFilename); + if (is == null) { throw new RuntimeException("Could not create input stream for "+defaultFilename); } - StringBuilder textBuilder = new StringBuilder(); + StringBuilder textBuilder = new StringBuilder(new String("".getBytes(), StandardCharsets.UTF_8)); Reader reader = new BufferedReader(new InputStreamReader(is, Charset.forName(StandardCharsets.UTF_8.name()))); int c; - while((c = reader.read()) != -1){ + while((c = reader.read()) != -1) { textBuilder.append((char) c); } - String yamlString = textBuilder.toString(); + String yamlString = new String(textBuilder.toString().getBytes(), StandardCharsets.UTF_8); Map<String, Object> temp = config.getValues(true); - for(Map.Entry<String, Object> entry: temp.entrySet()){ - if(entry.getValue() instanceof Integer || entry.getValue() instanceof Double || entry.getValue() instanceof String || entry.getValue() instanceof Boolean || entry.getValue() instanceof List){ + for(Map.Entry<String, Object> entry: temp.entrySet()) { + if (entry.getValue() instanceof Integer || entry.getValue() instanceof Double || entry.getValue() instanceof String || entry.getValue() instanceof Boolean || entry.getValue() instanceof List) { String[] parts = entry.getKey().split("\\."); int index = 0; int i = 0; for(String part : parts) { - if(i == 0) { + if (i == 0) { index = yamlString.indexOf(part+":", index); } else { index = yamlString.indexOf(" " + part+":", index); index++; } i++; - if(index == -1) break; + if (index == -1) break; } - if(index < 10) continue; + if (index < 10) continue; int start = yamlString.indexOf(' ', index); int end = yamlString.indexOf('\n', index); - if(end == -1) end = yamlString.length(); - String replace; - if(entry.getValue() instanceof List){ - if(((List<?>) entry.getValue()).isEmpty()) continue; - replace = "["; - for(Object o : (List<Object>)entry.getValue()){ - replace = replace + o.toString() + ", "; + if (end == -1) end = yamlString.length(); + StringBuilder replace = new StringBuilder(new String("".getBytes(), StandardCharsets.UTF_8)); + if (entry.getValue() instanceof List) { + if (((List<?>) entry.getValue()).isEmpty()) { + replace.append("[]"); + } else { + replace.append("["); + for (Object o : (List<?>) entry.getValue()) { + replace.append(o.toString()).append(", "); + } + replace = new StringBuilder(replace.substring(0, replace.length() - 2)); + replace.append("]"); } - replace = replace.substring(0, replace.length()-2); - replace = replace + "]"; } else { - replace = entry.getValue().toString(); + replace.append(entry.getValue()); } - if(entry.getValue() instanceof String){ - replace = "\"" + replace + "\""; + if (entry.getValue() instanceof String) { + replace.append("\""); + replace.reverse(); + replace.append("\""); + replace.reverse(); } StringBuilder builder = new StringBuilder(yamlString); - builder.replace(start+1, end, replace); + builder.replace(start+1, end, replace.toString()); yamlString = builder.toString(); } } - PrintWriter out = new PrintWriter(file); - out.print(yamlString); - out.close(); - } catch (IOException e){ + Writer fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)); + fileWriter.write(yamlString); + fileWriter.close(); + } catch (IOException e) { e.printStackTrace(); } } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java index 1c77ec2..dfdb197 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java @@ -20,16 +20,16 @@ package net.tylermurphy.hideAndSeek.configuration; import com.cryptomorin.xseries.XItemStack; -import net.tylermurphy.hideAndSeek.util.Version; +import net.tylermurphy.hideAndSeek.Main; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.*; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; import java.util.ArrayList; import java.util.List; -import java.util.Objects; public class Items { @@ -38,16 +38,16 @@ public class Items { public static void loadItems() { - ConfigManager manager = new ConfigManager("items.yml"); + ConfigManager manager = ConfigManager.create("items.yml"); SEEKER_ITEMS = new ArrayList<>(); ConfigurationSection SeekerItems = manager.getConfigurationSection("items.seeker"); int i = 1; while (true) { ConfigurationSection section = SeekerItems.getConfigurationSection(String.valueOf(i)); - if(section == null) break; + if (section == null) break; ItemStack item = createItem(section); - if(item != null) SEEKER_ITEMS.add(item); + if (item != null) SEEKER_ITEMS.add(item); i++; } @@ -56,9 +56,9 @@ public class Items { i = 1; while (true) { ConfigurationSection section = HiderItems.getConfigurationSection(String.valueOf(i)); - if(section == null) break; + if (section == null) break; ItemStack item = createItem(section); - if(item != null) HIDER_ITEMS.add(item); + if (item != null) HIDER_ITEMS.add(item); i++; } SEEKER_EFFECTS = new ArrayList<>(); @@ -66,9 +66,9 @@ public class Items { i = 1; while (true) { ConfigurationSection section = SeekerEffects.getConfigurationSection(String.valueOf(i)); - if(section == null) break; + if (section == null) break; PotionEffect effect = getPotionEffect(section); - if(effect != null) SEEKER_EFFECTS.add(effect); + if (effect != null) SEEKER_EFFECTS.add(effect); i++; } @@ -77,9 +77,9 @@ public class Items { i = 1; while (true) { ConfigurationSection section = HiderEffects.getConfigurationSection(String.valueOf(i)); - if(section == null) break; + if (section == null) break; PotionEffect effect = getPotionEffect(section); - if(effect != null) HIDER_EFFECTS.add(effect); + if (effect != null) HIDER_EFFECTS.add(effect); i++; } @@ -89,11 +89,11 @@ public class Items { ConfigurationSection config = new YamlConfiguration().createSection("temp"); String material = item.getString("material").toUpperCase(); boolean splash = false; - if(!Version.atLeast("1.9")){ - if(material.contains("POTION")){ + if (!Main.getInstance().supports(9)) { + if (material.contains("POTION")) { config.set("level", 1); } - if(material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION")){ + if (material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION")) { material = "POTION"; splash = true; } @@ -102,22 +102,27 @@ public class Items { config.set("material", material); config.set("enchants", item.getConfigurationSection("enchantments")); config.set("unbreakable", item.getBoolean("unbreakable")); - if(item.isSet("lore")) + if (Main.getInstance().supports(14)) { + if (item.contains("model-data")) { + config.set("model-data", item.getInt("model-data")); + } + } + if (item.isSet("lore")) config.set("lore", item.getStringList("lore")); if (material.equalsIgnoreCase("POTION") || material.equalsIgnoreCase("SPLASH_POTION") || material.equalsIgnoreCase("LINGERING_POTION")) config.set("base-effect", String.format("%s,%s,%s", item.getString("type"), false, splash)); ItemStack stack = XItemStack.deserialize(config); stack.setAmount(item.getInt("amount")); - if(stack.getData().getItemType() == Material.AIR) return null; + if (stack.getData().getItemType() == Material.AIR) return null; return stack; } - private static PotionEffect getPotionEffect(ConfigurationSection item){ + private static PotionEffect getPotionEffect(ConfigurationSection item) { String type = item.getString("type"); - if(type == null) return null; - if(PotionEffectType.getByName(type.toUpperCase()) == null) return null; + if (type == null) return null; + if (PotionEffectType.getByName(type.toUpperCase()) == null) return null; return new PotionEffect( - Objects.requireNonNull(PotionEffectType.getByName(type.toUpperCase())), + PotionEffectType.getByName(type.toUpperCase()), item.getInt("duration"), item.getInt("amplifier"), item.getBoolean("ambient"), diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java index b93329f..9418414 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java @@ -19,30 +19,31 @@ package net.tylermurphy.hideAndSeek.configuration; +import net.md_5.bungee.api.ChatColor; + import java.io.File; import java.util.HashMap; import java.util.Map; -import net.md_5.bungee.api.ChatColor; - public class Localization { public static final Map<String,LocalizationString> LOCAL = new HashMap<>(); + public static final Map<String,LocalizationString> DEFAULT_LOCAL = new HashMap<>(); private static final Map<String,String[][]> CHANGES = new HashMap<String,String[][]>() {{ put("en-US", new String[][]{{"WORLDBORDER_DECREASING"},{"START","TAUNTED"}}); - put("de-DE", new String[][]{{}}); + put("de-DE", new String[][]{{},{"TAUNTED"}}); }}; public static void loadLocalization() { - ConfigManager manager = new ConfigManager("localization.yml", "lang/localization_"+Config.locale +".yml"); + ConfigManager manager = ConfigManager.create("localization.yml", "lang/localization_"+Config.locale +".yml"); int PLUGIN_VERSION = manager.getDefaultInt("version"); int VERSION = manager.getInt("version"); - if(VERSION < PLUGIN_VERSION){ - for(int i = VERSION; i < PLUGIN_VERSION; i++){ - if(i < 1) continue; + if (VERSION < PLUGIN_VERSION) { + for(int i = VERSION; i < PLUGIN_VERSION; i++) { + if (i < 1) continue; String[] changeList = CHANGES.get(Config.locale)[i-1]; for(String change : changeList) manager.reset("Localization." + change); @@ -51,9 +52,9 @@ public class Localization { } String SELECTED_LOCAL = manager.getString("type"); - if(SELECTED_LOCAL == null){ + if (SELECTED_LOCAL == null) { manager.reset("type"); - } else if(!SELECTED_LOCAL.equals(Config.locale)){ + } else if (!SELECTED_LOCAL.equals(Config.locale)) { manager.resetFile("lang"+File.separator+"localization_"+Config.locale +".yml"); } @@ -61,17 +62,28 @@ public class Localization { for(String key : manager.getConfigurationSection("Localization").getKeys(false)) { LOCAL.put( - key, + key, new LocalizationString( ChatColor.translateAlternateColorCodes('&', manager.getString("Localization."+key) ) ) - ); + ); + } + + for(String key : manager.getDefaultConfigurationSection("Localization").getKeys(false)) { + DEFAULT_LOCAL.put( + key, + new LocalizationString( ChatColor.translateAlternateColorCodes('&', manager.getString("Localization."+key) ) ) + ); } } public static LocalizationString message(String key) { - LocalizationString temp = LOCAL.get(key); - if(temp == null) { - return new LocalizationString(ChatColor.RED + "" + ChatColor.ITALIC + key + " is not found in localization.yml. This is a plugin issue, please report it."); + LocalizationString message = LOCAL.get(key); + if (message == null) { + LocalizationString defaultMessage = DEFAULT_LOCAL.get(key); + if(defaultMessage == null) { + 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(defaultMessage.toString()); } - return new LocalizationString(temp.toString()); + return new LocalizationString(message.toString()); } } |