Applied Eclipse formatting.

This commit is contained in:
FrozenCow 2011-02-05 02:25:18 +01:00
parent 4f138a56da
commit 3940b91d0e
25 changed files with 1763 additions and 1708 deletions

View File

@ -2,38 +2,34 @@ package org.dynmap;
import java.util.HashMap; import java.util.HashMap;
public class Cache<K, V> public class Cache<K, V> {
{
private final int size; private final int size;
private int len; private int len;
private CacheNode head; private CacheNode head;
private CacheNode tail; private CacheNode tail;
private class CacheNode private class CacheNode {
{
public CacheNode prev; public CacheNode prev;
public CacheNode next; public CacheNode next;
public K key; public K key;
public V value; public V value;
public CacheNode(K key, V value) public CacheNode(K key, V value) {
{
this.key = key; this.key = key;
this.value = value; this.value = value;
prev = null; prev = null;
next = null; next = null;
} }
public void unlink() public void unlink() {
{ if (prev == null) {
if(prev == null) {
head = next; head = next;
} else { } else {
prev.next = next; prev.next = next;
} }
if(next == null) { if (next == null) {
tail = prev; tail = prev;
} else { } else {
next.prev = prev; next.prev = prev;
@ -42,12 +38,11 @@ public class Cache<K, V>
prev = null; prev = null;
next = null; next = null;
len --; len--;
} }
public void append() public void append() {
{ if (tail == null) {
if(tail == null) {
head = this; head = this;
tail = this; tail = this;
} else { } else {
@ -56,14 +51,13 @@ public class Cache<K, V>
tail = this; tail = this;
} }
len ++; len++;
} }
} }
private HashMap<K, CacheNode> map; private HashMap<K, CacheNode> map;
public Cache(int size) public Cache(int size) {
{
this.size = size; this.size = size;
len = 0; len = 0;
@ -73,27 +67,28 @@ public class Cache<K, V>
map = new HashMap<K, CacheNode>(); map = new HashMap<K, CacheNode>();
} }
/* returns value for key, if key exists in the cache /*
* otherwise null */ * returns value for key, if key exists in the cache otherwise null
public V get(K key) */
{ public V get(K key) {
CacheNode n = map.get(key); CacheNode n = map.get(key);
if(n == null) if (n == null)
return null; return null;
return n.value; return n.value;
} }
/* puts a new key-value pair in the cache /*
* if the key existed already, the value is updated, and the old value is returned * puts a new key-value pair in the cache if the key existed already, the
* if the key didn't exist, it is added; the oldest value (now pushed out of the * value is updated, and the old value is returned if the key didn't exist,
* cache) may be returned, or null if the cache isn't yet full */ * it is added; the oldest value (now pushed out of the cache) may be
public V put(K key, V value) * returned, or null if the cache isn't yet full
{ */
public V put(K key, V value) {
CacheNode n = map.get(key); CacheNode n = map.get(key);
if(n == null) { if (n == null) {
V ret = null; V ret = null;
if(len >= size) { if (len >= size) {
CacheNode first = head; CacheNode first = head;
first.unlink(); first.unlink();
map.remove(first.key); map.remove(first.key);

View File

@ -7,14 +7,12 @@ import org.bukkit.event.player.PlayerChatEvent;
public class ChatQueue { public class ChatQueue {
public class ChatMessage public class ChatMessage {
{
public long time; public long time;
public String playerName; public String playerName;
public String message; public String message;
public ChatMessage(PlayerChatEvent event) public ChatMessage(PlayerChatEvent event) {
{
time = System.currentTimeMillis(); time = System.currentTimeMillis();
playerName = event.getPlayer().getName(); playerName = event.getPlayer().getName();
message = event.getMessage(); message = event.getMessage();
@ -32,9 +30,8 @@ public class ChatQueue {
} }
/* put a chat message in the queue */ /* put a chat message in the queue */
public void pushChatMessage(PlayerChatEvent event) public void pushChatMessage(PlayerChatEvent event) {
{ synchronized (MapManager.lock) {
synchronized(MapManager.lock) {
messageQueue.add(new ChatMessage(event)); messageQueue.add(new ChatMessage(event));
} }
} }
@ -48,16 +45,12 @@ public class ChatQueue {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long deadline = now - maxChatAge; long deadline = now - maxChatAge;
synchronized(MapManager.lock) { synchronized (MapManager.lock) {
for (ChatMessage message : queue) for (ChatMessage message : queue) {
{ if (message.time < deadline) {
if (message.time < deadline)
{
messageQueue.remove(message); messageQueue.remove(message);
} } else if (message.time >= cutoff) {
else if (message.time >= cutoff)
{
updateList.add(message); updateList.add(message);
} }
} }

View File

@ -1,7 +1,8 @@
package org.dynmap; package org.dynmap;
public class DynmapChunk { public class DynmapChunk {
public int x,y; public int x, y;
public DynmapChunk(int x, int y) { public DynmapChunk(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;

View File

@ -25,14 +25,20 @@ public class DynmapPlayerListener extends PlayerListener {
} else if (split[1].equals("hide")) { } else if (split[1].equals("hide")) {
if (split.length == 2) { if (split.length == 2) {
playerList.hide(event.getPlayer().getName()); playerList.hide(event.getPlayer().getName());
} else for (int i=2;i<split.length;i++) } else {
for (int i = 2; i < split.length; i++) {
playerList.hide(split[i]); playerList.hide(split[i]);
}
}
event.setCancelled(true); event.setCancelled(true);
} else if (split[1].equals("show")) { } else if (split[1].equals("show")) {
if (split.length == 2) { if (split.length == 2) {
playerList.show(event.getPlayer().getName()); playerList.show(event.getPlayer().getName());
} else for (int i=2;i<split.length;i++) } else {
for (int i = 2; i < split.length; i++) {
playerList.show(split[i]); playerList.show(split[i]);
}
}
event.setCancelled(true); event.setCancelled(true);
} else if (split[1].equals("fullrender")) { } else if (split[1].equals("fullrender")) {
Player player = event.getPlayer(); Player player = event.getPlayer();
@ -48,10 +54,10 @@ public class DynmapPlayerListener extends PlayerListener {
/** /**
* Called when a player sends a chat message * Called when a player sends a chat message
* *
* @param event Relevant event details * @param event
* Relevant event details
*/ */
public void onPlayerChat(PlayerChatEvent event) public void onPlayerChat(PlayerChatEvent event) {
{
mgr.addChatEvent(event); mgr.addChatEvent(event);
} }
} }

View File

@ -56,7 +56,7 @@ public class DynmapPlugin extends JavaPlugin {
try { try {
webServer = new WebServer(mapManager, getWorld(), playerList, debugger, configuration); webServer = new WebServer(mapManager, getWorld(), playerList, debugger, configuration);
} catch(IOException e) { } catch (IOException e) {
log.info("position failed to start WebServer (IOException)"); log.info("position failed to start WebServer (IOException)");
} }
@ -66,7 +66,7 @@ public class DynmapPlugin extends JavaPlugin {
public void onDisable() { public void onDisable() {
mapManager.stopManager(); mapManager.stopManager();
if(webServer != null) { if (webServer != null) {
webServer.shutdown(); webServer.shutdown();
webServer = null; webServer = null;
} }

View File

@ -56,7 +56,7 @@ public class MapManager extends Thread {
} }
private static File combinePaths(File parent, File path) { private static File combinePaths(File parent, File path) {
if(path.isAbsolute()) if (path.isAbsolute())
return path; return path;
return new File(parent, path.getPath()); return new File(parent, path.getPath());
} }
@ -72,7 +72,7 @@ public class MapManager extends Thread {
renderWait = (int) (configuration.getDouble("renderinterval", 0.5) * 1000); renderWait = (int) (configuration.getDouble("renderinterval", 0.5) * 1000);
loadChunks = configuration.getBoolean("loadchunks", true); loadChunks = configuration.getBoolean("loadchunks", true);
if(!tileDirectory.isDirectory()) if (!tileDirectory.isDirectory())
tileDirectory.mkdirs(); tileDirectory.mkdirs();
maps = loadMapTypes(configuration); maps = loadMapTypes(configuration);
@ -82,8 +82,8 @@ public class MapManager extends Thread {
fullmapTiles.clear(); fullmapTiles.clear();
fullmapTilesRendered.clear(); fullmapTilesRendered.clear();
debugger.debug("Full render starting..."); debugger.debug("Full render starting...");
for(MapType map : maps) { for (MapType map : maps) {
for(MapTile tile : map.getTiles(l)) { for (MapTile tile : map.getTiles(l)) {
fullmapTiles.add(tile); fullmapTiles.add(tile);
invalidateTile(tile); invalidateTile(tile);
} }
@ -93,26 +93,26 @@ public class MapManager extends Thread {
void renderFullWorld(Location l) { void renderFullWorld(Location l) {
debugger.debug("Full render starting..."); debugger.debug("Full render starting...");
for(MapType map : maps) { for (MapType map : maps) {
HashSet<MapTile> found = new HashSet<MapTile>(); HashSet<MapTile> found = new HashSet<MapTile>();
LinkedList<MapTile> renderQueue = new LinkedList<MapTile>(); LinkedList<MapTile> renderQueue = new LinkedList<MapTile>();
for(MapTile tile : map.getTiles(l)) { for (MapTile tile : map.getTiles(l)) {
if(!(found.contains(tile) || map.isRendered(tile))) { if (!(found.contains(tile) || map.isRendered(tile))) {
found.add(tile); found.add(tile);
renderQueue.add(tile); renderQueue.add(tile);
} }
} }
while(!renderQueue.isEmpty()) { while (!renderQueue.isEmpty()) {
MapTile tile = renderQueue.pollFirst(); MapTile tile = renderQueue.pollFirst();
loadRequiredChunks(tile); loadRequiredChunks(tile);
debugger.debug("renderQueue: " + renderQueue.size() + "/" + found.size()); debugger.debug("renderQueue: " + renderQueue.size() + "/" + found.size());
if(map.render(tile)) { if (map.render(tile)) {
found.remove(tile); found.remove(tile);
staleQueue.onTileUpdated(tile); staleQueue.onTileUpdated(tile);
for(MapTile adjTile : map.getAdjecentTiles(tile)) { for (MapTile adjTile : map.getAdjecentTiles(tile)) {
if(!(found.contains(adjTile) || map.isRendered(adjTile))) { if (!(found.contains(adjTile) || map.isRendered(adjTile))) {
found.add(adjTile); found.add(adjTile);
renderQueue.add(adjTile); renderQueue.add(adjTile);
} }
@ -130,16 +130,16 @@ public class MapManager extends Thread {
public HashSet<MapTile> fullmapTilesRendered = new HashSet<MapTile>(); public HashSet<MapTile> fullmapTilesRendered = new HashSet<MapTile>();
void handleFullMapRender(MapTile tile) { void handleFullMapRender(MapTile tile) {
if(!fullmapTiles.contains(tile)) { if (!fullmapTiles.contains(tile)) {
debugger.debug("Non fullmap-render tile: " + tile); debugger.debug("Non fullmap-render tile: " + tile);
return; return;
} }
fullmapTilesRendered.add(tile); fullmapTilesRendered.add(tile);
MapType map = tile.getMap(); MapType map = tile.getMap();
MapTile[] adjecenttiles = map.getAdjecentTiles(tile); MapTile[] adjecenttiles = map.getAdjecentTiles(tile);
for(int i = 0; i < adjecenttiles.length; i++) { for (int i = 0; i < adjecenttiles.length; i++) {
MapTile adjecentTile = adjecenttiles[i]; MapTile adjecentTile = adjecenttiles[i];
if(!fullmapTiles.contains(adjecentTile)) { if (!fullmapTiles.contains(adjecentTile)) {
fullmapTiles.add(adjecentTile); fullmapTiles.add(adjecentTile);
staleQueue.pushStaleTile(adjecentTile); staleQueue.pushStaleTile(adjecentTile);
} }
@ -152,29 +152,29 @@ public class MapManager extends Thread {
} }
private void waitForMemory() { private void waitForMemory() {
if(!hasEnoughMemory()) { if (!hasEnoughMemory()) {
debugger.debug("Waiting for memory..."); debugger.debug("Waiting for memory...");
// Wait until there is at least 50mb of free memory. // Wait until there is at least 50mb of free memory.
do { do {
System.gc(); System.gc();
try { try {
Thread.sleep(500); Thread.sleep(500);
} catch(InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} while(!hasEnoughMemory()); } while (!hasEnoughMemory());
debugger.debug(Runtime.getRuntime().freeMemory() / (1024 * 1024) + "MB of memory free, will continue..."); debugger.debug(Runtime.getRuntime().freeMemory() / (1024 * 1024) + "MB of memory free, will continue...");
} }
} }
private void loadRequiredChunks(MapTile tile) { private void loadRequiredChunks(MapTile tile) {
if(!loadChunks) if (!loadChunks)
return; return;
waitForMemory(); waitForMemory();
// Actually load the chunks. // Actually load the chunks.
for(DynmapChunk chunk : tile.getMap().getRequiredChunks(tile)) { for (DynmapChunk chunk : tile.getMap().getRequiredChunks(tile)) {
if(!world.isChunkLoaded(chunk.x, chunk.y)) if (!world.isChunkLoaded(chunk.x, chunk.y))
world.loadChunk(chunk.x, chunk.y); world.loadChunk(chunk.x, chunk.y);
} }
} }
@ -182,7 +182,7 @@ public class MapManager extends Thread {
private MapType[] loadMapTypes(ConfigurationNode configuration) { private MapType[] loadMapTypes(ConfigurationNode configuration) {
List<?> configuredMaps = (List<?>) configuration.getProperty("maps"); List<?> configuredMaps = (List<?>) configuration.getProperty("maps");
ArrayList<MapType> mapTypes = new ArrayList<MapType>(); ArrayList<MapType> mapTypes = new ArrayList<MapType>();
for(Object configuredMapObj : configuredMaps) { for (Object configuredMapObj : configuredMaps) {
try { try {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> configuredMap = (Map<String, Object>) configuredMapObj; Map<String, Object> configuredMap = (Map<String, Object>) configuredMapObj;
@ -192,7 +192,7 @@ public class MapManager extends Thread {
Constructor<?> constructor = mapTypeClass.getConstructor(MapManager.class, World.class, Debugger.class, Map.class); Constructor<?> constructor = mapTypeClass.getConstructor(MapManager.class, World.class, Debugger.class, Map.class);
MapType mapType = (MapType) constructor.newInstance(this, world, debugger, configuredMap); MapType mapType = (MapType) constructor.newInstance(this, world, debugger, configuredMap);
mapTypes.add(mapType); mapTypes.add(mapType);
} catch(Exception e) { } catch (Exception e) {
debugger.error("Error loading map", e); debugger.error("Error loading map", e);
} }
} }
@ -203,13 +203,13 @@ public class MapManager extends Thread {
/* initialize and start map manager */ /* initialize and start map manager */
public void startManager() { public void startManager() {
synchronized(lock) { synchronized (lock) {
running = true; running = true;
this.start(); this.start();
try { try {
this.setPriority(MIN_PRIORITY); this.setPriority(MIN_PRIORITY);
log.info("Set minimum priority for worker thread"); log.info("Set minimum priority for worker thread");
} catch(SecurityException e) { } catch (SecurityException e) {
log.info("Failed to set minimum priority for worker thread!"); log.info("Failed to set minimum priority for worker thread!");
} }
} }
@ -217,8 +217,8 @@ public class MapManager extends Thread {
/* stop map manager */ /* stop map manager */
public void stopManager() { public void stopManager() {
synchronized(lock) { synchronized (lock) {
if(!running) if (!running)
return; return;
log.info("Stopping map renderer..."); log.info("Stopping map renderer...");
@ -226,7 +226,7 @@ public class MapManager extends Thread {
try { try {
this.join(); this.join();
} catch(InterruptedException e) { } catch (InterruptedException e) {
log.info("Waiting for map renderer to stop is interrupted"); log.info("Waiting for map renderer to stop is interrupted");
} }
} }
@ -237,41 +237,41 @@ public class MapManager extends Thread {
try { try {
log.info("Map renderer has started."); log.info("Map renderer has started.");
while(running) { while (running) {
MapTile t = staleQueue.popStaleTile(); MapTile t = staleQueue.popStaleTile();
if(t != null) { if (t != null) {
loadRequiredChunks(t); loadRequiredChunks(t);
debugger.debug("Rendering tile " + t + "..."); debugger.debug("Rendering tile " + t + "...");
boolean isNonEmptyTile = t.getMap().render(t); boolean isNonEmptyTile = t.getMap().render(t);
staleQueue.onTileUpdated(t); staleQueue.onTileUpdated(t);
if(isNonEmptyTile) if (isNonEmptyTile)
handleFullMapRender(t); handleFullMapRender(t);
try { try {
Thread.sleep(renderWait); Thread.sleep(renderWait);
} catch(InterruptedException e) { } catch (InterruptedException e) {
} }
} else { } else {
try { try {
Thread.sleep(500); Thread.sleep(500);
} catch(InterruptedException e) { } catch (InterruptedException e) {
} }
} }
} }
log.info("Map renderer has stopped."); log.info("Map renderer has stopped.");
} catch(Exception ex) { } catch (Exception ex) {
debugger.error("Exception on rendering-thread: " + ex.toString()); debugger.error("Exception on rendering-thread: " + ex.toString());
ex.printStackTrace(); ex.printStackTrace();
} }
} }
public void touch(int x, int y, int z) { public void touch(int x, int y, int z) {
for(int i = 0; i < maps.length; i++) { for (int i = 0; i < maps.length; i++) {
MapTile[] tiles = maps[i].getTiles(new Location(world, x, y, z)); MapTile[] tiles = maps[i].getTiles(new Location(world, x, y, z));
for(int j = 0; j < tiles.length; j++) { for (int j = 0; j < tiles.length; j++) {
invalidateTile(tiles[j]); invalidateTile(tiles[j]);
} }
} }

View File

@ -2,6 +2,7 @@ package org.dynmap;
public abstract class MapTile { public abstract class MapTile {
private MapType map; private MapType map;
public MapType getMap() { public MapType getMap() {
return map; return map;
} }

View File

@ -6,16 +6,19 @@ import org.dynmap.debug.Debugger;
public abstract class MapType { public abstract class MapType {
private MapManager manager; private MapManager manager;
public MapManager getMapManager() { public MapManager getMapManager() {
return manager; return manager;
} }
private World world; private World world;
public World getWorld() { public World getWorld() {
return world; return world;
} }
private Debugger debugger; private Debugger debugger;
public Debugger getDebugger() { public Debugger getDebugger() {
return debugger; return debugger;
} }
@ -27,8 +30,12 @@ public abstract class MapType {
} }
public abstract MapTile[] getTiles(Location l); public abstract MapTile[] getTiles(Location l);
public abstract MapTile[] getAdjecentTiles(MapTile tile); public abstract MapTile[] getAdjecentTiles(MapTile tile);
public abstract DynmapChunk[] getRequiredChunks(MapTile tile); public abstract DynmapChunk[] getRequiredChunks(MapTile tile);
public abstract boolean render(MapTile tile); public abstract boolean render(MapTile tile);
public abstract boolean isRendered(MapTile tile); public abstract boolean isRendered(MapTile tile);
} }

View File

@ -27,13 +27,13 @@ public class PlayerList {
try { try {
stream = new FileOutputStream(hiddenPlayersFile); stream = new FileOutputStream(hiddenPlayersFile);
OutputStreamWriter writer = new OutputStreamWriter(stream); OutputStreamWriter writer = new OutputStreamWriter(stream);
for(String player : hiddenPlayerNames) { for (String player : hiddenPlayerNames) {
writer.write(player); writer.write(player);
writer.write("\n"); writer.write("\n");
} }
writer.close(); writer.close();
stream.close(); stream.close();
} catch(IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -41,7 +41,7 @@ public class PlayerList {
public void load() { public void load() {
try { try {
Scanner scanner = new Scanner(hiddenPlayersFile); Scanner scanner = new Scanner(hiddenPlayersFile);
while(scanner.hasNextLine()) { while (scanner.hasNextLine()) {
String line = scanner.nextLine(); String line = scanner.nextLine();
hiddenPlayerNames.add(line); hiddenPlayerNames.add(line);
} }
@ -62,13 +62,16 @@ public class PlayerList {
} }
public void setVisible(String playerName, boolean visible) { public void setVisible(String playerName, boolean visible) {
if (visible) show(playerName); else hide(playerName); if (visible)
show(playerName);
else
hide(playerName);
} }
public Player[] getVisiblePlayers() { public Player[] getVisiblePlayers() {
ArrayList<Player> visiblePlayers = new ArrayList<Player>(); ArrayList<Player> visiblePlayers = new ArrayList<Player>();
Player[] onlinePlayers = server.getOnlinePlayers(); Player[] onlinePlayers = server.getOnlinePlayers();
for(int i=0;i<onlinePlayers.length;i++){ for (int i = 0; i < onlinePlayers.length; i++) {
Player p = onlinePlayers[i]; Player p = onlinePlayers[i];
if (!hiddenPlayerNames.contains(p.getName())) { if (!hiddenPlayerNames.contains(p.getName())) {
visiblePlayers.add(p); visiblePlayers.add(p);

View File

@ -30,10 +30,9 @@ public class StaleQueue {
} }
/* put a MapTile that needs to be regenerated on the list of stale tiles */ /* put a MapTile that needs to be regenerated on the list of stale tiles */
public boolean pushStaleTile(MapTile m) public boolean pushStaleTile(MapTile m) {
{ synchronized (MapManager.lock) {
synchronized(MapManager.lock) { if (staleTiles.add(m)) {
if(staleTiles.add(m)) {
staleTilesQueue.addLast(m); staleTilesQueue.addLast(m);
return true; return true;
} }
@ -41,18 +40,19 @@ public class StaleQueue {
} }
} }
/* get next MapTile that needs to be regenerated, or null /*
* the mapTile is removed from the list of stale tiles! */ * get next MapTile that needs to be regenerated, or null the mapTile is
public MapTile popStaleTile() * removed from the list of stale tiles!
{ */
synchronized(MapManager.lock) { public MapTile popStaleTile() {
synchronized (MapManager.lock) {
try { try {
MapTile t = staleTilesQueue.removeFirst(); MapTile t = staleTilesQueue.removeFirst();
if(!staleTiles.remove(t)) { if (!staleTiles.remove(t)) {
// This should never happen. // This should never happen.
} }
return t; return t;
} catch(NoSuchElementException e) { } catch (NoSuchElementException e) {
return null; return null;
} }
} }
@ -61,11 +61,11 @@ public class StaleQueue {
public void onTileUpdated(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) {
ListIterator<TileUpdate> it = tileUpdates.listIterator(0); ListIterator<TileUpdate> it = tileUpdates.listIterator(0);
while(it.hasNext()) { while (it.hasNext()) {
TileUpdate tu = it.next(); TileUpdate tu = it.next();
if(tu.at < deadline || tu.tile == t) if (tu.at < deadline || tu.tile == t)
it.remove(); it.remove();
} }
tileUpdates.addLast(new TileUpdate(now, t)); tileUpdates.addLast(new TileUpdate(now, t));
@ -73,18 +73,21 @@ public class StaleQueue {
} }
private ArrayList<TileUpdate> tmpupdates = new ArrayList<TileUpdate>(); private ArrayList<TileUpdate> tmpupdates = new ArrayList<TileUpdate>();
public TileUpdate[] getTileUpdates(long cutoff) { public TileUpdate[] getTileUpdates(long cutoff) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long deadline = now - maxTileAge; long deadline = now - maxTileAge;
TileUpdate[] updates; TileUpdate[] updates;
synchronized(MapManager.lock) { synchronized (MapManager.lock) {
tmpupdates.clear(); tmpupdates.clear();
Iterator<TileUpdate> it = tileUpdates.descendingIterator(); Iterator<TileUpdate> it = tileUpdates.descendingIterator();
while(it.hasNext()) { while (it.hasNext()) {
TileUpdate tu = it.next(); TileUpdate tu = it.next();
if(tu.at >= cutoff) { // Tile is new. if (tu.at >= cutoff) { // Tile is new.
tmpupdates.add(tu); tmpupdates.add(tu);
} else if(tu.at < deadline) { // Tile is too old, removing this one (will eventually decrease). } else if (tu.at < deadline) { // Tile is too old, removing this
// one (will eventually
// decrease).
it.remove(); it.remove();
break; break;
} else { // Tile is old, but not old enough for removal. } else { // Tile is old, but not old enough for removal.

View File

@ -6,8 +6,7 @@ public class TileUpdate {
public long at; public long at;
public MapTile tile; public MapTile tile;
public TileUpdate(long at, MapTile tile) public TileUpdate(long at, MapTile tile) {
{
this.at = at; this.at = at;
this.tile = tile; this.tile = tile;
} }

View File

@ -63,7 +63,8 @@ public class BukkitPlayerDebugger implements Debugger {
public synchronized void debug(String message) { public synchronized void debug(String message) {
sendToDebuggees(message); sendToDebuggees(message);
if (isLogging) log.info(prepend + message); if (isLogging)
log.info(prepend + message);
} }
public synchronized void error(String message) { public synchronized void error(String message) {

View File

@ -2,6 +2,8 @@ package org.dynmap.debug;
public interface Debugger { public interface Debugger {
void debug(String message); void debug(String message);
void error(String message); void error(String message);
void error(String message, Throwable thrown); void error(String message, Throwable thrown);
} }

View File

@ -2,6 +2,7 @@ package org.dynmap.debug;
public class NullDebugger implements Debugger { public class NullDebugger implements Debugger {
public static final NullDebugger instance = new NullDebugger(); public static final NullDebugger instance = new NullDebugger();
public void debug(String message) { public void debug(String message) {
} }

View File

@ -12,17 +12,16 @@ public class CaveTileRenderer extends DefaultTileRenderer {
} }
@Override @Override
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) {
{
boolean air = true; boolean air = true;
for(;;) { for (;;) {
if(y < 0) if (y < 0)
return translucent; return translucent;
int id = world.getBlockTypeIdAt(x, y, z); int id = world.getBlockTypeIdAt(x, y, z);
switch(seq) { switch (seq) {
case 0: case 0:
x--; x--;
break; break;
@ -39,7 +38,7 @@ public class CaveTileRenderer extends DefaultTileRenderer {
seq = (seq + 1) & 3; seq = (seq + 1) & 3;
switch(id) { switch (id) {
case 20: case 20:
case 18: case 18:
case 17: case 17:
@ -50,26 +49,26 @@ public class CaveTileRenderer extends DefaultTileRenderer {
default: default:
} }
if(id != 0) { if (id != 0) {
air = false; air = false;
continue; continue;
} }
if(id == 0 && !air) { if (id == 0 && !air) {
int cr, cg, cb; int cr, cg, cb;
int mult = 256; int mult = 256;
if(y < 64) { if (y < 64) {
cr = 0; cr = 0;
cg = 64 + y * 3; cg = 64 + y * 3;
cb = 255 - y * 4; cb = 255 - y * 4;
} else { } else {
cr = (y-64) * 4; cr = (y - 64) * 4;
cg = 255; cg = 255;
cb = 0; cb = 0;
} }
switch(seq) { switch (seq) {
case 0: case 0:
mult = 224; mult = 224;
break; break;

View File

@ -50,8 +50,14 @@ public class DefaultTileRenderer implements MapTileRenderer {
Color c1 = scan(world, jx, iy, jz, 0); Color c1 = scan(world, jx, iy, jz, 0);
Color c2 = scan(world, jx, iy, jz, 2); Color c2 = scan(world, jx, iy, jz, 2);
isempty = isempty && c1 == translucent && c2 == translucent; isempty = isempty && c1 == translucent && c2 == translucent;
r.setPixel(x, y, new int[] { c1.getRed(), c1.getGreen(), c1.getBlue() }); r.setPixel(x, y, new int[] {
r.setPixel(x - 1, y, new int[] { c2.getRed(), c2.getGreen(), c2.getBlue() }); c1.getRed(),
c1.getGreen(),
c1.getBlue() });
r.setPixel(x - 1, y, new int[] {
c2.getRed(),
c2.getGreen(),
c2.getBlue() });
jx++; jx++;
jz++; jz++;
@ -69,8 +75,14 @@ public class DefaultTileRenderer implements MapTileRenderer {
jz++; jz++;
Color c2 = scan(world, jx, iy, jz, 0); Color c2 = scan(world, jx, iy, jz, 0);
isempty = isempty && c1 == translucent && c2 == translucent; isempty = isempty && c1 == translucent && c2 == translucent;
r.setPixel(x, y, new int[] { c1.getRed(), c1.getGreen(), c1.getBlue() }); r.setPixel(x, y, new int[] {
r.setPixel(x - 1, y, new int[] { c2.getRed(), c2.getGreen(), c2.getBlue() }); c1.getRed(),
c1.getGreen(),
c1.getBlue() });
r.setPixel(x - 1, y, new int[] {
c2.getRed(),
c2.getGreen(),
c2.getBlue() });
} }
y++; y++;
@ -82,7 +94,7 @@ public class DefaultTileRenderer implements MapTileRenderer {
/* save the generated tile */ /* save the generated tile */
saveTile(tile, im, path); saveTile(tile, im, path);
((KzedMap) tile.getMap()).invalidateTile(new KzedZoomedMapTile((KzedMap)tile.getMap(), im, tile)); ((KzedMap) tile.getMap()).invalidateTile(new KzedZoomedMapTile((KzedMap) tile.getMap(), im, tile));
return !isempty; return !isempty;
} }

View File

@ -26,8 +26,10 @@ public class KzedMap extends MapType {
public static final int tileWidth = 128; public static final int tileWidth = 128;
public static final int tileHeight = 128; public static final int tileHeight = 128;
/* (logical!) dimensions of a zoomed out map tile /*
* must be twice the size of the normal tile */ * (logical!) dimensions of a zoomed out map tile must be twice the size of
* the normal tile
*/
public static final int zTileWidth = 256; public static final int zTileWidth = 256;
public static final int zTileHeight = 256; public static final int zTileHeight = 256;
@ -96,15 +98,23 @@ public class KzedMap extends MapType {
boolean redge = tilex(px + 4) != tx; boolean redge = tilex(px + 4) != tx;
boolean bedge = tiley(py + 4) != ty; boolean bedge = tiley(py + 4) != ty;
if (ledge) addTile(tiles, tx - tileWidth, ty); if (ledge)
if (redge) addTile(tiles, tx + tileWidth, ty); addTile(tiles, tx - tileWidth, ty);
if (tedge) addTile(tiles, tx, ty - tileHeight); if (redge)
if (bedge) addTile(tiles, tx, ty + tileHeight); addTile(tiles, tx + tileWidth, ty);
if (tedge)
addTile(tiles, tx, ty - tileHeight);
if (bedge)
addTile(tiles, tx, ty + tileHeight);
if (ledge && tedge) addTile(tiles, tx - tileWidth, ty - tileHeight); if (ledge && tedge)
if (ledge && bedge) addTile(tiles, tx - tileWidth, ty + tileHeight); addTile(tiles, tx - tileWidth, ty - tileHeight);
if (redge && tedge) addTile(tiles, tx + tileWidth, ty - tileHeight); if (ledge && bedge)
if (redge && bedge) addTile(tiles, tx + tileWidth, ty + tileHeight); addTile(tiles, tx - tileWidth, ty + tileHeight);
if (redge && tedge)
addTile(tiles, tx + tileWidth, ty - tileHeight);
if (redge && bedge)
addTile(tiles, tx + tileWidth, ty + tileHeight);
MapTile[] result = new MapTile[tiles.size()]; MapTile[] result = new MapTile[tiles.size()];
tiles.toArray(result); tiles.toArray(result);
@ -120,8 +130,7 @@ public class KzedMap extends MapType {
new KzedMapTile(this, renderer, t.px - tileWidth, t.py), new KzedMapTile(this, renderer, t.px - tileWidth, t.py),
new KzedMapTile(this, renderer, t.px + tileWidth, t.py), new KzedMapTile(this, renderer, t.px + tileWidth, t.py),
new KzedMapTile(this, renderer, t.px, t.py - tileHeight), new KzedMapTile(this, renderer, t.px, t.py - tileHeight),
new KzedMapTile(this, renderer, t.px, t.py + tileHeight) new KzedMapTile(this, renderer, t.px, t.py + tileHeight) };
};
} }
return new MapTile[0]; return new MapTile[0];
} }
@ -211,7 +220,7 @@ public class KzedMap extends MapType {
static int ztiley(int y) { static int ztiley(int y) {
if (y < 0) if (y < 0)
return y + y % zTileHeight; return y + y % zTileHeight;
//return y - (zTileHeight + (y % zTileHeight)); // return y - (zTileHeight + (y % zTileHeight));
else else
return y - (y % zTileHeight); return y - (y % zTileHeight);
} }

View File

@ -1,7 +1,7 @@
package org.dynmap.kzedmap; package org.dynmap.kzedmap;
public interface MapTileRenderer { public interface MapTileRenderer {
String getName(); String getName();
boolean render(KzedMapTile tile, String path); boolean render(KzedMapTile tile, String path);
} }

View File

@ -28,10 +28,10 @@ public class ZoomedTileRenderer {
try { try {
File file = new File(zoomPath); File file = new File(zoomPath);
zIm = ImageIO.read(file); zIm = ImageIO.read(file);
} catch(IOException e) { } catch (IOException e) {
} }
if(zIm == null) { if (zIm == null) {
/* create new one */ /* create new one */
zIm = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB); zIm = new BufferedImage(KzedMap.tileWidth, KzedMap.tileHeight, BufferedImage.TYPE_INT_RGB);
debugger.debug("New zoom-out tile created " + zoomPath); debugger.debug("New zoom-out tile created " + zoomPath);
@ -49,11 +49,13 @@ public class ZoomedTileRenderer {
int ox = 0; int ox = 0;
int oy = 0; int oy = 0;
if(zpx != px) ox = scw; if (zpx != px)
if(zpy != py) oy = sch; ox = scw;
if (zpy != py)
oy = sch;
/* blit scaled rendered tile onto zoom-out tile */ /* blit scaled rendered tile onto zoom-out tile */
//WritableRaster zr = zIm.getRaster(); // WritableRaster zr = zIm.getRaster();
Graphics2D g2 = zIm.createGraphics(); Graphics2D g2 = zIm.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, ox, oy, scw, sch, null); g2.drawImage(image, ox, oy, scw, sch, null);
@ -65,9 +67,9 @@ public class ZoomedTileRenderer {
File file = new File(zoomPath); File file = new File(zoomPath);
ImageIO.write(zIm, "png", file); ImageIO.write(zIm, "png", file);
debugger.debug("Saved zoom-out tile at " + zoomPath); debugger.debug("Saved zoom-out tile at " + zoomPath);
} catch(IOException e) { } catch (IOException e) {
debugger.error("Failed to save zoom-out tile: " + zoomPath, e); debugger.error("Failed to save zoom-out tile: " + zoomPath, e);
} catch(java.lang.NullPointerException e) { } catch (java.lang.NullPointerException e) {
debugger.error("Failed to save zoom-out tile (NullPointerException): " + zoomPath, e); debugger.error("Failed to save zoom-out tile (NullPointerException): " + zoomPath, e);
} }
zIm.flush(); zIm.flush();

View File

@ -27,8 +27,7 @@ public class WebServer extends Thread {
private PlayerList playerList; private PlayerList playerList;
private ConfigurationNode configuration; private ConfigurationNode configuration;
public WebServer(MapManager mgr, World world, PlayerList playerList, Debugger debugger, ConfigurationNode configuration) throws IOException public WebServer(MapManager mgr, World world, PlayerList playerList, Debugger debugger, ConfigurationNode configuration) throws IOException {
{
this.mgr = mgr; this.mgr = mgr;
this.world = world; this.world = world;
this.playerList = playerList; this.playerList = playerList;
@ -38,22 +37,22 @@ public class WebServer extends Thread {
String bindAddress = configuration.getString("webserver-bindaddress", "0.0.0.0"); String bindAddress = configuration.getString("webserver-bindaddress", "0.0.0.0");
int port = configuration.getInt("webserver-port", 8123); int port = configuration.getInt("webserver-port", 8123);
sock = new ServerSocket(port, 5, bindAddress.equals("0.0.0.0") ? null : InetAddress.getByName(bindAddress)); sock = new ServerSocket(port, 5, bindAddress.equals("0.0.0.0")
? null
: InetAddress.getByName(bindAddress));
running = true; running = true;
start(); start();
log.info("Dynmap WebServer started on " + bindAddress + ":" + port); log.info("Dynmap WebServer started on " + bindAddress + ":" + port);
} }
public void run() public void run() {
{
try { try {
while (running) { while (running) {
try { try {
Socket socket = sock.accept(); Socket socket = sock.accept();
WebServerRequest requestThread = new WebServerRequest(socket, mgr, world, playerList, configuration, debugger); WebServerRequest requestThread = new WebServerRequest(socket, mgr, world, playerList, configuration, debugger);
requestThread.start(); requestThread.start();
} } catch (IOException e) {
catch (IOException e) {
log.info("map WebServer.run() stops with IOException"); log.info("map WebServer.run() stops with IOException");
break; break;
} }
@ -64,13 +63,12 @@ public class WebServer extends Thread {
} }
} }
public void shutdown() public void shutdown() {
{
try { try {
if(sock != null) { if (sock != null) {
sock.close(); sock.close();
} }
} catch(IOException e) { } catch (IOException e) {
log.info("map stop() got IOException while closing socket"); log.info("map stop() got IOException while closing socket");
} }
running = false; running = false;

View File

@ -34,8 +34,7 @@ public class WebServerRequest extends Thread {
private PlayerList playerList; private PlayerList playerList;
private ConfigurationNode configuration; private ConfigurationNode configuration;
public WebServerRequest(Socket socket, MapManager mgr, World world, PlayerList playerList, ConfigurationNode configuration, Debugger debugger) public WebServerRequest(Socket socket, MapManager mgr, World world, PlayerList playerList, ConfigurationNode configuration, Debugger debugger) {
{
this.debugger = debugger; this.debugger = debugger;
this.socket = socket; this.socket = socket;
this.mgr = mgr; this.mgr = mgr;
@ -52,8 +51,8 @@ public class WebServerRequest extends Thread {
private static void writeHeaderField(BufferedOutputStream out, String name, String value) throws IOException { private static void writeHeaderField(BufferedOutputStream out, String name, String value) throws IOException {
out.write(name.getBytes()); out.write(name.getBytes());
out.write((int)':'); out.write((int) ':');
out.write((int)' '); out.write((int) ' ');
out.write(value.getBytes()); out.write(value.getBytes());
out.write(13); out.write(13);
out.write(10); out.write(10);
@ -64,8 +63,7 @@ public class WebServerRequest extends Thread {
out.write(10); out.write(10);
} }
public void run() public void run() {
{
BufferedReader in = null; BufferedReader in = null;
BufferedOutputStream out = null; BufferedOutputStream out = null;
try { try {
@ -94,14 +92,32 @@ public class WebServerRequest extends Thread {
} }
out.flush(); out.flush();
out.close(); out.close();
} catch (IOException e) {
if (out != null) {
try {
out.close();
} catch (Exception anye) {
}
}
if (in != null) {
try {
in.close();
} catch (Exception anye) {
}
}
} catch (Exception ex) {
if (out != null) {
try {
out.close();
} catch (Exception anye) {
}
}
if (in != null) {
try {
in.close();
} catch (Exception anye) {
} }
catch (IOException e) {
if (out != null) { try { out.close(); } catch (Exception anye) { } }
if (in != null) { try { in.close(); } catch (Exception anye) { } }
} }
catch(Exception ex) {
if (out != null) { try { out.close(); } catch (Exception anye) { } }
if (in != null) { try { in.close(); } catch (Exception anye) { } }
debugger.error("Exception on WebRequest-thread: " + ex.toString()); debugger.error("Exception on WebRequest-thread: " + ex.toString());
} }
} }
@ -110,20 +126,22 @@ public class WebServerRequest extends Thread {
if (o == null) { if (o == null) {
return "null"; return "null";
} else if (o instanceof Boolean) { } else if (o instanceof Boolean) {
return ((Boolean)o) ? "true" : "false"; return ((Boolean) o) ? "true" : "false";
} else if (o instanceof String) { } else if (o instanceof String) {
return "\"" + o + "\""; return "\"" + o + "\"";
} else if (o instanceof Integer || o instanceof Long || o instanceof Float || o instanceof Double) { } else if (o instanceof Integer || o instanceof Long || o instanceof Float || o instanceof Double) {
return o.toString(); return o.toString();
} else if (o instanceof LinkedHashMap<?, ?>) { } else if (o instanceof LinkedHashMap<?, ?>) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
LinkedHashMap<String, Object> m = (LinkedHashMap<String, Object>)o; LinkedHashMap<String, Object> m = (LinkedHashMap<String, Object>) o;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("{"); sb.append("{");
boolean first = true; boolean first = true;
for (String key : m.keySet()) { for (String key : m.keySet()) {
if (first) first = false; if (first)
else sb.append(","); first = false;
else
sb.append(",");
sb.append(stringifyJson(key)); sb.append(stringifyJson(key));
sb.append(": "); sb.append(": ");
@ -133,10 +151,10 @@ public class WebServerRequest extends Thread {
return sb.toString(); return sb.toString();
} else if (o instanceof ArrayList<?>) { } else if (o instanceof ArrayList<?>) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
ArrayList<Object> l = (ArrayList<Object>)o; ArrayList<Object> l = (ArrayList<Object>) o;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int count = 0; int count = 0;
for(int i=0;i<l.size();i++) { for (int i = 0; i < l.size(); i++) {
sb.append(count++ == 0 ? "[" : ","); sb.append(count++ == 0 ? "[" : ",");
sb.append(stringifyJson(l.get(i))); sb.append(stringifyJson(l.get(i)));
} }
@ -167,10 +185,10 @@ public class WebServerRequest extends Thread {
int current = (int) (System.currentTimeMillis() / 1000); int current = (int) (System.currentTimeMillis() / 1000);
long cutoff = 0; long cutoff = 0;
if(path.charAt(0) == '/') { if (path.charAt(0) == '/') {
try { try {
cutoff = ((long) Integer.parseInt(path.substring(1))) * 1000; cutoff = ((long) Integer.parseInt(path.substring(1))) * 1000;
} catch(NumberFormatException e) { } catch (NumberFormatException e) {
} }
} }
@ -179,21 +197,21 @@ public class WebServerRequest extends Thread {
sb.append(current + " " + relativeTime + "\n"); sb.append(current + " " + relativeTime + "\n");
Player[] players = playerList.getVisiblePlayers(); Player[] players = playerList.getVisiblePlayers();
for(Player player : players) { for (Player player : players) {
sb.append("player " + player.getName() + " " + player.getLocation().getX() + " " + player.getLocation().getY() + " " + player.getLocation().getZ() + "\n"); sb.append("player " + player.getName() + " " + player.getLocation().getX() + " " + player.getLocation().getY() + " " + player.getLocation().getZ() + "\n");
} }
TileUpdate[] tileUpdates = mgr.staleQueue.getTileUpdates(cutoff); TileUpdate[] tileUpdates = mgr.staleQueue.getTileUpdates(cutoff);
for(TileUpdate tu : tileUpdates) { for (TileUpdate tu : tileUpdates) {
sb.append("tile " + tu.tile.getName() + "\n"); sb.append("tile " + tu.tile.getName() + "\n");
} }
ChatQueue.ChatMessage[] messages = mgr.chatQueue.getChatMessages(cutoff); ChatQueue.ChatMessage[] messages = mgr.chatQueue.getChatMessages(cutoff);
for(ChatQueue.ChatMessage cu : messages) { for (ChatQueue.ChatMessage cu : messages) {
sb.append("chat " + cu.playerName + " " + cu.message + "\n"); sb.append("chat " + cu.playerName + " " + cu.message + "\n");
} }
debugger.debug("Sending " + players.length + " players, " + tileUpdates.length + " tile-updates, and " + messages.length + " chats. "+ path + ";" + cutoff); debugger.debug("Sending " + players.length + " players, " + tileUpdates.length + " tile-updates, and " + messages.length + " chats. " + path + ";" + cutoff);
byte[] bytes = sb.toString().getBytes(); byte[] bytes = sb.toString().getBytes();
@ -213,7 +231,8 @@ public class WebServerRequest extends Thread {
public void writeFile(BufferedOutputStream out, String path, InputStream fileInput) throws IOException { public void writeFile(BufferedOutputStream out, String path, InputStream fileInput) throws IOException {
int dotindex = path.lastIndexOf('.'); int dotindex = path.lastIndexOf('.');
String extension = null; String extension = null;
if (dotindex > 0) extension = path.substring(dotindex); if (dotindex > 0)
extension = path.substring(dotindex);
writeHttpHeader(out, 200, "OK"); writeHttpHeader(out, 200, "OK");
writeHeaderField(out, "Content-Type", getMimeTypeFromExtension(extension)); writeHeaderField(out, "Content-Type", getMimeTypeFromExtension(extension));
@ -221,10 +240,10 @@ public class WebServerRequest extends Thread {
writeEndOfHeaders(out); writeEndOfHeaders(out);
try { try {
int readBytes; int readBytes;
while((readBytes = fileInput.read(readBuffer)) > 0) { while ((readBytes = fileInput.read(readBuffer)) > 0) {
out.write(readBuffer, 0, readBytes); out.write(readBuffer, 0, readBytes);
} }
} catch(IOException e) { } catch (IOException e) {
fileInput.close(); fileInput.close();
throw e; throw e;
} }
@ -233,12 +252,14 @@ public class WebServerRequest extends Thread {
public String getFilePath(String path) { public String getFilePath(String path) {
int qmark = path.indexOf('?'); int qmark = path.indexOf('?');
if (qmark >= 0) path = path.substring(0, qmark); if (qmark >= 0)
path = path.substring(0, qmark);
path = path.substring(1); path = path.substring(1);
if (path.startsWith("/") || path.startsWith(".")) if (path.startsWith("/") || path.startsWith("."))
return null; return null;
if (path.length() == 0) path = "index.html"; if (path.length() == 0)
path = "index.html";
return path; return path;
} }
@ -279,9 +300,11 @@ public class WebServerRequest extends Thread {
mimes.put(".css", "text/css"); mimes.put(".css", "text/css");
mimes.put(".txt", "text/plain"); mimes.put(".txt", "text/plain");
} }
public static String getMimeTypeFromExtension(String extension) { public static String getMimeTypeFromExtension(String extension) {
String m = mimes.get(extension); String m = mimes.get(extension);
if (m != null) return m; if (m != null)
return m;
return "application/octet-steam"; return "application/octet-steam";
} }
} }