diff options
Diffstat (limited to 'src/main/java/net/tylermurphy/hideAndSeek/configuration')
4 files changed, 384 insertions, 77 deletions
diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java index 83b6ceb..f2b7680 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 { + + private static ConfigManager manager; public static String messagePrefix, @@ -33,94 +37,114 @@ public class Config { nametagsVisible, permissionsRequired, announceMessagesToNonPlayers, - worldborderEnabled; + worldborderEnabled, + tauntEnabled, + tauntCountdown, + tauntLast, + glowEnabled, + glowStackable, + pvpEnabled, + autoJoin, + teleportToExit; public static int minPlayers, worldborderSize, worldborderDelay, currentWorldborderSize, - gameLength; - - public static FileConfiguration getConfig() { - return Main.plugin.getConfig(); - } - - public static void saveConfig() { - Main.plugin.saveConfig(); - } + gameLength, + saveMinX, + saveMinZ, + saveMaxX, + saveMaxZ, + tauntDelay, + glowLength; public static void loadConfig() { - - Main.plugin.reloadConfig(); - + + manager = new ConfigManager("config.yml"); + manager.saveConfig(); + //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 = String.valueOf(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 = manager.getInt("bounds.min.x"); + saveMinZ = manager.getInt("bounds.min.z"); + saveMaxX = manager.getInt("bounds.max.x"); + saveMaxZ = manager.getInt("bounds.max.z"); + + //Taunt + tauntEnabled = manager.getBoolean("taunt.enabled"); + tauntCountdown = manager.getBoolean("taunt.showCountdown"); + tauntDelay = Math.max(60,manager.getInt("taunt.delay")); + tauntLast = manager.getBoolean("taunt.whenLastPerson"); + + //Glow + glowLength = Math.max(1,manager.getInt("glow.time")); + glowStackable = manager.getBoolean("glow.stackable"); + glowEnabled = manager.getBoolean("glow.enabled"); + //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(); - + nametagsVisible = manager.getBoolean("nametagsVisible"); + permissionsRequired = manager.getBoolean("permissionsRequired"); + minPlayers = Math.max(2, manager.getInt("minPlayers")); + gameLength = manager.getInt("gameLength"); + pvpEnabled = manager.getBoolean("pvp"); + autoJoin = manager.getBoolean("autoJoin"); + teleportToExit = manager.getBoolean("teleportToExit"); } - public static void addToSection(String sectionName, Map<String,Object> values) { - ConfigurationSection section = getConfig().getConfigurationSection(sectionName); - if(section == null) section = getConfig().createSection(sectionName); - Map<String,Object> sectionValues = section.getValues(true); - for(Entry<String, Object> entry : values.entrySet()) { - sectionValues.put(entry.getKey(), entry.getValue()); - } - getConfig().createSection(sectionName, sectionValues); - saveConfig(); + public static void addToConfig(String path, Object 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..09b3dcf --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java @@ -0,0 +1,146 @@ +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 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 true; + } + } + + 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<String, Object> temp = config.getValues(true); + for(Map.Entry<String, Object> 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); + if(end == -1) end = yamlString.length(); + 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/Items.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java new file mode 100644 index 0000000..fbf5d2c --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java @@ -0,0 +1,131 @@ +package net.tylermurphy.hideAndSeek.configuration; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.potion.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class Items { + + private static ConfigManager manager; + + public static List<ItemStack> HIDER_ITEMS, SEEKER_ITEMS; + public static List<PotionEffect> HIDER_EFFECTS, SEEKER_EFFECTS; + + public static void loadItems() { + + manager = new ConfigManager("items.yml"); + + SEEKER_ITEMS = new ArrayList<ItemStack>(); + ConfigurationSection SeekerItems = manager.getConfigurationSection("items.seeker"); + int i = 1; + while (true) { + ConfigurationSection section = SeekerItems.getConfigurationSection(String.valueOf(i)); + if(section == null) break; + ItemStack item = createItem(section); + if(item != null) SEEKER_ITEMS.add(item); + i++; + } + + HIDER_ITEMS = new ArrayList<ItemStack>(); + ConfigurationSection HiderItems = manager.getConfigurationSection("items.hider"); + i = 1; + while (true) { + ConfigurationSection section = HiderItems.getConfigurationSection(String.valueOf(i)); + if(section == null) break; + ItemStack item = createItem(section); + if(item != null) HIDER_ITEMS.add(item); + i++; + } + + SEEKER_EFFECTS = new ArrayList<PotionEffect>(); + ConfigurationSection SeekerEffects = manager.getConfigurationSection("effects.seeker"); + i = 1; + while (true) { + ConfigurationSection section = SeekerEffects.getConfigurationSection(String.valueOf(i)); + if(section == null) break; + PotionEffect effect = getPotionEffect(section); + if(effect != null) SEEKER_EFFECTS.add(effect); + i++; + } + + HIDER_EFFECTS = new ArrayList<PotionEffect>(); + ConfigurationSection HiderEffects = manager.getConfigurationSection("effects.hider"); + i = 1; + while (true) { + ConfigurationSection section = HiderEffects.getConfigurationSection(String.valueOf(i)); + if(section == null) break; + PotionEffect effect = getPotionEffect(section); + if(effect != null) HIDER_EFFECTS.add(effect); + i++; + } + + } + + private static ItemStack createItem(ConfigurationSection item) { + String material_string = item.getString("material"); + if(material_string == null) return null; + Material material = Material.valueOf(material_string.toUpperCase()); + int amount = item.getInt("amount"); + ItemStack stack = new ItemStack(material, amount); + if(material == Material.POTION || material == Material.SPLASH_POTION || material == Material.LINGERING_POTION){ + PotionMeta meta = getPotionMeta(stack, item); + stack.setItemMeta(meta); + } else { + ConfigurationSection enchantments = item.getConfigurationSection("enchantments"); + if (enchantments != null) + for (String enchantment_string : enchantments.getKeys(false)) { + Enchantment enchantment = Enchantment.getByKey(NamespacedKey.minecraft(enchantment_string)); + if (enchantment == null) continue; + stack.addUnsafeEnchantment( + enchantment, + enchantments.getInt(enchantment_string) + ); + } + ItemMeta meta = getItemMeta(stack,item); + stack.setItemMeta(meta); + } + return stack; + } + + private static ItemMeta getItemMeta(ItemStack stack, ConfigurationSection item){ + ItemMeta meta = stack.getItemMeta(); + assert meta != null; + String name = item.getString("name"); + if(name != null) + meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name)); + meta.setUnbreakable(item.getBoolean("unbreakable")); + meta.setLore(item.getStringList("lore")); + return meta; + } + + private static PotionMeta getPotionMeta(ItemStack stack, ConfigurationSection item){ + String type = item.getString("type"); + PotionMeta meta = (PotionMeta) stack.getItemMeta(); + if(type==null) return meta; + assert meta != null; + meta.setBasePotionData(new PotionData((PotionType.valueOf(type.toUpperCase())))); + return meta; + } + + private static PotionEffect getPotionEffect(ConfigurationSection item){ + String type = item.getString("type"); + if(type == null) return null; + return new PotionEffect( + Objects.requireNonNull(PotionEffectType.getByName(type.toUpperCase())), + item.getInt("duration"), + item.getInt("amplifier"), + item.getBoolean("ambient"), + item.getBoolean("particles") + ); + } +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java index 177b5bf..9980a4e 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Localization.java @@ -1,6 +1,9 @@ package net.tylermurphy.hideAndSeek.configuration; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; @@ -13,25 +16,28 @@ public class Localization { public static final Map<String,LocalizationString> LOCAL = new HashMap<String,LocalizationString>(); - static YamlConfiguration config; + private static ConfigManager manager; - public static boolean init() { - Main.plugin.saveResource("localization.yml", false); - String path = Main.data.getAbsolutePath()+File.separator + "localization.yml"; - config = YamlConfiguration.loadConfiguration(new File(path)); - for(String key : config.getConfigurationSection("Localization").getKeys(false)) { + public static boolean loadLocalization() { + + manager = new ConfigManager("localization.yml"); + manager.saveConfig(); + + 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) ) ) ); } + return true; } public static LocalizationString message(String key) { LocalizationString temp = LOCAL.get(key); - if(temp == null) - return new LocalizationString(key+" missing from localization.yml"); + 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."); + } return new LocalizationString(temp.toString()); } } |