2010-12-27 03:13:03 +01:00
|
|
|
|
|
|
|
package org.bukkit.craftbukkit;
|
|
|
|
|
2011-02-20 23:22:28 +01:00
|
|
|
import java.lang.ref.WeakReference;
|
2011-02-01 23:49:28 +01:00
|
|
|
import java.util.HashMap;
|
2011-03-07 19:48:35 +01:00
|
|
|
import net.minecraft.server.ChunkPosition;
|
2011-02-01 23:49:28 +01:00
|
|
|
|
|
|
|
import net.minecraft.server.WorldServer;
|
|
|
|
|
2010-12-27 03:13:03 +01:00
|
|
|
import org.bukkit.Chunk;
|
2011-01-04 15:17:05 +01:00
|
|
|
import org.bukkit.World;
|
2011-02-01 23:49:28 +01:00
|
|
|
import org.bukkit.block.Block;
|
2011-03-07 19:48:35 +01:00
|
|
|
import org.bukkit.block.BlockState;
|
2011-02-01 23:49:28 +01:00
|
|
|
import org.bukkit.craftbukkit.block.CraftBlock;
|
2011-03-07 19:48:35 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
2011-03-09 23:12:28 +01:00
|
|
|
import org.bukkit.craftbukkit.util.SoftMap;
|
2010-12-27 03:13:03 +01:00
|
|
|
|
|
|
|
public class CraftChunk implements Chunk {
|
2011-02-20 23:22:28 +01:00
|
|
|
private WeakReference<net.minecraft.server.Chunk> weakChunk;
|
2011-03-09 23:12:28 +01:00
|
|
|
private final SoftMap<Integer, Block> cache = new SoftMap<Integer, Block>();
|
2011-02-20 23:22:28 +01:00
|
|
|
private WorldServer worldServer;
|
|
|
|
private int x;
|
|
|
|
private int z;
|
|
|
|
|
2011-02-01 23:49:28 +01:00
|
|
|
public CraftChunk(net.minecraft.server.Chunk chunk) {
|
2011-02-20 23:22:28 +01:00
|
|
|
this.weakChunk = new WeakReference<net.minecraft.server.Chunk>(chunk);
|
|
|
|
worldServer = (WorldServer) getHandle().d;
|
|
|
|
x = getHandle().j;
|
|
|
|
z = getHandle().k;
|
2010-12-27 03:13:03 +01:00
|
|
|
}
|
|
|
|
|
2011-01-04 15:17:05 +01:00
|
|
|
public World getWorld() {
|
2011-02-20 23:22:28 +01:00
|
|
|
return worldServer.getWorld();
|
2011-02-01 23:49:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public net.minecraft.server.Chunk getHandle() {
|
2011-02-20 23:22:28 +01:00
|
|
|
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;
|
2011-01-04 15:17:05 +01:00
|
|
|
}
|
|
|
|
|
2010-12-27 03:13:03 +01:00
|
|
|
public int getX() {
|
2011-02-20 23:22:28 +01:00
|
|
|
return x;
|
2010-12-27 03:13:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getZ() {
|
2011-02-20 23:22:28 +01:00
|
|
|
return z;
|
2010-12-27 03:13:03 +01:00
|
|
|
}
|
2010-12-29 01:29:18 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2011-02-01 23:49:28 +01:00
|
|
|
return "CraftChunk{" + "x=" + getX() + "z=" + getZ() + '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
public Block getBlock(int x, int y, int z) {
|
|
|
|
int pos = (x & 0xF) << 11 | (z & 0xF) << 7 | (y & 0x7F);
|
|
|
|
Block block = this.cache.get( pos );
|
|
|
|
if (block == null) {
|
|
|
|
block = new CraftBlock( this, (getX() << 4) | (x & 0xF), y & 0x7F, (getZ() << 4) | (z & 0xF) );
|
|
|
|
this.cache.put( pos, block );
|
|
|
|
}
|
|
|
|
return block;
|
2010-12-29 01:29:18 +01:00
|
|
|
}
|
2011-03-07 19:48:35 +01:00
|
|
|
|
|
|
|
public Entity[] getEntities() {
|
|
|
|
int count = 0, index = 0;
|
|
|
|
net.minecraft.server.Chunk chunk = getHandle();
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
count += chunk.m[i].size();
|
|
|
|
}
|
|
|
|
Entity[] entities = new Entity[count];
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2011-03-11 22:25:35 +01:00
|
|
|
for (Object obj : (net.minecraft.server.Entity[])chunk.m[i].toArray()) {
|
|
|
|
if (!(obj instanceof net.minecraft.server.Entity)) continue;
|
|
|
|
net.minecraft.server.Entity entity = (net.minecraft.server.Entity) obj;
|
2011-03-07 19:48:35 +01:00
|
|
|
entities[index++] = entity.getBukkitEntity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockState[] getTileEntities() {
|
|
|
|
int index = 0;
|
|
|
|
net.minecraft.server.Chunk chunk = getHandle();
|
|
|
|
BlockState[] entities = new BlockState[chunk.l.size()];
|
2011-03-11 22:25:35 +01:00
|
|
|
for (Object obj : chunk.l.keySet().toArray()) {
|
|
|
|
if (!(obj instanceof ChunkPosition)) continue;
|
|
|
|
ChunkPosition position = (ChunkPosition) obj;
|
2011-03-11 23:57:59 +01:00
|
|
|
entities[index++] = worldServer.getWorld().getBlockAt(position.a + (chunk.j << 4), position.b, position.c + (chunk.k << 4)).getState();
|
2011-03-07 19:48:35 +01:00
|
|
|
}
|
|
|
|
return entities;
|
|
|
|
}
|
2010-12-27 03:13:03 +01:00
|
|
|
}
|
2011-02-20 23:22:28 +01:00
|
|
|
|