From 7373de85a29f28d42fa1b0806a6101a9ec30b529 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Fri, 23 Dec 2011 01:34:05 +0800 Subject: [PATCH] Add support for updating stock shaders.txt, perspectives.txt, lightings.txt with additional defaults. --- src/main/java/org/dynmap/DynmapPlugin.java | 57 +++++++++++++++++++ .../java/org/dynmap/hdmap/HDMapManager.java | 6 +- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index a9b27a94..a87a0752 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -21,6 +21,8 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.CustomEventListener; import org.bukkit.event.Event; @@ -1073,6 +1075,61 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI { } } + /* + * Add in any missing sections to existing file, using resource + */ + public boolean updateUsingDefaultResource(String resourcename, File deffile, String basenode) { + InputStream in = getClass().getResourceAsStream(resourcename); + if(in == null) { + Log.severe("Unable to find resource - " + resourcename); + return false; + } + if(deffile.canRead() == false) { /* Doesn't exist? */ + return createDefaultFileFromResource(resourcename, deffile); + } + /* Load default from resource */ + YamlConfiguration def_fc = YamlConfiguration.loadConfiguration(in); + /* Load existing from file */ + YamlConfiguration fc = YamlConfiguration.loadConfiguration(deffile); + /* Now, get the list associated with the base node default */ + List> existing = fc.getMapList(basenode); + Set existing_names = new HashSet(); + /* Make map, indexed by 'name' in map */ + if(existing != null) { + for(Map m : existing) { + Object name = m.get("name"); + if(name instanceof String) + existing_names.add((String)name); + } + } + boolean did_update = false; + /* Now, loop through defaults, and see if any are missing */ + List> defmaps = def_fc.getMapList(basenode); + if(defmaps != null) { + for(Map m : defmaps) { + Object name = m.get("name"); + if(name instanceof String) { + /* If not an existing one, need to add it */ + if(existing_names.contains((String)name) == false) { + existing.add(m); + did_update = true; + } + } + } + } + /* If we did update, save existing */ + if(did_update) { + try { + fc.set(basenode, existing); + fc.save(deffile); + } catch (IOException iox) { + Log.severe("Error saving migrated file - " + deffile.getPath()); + return false; + } + Log.info("Updated file " + deffile.getPath()); + } + return true; + } private BlockListener ourBlockEventHandler = new BlockListener() { diff --git a/src/main/java/org/dynmap/hdmap/HDMapManager.java b/src/main/java/org/dynmap/hdmap/HDMapManager.java index dd67593e..90cc361b 100644 --- a/src/main/java/org/dynmap/hdmap/HDMapManager.java +++ b/src/main/java/org/dynmap/hdmap/HDMapManager.java @@ -33,7 +33,7 @@ public class HDMapManager { Log.verboseinfo("Loading shaders..."); File f = new File(plugin.getDataFolder(), "shaders.txt"); - if(!plugin.createDefaultFileFromResource("/shaders.txt", f)) { + if(!plugin.updateUsingDefaultResource("/shaders.txt", f, "shaders")) { return; } org.bukkit.util.config.Configuration bukkitShaderConfig = new org.bukkit.util.config.Configuration(f); @@ -64,7 +64,7 @@ public class HDMapManager { public void loadHDPerspectives(DynmapPlugin plugin) { Log.verboseinfo("Loading perspectives..."); File f = new File(plugin.getDataFolder(), "perspectives.txt"); - if(!plugin.createDefaultFileFromResource("/perspectives.txt", f)) { + if(!plugin.updateUsingDefaultResource("/perspectives.txt", f, "perspectives")) { return; } org.bukkit.util.config.Configuration bukkitPerspectiveConfig = new org.bukkit.util.config.Configuration(f); @@ -92,7 +92,7 @@ public class HDMapManager { public void loadHDLightings(DynmapPlugin plugin) { Log.verboseinfo("Loading lightings..."); File f = new File(plugin.getDataFolder(), "lightings.txt"); - if(!plugin.createDefaultFileFromResource("/lightings.txt", f)) { + if(!plugin.updateUsingDefaultResource("/lightings.txt", f, "lightings")) { return; } org.bukkit.util.config.Configuration bukkitLightingsConfig = new org.bukkit.util.config.Configuration(f);