Made use of org.json.simple for updates.

This commit is contained in:
FrozenCow 2011-05-20 21:37:58 +02:00
parent 987e6bcb54
commit 9fa46597d1
2 changed files with 41 additions and 22 deletions

View File

@ -145,7 +145,7 @@ public class DynmapPlugin extends JavaPlugin {
webServer = new HttpServer(bindAddress, port); webServer = new HttpServer(bindAddress, port);
webServer.handlers.put("/", new FilesystemHandler(getFile(configuration.getString("webpath", "web")))); webServer.handlers.put("/", new FilesystemHandler(getFile(configuration.getString("webpath", "web"))));
webServer.handlers.put("/tiles/", new FilesystemHandler(tilesDirectory)); webServer.handlers.put("/tiles/", new FilesystemHandler(tilesDirectory));
webServer.handlers.put("/up/", new ClientUpdateHandler(mapManager, playerList, getServer(), configuration.getBoolean("health-in-json", false))); webServer.handlers.put("/up/", new ClientUpdateHandler(this, configuration.getBoolean("health-in-json", false)));
webServer.handlers.put("/up/configuration", new ClientConfigurationHandler(this)); webServer.handlers.put("/up/configuration", new ClientConfigurationHandler(this));
if (configuration.getBoolean("allowwebchat", false)) { if (configuration.getBoolean("allowwebchat", false)) {

View File

@ -11,6 +11,7 @@ import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
import org.dynmap.Client; import org.dynmap.Client;
import org.dynmap.DynmapPlugin;
import org.dynmap.MapManager; import org.dynmap.MapManager;
import org.dynmap.PlayerList; import org.dynmap.PlayerList;
import org.dynmap.web.HttpField; import org.dynmap.web.HttpField;
@ -19,17 +20,16 @@ import org.dynmap.web.HttpRequest;
import org.dynmap.web.HttpResponse; import org.dynmap.web.HttpResponse;
import org.dynmap.web.HttpStatus; import org.dynmap.web.HttpStatus;
import org.dynmap.web.Json; import org.dynmap.web.Json;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import static org.dynmap.JSONUtils.*;
public class ClientUpdateHandler implements HttpHandler { public class ClientUpdateHandler implements HttpHandler {
private MapManager mapManager; private DynmapPlugin plugin;
private PlayerList playerList;
private Server server;
private boolean showHealth; private boolean showHealth;
public ClientUpdateHandler(MapManager mapManager, PlayerList playerList, Server server, boolean showHealth) { public ClientUpdateHandler(DynmapPlugin plugin, boolean showHealth) {
this.mapManager = mapManager; this.plugin = plugin;
this.playerList = playerList;
this.server = server;
this.showHealth = showHealth; this.showHealth = showHealth;
} }
@ -48,7 +48,7 @@ public class ClientUpdateHandler implements HttpHandler {
String worldName = match.group(1); String worldName = match.group(1);
String timeKey = match.group(2); String timeKey = match.group(2);
World world = server.getWorld(worldName); World world = plugin.getServer().getWorld(worldName);
if (world == null) { if (world == null) {
response.status = WorldNotFound; response.status = WorldNotFound;
return; return;
@ -64,26 +64,45 @@ public class ClientUpdateHandler implements HttpHandler {
} }
} }
Client.Update update = new Client.Update(); JSONObject u = new JSONObject();
update.timestamp = current; //plugin.events.trigger("buildclientupdate", update);
update.servertime = world.getTime() % 24000; s(u, "timestamp", current);
update.hasStorm = world.hasStorm(); s(u, "servertime", world.getTime() % 24000);
update.isThundering = world.isThundering(); s(u, "hasStorm", world.hasStorm());
s(u, "isThundering", world.isThundering());
s(u, "players", new JSONArray());
Player[] players = playerList.getVisiblePlayers(); Player[] players = plugin.playerList.getVisiblePlayers();
update.players = new Client.Player[players.length];
for(int i=0;i<players.length;i++) { for(int i=0;i<players.length;i++) {
Player p = players[i]; Player p = players[i];
Location pl = p.getLocation(); Location pl = p.getLocation();
update.players[i] = new Client.Player(p.getDisplayName(), pl.getWorld().getName(), pl.getX(), pl.getY(), pl.getZ(), showHealth?p.getHealth():-1, JSONObject jp = new JSONObject();
p.getName()); s(jp, "type", "player");
s(jp, "name", p.getDisplayName());
s(jp, "account", p.getName());
s(jp, "world", p.getWorld().getName());
s(jp, "x", pl.getX());
s(jp, "y", pl.getY());
s(jp, "z", pl.getZ());
if (showHealth) {
s(jp, "health", p.getHealth());
}
a(u, "players", jp);
} }
update.updates = mapManager.getWorldUpdates(worldName, since); s(u, "updates", new JSONArray());
for(Object update : plugin.mapManager.getWorldUpdates(worldName, since)) {
if (update instanceof Client.Tile) {
Client.Tile tile = (Client.Tile)update;
JSONObject t = new JSONObject();
s(t, "type", "tile");
s(t, "timestamp", tile.timestamp);
s(t, "name", tile.name);
a(u, "updates", t);
}
}
byte[] bytes = u.toJSONString().getBytes();
byte[] bytes = Json.stringifyJson(update).getBytes();
String dateStr = new Date().toString(); String dateStr = new Date().toString();
response.fields.put(HttpField.Date, dateStr); response.fields.put(HttpField.Date, dateStr);