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.java.*;
import org.dynmap.debug.BukkitPlayerDebugger;
import org.dynmap.debug.NullDebugger;
public class DynmapPlugin extends JavaPlugin {
@ -35,7 +36,7 @@ public class DynmapPlugin extends JavaPlugin {
mgr.startManager();
try {
server = new WebServer(mgr.serverport, mgr, getServer(), debugger);
server = new WebServer(mgr.serverport, mgr, getServer(), new NullDebugger());
} catch(IOException e) {
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("/removesign", " [name] - removes a named sign to the map");
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.Location;
import org.bukkit.World;
import org.dynmap.debug.Debugger;
public abstract class Map {
private MapManager manager;
public MapManager getMapManager() {
return manager;
}
private World world;
public World getWorld() {
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.staleQueue = queue;
this.debugger = debugger;
}
public void invalidateTile(MapTile tile) {
staleQueue.pushStaleTile(tile);
manager.invalidateTile(tile);
}
public abstract void touch(Location l);

View File

@ -36,7 +36,7 @@ import java.util.logging.Logger;
import org.bukkit.*;
import org.dynmap.debug.Debugger;
import org.dynmap.kzedmap.CaveTileRenderer;
import org.dynmap.kzedmap.DayTileRenderer;
import org.dynmap.kzedmap.DefaultTileRenderer;
import org.dynmap.kzedmap.KzedMap;
import org.dynmap.kzedmap.MapTileRenderer;
@ -56,9 +56,6 @@ public class MapManager extends Thread {
/* whether the worker thread should be running now */
private boolean running = false;
/* path to colors.txt */
private String colorsetpath = "colors.txt";
/* path to image tile directory */
public String tilepath = "tiles/";
@ -86,17 +83,12 @@ public class MapManager extends Thread {
this.staleQueue = new StaleQueue();
tilepath = "/srv/http/dynmap/tiles/";
colorsetpath = "colors.txt";
serverport = 8123;
bindaddress = "0.0.0.0";
//webPath = "/srv/http/dynmap/";
webPath = "[JAR]";
Map<Integer, Color[]> colors = loadColorSet(colorsetpath);
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")
});
map = new KzedMap(this, world, debugger);
}
/* initialize and start map manager */
@ -141,7 +133,7 @@ public class MapManager extends Thread {
debugger.debug("rendering tile " + t + "...");
t.getMap().render(t);
staleQueue.freshenTile(t);
staleQueue.onTileUpdated(t);
try {
this.sleep(renderWait);
@ -166,45 +158,8 @@ public class MapManager extends Thread {
map.touch(new Location(world, x, y, z));
}
public Map<Integer, Color[]> loadColorSet(String colorsetpath) {
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();
log.info(nc + " colors loaded from " + colorsetpath);
} catch(Exception e) {
log.log(Level.SEVERE, "Failed to load colorset: " + colorsetpath, e);
}
return colors;
public void invalidateTile(MapTile tile) {
debugger.debug("invalidating tile " + tile.getName());
staleQueue.pushStaleTile(tile);
}
}

View File

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

View File

@ -122,7 +122,7 @@ public class WebServerRequest extends Thread {
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();

View File

@ -7,10 +7,10 @@ import org.bukkit.World;
import org.dynmap.MapManager;
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) {
super(debugger, colors, outputPath);
public CaveTileRenderer(String name, Debugger debugger) {
super(name, debugger);
}
@Override

View File

@ -18,30 +18,20 @@ import org.dynmap.MapManager;
import org.dynmap.MapTile;
import org.dynmap.debug.Debugger;
public class DayTileRenderer implements MapTileRenderer {
public class DefaultTileRenderer implements MapTileRenderer {
private String name;
protected Debugger debugger;
protected String outputPath;
protected String outputZoomPath;
private Map<Integer, Color[]> colors;
public DayTileRenderer(Debugger debugger, Map<Integer, Color[]> colors, String outputPath) {
this(debugger, colors, outputPath, convertToZoomPath(outputPath));
public String getName() {
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.colors = colors;
this.outputPath = outputPath;
this.outputZoomPath = outputZoomPath;
}
private static String convertToZoomPath(String outputPath) {
File outputFile = new File(outputPath);
String zoomFilename = "z" + outputFile.getName();
return new File(outputFile.getParentFile(), zoomFilename).getPath();
}
public void render(KzedMapTile tile) {
public void render(KzedMapTile tile, String path) {
World world = tile.getMap().getWorld();
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 */
saveTile(tile, im);
saveTile(tile, im, path);
}
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;
if(id != 0) {
Color[] colors = this.colors.get(id);
Color[] colors = KzedMap.colors.get(id);
if(colors != null) {
Color c = colors[seq];
if(c.getAlpha() > 0) {
@ -152,11 +142,9 @@ public class DayTileRenderer implements MapTileRenderer {
}
/* 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
.replace("{X}", Integer.toString(tile.px))
.replace("{Y}", Integer.toString(tile.py));
String tilePath = getPath(tile, path);
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 */
public BufferedImage loadTile(KzedMapTile tile)
/*public BufferedImage loadTile(KzedMapTile tile)
{
try {
String path = getPath(tile);
@ -252,14 +245,7 @@ public class DayTileRenderer implements MapTileRenderer {
}
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

View File

@ -1,14 +1,20 @@
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.World;
import org.dynmap.Map;
import org.dynmap.MapManager;
import org.dynmap.MapTile;
import org.dynmap.StaleQueue;
import org.dynmap.debug.Debugger;
public class KzedMap extends Map {
MapTileRenderer[] renderers;
/* dimensions of a map tile */
public static final int tileWidth = 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 anchorz = 0;
public KzedMap(World world, StaleQueue queue, MapTileRenderer[] renderers) {
super(world, queue);
this.renderers = renderers;
public static java.util.Map<Integer, Color[]> colors;
MapTileRenderer[] 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
@ -62,15 +76,15 @@ public class KzedMap extends Map {
}
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
public void render(MapTile tile) {
KzedMapTile t = (KzedMapTile)tile;
for(MapTileRenderer renderer : renderers) {
renderer.render(t);
}
t.renderer.render(t, getMapManager().tilepath);
}
/* tile X for position x */
@ -292,4 +306,44 @@ public class KzedMap extends Map {
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 String prefix;
public MapTileRenderer renderer;
/* projection position */
public int px, py;
/* projection position of zoom-out tile */
public int zpx, zpy;
/* minecraft space origin */
public int mx, my, mz;
/* 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);
this.prefix = prefix;
this.renderer = renderer;
this.px = px;
this.py = py;
this.zpx = zpx;
this.zpy = zpy;
mx = KzedMap.anchorx + px / 2 + py / 2;
my = KzedMap.anchory;
@ -48,7 +43,7 @@ public class KzedMapTile extends MapTile {
@Override
public String getName() {
return prefix + "_" + this.px + "_" + this.py;
return renderer.getName() + "_" + px + "_" + py;
}
/* 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 */
public int hashCode()
{
return (px << 16) ^ py;
return ((px << 16) ^ py) ^ getName().hashCode();
}
@Override
@ -140,12 +135,12 @@ public class KzedMapTile extends MapTile {
/* equality comparison - based on projection position */
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... */
public String toString()
{
return px + "_" + py;
return getName() + "_" + px + "_" + py;
}
}

View File

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