Initial Support for Regions system on dynmap

Included Worldguard support(default)
Also residence support(incomplete)
This commit is contained in:
Jason Booth 2011-04-13 19:58:25 -05:00
parent d53b3bc340
commit 8e59f37995
6 changed files with 425 additions and 0 deletions

View File

@ -95,6 +95,19 @@ web:
#- type: digitalclock
- type: timeofdayclock
showdigitalclock: true
- type: regions
name: WorldGuard
useworldpath: true
filename: regions.yml
basenode: regions
use3dregions: true
infowindow: '<div class="infowindow"><span style="font-size:120%;">%regionname% - %priority% (%parent%)</span><br /> Owners <span style="font-weight:bold;">%playerowners% %groupowners%</span><br />Members <span style="font-weight:bold;">%playermembers% %groupmembers%</span><br />Flags<br /><span style="font-weight:bold;">%flags%</span></div>'
regionstyle:
strokeColor: "#FF0000"
strokeOpacity: 0.8
strokeWeight: 3
fillColor: "#FF0000"
fillOpacity: 0.35
defaultworld: world
worlds:

View File

@ -6,6 +6,8 @@ import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -15,6 +17,7 @@ import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.util.config.Configuration;
import org.bukkit.util.config.ConfigurationNode;
import org.dynmap.web.Json;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@ -28,6 +31,7 @@ class JsonTimerTask extends TimerTask {
private Server server;
private MapManager mapManager;
private Configuration configuration;
private ConfigurationNode regions;
private static final JSONParser parser = new JSONParser();
private long lastTimestamp = 0;
@ -36,6 +40,14 @@ class JsonTimerTask extends TimerTask {
this.server = this.plugin.getServer();
this.mapManager = this.plugin.getMapManager();
this.configuration = config;
//this.regions = configuration.getNode("web").getNode("components").getNode("regions");
System.out.println();
for(ConfigurationNode type : configuration.getNodeList("web.components", null))
if(type.getString("type").equalsIgnoreCase("regions"))
{
this.regions = type;
break;
}
}
public void run() {
@ -77,6 +89,9 @@ class JsonTimerTask extends TimerTask {
//Handles Updates
for (World world : this.server.getWorlds()) {
if(regions.getBoolean("useworldpath", false))
parseRegionFile(world.getName() + "/" + regions.getString("filename", "regions.yml"), regions.getString("filename", "regions.yml").replace(".", "_" + world.getName() + ".yml"));
current = System.currentTimeMillis();
Client.Update update = new Client.Update();
@ -111,5 +126,46 @@ class JsonTimerTask extends TimerTask {
}
}
lastTimestamp = System.currentTimeMillis();
if (!regions.getBoolean("useworldpath", false))
parseRegionFile(regions.getString("filename", "regions.yml"), regions.getString("filename", "regions.yml"));
}
private void parseRegionFile(String regionFile, String outputFileName)
{
File outputFile;
Configuration regionConfig = null;
if(regions.getBoolean("useworldpath", false))
{
if(new File("plugins/"+regions.getString("name", "WorldGuard"), regionFile).exists())
regionConfig = new Configuration(new File("plugins/"+regions.getString("name", "WorldGuard"), regionFile));
else if(new File("plugins/"+regions.getString("name", "WorldGuard")+"/worlds", regionFile).exists())
regionConfig = new Configuration(new File("plugins/"+regions.getString("name", "WorldGuard")+"/worlds", regionFile));
}
else
regionConfig = new Configuration(new File("plugins/"+regions.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(regions.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.log(Level.SEVERE, "Exception while writing JSON-file.", ex);
} catch (IOException ioe) {
log.log(Level.SEVERE, "Exception while writing JSON-file.", ioe);
}
}
}

View File

@ -588,3 +588,6 @@
top: -3px;
left: 0px;
}
.infowindow {
color:black;
}

36
web/js/regions.js Normal file
View File

@ -0,0 +1,36 @@
// Author: nidefawl. contact me at bukkit.org or irc.esper.net #nide
var regionCfg;
var regionPolygons = {} ;
var regionInfoWindow = new google.maps.InfoWindow();
var regionConstructors = {};
function makeRegionPolygonCube(map, name, region)
{
new regionConstructors['polygon'](map, name, region);
}
function regionInfo(event, name, region)
{
new regionConstructors['info'](event, name, region);
}
componentconstructors['regions'] = function(dynmap, configuration)
{
regionCfg = configuration;
loadjs('js/regions_' + regionCfg.name + '.js', function()
{
var world_info = dynmap.map.mapTypeId.split('.');
new regionConstructors['update'](world_info[0]);
$(dynmap).bind('mapchanged', function() {
var world_info = dynmap.map.mapTypeId.split('.');
new regionConstructors['update'](world_info[0]);
});
});
}
function arrayReplace(replace, by, str)
{
for (var i=0; i<replace.length; i++)
str = str.replace(replace[i], by[i]);
return str;
}

144
web/js/regions_Residence.js Normal file
View File

@ -0,0 +1,144 @@
regionConstructors['polygon'] = function(map, name, region)
{
if(regionCfg.regiontype == 'worldguard')
{
region.x1 = region.min.x;
region.y1 = region.min.y;
region.z1 = region.min.z;
region.x2 = region.max.x;
region.y2 = region.max.y;
region.z2 = region.max.z;
}
if(regionCfg.use3dregions)
{
regionPolygons[name+'_bottom'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.x1,region.y1,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,region.y1,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,region.y1,region.z2),
map.getProjection().fromWorldToLatLng(region.x1,region.y1,region.z2)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_bottom'] , 'click', function(event) {
regionInfo(event, name, region);
});
regionPolygons[name+'_top'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.x1,region.y2,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,region.y2,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,region.y2,region.z2),
map.getProjection().fromWorldToLatLng(region.x1,region.y2,region.z2)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_top'] , 'click', function(event) {
regionInfo(event, name, region);
});
regionPolygons[name+'_east'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.x1,region.y1,region.z1),
map.getProjection().fromWorldToLatLng(region.x1,region.y2,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,region.y2,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,region.y1,region.z1)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_east'] , 'click', function(event) {
regionInfo(event, name, region);
});
regionPolygons[name+'_south'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.x2,region.y1,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,region.y2,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,region.y2,region.z2),
map.getProjection().fromWorldToLatLng(region.x2,region.y1,region.z2)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_south'] , 'click', function(event) {
regionInfo(event, name, region);
});
regionPolygons[name+'_west'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.x1,region.y1,region.z2),
map.getProjection().fromWorldToLatLng(region.x1,region.y2,region.z2),
map.getProjection().fromWorldToLatLng(region.x2,region.y2,region.z2),
map.getProjection().fromWorldToLatLng(region.x2,region.y1,region.z2)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_west'] , 'click', function(event) {
regionInfo(event, name, region);
});
regionPolygons[name+'_north'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.x1,region.y1,region.z1),
map.getProjection().fromWorldToLatLng(region.x1,region.y2,region.z1),
map.getProjection().fromWorldToLatLng(region.x1,region.y2,region.z2),
map.getProjection().fromWorldToLatLng(region.x1,region.y1,region.z2)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_west'] , 'click', function(event) {
regionInfo(event, name, region);
});
}
else
{
middleY = region.y2;
regionPolygons[name+'_bottom'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.x1,middleY,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,middleY,region.z1),
map.getProjection().fromWorldToLatLng(region.x2,middleY,region.z2),
map.getProjection().fromWorldToLatLng(region.x1,middleY,region.z2)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_bottom'] , 'click', function(event) {
regionInfo(event, name, region);
});
}
}
regionConstructors['info'] = function(event, name, region)
{
if(regionCfg.regiontype == 'residence')
{
$.each(region.areaflags, function(flag, status)
{
});
var replace = ['%regionname%','%owners%'];
var by = [name,region.permissions.owner];
}
var contentString = arrayReplace(replace, by, regionCfg.infowindow)
regionInfoWindow.setContent(contentString);
regionInfoWindow.setPosition(event.latLng);
regionInfoWindow.open(dynmap.map);
}
regionConstructors['update'] = function(map)
{
if(regionInfoWindow)
regionInfoWindow.close();
$.each(regionPolygons, function(index, region)
{
region.setMap(null);
});
regionPolygons = {};
$.getJSON("standalone/res.json", function(data)
{
$.each(data, function(name, residence)
{
if(map == residence.world)
makeRegionPolygonCube(dynmap.map, name, residence);
});
});
}

View File

@ -0,0 +1,173 @@
regionConstructors['polygon'] = function(map, name, region)
{
if(!region.min || !region.max)
return;
if(region.max.y > 64)
region.max.y = 64;
if(regionCfg.use3dregions)
{
regionPolygons[name+'_bottom'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.min.x,region.min.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.min.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.min.y,region.max.z),
map.getProjection().fromWorldToLatLng(region.min.x,region.min.y,region.max.z)
],
map: map
}));
regionPolygons[name+'_top'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.min.x,region.max.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.max.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.max.y,region.max.z),
map.getProjection().fromWorldToLatLng(region.min.x,region.max.y,region.max.z)
],
map: map
}));
regionPolygons[name+'_east'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.min.x,region.min.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.min.x,region.max.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.max.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.min.y,region.min.z)
],
map: map
}));
regionPolygons[name+'_south'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.max.x,region.min.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.max.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.max.y,region.max.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.min.y,region.max.z)
],
map: map
}));
regionPolygons[name+'_west'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.min.x,region.min.y,region.max.z),
map.getProjection().fromWorldToLatLng(region.min.x,region.max.y,region.max.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.max.y,region.max.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.min.y,region.max.z)
],
map: map
}));
regionPolygons[name+'_north'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.min.x,region.min.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.min.x,region.max.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.min.x,region.max.y,region.max.z),
map.getProjection().fromWorldToLatLng(region.min.x,region.min.y,region.max.z)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_bottom'] , 'click', function(event) {
regionInfo(event, name, region);
});
google.maps.event.addListener(regionPolygons[name+'_top'] , 'click', function(event) {
regionInfo(event, name, region);
});
google.maps.event.addListener(regionPolygons[name+'_east'] , 'click', function(event) {
regionInfo(event, name, region);
});
google.maps.event.addListener(regionPolygons[name+'_south'] , 'click', function(event) {
regionInfo(event, name, region);
});
google.maps.event.addListener(regionPolygons[name+'_west'] , 'click', function(event) {
regionInfo(event, name, region);
});
google.maps.event.addListener(regionPolygons[name+'_north'] , 'click', function(event) {
regionInfo(event, name, region);
});
}
else
{
regionPolygons[name+'_bottom'] = new google.maps.Polygon($.extend(regionCfg.regionstyle, {
paths: [
map.getProjection().fromWorldToLatLng(region.min.x,region.max.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.max.y,region.min.z),
map.getProjection().fromWorldToLatLng(region.max.x,region.max.y,region.max.z),
map.getProjection().fromWorldToLatLng(region.min.x,region.max.y,region.max.z)
],
map: map
}));
google.maps.event.addListener(regionPolygons[name+'_bottom'] , 'click', function(event) {
regionInfo(event, name, region);
});
}
}
regionConstructors['info'] = function(event, name, region)
{
var owners = {'players':'', 'groups':''};
$.each(region.owners, function(type, names)
{
$.each(names, function(index, name)
{
if(type == 'players')
owners['players'] += name+' ';
else if(type == 'groups')
owners['groups'] += name+' ';
});
});
var members = {'players':'', 'groups':''};
$.each(region.members, function(type, names)
{
$.each(names, function(index, name)
{
if(type == 'players')
members['players'] += name+' ';
else if(type == 'groups')
members['groups'] += name+' ';
});
});
var flags = '';
$.each(region.flags, function(name, value)
{
flags += name+': '+value+'<br />';
});
name = '<span class="regionname">'+name+'</span>';
owners['players'] = '<span class="playerowners">'+owners['players']+'</span>';
owners['groups'] = '<span class="groupowners">'+owners['groups']+'</span>';
members['players'] = '<span class="playermembers">'+members['players']+'</span>';
members['groups'] = '<span class="groupmembers">'+members['groups']+'</span>';
var region_parent = (region.parent) ? '<span class="regionparent">'+region.parent+'</span>' : '';
flags = '<span class="regionflags">'+flags+'</span>';
var region_priority = '<span class="regionpriority">'+region.priority+'</span>';
var replace = ['%regionname%','%playerowners%','%groupowners%','%playermembers%','%groupmembers%','%parent%','%flags%','%priority%'];
var by = [name,owners['players'],owners['groups'],members['players'], members['groups'],region_parent,flags,region_priority];
var contentString = arrayReplace(replace, by, regionCfg.infowindow)
regionInfoWindow.setContent(contentString);
regionInfoWindow.setPosition(event.latLng);
regionInfoWindow.open(dynmap.map);
}
regionConstructors['update'] = function(map)
{
if(regionInfoWindow)
regionInfoWindow.close();
$.each(regionPolygons, function(index, region)
{
region.setMap(null);
});
regionPolygons = {};
regionFile = regionCfg.filename.substr(0, regionCfg.filename.lastIndexOf('.'));
regionFile += '_'+map+'.json';
$.getJSON('standalone/'+regionFile, function(data)
{
var regionnames = '';
var count = 0;
$.each(data, function(name, residence)
{
count += 1;
regionnames += ", "+name;
makeRegionPolygonCube(dynmap.map, name, residence);
});
});
}