package org.dynmap.regions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import org.bukkit.World;
import org.dynmap.ClientComponent;
import org.dynmap.ClientUpdateEvent;
import org.dynmap.ConfigurationNode;
import org.dynmap.DynmapPlugin;
import org.dynmap.Event;
import org.dynmap.Log;
import org.dynmap.web.Json;

public class RegionsComponent extends ClientComponent {

    public RegionsComponent(final DynmapPlugin plugin, final ConfigurationNode configuration) {
        super(plugin, configuration);
        
        // For internal webserver.
        String fname = configuration.getString("filename", "regions.yml");
        plugin.webServer.handlers.put("/standalone/" + fname.substring(0, fname.lastIndexOf('.')) + "_*", new RegionHandler(configuration));
        
        // For external webserver.
        //Parse region file for multi world style
        if (configuration.getBoolean("useworldpath", false)) {
            plugin.events.addListener("clientupdatewritten", new Event.Listener<ClientUpdateEvent>() {
                @Override
                public void triggered(ClientUpdateEvent t) {
                    World world = t.world.world;
                    parseRegionFile(world.getName() + "/" + configuration.getString("filename", "regions.yml"), configuration.getString("filename", "regions.yml").replace(".", "_" + world.getName() + ".yml"));
                }
            });
        } else {
            plugin.events.addListener("clientupdateswritten", new Event.Listener<Object>() {
                @Override
                public void triggered(Object t) {
                    parseRegionFile(configuration.getString("filename", "regions.yml"), configuration.getString("filename", "regions.yml"));
                }
            });
        }
    }

    //handles parsing and writing region json files
    private void parseRegionFile(String regionFile, String outputFileName)
    {
        File outputFile;
        org.bukkit.util.config.Configuration regionConfig = null;
        if(configuration.getBoolean("useworldpath", false))
        {
            if(new File("plugins/"+configuration.getString("name", "WorldGuard"), regionFile).exists())
                regionConfig = new org.bukkit.util.config.Configuration(new File("plugins/"+configuration.getString("name", "WorldGuard"), regionFile));
            else if(new File("plugins/"+configuration.getString("name", "WorldGuard")+"/worlds", regionFile).exists())
                regionConfig = new org.bukkit.util.config.Configuration(new File("plugins/"+configuration.getString("name", "WorldGuard")+"/worlds", regionFile));
        }
        else
            regionConfig = new org.bukkit.util.config.Configuration(new File("plugins/"+configuration.getString("name", "WorldGuard"), regionFile));
        //File didn't exist
        if(regionConfig == null)
            return;
        regionConfig.load();

        outputFileName = outputFileName.substring(0, outputFileName.lastIndexOf("."))+".json";

        File webWorldPath = new File(plugin.getWebPath()+"/standalone/", outputFileName);
        Map<?, ?> regionData = (Map<?, ?>) regionConfig.getProperty(configuration.getString("basenode", "regions"));
        if (webWorldPath.isAbsolute())
            outputFile = webWorldPath;
        else {
            outputFile = new File(plugin.getDataFolder(), webWorldPath.toString());
        }
        try {
            FileOutputStream fos = new FileOutputStream(outputFile);
            fos.write(Json.stringifyJson(regionData).getBytes());
            fos.close();
        } catch (FileNotFoundException ex) {
            Log.severe("Exception while writing JSON-file.", ex);
        } catch (IOException ioe) {
            Log.severe("Exception while writing JSON-file.", ioe);
        }
    }

}

@@ -134,32 +134,110 @@
 io.IOException;%0A
+import java.util.Collection;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0A
 import java.util
@@ -3370,24 +3370,596 @@
 regions%22));%0A
+        /* See if we have explicit list of regions to report - limit to this list if we do */%0A        List%3CString%3E idlist = configuration.getStrings(%22visibleregions%22, null);%0A        if(idlist != null) %7B%0A            @SuppressWarnings(%22unchecked%22)%0A            HashSet%3CString%3E ids = new HashSet%3CString%3E((Collection%3C? extends String%3E) regionData.keySet());%0A            for(String id : ids) %7B%0A                /* If not in list, remove it */%0A                if(!idlist.contains(id)) %7B%0A                    regionData.remove(id);%0A                %7D%0A            %7D%0A        %7D%0A       %0A
         if (

Fix 'visibleregions' option on JSON (gotta kill those parallel code paths at some point....)