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;
|
2012-03-01 20:54:59 +01:00
|
|
|
import java.util.Arrays;
|
2011-02-01 23:49:28 +01:00
|
|
|
|
2012-03-01 20:54:59 +01:00
|
|
|
import net.minecraft.server.BiomeBase;
|
|
|
|
import net.minecraft.server.ChunkPosition;
|
|
|
|
import net.minecraft.server.ChunkSection;
|
2011-10-05 17:31:23 +02:00
|
|
|
import net.minecraft.server.EmptyChunk;
|
2012-03-01 20:54:59 +01:00
|
|
|
import net.minecraft.server.WorldChunkManager;
|
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;
|
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;
|
2012-09-10 06:19:28 +02:00
|
|
|
private final WorldServer worldServer;
|
|
|
|
private final int x;
|
|
|
|
private final int z;
|
2012-03-01 20:54:59 +01:00
|
|
|
private static final byte[] emptyData = new byte[2048];
|
|
|
|
private static final short[] emptyBlockIDs = new short[4096];
|
|
|
|
private static final byte[] emptySkyLight = new byte[2048];
|
2011-04-20 19:05:14 +02:00
|
|
|
|
2011-02-01 23:49:28 +01:00
|
|
|
public CraftChunk(net.minecraft.server.Chunk chunk) {
|
2012-01-14 23:02:10 +01:00
|
|
|
if (!(chunk instanceof EmptyChunk)) {
|
2011-10-05 17:31:23 +02:00
|
|
|
this.weakChunk = new WeakReference<net.minecraft.server.Chunk>(chunk);
|
|
|
|
}
|
2012-03-01 20:54:59 +01:00
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
worldServer = (WorldServer) getHandle().world;
|
2013-11-04 14:07:38 +01:00
|
|
|
x = getHandle().locX;
|
|
|
|
z = getHandle().locZ;
|
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
|
|
|
}
|
|
|
|
|
2011-12-08 06:33:59 +01:00
|
|
|
public CraftWorld getCraftWorld() {
|
|
|
|
return (CraftWorld) 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();
|
2012-03-01 20:54:59 +01:00
|
|
|
|
2011-02-20 23:22:28 +01:00
|
|
|
if (c == null) {
|
2011-06-12 01:12:43 +02:00
|
|
|
c = worldServer.getChunkAt(x, z);
|
2012-03-01 20:54:59 +01:00
|
|
|
|
2012-01-14 23:02:10 +01:00
|
|
|
if (!(c instanceof EmptyChunk)) {
|
2011-10-05 17:31:23 +02:00
|
|
|
weakChunk = new WeakReference<net.minecraft.server.Chunk>(c);
|
|
|
|
}
|
2011-02-20 23:22:28 +01:00
|
|
|
}
|
2012-03-01 20:54:59 +01:00
|
|
|
|
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) {
|
2012-03-01 22:13:53 +01:00
|
|
|
return new CraftBlock(this, (getX() << 4) | (x & 0xF), y & 0xFF, (getZ() << 4) | (z & 0xF));
|
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();
|
2012-03-01 20:54:59 +01:00
|
|
|
|
2012-03-09 19:09:15 +01:00
|
|
|
for (int i = 0; i < 16; 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];
|
2012-03-01 20:54:59 +01:00
|
|
|
|
2012-03-09 19:09:15 +01:00
|
|
|
for (int i = 0; i < 16; i++) {
|
2012-01-14 23:02:10 +01:00
|
|
|
for (Object obj : chunk.entitySlices[i].toArray()) {
|
2011-06-07 09:34:23 +02:00
|
|
|
if (!(obj instanceof net.minecraft.server.Entity)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-01 20:54:59 +01:00
|
|
|
|
2011-04-02 17:56:06 +02:00
|
|
|
entities[index++] = ((net.minecraft.server.Entity) obj).getBukkitEntity();
|
2011-03-07 19:48:35 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-01 20:54:59 +01:00
|
|
|
|
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()];
|
2012-03-01 20:54:59 +01:00
|
|
|
|
2011-04-20 19:05:14 +02:00
|
|
|
for (Object obj : chunk.tileEntities.keySet().toArray()) {
|
2011-06-07 09:34:23 +02:00
|
|
|
if (!(obj instanceof ChunkPosition)) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-01 20:54:59 +01:00
|
|
|
|
2011-03-11 22:25:35 +01:00
|
|
|
ChunkPosition position = (ChunkPosition) obj;
|
2013-11-04 14:07:38 +01:00
|
|
|
entities[index++] = worldServer.getWorld().getBlockAt(position.x + (chunk.locX << 4), position.y, position.z + (chunk.locZ << 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);
|
|
|
|
}
|
|
|
|
|
2012-03-01 20:54:59 +01:00
|
|
|
public ChunkSnapshot getChunkSnapshot(boolean includeMaxBlockY, boolean includeBiome, boolean includeBiomeTempRain) {
|
2011-06-07 09:34:23 +02:00
|
|
|
net.minecraft.server.Chunk chunk = getHandle();
|
2011-06-17 15:23:19 +02:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
ChunkSection[] cs = chunk.i(); /* Get sections */
|
2012-03-01 20:54:59 +01:00
|
|
|
short[][] sectionBlockIDs = new short[cs.length][];
|
|
|
|
byte[][] sectionBlockData = new byte[cs.length][];
|
|
|
|
byte[][] sectionSkyLights = new byte[cs.length][];
|
|
|
|
byte[][] sectionEmitLights = new byte[cs.length][];
|
|
|
|
boolean[] sectionEmpty = new boolean[cs.length];
|
|
|
|
|
|
|
|
for (int i = 0; i < cs.length; i++) {
|
|
|
|
if (cs[i] == null) { /* Section is empty? */
|
|
|
|
sectionBlockIDs[i] = emptyBlockIDs;
|
|
|
|
sectionBlockData[i] = emptyData;
|
|
|
|
sectionSkyLights[i] = emptySkyLight;
|
|
|
|
sectionEmitLights[i] = emptyData;
|
|
|
|
sectionEmpty[i] = true;
|
|
|
|
} else { /* Not empty */
|
|
|
|
short[] blockids = new short[4096];
|
2013-04-27 11:40:05 +02:00
|
|
|
byte[] baseids = cs[i].getIdArray();
|
2012-03-01 20:54:59 +01:00
|
|
|
|
|
|
|
/* Copy base IDs */
|
|
|
|
for (int j = 0; j < 4096; j++) {
|
|
|
|
blockids[j] = (short) (baseids[j] & 0xFF);
|
|
|
|
}
|
|
|
|
|
2013-04-27 11:40:05 +02:00
|
|
|
if (cs[i].getExtendedIdArray() != null) { /* If we've got extended IDs */
|
|
|
|
byte[] extids = cs[i].getExtendedIdArray().a;
|
2012-03-01 20:54:59 +01:00
|
|
|
|
|
|
|
for (int j = 0; j < 2048; j++) {
|
|
|
|
short b = (short) (extids[j] & 0xFF);
|
|
|
|
|
|
|
|
if (b == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-02 18:21:40 +02:00
|
|
|
blockids[j<<1] |= (b & 0x0F) << 8;
|
|
|
|
blockids[(j<<1)+1] |= (b & 0xF0) << 4;
|
2012-03-01 20:54:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sectionBlockIDs[i] = blockids;
|
2012-03-01 11:49:23 +01:00
|
|
|
|
2012-03-01 20:54:59 +01:00
|
|
|
/* Get block data nibbles */
|
|
|
|
sectionBlockData[i] = new byte[2048];
|
2013-04-27 11:40:05 +02:00
|
|
|
System.arraycopy(cs[i].getDataArray().a, 0, sectionBlockData[i], 0, 2048);
|
|
|
|
if (cs[i].getSkyLightArray() == null) {
|
2012-12-20 21:05:07 +01:00
|
|
|
sectionSkyLights[i] = emptyData;
|
|
|
|
} else {
|
|
|
|
sectionSkyLights[i] = new byte[2048];
|
2013-04-27 11:40:05 +02:00
|
|
|
System.arraycopy(cs[i].getSkyLightArray().a, 0, sectionSkyLights[i], 0, 2048);
|
2012-12-20 21:05:07 +01:00
|
|
|
}
|
2012-03-01 20:54:59 +01:00
|
|
|
sectionEmitLights[i] = new byte[2048];
|
2013-04-27 11:40:05 +02:00
|
|
|
System.arraycopy(cs[i].getEmittedLightArray().a, 0, sectionEmitLights[i], 0, 2048);
|
2012-03-01 20:54:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int[] hmap = null;
|
|
|
|
|
|
|
|
if (includeMaxBlockY) {
|
|
|
|
hmap = new int[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();
|
|
|
|
|
|
|
|
if (includeBiome) {
|
|
|
|
biome = new BiomeBase[256];
|
2012-03-04 17:41:06 +01:00
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
biome[i] = chunk.a(i & 0xF, i >> 4, wcm);
|
|
|
|
}
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeBiomeTempRain) {
|
|
|
|
biomeTemp = new double[256];
|
|
|
|
biomeRain = new double[256];
|
2013-11-04 14:07:38 +01:00
|
|
|
float[] dat = getTemperatures(wcm, getX() << 4, getZ() << 4);
|
2012-03-01 20:54:59 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
2011-09-15 22:57:37 +02:00
|
|
|
biomeTemp[i] = dat[i];
|
2012-03-01 20:54:59 +01:00
|
|
|
}
|
|
|
|
|
2013-03-25 05:22:32 +01:00
|
|
|
dat = wcm.getWetness(null, getX() << 4, getZ() << 4, 16, 16);
|
2012-03-01 20:54:59 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
2011-09-15 22:57:37 +02:00
|
|
|
biomeRain[i] = dat[i];
|
2012-03-01 20:54:59 +01:00
|
|
|
}
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 20:54:59 +01:00
|
|
|
World world = getWorld();
|
|
|
|
return new CraftChunkSnapshot(getX(), getZ(), world.getName(), world.getFullTime(), sectionBlockIDs, sectionBlockData, sectionSkyLights, sectionEmitLights, sectionEmpty, hmap, biome, biomeTemp, biomeRain);
|
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-06-17 15:23:19 +02:00
|
|
|
|
|
|
|
if (includeBiome) {
|
|
|
|
biome = new BiomeBase[256];
|
2012-03-04 17:41:06 +01:00
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
biome[i] = world.getHandle().getBiome((x << 4) + (i & 0xF), (z << 4) + (i >> 4));
|
|
|
|
}
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (includeBiomeTempRain) {
|
|
|
|
biomeTemp = new double[256];
|
|
|
|
biomeRain = new double[256];
|
2013-11-04 14:07:38 +01:00
|
|
|
float[] dat = getTemperatures(wcm, x << 4, z << 4);
|
2012-03-01 20:54:59 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
2011-09-15 22:57:37 +02:00
|
|
|
biomeTemp[i] = dat[i];
|
2012-03-01 20:54:59 +01:00
|
|
|
}
|
|
|
|
|
2013-03-25 05:22:32 +01:00
|
|
|
dat = wcm.getWetness(null, x << 4, z << 4, 16, 16);
|
2012-03-01 20:54:59 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 256; i++) {
|
2011-09-15 22:57:37 +02:00
|
|
|
biomeRain[i] = dat[i];
|
2012-03-01 20:54:59 +01:00
|
|
|
}
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-01 20:54:59 +01:00
|
|
|
|
|
|
|
/* Fill with empty data */
|
|
|
|
int hSection = world.getMaxHeight() >> 4;
|
|
|
|
short[][] blockIDs = new short[hSection][];
|
|
|
|
byte[][] skyLight = new byte[hSection][];
|
|
|
|
byte[][] emitLight = new byte[hSection][];
|
|
|
|
byte[][] blockData = new byte[hSection][];
|
|
|
|
boolean[] empty = new boolean[hSection];
|
|
|
|
|
|
|
|
for (int i = 0; i < hSection; i++) {
|
|
|
|
blockIDs[i] = emptyBlockIDs;
|
|
|
|
skyLight[i] = emptySkyLight;
|
|
|
|
emitLight[i] = emptyData;
|
|
|
|
blockData[i] = emptyData;
|
|
|
|
empty[i] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new CraftChunkSnapshot(x, z, world.getName(), world.getFullTime(), blockIDs, blockData, skyLight, emitLight, empty, new int[256], biome, biomeTemp, biomeRain);
|
|
|
|
}
|
|
|
|
|
2013-11-04 14:07:38 +01:00
|
|
|
private static float[] getTemperatures(WorldChunkManager chunkmanager, int chunkX, int chunkZ) {
|
|
|
|
BiomeBase[] biomes = chunkmanager.getBiomes(null, chunkX, chunkZ, 16, 16);
|
|
|
|
float[] temps = new float[biomes.length];
|
|
|
|
|
|
|
|
for (int i = 0; i < biomes.length; i++) {
|
|
|
|
float temp = biomes[i].temperature; // Vanilla of olde: ((int) biomes[i].temperature * 65536.0F) / 65536.0F
|
|
|
|
|
|
|
|
if (temp > 1F) {
|
|
|
|
temp = 1F;
|
|
|
|
}
|
|
|
|
|
|
|
|
temps[i] = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return temps;
|
|
|
|
}
|
|
|
|
|
2012-03-01 20:54:59 +01:00
|
|
|
static {
|
|
|
|
Arrays.fill(emptySkyLight, (byte) 0xFF);
|
2011-06-17 15:23:19 +02:00
|
|
|
}
|
2011-07-28 06:32:58 +02:00
|
|
|
}
|