2010-12-27 03:13:03 +01:00
|
|
|
package org.bukkit.craftbukkit;
|
|
|
|
|
2011-07-15 19:28:09 +02:00
|
|
|
import com.google.common.collect.MapMaker;
|
2011-02-20 23:22:28 +01:00
|
|
|
import java.lang.ref.WeakReference;
|
2011-07-15 19:28:09 +02:00
|
|
|
import java.util.concurrent.ConcurrentMap;
|
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-06-07 09:34:23 +02:00
|
|
|
import org.bukkit.ChunkSnapshot;
|
2011-06-17 15:23:19 +02:00
|
|
|
import net.minecraft.server.BiomeBase;
|
|
|
|
import net.minecraft.server.WorldChunkManager;
|
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-07-16 03:55:54 +02:00
|
|
|
private final ConcurrentMap<Integer, Block> cache = new MapMaker().softValues().makeMap();
|
2011-02-20 23:22:28 +01:00
|
|
|
private WorldServer worldServer;
|
|
|
|
private int x;
|
|
|
|
private int z;
|
2011-04-20 19:05:14 +02:00
|
|
|
|
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);
|
2011-04-20 19:05:14 +02:00
|
|
|
worldServer = (WorldServer) getHandle().world;
|
|
|
|
x = getHandle().x;
|
|
|
|
z = getHandle().z;
|
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) {
|
2011-06-12 01:12:43 +02:00
|
|
|
c = worldServer.getChunkAt(x, z);
|
2011-03-11 20:39:09 +01:00
|
|
|
weakChunk = new WeakReference<net.minecraft.server.Chunk>(c);
|
2011-02-20 23:22:28 +01:00
|
|
|
}
|
|
|
|
return c;
|
2011-01-04 15:17:05 +01:00
|
|
|
}
|
|
|
|
|
2011-03-11 20:39:09 +01:00
|
|
|
void breakLink() {
|
|
|
|
weakChunk.clear();
|
2011-05-14 16:29:42 +02:00
|
|
|
}
|
2011-03-11 20:39:09 +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);
|
2011-05-14 16:29:42 +02:00
|
|
|
Block block = this.cache.get(pos);
|
2011-02-01 23:49:28 +01:00
|
|
|
if (block == null) {
|
2011-05-14 16:29:42 +02:00
|
|
|
Block newBlock = new CraftBlock(this, (getX() << 4) | (x & 0xF), y & 0x7F, (getZ() << 4) | (z & 0xF));
|
|
|
|
Block oldBlock = this.cache.put(pos, newBlock);
|
2011-06-12 01:12:43 +02:00
|
|
|
if (oldBlock == null) {
|
2011-03-10 23:13:47 +01:00
|
|
|
block = newBlock;
|
|
|
|
} else {
|
|
|
|
block = oldBlock;
|
2011-05-14 16:29:42 +02:00
|
|
|
}
|
2011-02-01 23:49:28 +01:00
|
|
|
}
|
|
|
|
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++) {
|
2011-04-20 19:05:14 +02:00
|
|
|
count += chunk.entitySlices[i].size();
|
2011-03-07 19:48:35 +01:00
|
|
|
}
|
2011-04-02 17:56:06 +02:00
|
|
|
|
|
|
|
Entity[] entities = new Entity[count];
|
2011-03-07 19:48:35 +01:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2011-04-20 19:05:14 +02:00
|
|
|
for (Object obj: chunk.entitySlices[i].toArray()) {
|
2011-06-07 09:34:23 +02:00
|
|
|
if (!(obj instanceof net.minecraft.server.Entity)) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-04-02 17:56:06 +02:00
|
|
|
entities[index++] = ((net.minecraft.server.Entity) obj).getBukkitEntity();
|
2011-03-07 19:48:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
|
|
|
public BlockState[] getTileEntities() {
|
|
|
|
int index = 0;
|
|
|
|
net.minecraft.server.Chunk chunk = getHandle();
|
2011-04-20 19:05:14 +02:00
|
|
|
BlockState[] entities = new BlockState[chunk.tileEntities.size()];
|
|
|
|
for (Object obj : chunk.tileEntities.keySet().toArray()) {
|
2011-06-07 09:34:23 +02:00
|
|
|
if (!(obj instanceof ChunkPosition)) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-03-11 22:25:35 +01:00
|
|
|
ChunkPosition position = (ChunkPosition) obj;
|
2011-04-20 19:05:14 +02:00
|
|
|
entities[index++] = worldServer.getWorld().getBlockAt(position.x + (chunk.x << 4), position.y, position.z + (chunk.z << 4)).getState();
|
2011-03-07 19:48:35 +01:00
|
|
|
}
|
|
|
|
return entities;
|
|
|
|
}
|
2011-02-20 23:22:28 +01:00
|
|
|
|
2011-09-07 20:54:45 +02:00
|
|
|
public boolean isLoaded() {
|
2011-07-28 06:32:58 +02:00
|
|
|
return getWorld().isChunkLoaded(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean load() {
|
|
|
|
return getWorld().loadChunk(getX(), getZ(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean load(boolean generate) {
|
|
|
|
return getWorld().loadChunk(getX(), getZ(), generate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean unload() {
|
|
|
|
return getWorld().unloadChunk(getX(), getZ());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean unload(boolean save) {
|
|
|
|
return getWorld().unloadChunk(getX(), getZ(), save);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean unload(boolean save, boolean safe) {
|
|
|
|
return getWorld().unloadChunk(getX(), getZ(), save, safe);
|
|
|
|
}
|
|
|
|
|
2011-06-07 09:34:23 +02:00
|
|
|
public ChunkSnapshot getChunkSnapshot() {
|
2011-06-17 15:23:19 +02:00
|
|
|
return getChunkSnapshot(true, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ChunkSnapshot getChunkSnapshot(boolean includeMaxblocky, boolean includeBiome, boolean includeBiomeTempRain) {
|
2011-06-07 09:34:23 +02:00
|
|
|
net.minecraft.server.Chunk chunk = getHandle();
|
|
|
|
byte[] buf = new byte[32768 + 16384 + 16384 + 16384]; // Get big enough buffer for whole chunk
|
2011-06-27 00:25:01 +02:00
|
|
|
chunk.getData(buf, 0, 0, 0, 16, 128, 16, 0); // Get whole chunk
|
2011-06-17 15:23:19 +02:00
|
|
|
byte[] hmap = null;
|
|
|
|
|
|
|
|
if (includeMaxblocky) {
|
|
|
|
hmap = new byte[256]; // Get copy of height map
|
2011-06-27 00:25:01 +02:00
|
|
|
System.arraycopy(chunk.heightMap, 0, hmap, 0, 256);
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BiomeBase[] biome = null;
|
|
|
|
double[] biomeTemp = null;
|
|
|
|
double[] biomeRain = null;
|
|
|
|
|
|
|
|
if (includeBiome || includeBiomeTempRain) {
|
|
|
|
WorldChunkManager wcm = chunk.world.getWorldChunkManager();
|
2011-09-15 02:23:52 +02:00
|
|
|
BiomeBase[] biomeBase = wcm.b((BiomeBase[])null, getX() << 4, getZ() << 4, 16, 16);
|
2011-06-17 15:23:19 +02:00
|
|
|
|
|
|
|
if (includeBiome) {
|
|
|
|
biome = new BiomeBase[256];
|
2011-06-27 00:25:01 +02:00
|
|
|
System.arraycopy(biomeBase, 0, biome, 0, biome.length);
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeBiomeTempRain) {
|
|
|
|
biomeTemp = new double[256];
|
|
|
|
biomeRain = new double[256];
|
2011-09-15 02:23:52 +02:00
|
|
|
//System.arraycopy(wcm.temperature, 0, biomeTemp, 0, biomeTemp.length);
|
|
|
|
//System.arraycopy(wcm.rain, 0, biomeRain, 0, biomeRain.length);
|
|
|
|
// TODO: Figure out new snapshot stuff
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-27 00:25:01 +02:00
|
|
|
World world = getWorld();
|
|
|
|
return new CraftChunkSnapshot(getX(), getZ(), world.getName(), world.getFullTime(), buf, hmap, biome, biomeTemp, biomeRain);
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
2011-07-17 15:34:40 +02:00
|
|
|
|
2011-06-17 15:23:19 +02:00
|
|
|
/**
|
|
|
|
* Empty chunk snapshot - nothing but air blocks, but can include valid biome data
|
|
|
|
*/
|
|
|
|
private static class EmptyChunkSnapshot extends CraftChunkSnapshot {
|
2011-06-27 00:25:01 +02:00
|
|
|
EmptyChunkSnapshot(int x, int z, String worldName, long time, BiomeBase[] biome, double[] biomeTemp, double[] biomeRain) {
|
|
|
|
super(x, z, worldName, time, null, null, biome, biomeTemp, biomeRain);
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public final int getBlockTypeId(int x, int y, int z) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int getBlockData(int x, int y, int z) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int getBlockSkyLight(int x, int y, int z) {
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int getBlockEmittedLight(int x, int y, int z) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int getHighestBlockYAt(int x, int z) {
|
|
|
|
return 0;
|
|
|
|
}
|
2011-06-07 09:34:23 +02:00
|
|
|
}
|
|
|
|
|
2011-06-27 00:25:01 +02:00
|
|
|
public static ChunkSnapshot getEmptyChunkSnapshot(int x, int z, CraftWorld world, boolean includeBiome, boolean includeBiomeTempRain) {
|
2011-06-17 15:23:19 +02:00
|
|
|
BiomeBase[] biome = null;
|
|
|
|
double[] biomeTemp = null;
|
|
|
|
double[] biomeRain = null;
|
|
|
|
|
|
|
|
if (includeBiome || includeBiomeTempRain) {
|
2011-06-27 00:25:01 +02:00
|
|
|
WorldChunkManager wcm = world.getHandle().getWorldChunkManager();
|
2011-09-15 02:23:52 +02:00
|
|
|
BiomeBase[] biomeBase = wcm.b((BiomeBase[])null, x << 4, z << 4, 16, 16);
|
2011-06-17 15:23:19 +02:00
|
|
|
|
|
|
|
if (includeBiome) {
|
|
|
|
biome = new BiomeBase[256];
|
2011-06-27 00:25:01 +02:00
|
|
|
System.arraycopy(biomeBase, 0, biome, 0, biome.length);
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeBiomeTempRain) {
|
|
|
|
biomeTemp = new double[256];
|
|
|
|
biomeRain = new double[256];
|
2011-09-15 02:23:52 +02:00
|
|
|
//System.arraycopy(wcm.temperature, 0, biomeTemp, 0, biomeTemp.length);
|
|
|
|
//System.arraycopy(wcm.rain, 0, biomeRain, 0, biomeRain.length);
|
|
|
|
// TODO: Figure out new snapshot stuff
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-27 00:25:01 +02:00
|
|
|
return new EmptyChunkSnapshot(x, z, world.getName(), world.getFullTime(), biome, biomeTemp, biomeRain);
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
2011-07-28 06:32:58 +02:00
|
|
|
}
|