Second part of NPE and arctan update :3

By: VictorD <victor.danell@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2011-02-20 23:22:28 +01:00
parent 0baea2a20c
commit 094c4c043f
3 changed files with 24 additions and 15 deletions

View file

@ -1,6 +1,7 @@
package org.bukkit.craftbukkit; package org.bukkit.craftbukkit;
import java.lang.ref.WeakReference;
import java.util.HashMap; import java.util.HashMap;
import net.minecraft.server.WorldServer; import net.minecraft.server.WorldServer;
@ -11,27 +12,38 @@ import org.bukkit.block.Block;
import org.bukkit.craftbukkit.block.CraftBlock; import org.bukkit.craftbukkit.block.CraftBlock;
public class CraftChunk implements Chunk { public class CraftChunk implements Chunk {
private net.minecraft.server.Chunk chunk; private WeakReference<net.minecraft.server.Chunk> weakChunk;
private final HashMap<Integer, Block> cache = new HashMap<Integer, Block>(); private final HashMap<Integer, Block> cache = new HashMap<Integer, Block>();
private WorldServer worldServer;
private int x;
private int z;
public CraftChunk(net.minecraft.server.Chunk chunk) { public CraftChunk(net.minecraft.server.Chunk chunk) {
this.chunk = chunk; this.weakChunk = new WeakReference<net.minecraft.server.Chunk>(chunk);
worldServer = (WorldServer) getHandle().d;
x = getHandle().j;
z = getHandle().k;
} }
public World getWorld() { public World getWorld() {
return ((WorldServer) chunk.d).getWorld(); return worldServer.getWorld();
} }
public net.minecraft.server.Chunk getHandle() { public net.minecraft.server.Chunk getHandle() {
return chunk; net.minecraft.server.Chunk c = weakChunk.get();
if (c == null) {
weakChunk = new WeakReference<net.minecraft.server.Chunk>(worldServer.c(x,z));
c = weakChunk.get();
}
return c;
} }
public int getX() { public int getX() {
return chunk.j; return x;
} }
public int getZ() { public int getZ() {
return chunk.k; return z;
} }
@Override @Override
@ -48,8 +60,5 @@ public class CraftChunk implements Chunk {
} }
return block; return block;
} }
}
public void breakLink() {
this.chunk = null;
}
}

View file

@ -47,12 +47,11 @@ public class CraftWorld implements World {
} }
public void preserveChunk( CraftChunk chunk ) { public void preserveChunk( CraftChunk chunk ) {
chunk.breakLink(); unloadedChunks.put( (chunk.getX() << 16) + chunk.getZ(), chunk );
unloadedChunks.put( chunk.getX() << 16 + chunk.getZ(), chunk );
} }
public CraftChunk popPreservedChunk( int x, int z ) { public CraftChunk popPreservedChunk( int x, int z ) {
return unloadedChunks.remove( x << 16 + z ); return unloadedChunks.remove( (x << 16) + z );
} }
public Block getBlockAt(int x, int y, int z) { public Block getBlockAt(int x, int y, int z) {

View file

@ -1,6 +1,7 @@
package org.bukkit.craftbukkit; package org.bukkit.craftbukkit;
/** /**
* Got this code from a post by user aioobe on stackoverflow.com * Credits for this class goes to user aioobe on stackoverflow.com
* Source: http://stackoverflow.com/questions/4454630/j2me-calculate-the-the-distance-between-2-latitude-and-longitude
* *
*/ */
public class TrigMath { public class TrigMath {