<commit_before>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())<commit_msg>Fix 'visibleregions' option on JSON (gotta kill those parallel code paths at some point....)<commit_after>package org.dynmap.regions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
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"));
        /* See if we have explicit list of regions to report - limit to this list if we do */
        List<String> idlist = configuration.getStrings("visibleregions", null);
        if(idlist != null) {
            @SuppressWarnings("unchecked")
            HashSet<String> ids = new HashSet<String>((Collection<? extends String>) regionData.keySet());
            for(String id : ids) {
                /* If not in list, remove it */
                if(!idlist.contains(id)) {
                    regionData.remove(id);
                }
            }
        }
       
        if (webWorldPath.isAbsolute())
{'fraction_non_alphanumeric': 0.12846617548559947, 'fraction_numerical': 0.0005358338914936369, 'mean_word_length': 2.9089005235602095, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2977046', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1866, 'n_words': 397}