From a3ab2203beff7880989cb683237d5120262a6f42 Mon Sep 17 00:00:00 2001 From: garbagemule Date: Thu, 15 Aug 2013 14:37:10 +0200 Subject: [PATCH] Get rid of config adapter. This change involves many classes because the adapter was relied upon heavily throughout the project. The change is motivated by the fact that the wrapper created more problems than it solved. Those problems (listed here) are solved by this commit. Some classes have undergone notable changes (e.g. ArenaRegion). - The config-file will no longer overwrite and regenerate itself seemingly randomly. - The config-file will now save correctly when setting points and expanding regions. - The "no class items in lobby" problem has been fixed. - The "must be inside region" problem has been fixed. --- resources/config.yml | 121 +---- src/com/garbagemule/MobArena/ArenaImpl.java | 25 +- .../garbagemule/MobArena/ArenaListener.java | 4 +- .../garbagemule/MobArena/ArenaMasterImpl.java | 160 +++--- .../MobArena/ClassLimitManager.java | 15 +- src/com/garbagemule/MobArena/MAUtils.java | 35 +- src/com/garbagemule/MobArena/MobArena.java | 52 +- .../commands/admin/DisableCommand.java | 2 +- .../commands/admin/EnableCommand.java | 2 +- .../commands/setup/ClassChestCommand.java | 2 +- .../commands/setup/ContainersCommand.java | 4 +- .../commands/setup/ProtectCommand.java | 4 +- .../commands/setup/SpawnpointsCommand.java | 4 +- .../garbagemule/MobArena/framework/Arena.java | 6 +- .../listeners/MagicSpellsListener.java | 27 +- .../MobArena/region/ArenaRegion.java | 112 ++-- .../garbagemule/MobArena/util/FileUtils.java | 125 ----- .../MobArena/util/config/Config.java | 492 ------------------ .../MobArena/util/config/ConfigSection.java | 97 ---- .../MobArena/util/config/ConfigUtils.java | 155 +++--- .../MobArena/waves/WaveManager.java | 24 +- .../MobArena/waves/WaveParser.java | 38 +- .../MobArena/waves/ability/AbilityInfo.java | 2 +- 23 files changed, 318 insertions(+), 1190 deletions(-) delete mode 100644 src/com/garbagemule/MobArena/util/FileUtils.java delete mode 100644 src/com/garbagemule/MobArena/util/config/Config.java delete mode 100644 src/com/garbagemule/MobArena/util/config/ConfigSection.java diff --git a/resources/config.yml b/resources/config.yml index ff7c678..f341e78 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -1,118 +1,3 @@ -# MobArena v0.0.0 - Config-file -# Read the Wiki for details on how to set up this file: http://goo.gl/F5TTc -# Note: You -must- use spaces instead of tabs! -global-settings: - enabled: true - allowed-commands: /list, /pl - update-notification: true -classes: - Knight: - items: diamond_sword, potion:8197:3 - armor: 306,307,308,309 - Tank: - items: iron_sword, potion:8229:2 - armor: 310,311,312,313 - Archer: - items: wood_sword, bow, arrow:256, potion:8197:3, bone - armor: 298,299,300,301 - Chemist: - items: stone_sword, potion:16428:30, potion:16388:8, potion:16389:20, potion:8197:3, potion:8226:1 - armor: 314,315,316,317 - Oddjob: - items: stone_sword, flint_and_steel, netherrack:2, tnt:4, potion:8197:3 - armor: 298,299,300,301 -arenas: - default: - settings: - world: world - enabled: true - protect: true - entry-fee: '' - clear-wave-before-next: false - clear-boss-before-next: false - clear-wave-before-boss: false - lightning: true - auto-equip-armor: true - soft-restore: false - soft-restore-drops: false - require-empty-inv-join: false - require-empty-inv-spec: false - hellhounds: false - pvp-enabled: false - monster-infight: false - allow-teleporting: false - spectate-on-death: true - auto-respawn: true - share-items-in-arena: true - min-players: 0 - max-players: 0 - max-join-distance: 0 - first-wave-delay: 5 - wave-interval: 15 - final-wave: 0 - monster-limit: 100 - monster-exp: false - keep-exp: true - food-regen: false - lock-food-level: true - spout-class-select: false - player-time-in-arena: world - auto-ignite-tnt: false - auto-start-timer: 0 - auto-ready: false - use-class-chests: false - display-waves-as-level: false - display-timer-as-level: false - use-scoreboards: true - isolated-chat: false - global-first-join-announce: false - global-end-announce: false - waves: - recurrent: - def1: - type: default - priority: 1 - frequency: 1 - monsters: - zombies: 10 - skeletons: 10 - spiders: 10 - creepers: 10 - wolves: 10 - spec1: - type: special - priority: 2 - frequency: 4 - monsters: - powered-creepers: 10 - zombie-pigmen: 10 - angry-wolves: 10 - blazes: 10 - single: - swarm1: - type: swarm - wave: 6 - monster: slime - amount: low - boss1: - type: boss - wave: 10 - monster: cave_spider - health: high - abilities: arrows, root-target, throw-nearby - rewards: - waves: - every: - '3': feather, bone, stick - '5': dirt:4, gravel:4, stone:4 - '10': iron_ingot:10, gold_ingot:8 - after: - '7': minecart, storage_minecart, powered_minecart - '13': iron_sword, iron_pickaxe, iron_spade - '16': diamond_sword - class-limits: - Archer: -1 - Oddjob: -1 - Chemist: -1 - Tank: -1 - Knight: -1 \ No newline at end of file +global-settings: {} +classes: {} +arenas: {} diff --git a/src/com/garbagemule/MobArena/ArenaImpl.java b/src/com/garbagemule/MobArena/ArenaImpl.java index 31be7de..ea35985 100644 --- a/src/com/garbagemule/MobArena/ArenaImpl.java +++ b/src/com/garbagemule/MobArena/ArenaImpl.java @@ -13,6 +13,7 @@ import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockState; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.*; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -20,6 +21,8 @@ import org.bukkit.inventory.PlayerInventory; import org.bukkit.permissions.PermissionAttachment; import org.bukkit.potion.PotionEffect; +import static com.garbagemule.MobArena.util.config.ConfigUtils.makeSection; + import com.garbagemule.MobArena.ArenaClass.ArmorType; import com.garbagemule.MobArena.autostart.AutoStartTimer; import com.garbagemule.MobArena.events.*; @@ -33,8 +36,6 @@ import com.garbagemule.MobArena.time.TimeStrategy; import com.garbagemule.MobArena.time.TimeStrategyLocked; import com.garbagemule.MobArena.time.TimeStrategyNull; import com.garbagemule.MobArena.util.*; -import com.garbagemule.MobArena.util.config.Config; -import com.garbagemule.MobArena.util.config.ConfigSection; import com.garbagemule.MobArena.util.inventory.InventoryManager; import com.garbagemule.MobArena.util.inventory.InventoryUtils; import com.garbagemule.MobArena.waves.*; @@ -48,7 +49,7 @@ public class ArenaImpl implements Arena private World world; // Settings section of the config-file for this arena. - private ConfigSection settings; + private ConfigurationSection settings; // Run-time settings and critical config settings private boolean enabled, protect, running, edit; @@ -105,15 +106,15 @@ public class ArenaImpl implements Arena /** * Primary constructor. Requires a name and a world. */ - public ArenaImpl(MobArena plugin, Config config, String name, World world) { + public ArenaImpl(MobArena plugin, ConfigurationSection section, String name, World world) { if (world == null) throw new NullPointerException("[MobArena] ERROR! World for arena '" + name + "' does not exist!"); this.name = name; this.world = world; this.plugin = plugin; - this.settings = new ConfigSection(config, "arenas." + name + ".settings"); - this.region = new ArenaRegion(new ConfigSection(config, "arenas." + name + ".coords"), this); + this.settings = makeSection(section, "settings"); + this.region = new ArenaRegion(section, this); this.enabled = settings.getBoolean("enabled", false); this.protect = settings.getBoolean("protect", true); @@ -138,7 +139,7 @@ public class ArenaImpl implements Arena // Classes, items and permissions this.classes = plugin.getArenaMaster().getClasses(); this.attachments = new HashMap(); - this.limitManager = new ClassLimitManager(this, classes, new ConfigSection(config, "arenas." + name + ".class-limits")); + this.limitManager = new ClassLimitManager(this, classes, makeSection(section, "class-limits")); // Blocks and pets this.repairQueue = new PriorityBlockingQueue(100, new RepairableComparator()); @@ -150,9 +151,9 @@ public class ArenaImpl implements Arena this.monsterManager = new MonsterManager(); // Wave stuff - this.waveManager = new WaveManager(this, config); - this.everyWaveMap = MAUtils.getArenaRewardMap(plugin, config, name, "every"); - this.afterWaveMap = MAUtils.getArenaRewardMap(plugin, config, name, "after"); + this.waveManager = new WaveManager(this, section.getConfigurationSection("waves")); + this.everyWaveMap = MAUtils.getArenaRewardMap(plugin, section, name, "every"); + this.afterWaveMap = MAUtils.getArenaRewardMap(plugin, section, name, "after"); // Misc this.eventListener = new ArenaListener(this, plugin); @@ -182,7 +183,7 @@ public class ArenaImpl implements Arena /////////////////////////////////////////////////////////////////////////*/ @Override - public ConfigSection getSettings() { + public ConfigurationSection getSettings() { return settings; } @@ -195,7 +196,7 @@ public class ArenaImpl implements Arena public void setWorld(World world) { this.world = world; settings.set("world", world.getName()); - settings.getParent().save(); + plugin.saveConfig(); if (region != null) region.refreshWorld(); } diff --git a/src/com/garbagemule/MobArena/ArenaListener.java b/src/com/garbagemule/MobArena/ArenaListener.java index 3e89b40..3ef3a14 100644 --- a/src/com/garbagemule/MobArena/ArenaListener.java +++ b/src/com/garbagemule/MobArena/ArenaListener.java @@ -15,6 +15,7 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; import org.bukkit.block.Sign; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.*; import org.bukkit.event.Event.Result; import org.bukkit.event.block.Action; @@ -77,7 +78,6 @@ import com.garbagemule.MobArena.region.ArenaRegion; import com.garbagemule.MobArena.region.RegionPoint; import com.garbagemule.MobArena.repairable.*; import com.garbagemule.MobArena.util.TextUtils; -import com.garbagemule.MobArena.util.config.ConfigSection; import com.garbagemule.MobArena.waves.MABoss; public class ArenaListener @@ -118,7 +118,7 @@ public class ArenaListener * fairly easy to implement an observer pattern - More private fields - * Uglier code */ - ConfigSection s = arena.getSettings(); + ConfigurationSection s = arena.getSettings(); this.softRestore = s.getBoolean("soft-restore", false); this.softRestoreDrops = s.getBoolean("soft-restore-drops", false); this.protect = s.getBoolean("protect", true); diff --git a/src/com/garbagemule/MobArena/ArenaMasterImpl.java b/src/com/garbagemule/MobArena/ArenaMasterImpl.java index 7ce5937..d8c21f1 100644 --- a/src/com/garbagemule/MobArena/ArenaMasterImpl.java +++ b/src/com/garbagemule/MobArena/ArenaMasterImpl.java @@ -11,6 +11,8 @@ import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -19,19 +21,20 @@ import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.PluginManager; +import static com.garbagemule.MobArena.util.config.ConfigUtils.makeSection; +import static com.garbagemule.MobArena.util.config.ConfigUtils.parseLocation; + import com.garbagemule.MobArena.ArenaClass.ArmorType; import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.ArenaMaster; import com.garbagemule.MobArena.util.ItemParser; import com.garbagemule.MobArena.util.TextUtils; -import com.garbagemule.MobArena.util.config.Config; -import com.garbagemule.MobArena.util.config.ConfigSection; import com.garbagemule.MobArena.util.config.ConfigUtils; public class ArenaMasterImpl implements ArenaMaster { private MobArena plugin; - private Config config; + private FileConfiguration config; private List arenas; private Map arenaMap; @@ -48,7 +51,7 @@ public class ArenaMasterImpl implements ArenaMaster */ public ArenaMasterImpl(MobArena plugin) { this.plugin = plugin; - this.config = plugin.getMAConfig(); + this.config = plugin.getConfig(); this.arenas = new LinkedList(); this.arenaMap = new HashMap(); @@ -239,21 +242,17 @@ public class ArenaMasterImpl implements ArenaMaster */ public void initialize() { - config.load(); loadSettings(); loadClasses(); loadArenas(); - config.save(); - // Apparently necessary... - reloadConfig(); } /** * Load the global settings. */ public void loadSettings() { - ConfigUtils.replaceAllNodes(plugin.getFilename(), config, "global-settings", "global-settings.yml"); - ConfigSection section = config.getConfigSection("global-settings"); + ConfigurationSection section = plugin.getConfig().getConfigurationSection("global-settings"); + ConfigUtils.addMissingRemoveObsolete(plugin, "global-settings.yml", section); // Grab the commands string String cmds = section.getString("allowed-commands", ""); @@ -274,16 +273,13 @@ public class ArenaMasterImpl implements ArenaMaster * Load all class-related stuff. */ public void loadClasses() { - Set classNames = config.getKeys("classes"); + ConfigurationSection section = makeSection(plugin.getConfig(), "classes"); + ConfigUtils.addIfEmpty(plugin, "classes.yml", section); - // If no classes were found, load the defaults. - if (classNames == null || classNames.isEmpty()) { - loadDefaultClasses(); - classNames = config.getKeys("classes"); - } // Establish the map. classes = new HashMap(); + Set classNames = section.getKeys(false); // Load each individual class. for (String className : classNames) { @@ -291,22 +287,12 @@ public class ArenaMasterImpl implements ArenaMaster } } - /** - * Loads the classes in res/classes.yml into the config-file. - */ - public void loadDefaultClasses() { - ConfigUtils.addMissingNodes(plugin.getFilename(), config, "classes", "classes.yml"); - } - /** * Helper method for loading a single class. */ private ArenaClass loadClass(String classname) { - // Lowercase version. + ConfigurationSection section = config.getConfigurationSection("classes." + classname); String lowercase = classname.toLowerCase(); - - // Grab the class section. - ConfigSection section = config.getConfigSection("classes." + classname); // If the section doesn't exist, the class doesn't either. if (section == null) { @@ -361,7 +347,7 @@ public class ArenaMasterImpl implements ArenaMaster registerPermission("mobarena.classes." + lowercase, PermissionDefault.TRUE).addParent("mobarena.classes", true); // Check for class chests - Location cc = section.getLocation("classchest", null); + Location cc = parseLocation(section, "classchest", null); arenaClass.setClassChest(cc); // Finally add the class to the classes map. @@ -369,8 +355,8 @@ public class ArenaMasterImpl implements ArenaMaster return arenaClass; } - private void loadClassPermissions(ArenaClass arenaClass, ConfigSection section) { - List perms = section.getStringList("permissions", null); + private void loadClassPermissions(ArenaClass arenaClass, ConfigurationSection section) { + List perms = section.getStringList("permissions"); if (perms.isEmpty()) return; for (String perm : perms) { @@ -384,8 +370,8 @@ public class ArenaMasterImpl implements ArenaMaster } } - private void loadClassLobbyPermissions(ArenaClass arenaClass, ConfigSection section) { - List perms = section.getStringList("lobby-permissions", null); + private void loadClassLobbyPermissions(ArenaClass arenaClass, ConfigurationSection section) { + List perms = section.getStringList("lobby-permissions"); if (perms.isEmpty()) return; for (String perm : perms) { @@ -401,7 +387,7 @@ public class ArenaMasterImpl implements ArenaMaster public ArenaClass createClassNode(String classname, PlayerInventory inv, boolean safe) { String path = "classes." + classname; - if (safe && config.getConfigSection(path) != null) { + if (safe && config.getConfigurationSection(path) != null) { return null; } @@ -409,7 +395,7 @@ public class ArenaMasterImpl implements ArenaMaster config.set(path, ""); // Grab the section. - ConfigSection section = config.getConfigSection(path); + ConfigurationSection section = config.getConfigurationSection(path); // Take the current items and armor. section.set("items", ItemParser.parseString(inv.getContents())); @@ -422,7 +408,7 @@ public class ArenaMasterImpl implements ArenaMaster } // Save changes. - config.save(); + plugin.saveConfig(); // Load the class return loadClass(classname); @@ -434,8 +420,8 @@ public class ArenaMasterImpl implements ArenaMaster throw new IllegalArgumentException("Class does not exist!"); // Remove the class from the config-file and save it. - config.remove("classes." + classname); - config.save(); + config.set("classes." + classname, null); + plugin.saveConfig(); // Remove the class from the map. classes.remove(lowercase); @@ -454,15 +440,14 @@ public class ArenaMasterImpl implements ArenaMaster private boolean addRemoveClassPermission(String classname, String perm, boolean add) { classname = TextUtils.camelCase(classname); String path = "classes." + classname; - if (config.getConfigSection(path) == null) + if (config.getConfigurationSection(path) == null) return false; // Grab the class section - ConfigSection section = config.getConfigSection(path); + ConfigurationSection section = config.getConfigurationSection(path); // Get any previous nodes - List nodes = section.getStringList("permissions", null); - + List nodes = section.getStringList("permissions"); if (nodes.contains(perm) && add) { return false; } @@ -479,7 +464,7 @@ public class ArenaMasterImpl implements ArenaMaster // Replace the set. section.set("permissions", nodes); - config.save(); + plugin.saveConfig(); // Reload the class. loadClass(classname); @@ -506,11 +491,12 @@ public class ArenaMasterImpl implements ArenaMaster * Load all arena-related stuff. */ public void loadArenas() { - Set arenanames = config.getKeys("arenas"); + ConfigurationSection section = makeSection(config, "arenas"); + Set arenanames = section.getKeys(false); // If no arenas were found, create a default node. if (arenanames == null || arenanames.isEmpty()) { - createArenaNode("default", plugin.getServer().getWorlds().get(0), false); + createArenaNode(section, "default", plugin.getServer().getWorlds().get(0), false); } arenas = new LinkedList(); @@ -520,7 +506,7 @@ public class ArenaMasterImpl implements ArenaMaster } public void loadArenasInWorld(String worldName) { - Set arenaNames = config.getKeys("arenas"); + Set arenaNames = config.getConfigurationSection("arenas").getKeys(false); if (arenaNames == null || arenaNames.isEmpty()) { return; } @@ -528,7 +514,7 @@ public class ArenaMasterImpl implements ArenaMaster Arena arena = getArenaWithName(arenaName); if (arena != null) continue; - String arenaWorld = config.getString("arenas." + arenaName + ".settings.world", null); + String arenaWorld = config.getString("arenas." + arenaName + ".settings.world", ""); if (!arenaWorld.equals(worldName)) continue; loadArena(arenaName); @@ -536,7 +522,7 @@ public class ArenaMasterImpl implements ArenaMaster } public void unloadArenasInWorld(String worldName) { - Set arenaNames = config.getKeys("arenas"); + Set arenaNames = config.getConfigurationSection("arenas").getKeys(false); if (arenaNames == null || arenaNames.isEmpty()) { return; } @@ -552,90 +538,74 @@ public class ArenaMasterImpl implements ArenaMaster } } + // Load an already existing arena node private Arena loadArena(String arenaname) { - String path = "arenas." + arenaname; - String worldName = config.getString(path + ".settings.world", ""); - World world = null; + ConfigurationSection section = makeSection(config, "arenas." + arenaname); + ConfigurationSection settings = makeSection(section, "settings"); + String worldName = settings.getString("world", ""); + World world; - // If a string was found, try to fetch the world from the server. if (!worldName.equals("")) { world = plugin.getServer().getWorld(worldName); - if (world == null) { Messenger.warning("World '" + worldName + "' for arena '" + arenaname + "' was not found..."); return null; } - } - // Otherwise, use the default world. - else { + } else { world = plugin.getServer().getWorlds().get(0); Messenger.warning("Could not find the world for arena '" + arenaname + "'. Using default world ('" + world.getName() + "')! Check the config-file!"); } - // Assert all settings nodes. - ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".settings", "settings.yml"); + ConfigUtils.addMissingRemoveObsolete(plugin, "settings.yml", settings); + ConfigUtils.addIfEmpty(plugin, "waves.yml", makeSection(section, "waves")); - // Create an Arena with the name and world. - Arena arena = new ArenaImpl(plugin, config, arenaname, world); - - // Register the permission + Arena arena = new ArenaImpl(plugin, section, arenaname, world); registerPermission("mobarena.arenas." + arenaname.toLowerCase(), PermissionDefault.TRUE); - - // Finally, add it to the arena list. arenas.add(arena); + plugin.getLogger().info("Loaded arena '" + arenaname + "'"); return arena; } + // Create and load a new arena node public Arena createArenaNode(String arenaName, World world) { - return createArenaNode(arenaName, world, true); + ConfigurationSection section = makeSection(config, "arenas"); + return createArenaNode(section, arenaName, world, true); } - private Arena createArenaNode(String arenaName, World world, boolean load) { - String path = "arenas." + arenaName; - if (config.getConfigSection(path) != null) + // Create a new arena node, and (optionally) load it + private Arena createArenaNode(ConfigurationSection arenas, String arenaName, World world, boolean load) { + if (arenas.contains(arenaName)) { throw new IllegalArgumentException("Arena already exists!"); + } + ConfigurationSection section = makeSection(arenas, arenaName); - // Extract the default settings and update the world-node. - ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".settings", "settings.yml"); - config.set(path + ".settings.world", world.getName()); - - // Extract the default waves. - ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".waves", "waves.yml"); - - // Extract the default rewards. - ConfigUtils.replaceAllNodes(plugin.getFilename(), config, path + ".rewards", "rewards.yml"); - - // Save the changes. - config.save(); + // Add missing settings and remove obsolete ones + ConfigUtils.addMissingRemoveObsolete(plugin, "settings.yml", makeSection(section, "settings")); + section.set("settings.world", world.getName()); + ConfigUtils.addIfEmpty(plugin, "waves.yml", makeSection(section, "waves")); + ConfigUtils.addIfEmpty(plugin, "rewards.yml", makeSection(section, "rewards")); + plugin.saveConfig(); // Load the arena return (load ? loadArena(arenaName) : null); } public void removeArenaNode(Arena arena) { - // Remove the arena from the config-file and save it. - config.remove("arenas." + arena.configName()); - config.save(); - - // Remove the arena from the list. arenas.remove(arena); - unregisterPermission("mobarena.arenas." + arena.configName()); + + config.set("arenas." + arena.configName(), null); + plugin.saveConfig(); } public void reloadConfig() { boolean wasEnabled = isEnabled(); - - // If MobArena was enabled, disable it before updating. - if (wasEnabled) { - setEnabled(false); - } + if (wasEnabled) setEnabled(false); for (Arena a : arenas) { a.forceEnd(); } - config.load(); loadSettings(); loadClasses(); loadArenas(); @@ -643,15 +613,11 @@ public class ArenaMasterImpl implements ArenaMaster for (Arena arena : arenas) { Messenger.info("Loaded arena '" + arena.configName() + "'"); } - - // If MobArena was enabled, re-enable it after updating. - if (wasEnabled) { - setEnabled(true); - } + setEnabled(wasEnabled); } public void saveConfig() { - config.save(); + plugin.saveConfig(); } private Permission registerPermission(String permString, PermissionDefault value) { diff --git a/src/com/garbagemule/MobArena/ClassLimitManager.java b/src/com/garbagemule/MobArena/ClassLimitManager.java index 4a2154c..6666fa9 100644 --- a/src/com/garbagemule/MobArena/ClassLimitManager.java +++ b/src/com/garbagemule/MobArena/ClassLimitManager.java @@ -4,36 +4,37 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.util.MutableInt; -import com.garbagemule.MobArena.util.config.ConfigSection; +import org.bukkit.plugin.Plugin; public class ClassLimitManager { private HashMap classLimits; private HashMap> classesInUse; - private ConfigSection limits; + private ConfigurationSection limits; private Map classes; - public ClassLimitManager(Arena arena, Map classes, ConfigSection limits) { + public ClassLimitManager(Arena arena, Map classes, ConfigurationSection limits) { this.limits = limits; this.classes = classes; this.classLimits = new HashMap(); this.classesInUse = new HashMap>(); - loadLimitMap(); + loadLimitMap(arena.getPlugin()); initInUseMap(); } - private void loadLimitMap() { + private void loadLimitMap(Plugin plugin) { // If the config-section is empty, create and populate it. - if (limits.getKeys() == null) { + if (limits.getKeys(false).isEmpty()) { for (ArenaClass ac : classes.values()) { limits.set(ac.getConfigName(), -1); } - limits.getParent().save(); + plugin.saveConfig(); } // Populate the limits map using the values in the config-file. diff --git a/src/com/garbagemule/MobArena/MAUtils.java b/src/com/garbagemule/MobArena/MAUtils.java index fb88ebd..54e6bff 100644 --- a/src/com/garbagemule/MobArena/MAUtils.java +++ b/src/com/garbagemule/MobArena/MAUtils.java @@ -19,6 +19,7 @@ import org.bukkit.block.Sign; import org.bukkit.World; import org.bukkit.Material; import org.bukkit.Location; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.entity.Wolf; @@ -34,7 +35,6 @@ import com.garbagemule.MobArena.region.ArenaRegion; import com.garbagemule.MobArena.util.EntityPosition; import com.garbagemule.MobArena.util.ItemParser; import com.garbagemule.MobArena.util.TextUtils; -import com.garbagemule.MobArena.util.config.Config; import com.garbagemule.MobArena.util.config.ConfigUtils; public class MAUtils @@ -54,30 +54,16 @@ public class MAUtils * type of wave ("after" or "every") and the config-file. If * no keys exist in the config-file, an empty map is returned. */ - public static Map> getArenaRewardMap(MobArena plugin, Config config, String arena, String type) + public static Map> getArenaRewardMap(MobArena plugin, ConfigurationSection config, String arena, String type) { //String arenaPath = "arenas." + arena + ".rewards.waves."; - String typePath = ConfigUtils.waveRewardList(arena, type); Map> result = new HashMap>(); - - if (config.getKeys(typePath) == null) - { - if (type.equals("every")) - { - config.set(typePath + ".3", "feather, bone, stick"); - config.set(typePath + ".5", "dirt:4, gravel:4, stone:4"); - config.set(typePath + ".10", "iron_ingot:10, gold_ingot:8"); - } - else if (type.equals("after")) - { - config.set(typePath + ".7", "minecart, storage_minecart, powered_minecart"); - config.set(typePath + ".13", "iron_sword, iron_pickaxe, iron_spade"); - config.set(typePath + ".16", "diamond_sword"); - } - } + + String typePath = "rewards." + type; + if (!config.contains(typePath)) return result; //Set waves = config.getKeys(arenaPath + type); - Set waves = config.getKeys(typePath); + Set waves = config.getConfigurationSection(typePath).getKeys(false); if (waves == null) return result; for (String n : waves) @@ -86,7 +72,7 @@ public class MAUtils continue; int wave = Integer.parseInt(n); - String path = ConfigUtils.waveReward(arena, type, wave); + String path = typePath + "." + wave; String rewards = config.getString(path); result.put(wave, ItemParser.parseItems(rewards)); @@ -494,10 +480,9 @@ public class MAUtils { world.getBlockAt(entry.getKey().getLocation(world)).setTypeId(entry.getValue()); } - - Config config = plugin.getMAConfig(); - config.remove("arenas." + name); - config.save(); + + plugin.getConfig().set("arenas." + name, null); + plugin.saveConfig(); file.delete(); diff --git a/src/com/garbagemule/MobArena/MobArena.java b/src/com/garbagemule/MobArena/MobArena.java index 3e8f53f..7abb8ba 100644 --- a/src/com/garbagemule/MobArena/MobArena.java +++ b/src/com/garbagemule/MobArena/MobArena.java @@ -29,9 +29,7 @@ import com.garbagemule.MobArena.listeners.MAGlobalListener; import com.garbagemule.MobArena.listeners.MagicSpellsListener; import com.garbagemule.MobArena.listeners.SpoutScreenListener; import com.garbagemule.MobArena.metrics.Metrics; -import com.garbagemule.MobArena.util.FileUtils; import com.garbagemule.MobArena.util.VersionChecker; -import com.garbagemule.MobArena.util.config.Config; import com.garbagemule.MobArena.util.config.ConfigUtils; import com.garbagemule.MobArena.util.inventory.InventoryManager; import com.garbagemule.MobArena.waves.ability.AbilityManager; @@ -42,7 +40,6 @@ import com.garbagemule.MobArena.waves.ability.AbilityManager; */ public class MobArena extends JavaPlugin { - private Config config; private ArenaMaster arenaMaster; private CommandHandler commandHandler; @@ -69,35 +66,35 @@ public class MobArena extends JavaPlugin // Load boss abilities loadAbilities(); - + // Set up soft dependencies setupVault(); setupHeroes(); setupSpout(); setupMagicSpells(); setupStrategies(); - + // Set up the ArenaMaster arenaMaster = new ArenaMasterImpl(this); arenaMaster.initialize(); - + // Register any inventories to restore. registerInventories(); - + // Make sure all the announcements are configured. MAMessages.init(this); - + // Register event listeners registerListeners(); - + // Go go Metrics startMetrics(); - + // Announce enable! Messenger.info("v" + this.getDescription().getVersion() + " enabled."); - + // Check for updates - if (config.getBoolean("global-settings.update-notification", false)) { + if (getConfig().getBoolean("global-settings.update-notification", false)) { VersionChecker.checkForUpdates(this, null); } } @@ -117,11 +114,12 @@ public class MobArena extends JavaPlugin } private void loadConfigFile() { + // Create if missing saveDefaultConfig(); - config = new Config(new File(getDataFolder(), "config.yml")); - updateSettings(config); - config.setHeader(getHeader()); - config.save(); + + // Set the header and save + getConfig().options().header(getHeader()); + saveConfig(); } private void registerListeners() { @@ -184,7 +182,7 @@ public class MobArena extends JavaPlugin private void setupMagicSpells() { Plugin spells = this.getServer().getPluginManager().getPlugin("MagicSpells"); if (spells == null) return; - + this.getServer().getPluginManager().registerEvents(new MagicSpellsListener(this), this); } @@ -213,10 +211,6 @@ public class MobArena extends JavaPlugin return healthStrategy; } - public Config getMAConfig() { - return config; - } - public ArenaMaster getArenaMaster() { return arenaMaster; } @@ -225,21 +219,11 @@ public class MobArena extends JavaPlugin return commandHandler; } - private void updateSettings(Config config) { - Set arenas = config.getKeys("arenas"); - if (arenas == null) return; - - for (String arena : arenas) { - String path = "arenas." + arena + ".settings"; - ConfigUtils.replaceAllNodes(getFilename(), config, path, "settings.yml"); - } - } - private String getHeader() { String sep = System.getProperty("line.separator"); return "MobArena v" + this.getDescription().getVersion() + " - Config-file" + sep + "Read the Wiki for details on how to set up this file: http://goo.gl/F5TTc" + sep + - "Note: You -must- use spaces instead of tabs!\r"; + "Note: You -must- use spaces instead of tabs!"; } private void registerInventories() { @@ -303,8 +287,4 @@ public class MobArena extends JavaPlugin double minor = item.getDurability() / 100D; return major + minor; } - - public String getFilename() { - return super.getFile().getName(); - } } \ No newline at end of file diff --git a/src/com/garbagemule/MobArena/commands/admin/DisableCommand.java b/src/com/garbagemule/MobArena/commands/admin/DisableCommand.java index 42d6bf6..382b452 100644 --- a/src/com/garbagemule/MobArena/commands/admin/DisableCommand.java +++ b/src/com/garbagemule/MobArena/commands/admin/DisableCommand.java @@ -49,7 +49,7 @@ public class DisableCommand implements Command private void disable(Arena arena, CommandSender sender) { arena.setEnabled(false); - arena.getSettings().getParent().save(); + arena.getPlugin().saveConfig(); Messenger.tellPlayer(sender, "Arena '" + arena.configName() + "' " + ChatColor.RED + "disabled"); } } diff --git a/src/com/garbagemule/MobArena/commands/admin/EnableCommand.java b/src/com/garbagemule/MobArena/commands/admin/EnableCommand.java index 25246dc..3c871b3 100644 --- a/src/com/garbagemule/MobArena/commands/admin/EnableCommand.java +++ b/src/com/garbagemule/MobArena/commands/admin/EnableCommand.java @@ -49,7 +49,7 @@ public class EnableCommand implements Command private void enable(Arena arena, CommandSender sender) { arena.setEnabled(true); - arena.getSettings().getParent().save(); + arena.getPlugin().saveConfig(); Messenger.tellPlayer(sender, "Arena '" + arena.configName() + "' " + ChatColor.GREEN + "enabled"); } } diff --git a/src/com/garbagemule/MobArena/commands/setup/ClassChestCommand.java b/src/com/garbagemule/MobArena/commands/setup/ClassChestCommand.java index 90fd968..2e97ab3 100644 --- a/src/com/garbagemule/MobArena/commands/setup/ClassChestCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/ClassChestCommand.java @@ -51,7 +51,7 @@ public class ClassChestCommand implements Command { return true; } - am.getPlugin().getMAConfig().set("classes." + ac.getConfigName() + ".classchest", b.getLocation()); + am.getPlugin().getConfig().set("classes." + ac.getConfigName() + ".classchest", b.getLocation()); am.saveConfig(); Messenger.tellPlayer(sender, "Class chest updated for class " + ac.getConfigName()); am.loadClasses(); diff --git a/src/com/garbagemule/MobArena/commands/setup/ContainersCommand.java b/src/com/garbagemule/MobArena/commands/setup/ContainersCommand.java index c5f4fba..78d2e02 100644 --- a/src/com/garbagemule/MobArena/commands/setup/ContainersCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/ContainersCommand.java @@ -19,8 +19,8 @@ public class ContainersCommand implements Command { @Override public boolean execute(ArenaMaster am, CommandSender sender, String... args) { - StringBuffer buffy = new StringBuffer(); - Set containers = am.getPlugin().getMAConfig().getKeys("arenas." + am.getSelectedArena().configName() + ".coords.containers"); + StringBuilder buffy = new StringBuilder(); + Set containers = am.getPlugin().getConfig().getConfigurationSection("arenas." + am.getSelectedArena().configName() + ".coords.containers").getKeys(false); if (containers != null) { for (String c : containers) { diff --git a/src/com/garbagemule/MobArena/commands/setup/ProtectCommand.java b/src/com/garbagemule/MobArena/commands/setup/ProtectCommand.java index 4770e59..64c359e 100644 --- a/src/com/garbagemule/MobArena/commands/setup/ProtectCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/ProtectCommand.java @@ -67,8 +67,8 @@ public class ProtectCommand implements Command } arena.setProtected(arg2.equals("true")); } - - arena.getSettings().getParent().save(); + + arena.getPlugin().saveConfig(); Messenger.tellPlayer(sender, "Protection for arena '" + arena.configName() + "': " + ((arena.isProtected()) ? ChatColor.GREEN + "on" : ChatColor.RED + "off")); return true; } diff --git a/src/com/garbagemule/MobArena/commands/setup/SpawnpointsCommand.java b/src/com/garbagemule/MobArena/commands/setup/SpawnpointsCommand.java index fc5e5c6..56b7f11 100644 --- a/src/com/garbagemule/MobArena/commands/setup/SpawnpointsCommand.java +++ b/src/com/garbagemule/MobArena/commands/setup/SpawnpointsCommand.java @@ -19,8 +19,8 @@ public class SpawnpointsCommand implements Command { @Override public boolean execute(ArenaMaster am, CommandSender sender, String... args) { - StringBuffer buffy = new StringBuffer(); - Set spawnpoints = am.getPlugin().getMAConfig().getKeys("arenas." + am.getSelectedArena().configName() + ".coords.spawnpoints"); + StringBuilder buffy = new StringBuilder(); + Set spawnpoints = am.getPlugin().getConfig().getConfigurationSection("arenas." + am.getSelectedArena().configName() + ".coords.spawnpoints").getKeys(false); if (spawnpoints != null) { for (String s : spawnpoints) { diff --git a/src/com/garbagemule/MobArena/framework/Arena.java b/src/com/garbagemule/MobArena/framework/Arena.java index 90071df..72df204 100644 --- a/src/com/garbagemule/MobArena/framework/Arena.java +++ b/src/com/garbagemule/MobArena/framework/Arena.java @@ -6,6 +6,7 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Block; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -22,7 +23,6 @@ import com.garbagemule.MobArena.ScoreboardManager; import com.garbagemule.MobArena.leaderboards.Leaderboard; import com.garbagemule.MobArena.region.ArenaRegion; import com.garbagemule.MobArena.repairable.Repairable; -import com.garbagemule.MobArena.util.config.ConfigSection; import com.garbagemule.MobArena.util.inventory.InventoryManager; import com.garbagemule.MobArena.waves.WaveManager; @@ -33,8 +33,8 @@ public interface Arena // NEW METHODS IN REFACTORING // /////////////////////////////////////////////////////////////////////////*/ - - public ConfigSection getSettings(); + + public ConfigurationSection getSettings(); public World getWorld(); diff --git a/src/com/garbagemule/MobArena/listeners/MagicSpellsListener.java b/src/com/garbagemule/MobArena/listeners/MagicSpellsListener.java index b140d6d..2f35b83 100644 --- a/src/com/garbagemule/MobArena/listeners/MagicSpellsListener.java +++ b/src/com/garbagemule/MobArena/listeners/MagicSpellsListener.java @@ -4,13 +4,14 @@ import java.io.File; import java.util.LinkedList; import java.util.List; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import com.garbagemule.MobArena.framework.Arena; -import com.garbagemule.MobArena.util.FileUtils; -import com.garbagemule.MobArena.util.config.Config; import com.garbagemule.MobArena.waves.enums.*; import com.garbagemule.MobArena.MobArena; import com.nisovin.magicspells.events.SpellCastEvent; @@ -23,12 +24,16 @@ public class MagicSpellsListener implements Listener public MagicSpellsListener(MobArena plugin) { this.plugin = plugin; - + // Set up the MagicSpells config-file. - File spellFile = FileUtils.extractResource(plugin.getDataFolder(), "magicspells.yml", plugin.getClass()); - Config spellConfig = new Config(spellFile); - spellConfig.load(); - setupSpells(spellConfig); + plugin.saveResource("res/magicspells.yml", false); + FileConfiguration config = new YamlConfiguration(); + try { + config.load(new File(plugin.getDataFolder(), "magicspells.yml")); + setupSpells(config); + } catch (Exception e) { + e.printStackTrace(); + } } @EventHandler(priority = EventPriority.NORMAL) @@ -47,10 +52,10 @@ public class MagicSpellsListener implements Listener } } - private void setupSpells(Config config) + private void setupSpells(ConfigurationSection config) { - this.disabled = config.getStringList("disabled-spells", new LinkedList()); - this.disabledOnBoss = config.getStringList("disabled-on-bosses", new LinkedList()); - this.disabledOnSwarm = config.getStringList("disabled-on-swarms", new LinkedList()); + this.disabled = config.getStringList("disabled-spells"); + this.disabledOnBoss = config.getStringList("disabled-on-bosses"); + this.disabledOnSwarm = config.getStringList("disabled-on-swarms"); } } diff --git a/src/com/garbagemule/MobArena/region/ArenaRegion.java b/src/com/garbagemule/MobArena/region/ArenaRegion.java index bf005bc..2a2f983 100644 --- a/src/com/garbagemule/MobArena/region/ArenaRegion.java +++ b/src/com/garbagemule/MobArena/region/ArenaRegion.java @@ -1,13 +1,9 @@ package com.garbagemule.MobArena.region; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - import com.garbagemule.MobArena.MAUtils; +import com.garbagemule.MobArena.Messenger; +import com.garbagemule.MobArena.MobArena; +import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.util.Enums; import org.bukkit.ChatColor; import org.bukkit.Chunk; @@ -16,12 +12,12 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; -import com.garbagemule.MobArena.Messenger; -import com.garbagemule.MobArena.MobArena; -import com.garbagemule.MobArena.framework.Arena; -import com.garbagemule.MobArena.util.config.ConfigSection; +import java.util.*; + +import static com.garbagemule.MobArena.util.config.ConfigUtils.*; public class ArenaRegion { @@ -34,17 +30,17 @@ public class ArenaRegion private boolean setup, lobbySetup; - private ConfigSection coords; - private ConfigSection spawns; - private ConfigSection chests; + private ConfigurationSection coords; + private ConfigurationSection spawns; + private ConfigurationSection chests; - public ArenaRegion(ConfigSection coords, Arena arena) { + public ArenaRegion(ConfigurationSection section, Arena arena) { this.arena = arena; refreshWorld(); - - this.coords = coords; - this.spawns = coords.getConfigSection("spawnpoints"); - this.chests = coords.getConfigSection("containers"); + + this.coords = makeSection(section, "coords"); + this.spawns = makeSection(coords, "spawnpoints"); + this.chests = makeSection(coords, "containers"); reloadAll(); } @@ -64,24 +60,24 @@ public class ArenaRegion } public void reloadRegion() { - p1 = coords.getLocation("p1", world); - p2 = coords.getLocation("p2", world); + p1 = parseLocation(coords, "p1", world); + p2 = parseLocation(coords, "p2", world); //fixRegion(); - l1 = coords.getLocation("l1", world); - l2 = coords.getLocation("l2", world); + l1 = parseLocation(coords, "l1", world); + l2 = parseLocation(coords, "l2", world); //fixLobbyRegion(); } public void reloadWarps() { - arenaWarp = coords.getLocation("arena", world); - lobbyWarp = coords.getLocation("lobby", world); - specWarp = coords.getLocation("spectator", world); - exitWarp = coords.getLocation("exit", null); + arenaWarp = parseLocation(coords, "arena", world); + lobbyWarp = parseLocation(coords, "lobby", world); + specWarp = parseLocation(coords, "spectator", world); + exitWarp = parseLocation(coords, "exit", null); } public void reloadLeaderboards() { - leaderboard = coords.getLocation("leaderboard", null); + leaderboard = parseLocation(coords, "leaderboard", null); if (leaderboard != null && leaderboard.getWorld() == null) { leaderboard.setWorld(world); } @@ -89,20 +85,20 @@ public class ArenaRegion public void reloadSpawnpoints() { spawnpoints = new HashMap(); - Set keys = spawns.getKeys(); + Set keys = spawns.getKeys(false); if (keys != null) { for (String spwn : keys) { - spawnpoints.put(spwn, spawns.getLocation(spwn, world)); + spawnpoints.put(spwn, parseLocation(spawns, spwn, world)); } } } public void reloadChests() { containers = new HashMap(); - Set keys = chests.getKeys(); + Set keys = chests.getKeys(false); if (keys != null) { for (String chst : keys) { - containers.put(chst, chests.getLocation(chst, world)); + containers.put(chst, parseLocation(chests, chst, world)); } } } @@ -229,25 +225,31 @@ public class ArenaRegion // Region expand public void expandUp(int amount) { - p2.setY(Math.min(arena.getWorld().getMaxHeight(), p2.getY() + amount)); - set(RegionPoint.P2, p2); + Location l = new Location(p2.getWorld(), p2.getX(), Math.min(p2.getWorld().getMaxHeight(), p2.getY() + amount), p2.getZ()); + setLocation(coords, "p2", l); + save(); + reloadRegion(); } public void expandDown(int amount) { - p1.setY(Math.max(0D, p1.getY() - amount)); - set(RegionPoint.P1, p1); + Location l = new Location(p1.getWorld(), p1.getX(), Math.max(0D, p1.getY() - amount), p1.getZ()); + setLocation(coords, "p1", l); + save(); + reloadRegion(); } public void expandP1(int x, int z) { - p1.setX(p1.getX() - x); - p1.setZ(p1.getZ() - z); - set(RegionPoint.P1, p1); + Location l = new Location(p1.getWorld(), p1.getX() - x, p1.getY(), p1.getZ() - z); + setLocation(coords, "p1", l); + save(); + reloadRegion(); } public void expandP2(int x, int z) { - p2.setX(p2.getX() + x); - p2.setZ(p2.getZ() + z); - set(RegionPoint.P2, p2); + Location l = new Location(p2.getWorld(), p2.getX() + x, p2.getY(), p2.getZ() + z); + setLocation(coords, "p2", l); + save(); + reloadRegion(); } public void expandOut(int amount) { @@ -292,8 +294,8 @@ public class ArenaRegion } private void fix(String location1, String location2) { - Location loc1 = coords.getLocation(location1, world); - Location loc2 = coords.getLocation(location2, world); + Location loc1 = parseLocation(coords, location1, world); + Location loc2 = parseLocation(coords, location2, world); if (loc1 == null || loc2 == null) { return; @@ -331,8 +333,8 @@ public class ArenaRegion return; } - coords.set(location1, loc1); - coords.set(location2, loc2); + setLocation(coords, location1, loc1); + setLocation(coords, location2, loc2); save(); } @@ -479,8 +481,8 @@ public class ArenaRegion } // Set the coords and save - if (lower != null) coords.set(r1.name().toLowerCase(), lower); - if (upper != null) coords.set(r2.name().toLowerCase(), upper); + if (lower != null) setLocation(coords, r1.name().toLowerCase(), lower); + if (upper != null) setLocation(coords, r2.name().toLowerCase(), upper); save(); // Reload regions and verify data @@ -499,7 +501,7 @@ public class ArenaRegion public void setWarp(RegionPoint point, Location l) { // Set the point and save - coords.set(point.toString(), l); + setLocation(coords, point.toString(), l); save(); // Then reload warps @@ -508,7 +510,7 @@ public class ArenaRegion public void setLeaderboard(Location l) { // Set the point and save - coords.set("leaderboard", l); + setLocation(coords, "leaderboard", l); save(); // Then reload the leaderboards @@ -517,7 +519,7 @@ public class ArenaRegion public void addSpawn(String name, Location loc) { // Add the spawn and save - spawns.set(name, loc); + setLocation(spawns, name, loc); save(); // Reload spawnpoints and verify data @@ -532,7 +534,7 @@ public class ArenaRegion } // Null the spawnpoint and save - spawns.set(name, null); + setLocation(spawns, name, null); save(); // Reload spawnpoints and verify data @@ -543,7 +545,7 @@ public class ArenaRegion public void addChest(String name, Location loc) { // Add the chest location and save - chests.set(name, loc); + setLocation(chests, name, loc); save(); // Reload the chests @@ -557,7 +559,7 @@ public class ArenaRegion } // Null the chest and save - chests.set(name, null); + setLocation(chests, name, null); save(); // Reload the chests @@ -566,7 +568,7 @@ public class ArenaRegion } public void save() { - spawns.getParent().save(); + arena.getPlugin().saveConfig(); } public void showRegion(final Player p) { diff --git a/src/com/garbagemule/MobArena/util/FileUtils.java b/src/com/garbagemule/MobArena/util/FileUtils.java deleted file mode 100644 index 9b2e060..0000000 --- a/src/com/garbagemule/MobArena/util/FileUtils.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.garbagemule.MobArena.util; - -import java.io.*; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; - -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; - -import com.garbagemule.MobArena.Messenger; -import com.garbagemule.MobArena.MobArena; - -public class FileUtils -{ - /** - * Extracts all of the given resources to the given directory. - * Note that even if the resources have different paths, they will all - * be extracted to the given directory. - * @param dir a directory - * @param resources a list of resources to extract - * @return a list of all the files that were written - */ - public static List extractResources(File dir, List resources, Class cls) { - return extractResources(dir, "", resources, cls); - } - - public static List extractResources(File dir, String path, List filenames, Class cls) { - List files = new ArrayList(); - - // If the path is empty, just forget about it. - if (!path.equals("")) { - // We want no leading slashes - if (path.startsWith("/")) { - path = path.substring(1); - } - - // But we do want trailing slashes - if (!path.endsWith("/")) { - path = path + "/"; - } - } - - // Extract each resource - for (String filename : filenames) { - File file = extractResource(dir, path + filename, cls); - - if (file != null) { - files.add(file); - } - } - return files; - } - - /** - * Extracts the given resource to the given directory. - * @param dir a directory - * @param resource a resource to extract - * @return the file that was written, or null - */ - public static File extractResource(File dir, String resource, Class cls) { - if (!dir.exists()) dir.mkdirs(); - - // Set up our new file. - String filename = getFilename(resource); - File file = new File(dir, filename); - - // If the file already exists, don't do anything. - if (file.exists()) return file; - - // Grab the resource input stream. - InputStream in = cls.getResourceAsStream("/res/" + resource); - if (in == null) return null; - - try { - // Set up an output stream. - FileOutputStream out = new FileOutputStream(file); - byte[] buffer = new byte[4096]; - - // Read into the buffer and write it out to the file. - int read = 0; - while ((read = in.read(buffer)) > 0) { - out.write(buffer, 0, read); - } - - // Close stuff. - in.close(); - out.close(); - - // Return the new file. - return file; - } - catch (Exception e) {} - - return null; - } - - private static String getFilename(String resource) { - int slash = resource.lastIndexOf("/"); - return (slash < 0 ? resource : resource.substring(slash + 1)); - } - - private static final String RES = "res/"; - private static final String PLUGINS = "plugins/"; - - /** - * Get a YamlConfiguration of a given resource. - * @param filename the name of the resource - * @return a YamlConfiguration for the given resource - * @throws IOException if the resource does not exist - * @throws InvalidConfigurationException if the resource is not a valid config - */ - public static YamlConfiguration getConfig(String filename, String resourcename) throws IOException, InvalidConfigurationException { - ZipFile zip = new ZipFile(PLUGINS + filename); - ZipEntry entry = zip.getEntry(RES + resourcename); - YamlConfiguration yaml = new YamlConfiguration(); - yaml.load(zip.getInputStream(entry)); - return yaml; - } -} diff --git a/src/com/garbagemule/MobArena/util/config/Config.java b/src/com/garbagemule/MobArena/util/config/Config.java deleted file mode 100644 index 3ff0cfb..0000000 --- a/src/com/garbagemule/MobArena/util/config/Config.java +++ /dev/null @@ -1,492 +0,0 @@ -package com.garbagemule.MobArena.util.config; - -import java.io.File; -import java.text.DecimalFormat; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.apache.commons.lang.Validate; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.inventory.ItemStack; - -import com.garbagemule.MobArena.Messenger; - -/** - * Wrapper class for Bukkit's Configuration classes. - * - * The motivation for this class is two-fold: - * - * 1) Provide a means of keeping a reference to the physical disk file - * in the same class as the YamlConfiguration for easy load and save. - * 2) Speed up the access times for the YamlConfiguration by loading all - * values into maps, as to avoid local fields in certain classes. - * - * The specific getters (getInteger, getBoolean, etc.) never call methods on - * the YamlConfiguration. Instead, they interact only with the value maps, - * which speeds up the access times 20-fold. The generic getter (get), does, - * however, interact with the YamlConfiguration (for now). - * - * The mutators (set, remove, etc.) interact with both the YamlConfiguration - * and the value maps for consistency. This means modifications become slightly - * slower, but the difference is neglegible. - * - * @author garbagemule, slipcor - * - * Use this class however you see fit, but please leave this description in, - * as to not unrightfully take credit for our work :) - */ -public class Config -{ - private YamlConfiguration config; - private File configFile; - private Map booleans; - private Map ints; - private Map doubles; - private Map strings; - private Map items; - - /** - * Create a new Config instance that uses the specified file for - * loading and saving. - * @param configFile a YAML file - */ - public Config(File configFile) { - this.config = new YamlConfiguration(); - this.configFile = configFile; - - this.booleans = new HashMap(); - this.ints = new HashMap(); - this.doubles = new HashMap(); - this.strings = new HashMap(); - this.items = new HashMap(); - - this.config.options().indent(4); - } - - /** - * Load the config-file into the YamlConfiguration, and then populate - * the value maps. - * @return true, if the load succeeded, false otherwise. - */ - public boolean load() { - try { - if (!configFile.exists()) { - configFile.createNewFile(); - } - config.load(configFile); - reloadMaps(); - return true; - } - catch (Exception e) { - Messenger.severe("The config-file failed to load. This is the error:\n" + e.getMessage()); - return false; - } - } - - /** - * Iterates through all keys in the config-file, and populates the - * value maps. Boolean values are stored in the booleans-map, Strings - * in the strings-map, etc. - */ - public void reloadMaps() { - for (String s : config.getKeys(true)) { - Object o = config.get(s); - - if (o instanceof Boolean) { - booleans.put(s, (Boolean) o); - } else if (o instanceof Integer) { - ints.put(s, (Integer) o); - } else if (o instanceof Double) { - doubles.put(s, (Double) o); - } else if (o instanceof ItemStack) { - items.put(s, (ItemStack) o); - } else if (o instanceof String) { - strings.put(s, (String) o); - } - } - } - - /** - * Save the YamlConfiguration to the config-file. - * @return true, if the save succeeded, false otherwise. - */ - public boolean save() { - try { - config.save(configFile); - load(); // enforce consistency - return true; - } - catch (Exception e) { - e.printStackTrace(); - return false; - } - } - - /** - * Get the header of the config-file. - * @return the header - */ - public String getHeader() { - return config.options().header(); - } - - /** - * Set the header of the config-file. - * @param header the header - */ - public void setHeader(String header) { - config.options().header(header); - } - - - - /////////////////////////////////////////////////////////////////////////// - // // - // GETTERS // - // // - /////////////////////////////////////////////////////////////////////////// - - /** - * Get the YamlConfiguration associated with this Config instance. - * Note that changes made directly to the YamlConfiguration will cause an - * inconsistency with the value maps unless reloadMaps() is called. - * @return the YamlConfiguration of this Config instance - */ - public YamlConfiguration getYamlConfiguration() { - return config; - } - - /** - * Grab a section of this Config. - * @param node - * @return - */ - public ConfigSection getConfigSection(String node) { - if (get(node) != null) { - return new ConfigSection(this, node); - } - return null; - } - - /** - * Retrieve a value from the YamlConfiguration. - * @param path the path of the value - * @return the value of the path - */ - public Object get(String path) { - return config.get(path); - } - - /** - * Retrieve a boolean from the value maps. - * @param path the path of the value - * @return the boolean value of the path if the path exists, false otherwise - */ - public boolean getBoolean(String path) { - return getBoolean(path, false); - } - - /** - * Retrieve a boolean from the value maps. - * @param path the path of the value - * @param def a default value to return if the value was not in the map - * @return the boolean value of the path if it exists, def otherwise - */ - public boolean getBoolean(String path, boolean def) { - Boolean result = booleans.get(path); - return (result != null ? result : def); - } - - /** - * Retrieve an int from the value maps. - * @param path the path of the value - * @return the int value of the path if the path exists, 0 otherwise - */ - public int getInt(String path) { - return getInt(path, 0); - } - - /** - * Retrieve an int from the value maps. - * @param path the path of the value - * @param def a default value to return if the value was not in the map - * @return the int value of the path if it exists, def otherwise - */ - public int getInt(String path, int def) { - Integer result = ints.get(path); - return (result != null ? result : def); - } - - /** - * Retrieve a double from the value maps. - * @param path the path of the value - * @return the double value of the path if the path exists, 0D otherwise - */ - public double getDouble(String path) { - return getDouble(path, 0D); - } - - /** - * Retrieve a double from the value maps. - * @param path the path of the value - * @param def a default value to return if the value was not in the map - * @return the double value of the path if it exists, def otherwise - */ - public double getDouble(String path, double def) { - Double result = doubles.get(path); - return (result != null ? result : def); - } - - /** - * Retrieve a string from the value maps. - * @param path the path of the value - * @return the string value of the path if the path exists, null otherwise - */ - public String getString(String path) { - return getString(path, null); - } - - /** - * Retrieve a string from the value maps. - * @param path the path of the value - * @param def a default value to return if the value was not in the map - * @return the string value of the path if it exists, def otherwise - */ - public String getString(String path, String def) { - String result = strings.get(path); - return (result != null ? result : def); - } - - public Location getLocation(String path, World world) { - return getLocation(path, world, null); - } - - public Location getLocation(String path, World world, Location def) { - String string = getString(path); - - if (string == null) - return def; - - if (world == null && string.split(",").length == 6) - return parseLocation(string); - - return parseLocation(world, string); - } - - public ItemStack getItemStack(String path) { - return getItemStack(path, null); - } - - public ItemStack getItemStack(String path, ItemStack def) { - ItemStack result = items.get(path); - return (result != null ? result : def); - } - - public Set getKeys(String path) { - if (config.get(path) == null) - return null; - - ConfigurationSection section = config.getConfigurationSection(path); - if (section == null) - return null; - - return section.getKeys(false); - } - - @SuppressWarnings("unchecked") - public List getStringList(String path, List def) { - if (config.get(path) == null) - return def != null ? def : new ArrayList(); - - List list = config.getStringList(path); - return (List) list; - } - - - - /////////////////////////////////////////////////////////////////////////// - // // - // MUTATORS // - // // - /////////////////////////////////////////////////////////////////////////// - - /** - * Set the value of the given path in both the value maps and the - * YamlConfiguration. Note that this will only properly put the value - * in its relevant value map, if it is of one of the supported types. - * The method can also be used to remove values from their maps and - * the YamlConfiguration by passing null for the value. - * @param path the path on which to set the value - * @param value the value to set - */ - public void set(String path, Object value) { - if (value instanceof Boolean) { - booleans.put(path, (Boolean) value); - } else if (value instanceof Integer) { - ints.put(path, (Integer) value); - } else if (value instanceof Double) { - doubles.put(path, (Double) value); - } else if (value instanceof ItemStack) { - items.put(path, (ItemStack) value); - } else if (value instanceof String) { - strings.put(path, (String) value); - } else if (value instanceof Location) { - value = locationToString((Location) value); - strings.put(path, value.toString()); - } - - if (value == null) { - booleans.remove(path); - ints.remove(path); - doubles.remove(path); - strings.remove(path); - items.remove(path); - } - - config.set(path, value); - } - - /** - * Remove the node at the given path. Uses the set() method with null - * as the value. - * @param path the path of the node to remove - */ - public void remove(String path) { - this.set(path, null); - } - - - - /////////////////////////////////////////////////////////////////////////// - // // - // UTILITY METHODS // - // // - /////////////////////////////////////////////////////////////////////////// - - /** - * Parse an input string on the form "x,y,z" and an input World to create - * a Location. This method will only accept strings of the specified form. - * @param world the World in which the Location exists - * @param coords a string on the form "x,y,z" - * @return a Location in the given world with the given coordinates - */ - public static Location parseSimpleLocation(World world, String coords) { - String[] parts = coords.split(","); - if (parts.length != 3) - throw new IllegalArgumentException("Input string must contain only x, y, and z"); - - Integer x = parseInteger(parts[0]); - Integer y = parseInteger(parts[1]); - Integer z = parseInteger(parts[2]); - - if (x == null || y == null || z == null) - throw new NullPointerException("Some of the parsed values are null!"); - - return new Location(world, x, y, z); - } - - public static Location parseLocation(String coords) { - String[] parts = coords.split(","); - if (parts.length != 6) - throw new IllegalArgumentException("Input string must contain x, y, z, yaw, pitch, and world"); - - Float x = parseFloat(parts[0]); - Float y = parseFloat(parts[1]); - Float z = parseFloat(parts[2]); - Float yaw = parseFloat(parts[3]); - Float pitch = parseFloat(parts[4]); - World world = Bukkit.getServer().getWorld(parts[5]); - - Validate.notNull(x, "x value is null"); - Validate.notNull(y, "y value is null"); - Validate.notNull(z, "z value is null"); - Validate.notNull(yaw, "yaw value is null"); - Validate.notNull(pitch, "pitch value is null"); - Validate.notNull(world, "pitch value is null"); - - return new Location(world, x, y, z, yaw, pitch); - } - - /** - * Parse an input string on the form "x,y,z,yaw,pitch" and an input World - * to create a Location. This method will only accept strings of the - * specified form. - * @param world the World in which the Location exists - * @param coords a string on the form "x,y,z,yaw,pitch" - * @return a Location in the given world with the given coordinates - */ - public static Location parseLocation(World world, String coords) { - String[] parts = coords.split(","); - if (parts.length < 5) - throw new IllegalArgumentException("Input string must contain x, y, z, yaw and pitch"); - - Float x = parseFloat(parts[0]); - Float y = parseFloat(parts[1]); - Float z = parseFloat(parts[2]); - Float yaw = parseFloat(parts[3]); - Float pitch = parseFloat(parts[4]); - - Validate.notNull(x, "x value is null"); - Validate.notNull(y, "y value is null"); - Validate.notNull(z, "z value is null"); - Validate.notNull(yaw, "yaw value is null"); - Validate.notNull(pitch, "pitch value is null"); - - return new Location(world, x, y, z, yaw, pitch); - } - - /** - * Create a String representation of a Location object. - * @param loc a Location - * @return a String of the form "x,y,z,yaw,pitch" - */ - public static String locationToString(Location loc) { - StringBuilder result = new StringBuilder(); - - result.append(twoPlaces(loc.getX())).append(","); - result.append(twoPlaces(loc.getY())).append(","); - result.append(twoPlaces(loc.getZ())).append(","); - result.append(twoPlaces(loc.getYaw(), true)).append(","); - result.append(twoPlaces(loc.getPitch(), true)).append(","); - result.append(loc.getWorld().getName()); - - return result.toString(); - } - - private static Integer parseInteger(String s) { - try { - return Integer.parseInt(s.trim()); - } - catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - private static Float parseFloat(String s) { - try { - return Float.parseFloat(s.trim()); - } - catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - private static String twoPlaces(double value, boolean force) { - return force ? DF_FORCE.format(value) : DF_NORMAL.format(value); - } - - private static String twoPlaces(double value) { - return twoPlaces(value, false); - } - private static final DecimalFormat DF_NORMAL = new DecimalFormat("0.##"); - private static final DecimalFormat DF_FORCE = new DecimalFormat("0.0#"); -} diff --git a/src/com/garbagemule/MobArena/util/config/ConfigSection.java b/src/com/garbagemule/MobArena/util/config/ConfigSection.java deleted file mode 100644 index f155d03..0000000 --- a/src/com/garbagemule/MobArena/util/config/ConfigSection.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.garbagemule.MobArena.util.config; - -import java.util.List; -import java.util.Set; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.inventory.ItemStack; - -import com.garbagemule.MobArena.util.config.Config; - -public class ConfigSection -{ - private Config config; - private String path; - - public ConfigSection(Config config, String path) { - this.config = config; - this.path = (path.endsWith(".") ? path : path + "."); - } - - public Config getParent() { - return config; - } - - public ConfigSection getConfigSection(String path) { - return new ConfigSection(config, this.path + path); - } - - public Object get(String node) { - return config.get(path + node); - } - - public int getInt(String node) { - return config.getInt(path + node); - } - - public int getInt(String node, int def) { - return config.getInt(path + node, def); - } - - public double getDouble(String node) { - return config.getDouble(path + node); - } - - public double getDouble(String node, double def) { - return config.getDouble(path + node, def); - } - - public boolean getBoolean(String node) { - return config.getBoolean(path + node); - } - - public boolean getBoolean(String node, boolean def) { - return config.getBoolean(path + node, def); - } - - public String getString(String node) { - return config.getString(path + node); - } - - public String getString(String node, String def) { - return config.getString(path + node, def); - } - - public Location getLocation(String node, World world) { - return config.getLocation(path + node, world); - } - - public Location getLocation(String node, World world, Location def) { - return config.getLocation(path + node, world, def); - } - - public ItemStack getItemStack(String path) { - return config.getItemStack(path); - } - - public ItemStack getItemStack(String path, ItemStack def) { - return config.getItemStack(path, def); - } - - public Set getKeys() { - return config.getKeys(path); - } - - public Set getKeys(String node) { - return config.getKeys(path + node); - } - - public List getStringList(String node, List def) { - return config.getStringList(path + node, def); - } - - public void set(String node, Object value) { - config.set(path + node, value); - } -} diff --git a/src/com/garbagemule/MobArena/util/config/ConfigUtils.java b/src/com/garbagemule/MobArena/util/config/ConfigUtils.java index aeb3cda..79f1463 100644 --- a/src/com/garbagemule/MobArena/util/config/ConfigUtils.java +++ b/src/com/garbagemule/MobArena/util/config/ConfigUtils.java @@ -1,85 +1,112 @@ package com.garbagemule.MobArena.util.config; -import java.util.Set; - -import com.garbagemule.MobArena.Messenger; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; -import com.garbagemule.MobArena.MobArena; -import com.garbagemule.MobArena.framework.Arena; -import com.garbagemule.MobArena.util.FileUtils; +import java.text.DecimalFormat; +import java.util.Set; public class ConfigUtils { - public static void addMissingNodes(String filename, Config config, String path, String resourcename) { - assertNodes(filename, config, path, resourcename, true); + public static void addIfEmpty(Plugin plugin, String resource, ConfigurationSection section) { + process(plugin, resource, section, true, false); } - - public static void replaceAllNodes(String filename, Config config, String path, String resourcename) { - assertNodes(filename, config, path, resourcename, false); - } - - private static void assertNodes(String filename, Config config, String path, String resourcename, boolean keepOthers) { - // Grab the section that the path is pointing to. - ConfigSection section = config.getConfigSection(path); - - // If null, create the node. - if (section == null) { - config.set(path, ""); - section = config.getConfigSection(path); - } + public static void addMissingRemoveObsolete(Plugin plugin, String resource, ConfigurationSection section) { + process(plugin, resource, section, false, true); + } + + private static void process(Plugin plugin, String resource, ConfigurationSection section, boolean addOnlyIfEmpty, boolean removeObsolete) { try { - // Extract the yml file. - YamlConfiguration ymlConfig = FileUtils.getConfig(filename, resourcename); - - // Assert the nodes. - assertNodes(section, ymlConfig, keepOthers); + YamlConfiguration defaults = new YamlConfiguration(); + defaults.load(plugin.getResource("res/" + resource)); + + Set present = section.getKeys(true); + Set required = defaults.getKeys(true); + + if (!addOnlyIfEmpty || present.isEmpty()) { + for (String req : required) { + if (!present.remove(req)) { + section.set(req, defaults.get(req)); + } + } + } + if (removeObsolete) { + for (String obs : present) { + section.set(obs, null); + } + } + plugin.saveConfig(); } catch (Exception e) { e.printStackTrace(); - Messenger.severe("Failed to load '" + resourcename + "'. Restart required!"); } } - - private static void assertNodes(ConfigSection config, YamlConfiguration ymlConfig, boolean keepOthers) { - if (config == null || ymlConfig == null) return; - - // Grab the default keys. - Set keys = ymlConfig.getKeys(false); - if (keys == null || keys.isEmpty()) return; - - // First ensure that all default nodes exist - for (String key : keys) { - if (config.get(key) == null) { - Object o = ymlConfig.get(key); - config.set(key, o); - } - } - - // If any other nodes in the config should remain, return - if (keepOthers) return; - // Otherwise, grab all the current nodes. - Set oldKeys = config.getKeys(); - if (oldKeys == null || oldKeys.isEmpty()) return; - - // Remove all nodes that aren't in the defaults. - for (String old : oldKeys) { - if (!keys.contains(old)) { - config.set(old, null); - } + public static ConfigurationSection makeSection(ConfigurationSection config, String section) { + if (!config.contains(section)) { + return config.createSection(section); + } else { + return config.getConfigurationSection(section); } } - - public static String waveRewardList(String arena, String type) { - return "arenas." + arena + ".rewards.waves." + type; + + public static Location parseLocation(ConfigurationSection config, String path, World world) { + String value = config.getString(path); + if (value == null) return null; + + String[] parts = value.split(","); + if (parts.length < 3) { + throw new IllegalArgumentException("A location must be at least (x,y,z)"); + } + Double x = Double.parseDouble(parts[0]); + Double y = Double.parseDouble(parts[1]); + Double z = Double.parseDouble(parts[2]); + if (parts.length == 3) { + return new Location(world, x, y, z); + } + if (parts.length < 5) { + throw new IllegalArgumentException("Expected location of type (x,y,z,yaw,pitch)"); + } + Float yaw = Float.parseFloat(parts[3]); + Float pit = Float.parseFloat(parts[4]); + if (world == null) { + if (parts.length != 6) { + throw new IllegalArgumentException("Expected location of type (x,y,z,yaw,pitch,world)"); + } + world = Bukkit.getWorld(parts[5]); + } + return new Location(world, x, y, z, yaw, pit); } - - public static String waveReward(String arena, String type, int wave) { - return "arenas." + arena + ".rewards.waves." + type + "." + wave; + + public static void setLocation(ConfigurationSection config, String path, Location location) { + String x = twoPlaces(location.getX()); + String y = twoPlaces(location.getY()); + String z = twoPlaces(location.getZ()); + + String yaw = twoPlaces(location.getYaw(), true); + String pit = twoPlaces(location.getPitch(), true); + + String world = location.getWorld().getName(); + + StringBuilder buffy = new StringBuilder(); + buffy.append(x).append(",").append(y).append(",").append(z); + buffy.append(",").append(yaw).append(",").append(pit); + buffy.append(",").append(world); + + config.set(path, buffy.toString()); } - - public static String waveReward(Arena arena, String type, int wave) { - return "arenas." + arena.configName() + ".rewards.waves." + type + "." + wave; + + private static String twoPlaces(double value, boolean force) { + return force ? DF_FORCE.format(value) : DF_NORMAL.format(value); } + + private static String twoPlaces(double value) { + return twoPlaces(value, false); + } + private static final DecimalFormat DF_NORMAL = new DecimalFormat("0.##"); + private static final DecimalFormat DF_FORCE = new DecimalFormat("0.0#"); } diff --git a/src/com/garbagemule/MobArena/waves/WaveManager.java b/src/com/garbagemule/MobArena/waves/WaveManager.java index 8a9e7bf..1c6717f 100644 --- a/src/com/garbagemule/MobArena/waves/WaveManager.java +++ b/src/com/garbagemule/MobArena/waves/WaveManager.java @@ -5,38 +5,28 @@ import java.util.TreeSet; import com.garbagemule.MobArena.Messenger; import com.garbagemule.MobArena.framework.Arena; -import com.garbagemule.MobArena.util.config.Config; -import com.garbagemule.MobArena.util.config.ConfigSection; import com.garbagemule.MobArena.waves.enums.*; +import org.bukkit.configuration.ConfigurationSection; public class WaveManager { private Arena arena; - private Config config; + private ConfigurationSection section; private Wave defaultWave, currentWave; private TreeSet recurrentWaves, singleWaves, singleWavesInstance; private int wave, finalWave; - public WaveManager(Arena arena, Config config) { + public WaveManager(Arena arena, ConfigurationSection section) { this.arena = arena; - this.config = config; + this.section = section; this.wave = 0; this.finalWave = 0; reloadWaves(); } - public void reload() { - config.load(); - reloadWaves(); - } - - public void save() { - config.save(); - } - public TreeSet getRecurrentWaves() { return recurrentWaves; } @@ -48,13 +38,13 @@ public class WaveManager } public void reloadWaves() { - ConfigSection rConfig = config.getConfigSection("arenas." + arena.configName() + ".waves.recurrent"); - ConfigSection sConfig = config.getConfigSection("arenas." + arena.configName() + ".waves.single"); + ConfigurationSection rConfig = section.getConfigurationSection("recurrent"); + ConfigurationSection sConfig = section.getConfigurationSection("single"); recurrentWaves = WaveParser.parseWaves(arena, rConfig, WaveBranch.RECURRENT); singleWaves = WaveParser.parseWaves(arena, sConfig, WaveBranch.SINGLE); - finalWave = config.getInt("arenas." + arena.configName() + ".settings.final-wave", 0); + finalWave = section.getInt("arenas." + arena.configName() + ".settings.final-wave", 0); if (recurrentWaves.isEmpty()) { Messenger.warning(WaveError.NO_RECURRENT_WAVES.format(arena.configName())); diff --git a/src/com/garbagemule/MobArena/waves/WaveParser.java b/src/com/garbagemule/MobArena/waves/WaveParser.java index c88c00c..edf0a2b 100644 --- a/src/com/garbagemule/MobArena/waves/WaveParser.java +++ b/src/com/garbagemule/MobArena/waves/WaveParser.java @@ -4,13 +4,13 @@ import java.util.*; import com.garbagemule.MobArena.ArenaClass; import org.bukkit.Location; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; import com.garbagemule.MobArena.Messenger; import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.region.ArenaRegion; import com.garbagemule.MobArena.util.ItemParser; -import com.garbagemule.MobArena.util.config.ConfigSection; import com.garbagemule.MobArena.waves.ability.Ability; import com.garbagemule.MobArena.waves.ability.AbilityManager; import com.garbagemule.MobArena.waves.enums.*; @@ -24,7 +24,7 @@ import com.garbagemule.MobArena.waves.types.UpgradeWave.*; public class WaveParser { - public static TreeSet parseWaves(Arena arena, ConfigSection config, WaveBranch branch) { + public static TreeSet parseWaves(Arena arena, ConfigurationSection config, WaveBranch branch) { // Create a TreeSet with the Comparator for the specific branch. TreeSet result = new TreeSet(WaveUtils.getComparator(branch)); @@ -35,15 +35,15 @@ public class WaveParser } // If no waves were found, return the empty set. - Set waves = config.getKeys(); - if (waves == null/* || waves.isEmpty()*/) { + Set waves = config.getKeys(false); + if (waves == null) { Messenger.warning(WaveError.BRANCH_MISSING.format(branch.toString().toLowerCase(), arena.configName())); return result; } // Otherwise, parse each wave in the branch. for (String wave : waves) { - ConfigSection waveSection = config.getConfigSection(wave); + ConfigurationSection waveSection = config.getConfigurationSection(wave); Wave w = parseWave(arena, wave, waveSection, branch); // Only add properly parsed waves. @@ -57,7 +57,7 @@ public class WaveParser return result; } - public static Wave parseWave(Arena arena, String name, ConfigSection config, WaveBranch branch) { + public static Wave parseWave(Arena arena, String name, ConfigurationSection config, WaveBranch branch) { // Grab the WaveType and verify that it isn't null. String t = config.getString("type", null); WaveType type = WaveType.fromString(t); @@ -143,7 +143,7 @@ public class WaveParser return result; } - private static Wave parseDefaultWave(Arena arena, String name, ConfigSection config) { + private static Wave parseDefaultWave(Arena arena, String name, ConfigurationSection config) { // Grab the monster map. SortedMap monsters = getMonsterMap(config); if (monsters == null || monsters.isEmpty()) { @@ -169,7 +169,7 @@ public class WaveParser return result; } - private static Wave parseSpecialWave(Arena arena, String name, ConfigSection config) { + private static Wave parseSpecialWave(Arena arena, String name, ConfigurationSection config) { SortedMap monsters = getMonsterMap(config); if (monsters == null || monsters.isEmpty()) { Messenger.warning(WaveError.MONSTER_MAP_MISSING.format(name, arena.configName())); @@ -180,7 +180,7 @@ public class WaveParser return result; } - private static Wave parseSwarmWave(Arena arena, String name, ConfigSection config) { + private static Wave parseSwarmWave(Arena arena, String name, ConfigurationSection config) { MACreature monster = getSingleMonster(config); if (monster == null) { Messenger.warning(WaveError.SINGLE_MONSTER_MISSING.format(name, arena.configName())); @@ -197,7 +197,7 @@ public class WaveParser return result; } - private static Wave parseSupplyWave(Arena arena, String name, ConfigSection config) { + private static Wave parseSupplyWave(Arena arena, String name, ConfigurationSection config) { SortedMap monsters = getMonsterMap(config); if (monsters == null || monsters.isEmpty()) { Messenger.warning(WaveError.MONSTER_MAP_MISSING.format(name, arena.configName())); @@ -214,7 +214,7 @@ public class WaveParser return result; } - private static Wave parseUpgradeWave(Arena arena, String name, ConfigSection config) { + private static Wave parseUpgradeWave(Arena arena, String name, ConfigurationSection config) { Map> upgrades = getUpgradeMap(config); if (upgrades == null || upgrades.isEmpty()) { Messenger.warning(WaveError.UPGRADE_MAP_MISSING.format(name, arena.configName())); @@ -230,7 +230,7 @@ public class WaveParser return result; } - private static Wave parseBossWave(Arena arena, String name, ConfigSection config) { + private static Wave parseBossWave(Arena arena, String name, ConfigurationSection config) { MACreature monster = getSingleMonster(config); if (monster == null) { Messenger.warning(WaveError.SINGLE_MONSTER_MISSING.format(name, arena.configName())); @@ -301,7 +301,7 @@ public class WaveParser * @param config a ConfigSection * @return an MACreature, if the monster node contains one that is valid */ - private static MACreature getSingleMonster(ConfigSection config) { + private static MACreature getSingleMonster(ConfigurationSection config) { String monster = config.getString("monster"); if (monster == null) { return null; @@ -317,8 +317,8 @@ public class WaveParser * @param config * @return a "reverse" map of monsters and numbers */ - private static SortedMap getMonsterMap(ConfigSection config) { - Set monsters = config.getKeys("monsters"); + private static SortedMap getMonsterMap(ConfigurationSection config) { + Set monsters = config.getConfigurationSection("monsters").getKeys(false); if (monsters == null || monsters.isEmpty()) { return null; } @@ -343,7 +343,7 @@ public class WaveParser return monsterMap; } - private static List getSpawnpoints(Arena arena, String name, ConfigSection config) { + private static List getSpawnpoints(Arena arena, String name, ConfigurationSection config) { List result = new ArrayList(); String spawnString = config.getString("spawnpoints"); @@ -369,8 +369,8 @@ public class WaveParser return result; } - private static Map> getUpgradeMap(ConfigSection config) { - Set classes = config.getKeys("upgrades"); + private static Map> getUpgradeMap(ConfigurationSection config) { + Set classes = config.getConfigurationSection("upgrades").getKeys(false); if (classes == null || classes.isEmpty()) { return null; } @@ -410,7 +410,7 @@ public class WaveParser } // Permissions - List perms = config.getStringList(path + className + ".permissions", Collections.EMPTY_LIST); + List perms = config.getStringList(path + className + ".permissions"); if (!perms.isEmpty()) { for (String perm : perms) { list.add(new PermissionUpgrade(perm)); diff --git a/src/com/garbagemule/MobArena/waves/ability/AbilityInfo.java b/src/com/garbagemule/MobArena/waves/ability/AbilityInfo.java index 0c0ef6e..25ff0db 100644 --- a/src/com/garbagemule/MobArena/waves/ability/AbilityInfo.java +++ b/src/com/garbagemule/MobArena/waves/ability/AbilityInfo.java @@ -1,7 +1,7 @@ package com.garbagemule.MobArena.waves.ability; import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy;; +import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface AbilityInfo