Retire regions components (Residence, Towny, WorldGuard, Factions)

This commit is contained in:
Mike Primm 2011-12-05 14:57:16 +08:00 committed by mikeprimm
parent 4f2f968353
commit 3e4bc59834
9 changed files with 5 additions and 1301 deletions

View file

@ -1,213 +1,22 @@
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 java.util.Set;
import org.bukkit.World;
import org.dynmap.ClientComponent;
import org.dynmap.ClientUpdateEvent;
import org.dynmap.Component;
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 {
private TownyConfigHandler towny;
private FactionsConfigHandler factions;
private String regiontype;
public class RegionsComponent extends Component {
private static String deprecated_ids[] = { "Residence", "Factions", "Towny", "WorldGuard" };
private static String deprecated_new_plugins[] = { "dynmap-residence", "Dynmap-Factions", "Dynmap-Towny", "Dynmap-WorldGuard" };
public RegionsComponent(final DynmapPlugin plugin, final ConfigurationNode configuration) {
super(plugin, configuration);
regiontype = configuration.getString("name", "WorldGuard");
String regiontype = configuration.getString("name", "WorldGuard");
/* Check if a deprecated component */
for(int i = 0; i < deprecated_ids.length; i++) {
if(regiontype.equals(deprecated_ids[i])) { /* If match */
/* See if new plugin is installed - disable if it is */
if(plugin.getServer().getPluginManager().getPlugin(deprecated_new_plugins[i]) != null) {
Log.info("Region component for '" + regiontype + "' disabled, replaced by '" + deprecated_new_plugins[i] + "' plugin, which is installed");
disableComponent();
return;
}
else {
Log.info("Region component for '" + regiontype + "' has been DEPRECATED - migrate to '" + deprecated_new_plugins[i] + "' plugin");
}
}
}
// For internal webserver.
String fname = configuration.getString("filename", "regions.yml");
/* Load special handler for Towny */
if(regiontype.equals("Towny")) {
towny = new TownyConfigHandler(configuration);
plugin.webServer.handlers.put("/standalone/towny_*", new RegionHandler(configuration));
}
/* Load special handler for Factions */
else if(regiontype.equals("Factions")) {
factions = new FactionsConfigHandler(configuration);
plugin.webServer.handlers.put("/standalone/factions_*", new RegionHandler(configuration));
}
else {
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(), world.getName() + "/" + configuration.getString("filename", "regions.yml"), configuration.getString("filename", "regions.yml").replace(".", "_" + world.getName() + ".yml"));
}
});
} else {
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"));
}
});
}
}
//handles parsing and writing region json files
private void parseRegionFile(String wname, String regionFile, String outputFileName)
{
File outputFile;
org.bukkit.util.config.Configuration regionConfig = null;
Map<?, ?> regionData = null;
File webWorldPath;
if(regiontype.equals("Towny")) {
regionData = towny.getRegionData(wname);
outputFileName = "towny_" + wname + ".json";
webWorldPath = new File(plugin.getWebPath()+"/standalone/", outputFileName);
}
else if(regiontype.equals("Factions")) {
regionData = factions.getRegionData(wname);
outputFileName = "factions_" + wname + ".json";
webWorldPath = new File(plugin.getWebPath()+"/standalone/", outputFileName);
}
else if(regiontype.equals("Residence")) {
File f = new File("plugins/Residence/Save/Worlds", "res_" + wname + ".yml");
if(f.exists()) {
regionConfig = new org.bukkit.util.config.Configuration(f);
}
else {
f = new File("plugins/Residence", "res.yml");
if(f.exists()) {
regionConfig = new org.bukkit.util.config.Configuration(f);
}
}
if(regionConfig == null) return;
outputFileName = "res_" + wname + ".json";
webWorldPath = new File(plugin.getWebPath()+"/standalone/", outputFileName);
regionConfig.load();
regionData = (Map<?, ?>) regionConfig.getProperty("Residences");
}
else {
if(configuration.getBoolean("useworldpath", false))
{
if(new File("plugins/"+regiontype, regionFile).exists())
regionConfig = new org.bukkit.util.config.Configuration(new File("plugins/"+regiontype, regionFile));
else if(new File("plugins/"+regiontype+"/worlds", regionFile).exists())
regionConfig = new org.bukkit.util.config.Configuration(new File("plugins/"+regiontype+"/worlds", regionFile));
}
else
regionConfig = new org.bukkit.util.config.Configuration(new File("plugins/"+regiontype, regionFile));
//File didn't exist
if(regionConfig == null)
return;
regionConfig.load();
regionData = (Map<?, ?>) regionConfig.getProperty(configuration.getString("basenode", "regions"));
outputFileName = outputFileName.substring(0, outputFileName.lastIndexOf("."))+".json";
webWorldPath = new File(plugin.getWebPath()+"/standalone/", outputFileName);
}
/* Process out hidden data */
filterOutHidden(configuration.getStrings("visibleregions", null), configuration.getStrings("hiddenregions", null), regionData, regiontype);
if (webWorldPath.isAbsolute())
outputFile = webWorldPath;
else {
outputFile = new File(plugin.getDataFolder(), webWorldPath.toString());
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputFile);
fos.write(Json.stringifyJson(regionData).getBytes());
} catch (FileNotFoundException ex) {
Log.severe("Exception while writing JSON-file.", ex);
} catch (IOException ioe) {
Log.severe("Exception while writing JSON-file.", ioe);
} finally {
if(fos != null) {
try {
fos.close();
} catch (IOException iox) {}
fos = null;
}
}
}
public static void filterOutHidden(List<String> idlist, List<String> hidlist, Map<?,?> regionData, String regiontype) {
/* See if we have explicit list of regions to report - limit to this list if we do */
if((regionData != null) && ((idlist != null) || (hidlist != null))) {
@SuppressWarnings("unchecked")
HashSet<String> ids = new HashSet<String>((Collection<? extends String>) regionData.keySet());
for(String id : ids) {
/* If include list defined, and we're not in it, remove */
if((idlist != null) && (!idlist.contains(id))) {
regionData.remove(id);
}
/* If exclude list defined, and we're on it, remove */
else if(hidlist != null) {
if(hidlist.contains(id)) {
/* If residence, we want to zap the areas list, so that we still get subregions */
if(regiontype.equals("Residence")) {
Map<?,?> m = (Map<?,?>)regionData.get(id);
if(m != null) {
Map<?,?> a = (Map<?,?>)m.get("Areas");
if(a != null) {
a.clear();
}
}
}
else {
regionData.remove(id);
}
}
if(regiontype.equals("Residence")) {
Map<?,?> m = (Map<?,?>)regionData.get(id);
if(m != null) {
m = (Map<?,?>)m.get("Subzones");
if(m != null) {
Set<?> ks = m.keySet();
for(Object k : ks) {
String sid = id + "." + k;
if(hidlist.contains(sid)) {
m.remove(k);
}
}
}
}
}
}
Log.info("Region component for '" + regiontype + "' has been RETIRED - migrate to '" + deprecated_new_plugins[i] + "' plugin");
}
}
}