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

View File

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

View File

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

View File

@ -25,14 +25,20 @@ public class DynmapPlayerListener extends PlayerListener {
} else if (split[1].equals("hide")) {
if (split.length == 2) {
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]);
}
}
event.setCancelled(true);
} else if (split[1].equals("show")) {
if (split.length == 2) {
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]);
}
}
event.setCancelled(true);
} else if (split[1].equals("fullrender")) {
Player player = event.getPlayer();
@ -48,10 +54,10 @@ public class DynmapPlayerListener extends PlayerListener {
/**
* 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);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -27,13 +27,13 @@ public class PlayerList {
try {
stream = new FileOutputStream(hiddenPlayersFile);
OutputStreamWriter writer = new OutputStreamWriter(stream);
for(String player : hiddenPlayerNames) {
for (String player : hiddenPlayerNames) {
writer.write(player);
writer.write("\n");
}
writer.close();
stream.close();
} catch(IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
@ -41,7 +41,7 @@ public class PlayerList {
public void load() {
try {
Scanner scanner = new Scanner(hiddenPlayersFile);
while(scanner.hasNextLine()) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
hiddenPlayerNames.add(line);
}
@ -62,13 +62,16 @@ public class PlayerList {
}
public void setVisible(String playerName, boolean visible) {
if (visible) show(playerName); else hide(playerName);
if (visible)
show(playerName);
else
hide(playerName);
}
public Player[] getVisiblePlayers() {
ArrayList<Player> visiblePlayers = new ArrayList<Player>();
Player[] onlinePlayers = server.getOnlinePlayers();
for(int i=0;i<onlinePlayers.length;i++){
for (int i = 0; i < onlinePlayers.length; i++) {
Player p = onlinePlayers[i];
if (!hiddenPlayerNames.contains(p.getName())) {
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 */
public boolean pushStaleTile(MapTile m)
{
synchronized(MapManager.lock) {
if(staleTiles.add(m)) {
public boolean pushStaleTile(MapTile m) {
synchronized (MapManager.lock) {
if (staleTiles.add(m)) {
staleTilesQueue.addLast(m);
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! */
public MapTile popStaleTile()
{
synchronized(MapManager.lock) {
/*
* get next MapTile that needs to be regenerated, or null the mapTile is
* removed from the list of stale tiles!
*/
public MapTile popStaleTile() {
synchronized (MapManager.lock) {
try {
MapTile t = staleTilesQueue.removeFirst();
if(!staleTiles.remove(t)) {
if (!staleTiles.remove(t)) {
// This should never happen.
}
return t;
} catch(NoSuchElementException e) {
} catch (NoSuchElementException e) {
return null;
}
}
@ -61,11 +61,11 @@ public class StaleQueue {
public void onTileUpdated(MapTile t) {
long now = System.currentTimeMillis();
long deadline = now - maxTileAge;
synchronized(MapManager.lock) {
synchronized (MapManager.lock) {
ListIterator<TileUpdate> it = tileUpdates.listIterator(0);
while(it.hasNext()) {
while (it.hasNext()) {
TileUpdate tu = it.next();
if(tu.at < deadline || tu.tile == t)
if (tu.at < deadline || tu.tile == t)
it.remove();
}
tileUpdates.addLast(new TileUpdate(now, t));
@ -73,18 +73,21 @@ public class StaleQueue {
}
private ArrayList<TileUpdate> tmpupdates = new ArrayList<TileUpdate>();
public TileUpdate[] getTileUpdates(long cutoff) {
long now = System.currentTimeMillis();
long deadline = now - maxTileAge;
TileUpdate[] updates;
synchronized(MapManager.lock) {
synchronized (MapManager.lock) {
tmpupdates.clear();
Iterator<TileUpdate> it = tileUpdates.descendingIterator();
while(it.hasNext()) {
while (it.hasNext()) {
TileUpdate tu = it.next();
if(tu.at >= cutoff) { // Tile is new.
if (tu.at >= cutoff) { // Tile is new.
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();
break;
} else { // Tile is old, but not old enough for removal.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -50,8 +50,14 @@ public class DefaultTileRenderer implements MapTileRenderer {
Color c1 = scan(world, jx, iy, jz, 0);
Color c2 = scan(world, jx, iy, jz, 2);
isempty = isempty && c1 == translucent && c2 == translucent;
r.setPixel(x, y, new int[] { c1.getRed(), c1.getGreen(), c1.getBlue() });
r.setPixel(x - 1, y, new int[] { c2.getRed(), c2.getGreen(), c2.getBlue() });
r.setPixel(x, y, new int[] {
c1.getRed(),
c1.getGreen(),
c1.getBlue() });
r.setPixel(x - 1, y, new int[] {
c2.getRed(),
c2.getGreen(),
c2.getBlue() });
jx++;
jz++;
@ -69,8 +75,14 @@ public class DefaultTileRenderer implements MapTileRenderer {
jz++;
Color c2 = scan(world, jx, iy, jz, 0);
isempty = isempty && c1 == translucent && c2 == translucent;
r.setPixel(x, y, new int[] { c1.getRed(), c1.getGreen(), c1.getBlue() });
r.setPixel(x - 1, y, new int[] { c2.getRed(), c2.getGreen(), c2.getBlue() });
r.setPixel(x, y, new int[] {
c1.getRed(),
c1.getGreen(),
c1.getBlue() });
r.setPixel(x - 1, y, new int[] {
c2.getRed(),
c2.getGreen(),
c2.getBlue() });
}
y++;
@ -82,7 +94,7 @@ public class DefaultTileRenderer implements MapTileRenderer {
/* save the generated tile */
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;
}

View File

@ -26,8 +26,10 @@ public class KzedMap extends MapType {
public static final int tileWidth = 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 zTileHeight = 256;
@ -96,15 +98,23 @@ public class KzedMap extends MapType {
boolean redge = tilex(px + 4) != tx;
boolean bedge = tiley(py + 4) != ty;
if (ledge) addTile(tiles, tx - tileWidth, ty);
if (redge) addTile(tiles, tx + tileWidth, ty);
if (tedge) addTile(tiles, tx, ty - tileHeight);
if (bedge) addTile(tiles, tx, ty + tileHeight);
if (ledge)
addTile(tiles, tx - tileWidth, ty);
if (redge)
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 && bedge) addTile(tiles, tx - tileWidth, ty + tileHeight);
if (redge && tedge) addTile(tiles, tx + tileWidth, ty - tileHeight);
if (redge && bedge) addTile(tiles, tx + tileWidth, ty + tileHeight);
if (ledge && tedge)
addTile(tiles, tx - tileWidth, ty - tileHeight);
if (ledge && bedge)
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()];
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, 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];
}
@ -211,7 +220,7 @@ public class KzedMap extends MapType {
static int ztiley(int y) {
if (y < 0)
return y + y % zTileHeight;
//return y - (zTileHeight + (y % zTileHeight));
// return y - (zTileHeight + (y % zTileHeight));
else
return y - (y % zTileHeight);
}

View File

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

View File

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

View File

@ -27,8 +27,7 @@ public class WebServer extends Thread {
private PlayerList playerList;
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.world = world;
this.playerList = playerList;
@ -38,22 +37,22 @@ public class WebServer extends Thread {
String bindAddress = configuration.getString("webserver-bindaddress", "0.0.0.0");
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;
start();
log.info("Dynmap WebServer started on " + bindAddress + ":" + port);
}
public void run()
{
public void run() {
try {
while (running) {
try {
Socket socket = sock.accept();
WebServerRequest requestThread = new WebServerRequest(socket, mgr, world, playerList, configuration, debugger);
requestThread.start();
}
catch (IOException e) {
} catch (IOException e) {
log.info("map WebServer.run() stops with IOException");
break;
}
@ -64,13 +63,12 @@ public class WebServer extends Thread {
}
}
public void shutdown()
{
public void shutdown() {
try {
if(sock != null) {
if (sock != null) {
sock.close();
}
} catch(IOException e) {
} catch (IOException e) {
log.info("map stop() got IOException while closing socket");
}
running = false;

View File

@ -34,8 +34,7 @@ public class WebServerRequest extends Thread {
private PlayerList playerList;
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.socket = socket;
this.mgr = mgr;
@ -52,8 +51,8 @@ public class WebServerRequest extends Thread {
private static void writeHeaderField(BufferedOutputStream out, String name, String value) throws IOException {
out.write(name.getBytes());
out.write((int)':');
out.write((int)' ');
out.write((int) ':');
out.write((int) ' ');
out.write(value.getBytes());
out.write(13);
out.write(10);
@ -64,8 +63,7 @@ public class WebServerRequest extends Thread {
out.write(10);
}
public void run()
{
public void run() {
BufferedReader in = null;
BufferedOutputStream out = null;
try {
@ -94,14 +92,32 @@ public class WebServerRequest extends Thread {
}
out.flush();
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());
}
}
@ -110,20 +126,22 @@ public class WebServerRequest extends Thread {
if (o == null) {
return "null";
} else if (o instanceof Boolean) {
return ((Boolean)o) ? "true" : "false";
return ((Boolean) o) ? "true" : "false";
} else if (o instanceof String) {
return "\"" + o + "\"";
} else if (o instanceof Integer || o instanceof Long || o instanceof Float || o instanceof Double) {
return o.toString();
} else if (o instanceof LinkedHashMap<?, ?>) {
@SuppressWarnings("unchecked")
LinkedHashMap<String, Object> m = (LinkedHashMap<String, Object>)o;
LinkedHashMap<String, Object> m = (LinkedHashMap<String, Object>) o;
StringBuilder sb = new StringBuilder();
sb.append("{");
boolean first = true;
for (String key : m.keySet()) {
if (first) first = false;
else sb.append(",");
if (first)
first = false;
else
sb.append(",");
sb.append(stringifyJson(key));
sb.append(": ");
@ -133,10 +151,10 @@ public class WebServerRequest extends Thread {
return sb.toString();
} else if (o instanceof ArrayList<?>) {
@SuppressWarnings("unchecked")
ArrayList<Object> l = (ArrayList<Object>)o;
ArrayList<Object> l = (ArrayList<Object>) o;
StringBuilder sb = new StringBuilder();
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(stringifyJson(l.get(i)));
}
@ -167,10 +185,10 @@ public class WebServerRequest extends Thread {
int current = (int) (System.currentTimeMillis() / 1000);
long cutoff = 0;
if(path.charAt(0) == '/') {
if (path.charAt(0) == '/') {
try {
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");
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");
}
TileUpdate[] tileUpdates = mgr.staleQueue.getTileUpdates(cutoff);
for(TileUpdate tu : tileUpdates) {
for (TileUpdate tu : tileUpdates) {
sb.append("tile " + tu.tile.getName() + "\n");
}
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");
}
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();
@ -213,7 +231,8 @@ public class WebServerRequest extends Thread {
public void writeFile(BufferedOutputStream out, String path, InputStream fileInput) throws IOException {
int dotindex = path.lastIndexOf('.');
String extension = null;
if (dotindex > 0) extension = path.substring(dotindex);
if (dotindex > 0)
extension = path.substring(dotindex);
writeHttpHeader(out, 200, "OK");
writeHeaderField(out, "Content-Type", getMimeTypeFromExtension(extension));
@ -221,10 +240,10 @@ public class WebServerRequest extends Thread {
writeEndOfHeaders(out);
try {
int readBytes;
while((readBytes = fileInput.read(readBuffer)) > 0) {
while ((readBytes = fileInput.read(readBuffer)) > 0) {
out.write(readBuffer, 0, readBytes);
}
} catch(IOException e) {
} catch (IOException e) {
fileInput.close();
throw e;
}
@ -233,12 +252,14 @@ public class WebServerRequest extends Thread {
public String getFilePath(String path) {
int qmark = path.indexOf('?');
if (qmark >= 0) path = path.substring(0, qmark);
if (qmark >= 0)
path = path.substring(0, qmark);
path = path.substring(1);
if (path.startsWith("/") || path.startsWith("."))
return null;
if (path.length() == 0) path = "index.html";
if (path.length() == 0)
path = "index.html";
return path;
}
@ -279,9 +300,11 @@ public class WebServerRequest extends Thread {
mimes.put(".css", "text/css");
mimes.put(".txt", "text/plain");
}
public static String getMimeTypeFromExtension(String extension) {
String m = mimes.get(extension);
if (m != null) return m;
if (m != null)
return m;
return "application/octet-steam";
}
}