Moved regions-code to regions-component.
This commit is contained in:
parent
e57301b14e
commit
b2cb15b75c
10 changed files with 159 additions and 97 deletions
75
src/main/java/org/dynmap/regions/RegionHandler.java
Normal file
75
src/main/java/org/dynmap/regions/RegionHandler.java
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package org.dynmap.regions;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import org.bukkit.util.config.Configuration;
|
||||
import org.dynmap.ConfigurationNode;
|
||||
import org.dynmap.web.HttpRequest;
|
||||
import org.dynmap.web.HttpResponse;
|
||||
import org.dynmap.web.Json;
|
||||
import org.dynmap.web.handlers.FileHandler;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
public class RegionHandler extends FileHandler {
|
||||
private ConfigurationNode regions;
|
||||
public RegionHandler(ConfigurationNode regions) {
|
||||
this.regions = regions;
|
||||
}
|
||||
@Override
|
||||
protected InputStream getFileInput(String path, HttpRequest request, HttpResponse response) {
|
||||
if(regions == null)
|
||||
return null;
|
||||
/* Right path? */
|
||||
if(path.endsWith(".json") == false)
|
||||
return null;
|
||||
|
||||
String worldname = path.substring(0, path.lastIndexOf(".json"));
|
||||
Configuration regionConfig = null;
|
||||
File infile;
|
||||
String regionFile;
|
||||
|
||||
/* If using worldpath, format is either plugins/<plugin>/<worldname>/<filename> OR
|
||||
* plugins/<plugin>/worlds/<worldname>/<filename>
|
||||
*/
|
||||
File basepath = new File("plugins", regions.getString("name", "WorldGuard"));
|
||||
if(basepath.exists() == false)
|
||||
return null;
|
||||
if(regions.getBoolean("useworldpath", false)) {
|
||||
regionFile = worldname + "/" + regions.getString("filename", "regions.yml");
|
||||
infile = new File(basepath, regionFile);
|
||||
if(!infile.exists()) {
|
||||
infile = new File(basepath, "worlds/" + regionFile);
|
||||
}
|
||||
}
|
||||
else { /* Else, its plugins/<plugin>/<filename> */
|
||||
regionFile = regions.getString("filename", "regions.yml");
|
||||
infile = new File(basepath, regionFile);
|
||||
}
|
||||
if(infile.exists()) {
|
||||
regionConfig = new Configuration(infile);
|
||||
}
|
||||
//File didn't exist
|
||||
if(regionConfig == null)
|
||||
return null;
|
||||
regionConfig.load();
|
||||
/* Parse region data and store in MemoryInputStream */
|
||||
Map<?, ?> regionData = (Map<?, ?>) regionConfig.getProperty(regions.getString("basenode", "regions"));
|
||||
try {
|
||||
ByteArrayOutputStream fos = new ByteArrayOutputStream();
|
||||
fos.write(Json.stringifyJson(regionData).getBytes());
|
||||
fos.close();
|
||||
return new ByteArrayInputStream(fos.toByteArray());
|
||||
} catch (FileNotFoundException ex) {
|
||||
log.log(Level.SEVERE, "Exception while writing JSON-file.", ex);
|
||||
} catch (IOException ioe) {
|
||||
log.log(Level.SEVERE, "Exception while writing JSON-file.", ioe);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
86
src/main/java/org/dynmap/regions/RegionsComponent.java
Normal file
86
src/main/java/org/dynmap/regions/RegionsComponent.java
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
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.ConfigurationNode;
|
||||
import org.dynmap.DynmapPlugin;
|
||||
import org.dynmap.Event;
|
||||
import org.dynmap.Log;
|
||||
import org.dynmap.WorldUpdate;
|
||||
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("updatewritten", new Event.Listener<WorldUpdate>() {
|
||||
@Override
|
||||
public void triggered(WorldUpdate 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("updateswritten", 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(this.configuration.getString("webpath", "web")+"/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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue