Cleanup/Refactoring.

This commit is contained in:
FrozenCow 2011-01-08 18:56:08 +01:00
parent 8860eb8c04
commit e1a3ac60e2
10 changed files with 119 additions and 116 deletions

View File

@ -10,6 +10,7 @@ import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.*; import org.bukkit.plugin.*;
import org.bukkit.plugin.java.*; import org.bukkit.plugin.java.*;
import org.dynmap.debug.BukkitPlayerDebugger; import org.dynmap.debug.BukkitPlayerDebugger;
import org.dynmap.debug.NullDebugger;
public class DynmapPlugin extends JavaPlugin { public class DynmapPlugin extends JavaPlugin {
@ -35,7 +36,7 @@ public class DynmapPlugin extends JavaPlugin {
mgr.startManager(); mgr.startManager();
try { try {
server = new WebServer(mgr.serverport, mgr, getServer(), debugger); server = new WebServer(mgr.serverport, mgr, getServer(), new NullDebugger());
} catch(IOException e) { } catch(IOException e) {
log.info("position failed to start WebServer (IOException)"); log.info("position failed to start WebServer (IOException)");
} }
@ -71,7 +72,8 @@ public class DynmapPlugin extends JavaPlugin {
etc.getInstance().addCommand("/addsign", " [name] - adds a named sign to the map"); etc.getInstance().addCommand("/addsign", " [name] - adds a named sign to the map");
etc.getInstance().addCommand("/removesign", " [name] - removes a named sign to the map"); etc.getInstance().addCommand("/removesign", " [name] - removes a named sign to the map");
etc.getInstance().addCommand("/listsigns", " - list all named signs"); etc.getInstance().addCommand("/listsigns", " - list all named signs");
etc.getInstance().addCommand("/tpsign", " [name] - teleport to a named sign");*/ etc.getInstance().addCommand("/tpsign", " [name] - teleport to a named sign");
*/
} }
} }

View File

@ -3,22 +3,32 @@ package org.dynmap;
import org.bukkit.Block; import org.bukkit.Block;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.dynmap.debug.Debugger;
public abstract class Map { public abstract class Map {
private MapManager manager;
public MapManager getMapManager() {
return manager;
}
private World world; private World world;
public World getWorld() { public World getWorld() {
return world; return world;
} }
private StaleQueue staleQueue; private Debugger debugger;
public Debugger getDebugger() {
return debugger;
}
public Map(World world, StaleQueue queue) { public Map(MapManager manager, World world, Debugger debugger) {
this.manager = manager;
this.world = world; this.world = world;
this.staleQueue = queue; this.debugger = debugger;
} }
public void invalidateTile(MapTile tile) { public void invalidateTile(MapTile tile) {
staleQueue.pushStaleTile(tile); manager.invalidateTile(tile);
} }
public abstract void touch(Location l); public abstract void touch(Location l);

View File

@ -36,7 +36,7 @@ import java.util.logging.Logger;
import org.bukkit.*; import org.bukkit.*;
import org.dynmap.debug.Debugger; import org.dynmap.debug.Debugger;
import org.dynmap.kzedmap.CaveTileRenderer; import org.dynmap.kzedmap.CaveTileRenderer;
import org.dynmap.kzedmap.DayTileRenderer; import org.dynmap.kzedmap.DefaultTileRenderer;
import org.dynmap.kzedmap.KzedMap; import org.dynmap.kzedmap.KzedMap;
import org.dynmap.kzedmap.MapTileRenderer; import org.dynmap.kzedmap.MapTileRenderer;
@ -56,9 +56,6 @@ public class MapManager extends Thread {
/* whether the worker thread should be running now */ /* whether the worker thread should be running now */
private boolean running = false; private boolean running = false;
/* path to colors.txt */
private String colorsetpath = "colors.txt";
/* path to image tile directory */ /* path to image tile directory */
public String tilepath = "tiles/"; public String tilepath = "tiles/";
@ -86,17 +83,12 @@ public class MapManager extends Thread {
this.staleQueue = new StaleQueue(); this.staleQueue = new StaleQueue();
tilepath = "/srv/http/dynmap/tiles/"; tilepath = "/srv/http/dynmap/tiles/";
colorsetpath = "colors.txt";
serverport = 8123; serverport = 8123;
bindaddress = "0.0.0.0"; bindaddress = "0.0.0.0";
//webPath = "/srv/http/dynmap/"; //webPath = "/srv/http/dynmap/";
webPath = "[JAR]"; webPath = "[JAR]";
Map<Integer, Color[]> colors = loadColorSet(colorsetpath); map = new KzedMap(this, world, debugger);
map = new KzedMap(world, staleQueue, new MapTileRenderer[] {
new DayTileRenderer(debugger, colors, tilepath + "t_{X}_{Y}.png"),
new CaveTileRenderer(debugger, colors, tilepath + "ct_{X}_{Y}.png")
});
} }
/* initialize and start map manager */ /* initialize and start map manager */
@ -141,7 +133,7 @@ public class MapManager extends Thread {
debugger.debug("rendering tile " + t + "..."); debugger.debug("rendering tile " + t + "...");
t.getMap().render(t); t.getMap().render(t);
staleQueue.freshenTile(t); staleQueue.onTileUpdated(t);
try { try {
this.sleep(renderWait); this.sleep(renderWait);
@ -166,45 +158,8 @@ public class MapManager extends Thread {
map.touch(new Location(world, x, y, z)); map.touch(new Location(world, x, y, z));
} }
public Map<Integer, Color[]> loadColorSet(String colorsetpath) { public void invalidateTile(MapTile tile) {
Map<Integer, Color[]> colors = new HashMap<Integer, Color[]>(); debugger.debug("invalidating tile " + tile.getName());
staleQueue.pushStaleTile(tile);
/* load colorset */
File cfile = new File(colorsetpath);
try {
Scanner scanner = new Scanner(cfile);
int nc = 0;
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#") || line.equals("")) {
continue;
}
String[] split = line.split("\t");
if (split.length < 17) {
continue;
}
Integer id = new Integer(split[0]);
Color[] c = new Color[4];
/* store colors by raycast sequence number */
c[0] = new Color(Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]));
c[3] = new Color(Integer.parseInt(split[5]), Integer.parseInt(split[6]), Integer.parseInt(split[7]), Integer.parseInt(split[8]));
c[1] = new Color(Integer.parseInt(split[9]), Integer.parseInt(split[10]), Integer.parseInt(split[11]), Integer.parseInt(split[12]));
c[2] = new Color(Integer.parseInt(split[13]), Integer.parseInt(split[14]), Integer.parseInt(split[15]), Integer.parseInt(split[16]));
colors.put(id, c);
nc += 1;
}
scanner.close();
log.info(nc + " colors loaded from " + colorsetpath);
} catch(Exception e) {
log.log(Level.SEVERE, "Failed to load colorset: " + colorsetpath, e);
}
return colors;
} }
} }

View File

@ -54,7 +54,7 @@ public class StaleQueue {
} }
} }
public void freshenTile(MapTile t) { public void onTileUpdated(MapTile t) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long deadline = now - maxTileAge; long deadline = now - maxTileAge;
synchronized(MapManager.lock) { synchronized(MapManager.lock) {

View File

@ -122,7 +122,7 @@ public class WebServerRequest extends Thread {
sb.append("tile " + tu.tile.getName() + "\n"); sb.append("tile " + tu.tile.getName() + "\n");
} }
//debugger.debug("Sending " + players.length + " players and " + tileUpdates.length + " tile-updates. " + path + ";" + cutoff); debugger.debug("Sending " + players.length + " players and " + tileUpdates.length + " tile-updates. " + path + ";" + cutoff);
byte[] bytes = sb.toString().getBytes(); byte[] bytes = sb.toString().getBytes();

View File

@ -7,10 +7,10 @@ import org.bukkit.World;
import org.dynmap.MapManager; import org.dynmap.MapManager;
import org.dynmap.debug.Debugger; import org.dynmap.debug.Debugger;
public class CaveTileRenderer extends DayTileRenderer { public class CaveTileRenderer extends DefaultTileRenderer {
public CaveTileRenderer(Debugger debugger, Map<Integer, Color[]> colors, String outputPath) { public CaveTileRenderer(String name, Debugger debugger) {
super(debugger, colors, outputPath); super(name, debugger);
} }
@Override @Override

View File

@ -18,30 +18,20 @@ import org.dynmap.MapManager;
import org.dynmap.MapTile; import org.dynmap.MapTile;
import org.dynmap.debug.Debugger; import org.dynmap.debug.Debugger;
public class DayTileRenderer implements MapTileRenderer { public class DefaultTileRenderer implements MapTileRenderer {
private String name;
protected Debugger debugger; protected Debugger debugger;
protected String outputPath;
protected String outputZoomPath;
private Map<Integer, Color[]> colors;
public DayTileRenderer(Debugger debugger, Map<Integer, Color[]> colors, String outputPath) { public String getName() {
this(debugger, colors, outputPath, convertToZoomPath(outputPath)); return name;
} }
public DayTileRenderer(Debugger debugger, Map<Integer, Color[]> colors, String outputPath, String outputZoomPath) { public DefaultTileRenderer(String name, Debugger debugger) {
this.name = name;
this.debugger = debugger; this.debugger = debugger;
this.colors = colors;
this.outputPath = outputPath;
this.outputZoomPath = outputZoomPath;
} }
private static String convertToZoomPath(String outputPath) { public void render(KzedMapTile tile, String path) {
File outputFile = new File(outputPath);
String zoomFilename = "z" + outputFile.getName();
return new File(outputFile.getParentFile(), zoomFilename).getPath();
}
public void render(KzedMapTile tile) {
World world = tile.getMap().getWorld(); World world = tile.getMap().getWorld();
BufferedImage im = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB); BufferedImage im = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB);
@ -93,7 +83,7 @@ public class DayTileRenderer implements MapTileRenderer {
} }
/* save the generated tile */ /* save the generated tile */
saveTile(tile, im); saveTile(tile, im, path);
} }
protected Color scan(World world, int x, int y, int z, int seq) protected Color scan(World world, int x, int y, int z, int seq)
@ -122,7 +112,7 @@ public class DayTileRenderer implements MapTileRenderer {
seq = (seq + 1) & 3; seq = (seq + 1) & 3;
if(id != 0) { if(id != 0) {
Color[] colors = this.colors.get(id); Color[] colors = KzedMap.colors.get(id);
if(colors != null) { if(colors != null) {
Color c = colors[seq]; Color c = colors[seq];
if(c.getAlpha() > 0) { if(c.getAlpha() > 0) {
@ -152,11 +142,9 @@ public class DayTileRenderer implements MapTileRenderer {
} }
/* save rendered tile, update zoom-out tile */ /* save rendered tile, update zoom-out tile */
public void saveTile(KzedMapTile tile, BufferedImage im) public void saveTile(KzedMapTile tile, BufferedImage im, String path)
{ {
String tilePath = outputPath String tilePath = getPath(tile, path);
.replace("{X}", Integer.toString(tile.px))
.replace("{Y}", Integer.toString(tile.py));
debugger.debug("saving tile " + tilePath); debugger.debug("saving tile " + tilePath);
@ -237,8 +225,13 @@ public class DayTileRenderer implements MapTileRenderer {
}*/ }*/
} }
public String getPath(KzedMapTile tile, String outputPath)
{
return new File(new File(outputPath), tile.getName() + ".png").getPath();
}
/* try to load already generated image */ /* try to load already generated image */
public BufferedImage loadTile(KzedMapTile tile) /*public BufferedImage loadTile(KzedMapTile tile)
{ {
try { try {
String path = getPath(tile); String path = getPath(tile);
@ -252,14 +245,7 @@ public class DayTileRenderer implements MapTileRenderer {
} }
return null; return null;
} }*/
public String getPath(KzedMapTile tile)
{
return outputPath
.replace("{X}", Integer.toString(tile.px))
.replace("{Y}", Integer.toString(tile.py));
}
/* /*
// generate a path name for this map tile // generate a path name for this map tile

View File

@ -1,14 +1,20 @@
package org.dynmap.kzedmap; package org.dynmap.kzedmap;
import java.awt.Color;
import java.io.File;
import java.util.HashMap;
import java.util.Scanner;
import java.util.logging.Level;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.dynmap.Map; import org.dynmap.Map;
import org.dynmap.MapManager;
import org.dynmap.MapTile; import org.dynmap.MapTile;
import org.dynmap.StaleQueue; import org.dynmap.StaleQueue;
import org.dynmap.debug.Debugger;
public class KzedMap extends Map { public class KzedMap extends Map {
MapTileRenderer[] renderers;
/* dimensions of a map tile */ /* dimensions of a map tile */
public static final int tileWidth = 128; public static final int tileWidth = 128;
public static final int tileHeight = 128; public static final int tileHeight = 128;
@ -23,9 +29,17 @@ public class KzedMap extends Map {
public static final int anchory = 127; public static final int anchory = 127;
public static final int anchorz = 0; public static final int anchorz = 0;
public KzedMap(World world, StaleQueue queue, MapTileRenderer[] renderers) { public static java.util.Map<Integer, Color[]> colors;
super(world, queue); MapTileRenderer[] renderers;
this.renderers = renderers;
public KzedMap(MapManager manager, World world, Debugger debugger) {
super(manager, world, debugger);
if (colors == null)
colors = loadColorSet("colors.txt");
renderers = new MapTileRenderer[] {
new DefaultTileRenderer("t", debugger),
new CaveTileRenderer("ct", debugger)
};
} }
@Override @Override
@ -62,15 +76,15 @@ public class KzedMap extends Map {
} }
public void invalidateTile(int px, int py) { public void invalidateTile(int px, int py) {
invalidateTile(new KzedMapTile(this, "t", px, py, ztilex(px), ztiley(py))); for(MapTileRenderer renderer : renderers) {
invalidateTile(new KzedMapTile(this, renderer, px, py));
}
} }
@Override @Override
public void render(MapTile tile) { public void render(MapTile tile) {
KzedMapTile t = (KzedMapTile)tile; KzedMapTile t = (KzedMapTile)tile;
for(MapTileRenderer renderer : renderers) { t.renderer.render(t, getMapManager().tilepath);
renderer.render(t);
}
} }
/* tile X for position x */ /* tile X for position x */
@ -292,4 +306,44 @@ public class KzedMap extends Map {
return good; return good;
} }
*/ */
public static java.util.Map<Integer, Color[]> loadColorSet(String colorsetpath) {
java.util.Map<Integer, Color[]> colors = new HashMap<Integer, Color[]>();
/* load colorset */
File cfile = new File(colorsetpath);
try {
Scanner scanner = new Scanner(cfile);
int nc = 0;
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("#") || line.equals("")) {
continue;
}
String[] split = line.split("\t");
if (split.length < 17) {
continue;
}
Integer id = new Integer(split[0]);
Color[] c = new Color[4];
/* store colors by raycast sequence number */
c[0] = new Color(Integer.parseInt(split[1]), Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]));
c[3] = new Color(Integer.parseInt(split[5]), Integer.parseInt(split[6]), Integer.parseInt(split[7]), Integer.parseInt(split[8]));
c[1] = new Color(Integer.parseInt(split[9]), Integer.parseInt(split[10]), Integer.parseInt(split[11]), Integer.parseInt(split[12]));
c[2] = new Color(Integer.parseInt(split[13]), Integer.parseInt(split[14]), Integer.parseInt(split[15]), Integer.parseInt(split[16]));
colors.put(id, c);
nc += 1;
}
scanner.close();
} catch(Exception e) {
return null;
}
return colors;
}
} }

View File

@ -21,25 +21,20 @@ public class KzedMapTile extends MapTile {
public KzedMap map; public KzedMap map;
public String prefix; public MapTileRenderer renderer;
/* projection position */ /* projection position */
public int px, py; public int px, py;
/* projection position of zoom-out tile */
public int zpx, zpy;
/* minecraft space origin */ /* minecraft space origin */
public int mx, my, mz; public int mx, my, mz;
/* create new MapTile */ /* create new MapTile */
public KzedMapTile(KzedMap map, String prefix, int px, int py, int zpx, int zpy) { public KzedMapTile(KzedMap map, MapTileRenderer renderer, int px, int py) {
super(map); super(map);
this.prefix = prefix; this.renderer = renderer;
this.px = px; this.px = px;
this.py = py; this.py = py;
this.zpx = zpx;
this.zpy = zpy;
mx = KzedMap.anchorx + px / 2 + py / 2; mx = KzedMap.anchorx + px / 2 + py / 2;
my = KzedMap.anchory; my = KzedMap.anchory;
@ -48,7 +43,7 @@ public class KzedMapTile extends MapTile {
@Override @Override
public String getName() { public String getName() {
return prefix + "_" + this.px + "_" + this.py; return renderer.getName() + "_" + px + "_" + py;
} }
/* try to get the server to load the relevant chunks */ /* try to get the server to load the relevant chunks */
@ -126,7 +121,7 @@ public class KzedMapTile extends MapTile {
/* hash value, based on projection position */ /* hash value, based on projection position */
public int hashCode() public int hashCode()
{ {
return (px << 16) ^ py; return ((px << 16) ^ py) ^ getName().hashCode();
} }
@Override @Override
@ -140,12 +135,12 @@ public class KzedMapTile extends MapTile {
/* equality comparison - based on projection position */ /* equality comparison - based on projection position */
public boolean equals(KzedMapTile o) public boolean equals(KzedMapTile o)
{ {
return o.px == px && o.py == py; return o.getName().equals(getName()) && o.px == px && o.py == py;
} }
/* return a simple string representation... */ /* return a simple string representation... */
public String toString() public String toString()
{ {
return px + "_" + py; return getName() + "_" + px + "_" + py;
} }
} }

View File

@ -3,5 +3,6 @@ package org.dynmap.kzedmap;
import org.dynmap.MapTile; import org.dynmap.MapTile;
public interface MapTileRenderer { public interface MapTileRenderer {
void render(KzedMapTile tile); String getName();
void render(KzedMapTile tile, String path);
} }