From fb01f6ecf7ef393480eaa5febc7941d94360b1f0 Mon Sep 17 00:00:00 2001 From: Jason Booth Date: Wed, 9 Feb 2011 11:35:09 -0600 Subject: [PATCH] New config to Disable Webserver New feature and config to output JSON to file --- configuration.txt | 9 +++ src/main/java/org/dynmap/DynmapPlugin.java | 88 ++++++++++++++++------ 2 files changed, 75 insertions(+), 22 deletions(-) diff --git a/configuration.txt b/configuration.txt index 0c880ec7..c8190d3d 100755 --- a/configuration.txt +++ b/configuration.txt @@ -15,6 +15,15 @@ webserver-bindaddress: 0.0.0.0 # The TCP-port the webserver will listen on. webserver-port: 8123 +# Disables Webserver portion of Dynmap (Advanced users only) +disable-webserver: true + +# Writes JSON to file in the webpath +jsonfile: true + +# How often the json file gets written to(in seconds) +jsonfile-interval: 1000 + disabledcommands: - fullrender diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index e3aab1ea..b660d14a 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -1,11 +1,14 @@ package org.dynmap; import java.io.File; +import java.io.FileOutputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Map; import java.util.logging.Logger; +import java.util.Timer; import org.bukkit.Server; import org.bukkit.World; @@ -22,6 +25,7 @@ import org.dynmap.web.HttpServer; import org.dynmap.web.handlers.ClientConfigurationHandler; import org.dynmap.web.handlers.ClientUpdateHandler; import org.dynmap.web.handlers.FilesystemHandler; +import org.dynmap.web.Json; public class DynmapPlugin extends JavaPlugin { @@ -32,6 +36,8 @@ public class DynmapPlugin extends JavaPlugin { private PlayerList playerList; private Configuration configuration; + private Timer timer; + private BukkitPlayerDebugger debugger = new BukkitPlayerDebugger(this); public static File dataRoot; @@ -64,31 +70,43 @@ public class DynmapPlugin extends JavaPlugin { mapManager = new MapManager(getWorld(), debugger, configuration); mapManager.startManager(); - InetAddress bindAddress; - { - String address = configuration.getString("webserver-bindaddress", "0.0.0.0"); - try { - bindAddress = address.equals("0.0.0.0") - ? null - : InetAddress.getByName(address); - } catch (UnknownHostException e) { - bindAddress = null; - } - } - int port = configuration.getInt("webserver-port", 8123); + if(!configuration.getBoolean("disable-webserver", true)) { + InetAddress bindAddress; + { + String address = configuration.getString("webserver-bindaddress", "0.0.0.0"); + try { + bindAddress = address.equals("0.0.0.0") + ? null + : InetAddress.getByName(address); + } catch (UnknownHostException e) { + bindAddress = null; + } + } + int port = configuration.getInt("webserver-port", 8123); - webServer = new HttpServer(bindAddress, port); - webServer.handlers.put("/", new FilesystemHandler(mapManager.webDirectory)); - webServer.handlers.put("/tiles/", new FilesystemHandler(mapManager.tileDirectory)); - webServer.handlers.put("/up/", new ClientUpdateHandler(mapManager, playerList, getWorld())); - webServer.handlers.put("/up/configuration", new ClientConfigurationHandler((Map) configuration.getProperty("web"))); + webServer = new HttpServer(bindAddress, port); + webServer.handlers.put("/", new FilesystemHandler(mapManager.webDirectory)); + webServer.handlers.put("/tiles/", new FilesystemHandler(mapManager.tileDirectory)); + webServer.handlers.put("/up/", new ClientUpdateHandler(mapManager, playerList, getWorld())); + webServer.handlers.put("/up/configuration", new ClientConfigurationHandler((Map) configuration.getProperty("web"))); - try { - webServer.startServer(); - } catch (IOException e) { - log.severe("Failed to start WebServer on " + bindAddress + ":" + port + "!"); - } + try { + webServer.startServer(); + } catch (IOException e) { + log.severe("Failed to start WebServer on " + bindAddress + ":" + port + "!"); + } + } + else + System.out.println("WebServer Disabled"); + if(configuration.getBoolean("jsonfile", false)) { + jsonConfig(); + int jsonInterval = configuration.getInt("jsonfile", 1) * 1000; + timer = new Timer(); + timer.scheduleAtFixedRate(new JsonTimerTask(this, configuration), jsonInterval, jsonInterval); + } + else + System.out.println("JsonFile Writing Disabled"); registerEvents(); } @@ -111,4 +129,30 @@ public class DynmapPlugin extends JavaPlugin { getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.Normal, this); getServer().getPluginManager().registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.Normal, this); } + + private void jsonConfig() + { + File outputFile; + Map clientConfig = (Map) configuration.getProperty("web"); + File webpath = new File(configuration.getString("webpath", "web"), "dynmap_config.json"); + if(webpath.isAbsolute()) + outputFile = webpath; + else + outputFile = new File(DynmapPlugin.dataRoot, webpath.toString()); + + try + { + FileOutputStream fos = new FileOutputStream(outputFile); + fos.write(Json.stringifyJson(clientConfig).getBytes()); + fos.close(); + } + catch(FileNotFoundException ex) + { + System.out.println("FileNotFoundException : " + ex); + } + catch(IOException ioe) + { + System.out.println("IOException : " + ioe); + } + } }