diff options
author | EvilSeph <evilseph@unaligned.org> | 2011-06-07 03:34:23 -0400 |
---|---|---|
committer | EvilSeph <evilseph@unaligned.org> | 2011-06-07 03:34:23 -0400 |
commit | 95c19d5f226d0af7c20073602131590d6b7bbb44 (patch) | |
tree | 1a4d6cf07592a4bf87dc19dc00b064f20d550925 /src/main/java | |
parent | 68608169cd75cb5df86f087abd2ba1ff32b9c3ae (diff) | |
download | craftbukkit-95c19d5f226d0af7c20073602131590d6b7bbb44.tar craftbukkit-95c19d5f226d0af7c20073602131590d6b7bbb44.tar.gz craftbukkit-95c19d5f226d0af7c20073602131590d6b7bbb44.tar.lz craftbukkit-95c19d5f226d0af7c20073602131590d6b7bbb44.tar.xz craftbukkit-95c19d5f226d0af7c20073602131590d6b7bbb44.zip |
Added ChunkSnapshot for efficient, thread-safe copies of Chunk data. Thanks mikeprimm!
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/CraftChunk.java | 26 | ||||
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/CraftChunkSnapshot.java | 130 |
2 files changed, 152 insertions, 4 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftChunk.java b/src/main/java/org/bukkit/craftbukkit/CraftChunk.java index ca4be8ce..ad0b5c8c 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftChunk.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftChunk.java @@ -1,4 +1,3 @@ - package org.bukkit.craftbukkit; import java.lang.ref.WeakReference; @@ -13,6 +12,7 @@ import org.bukkit.block.BlockState; import org.bukkit.craftbukkit.block.CraftBlock; import org.bukkit.entity.Entity; import org.bukkit.craftbukkit.util.ConcurrentSoftMap; +import org.bukkit.ChunkSnapshot; public class CraftChunk implements Chunk { private WeakReference<net.minecraft.server.Chunk> weakChunk; @@ -83,7 +83,9 @@ public class CraftChunk implements Chunk { Entity[] entities = new Entity[count]; for (int i = 0; i < 8; i++) { for (Object obj: chunk.entitySlices[i].toArray()) { - if (!(obj instanceof net.minecraft.server.Entity)) continue; + if (!(obj instanceof net.minecraft.server.Entity)) { + continue; + } entities[index++] = ((net.minecraft.server.Entity) obj).getBukkitEntity(); } } @@ -95,11 +97,27 @@ public class CraftChunk implements Chunk { net.minecraft.server.Chunk chunk = getHandle(); BlockState[] entities = new BlockState[chunk.tileEntities.size()]; for (Object obj : chunk.tileEntities.keySet().toArray()) { - if (!(obj instanceof ChunkPosition)) continue; + if (!(obj instanceof ChunkPosition)) { + continue; + } ChunkPosition position = (ChunkPosition) obj; entities[index++] = worldServer.getWorld().getBlockAt(position.x + (chunk.x << 4), position.y, position.z + (chunk.z << 4)).getState(); } return entities; } -} + /** + * Capture thread-safe read-only snapshot of chunk data + * @return ChunkSnapshot + */ + public ChunkSnapshot getChunkSnapshot() { + net.minecraft.server.Chunk chunk = getHandle(); + byte[] buf = new byte[32768 + 16384 + 16384 + 16384]; // Get big enough buffer for whole chunk + chunk.a(buf, 0, 0, 0, 16, 128, 16, 0); // Get whole chunk + byte[] hmap = new byte[256]; // Get copy of height map + System.arraycopy(chunk.h, 0, hmap, 0, 256); + World w = getWorld(); + return new CraftChunkSnapshot(getX(), getZ(), w.getName(), w.getFullTime(), buf, hmap); + } + +}
\ No newline at end of file diff --git a/src/main/java/org/bukkit/craftbukkit/CraftChunkSnapshot.java b/src/main/java/org/bukkit/craftbukkit/CraftChunkSnapshot.java new file mode 100644 index 00000000..5004575e --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/CraftChunkSnapshot.java @@ -0,0 +1,130 @@ +package org.bukkit.craftbukkit; + +import org.bukkit.ChunkSnapshot; +/** + * Represents a static, thread-safe snapshot of chunk of blocks + * Purpose is to allow clean, efficient copy of a chunk data to be made, and then handed off for processing in another thread (e.g. map rendering) + */ +public class CraftChunkSnapshot implements ChunkSnapshot { + private final int x, z; + private final String worldname; + private final byte[] buf; // Flat buffer in uncompressed chunk file format + private final byte[] hmap; // Height map + private final long capture_fulltime; + + private static final int BLOCKDATA_OFF = 32768; + private static final int BLOCKLIGHT_OFF = BLOCKDATA_OFF + 16384; + private static final int SKYLIGHT_OFF = BLOCKLIGHT_OFF + 16384; + + /** + * Constructor + */ + CraftChunkSnapshot(int x, int z, String wname, long wtime, byte[] buf, byte[] hmap) { + this.x = x; + this.z = z; + this.worldname = wname; + this.capture_fulltime = wtime; + this.buf = buf; + this.hmap = hmap; + } + + /** + * Gets the X-coordinate of this chunk + * + * @return X-coordinate + */ + public int getX() { + return x; + } + + /** + * Gets the Z-coordinate of this chunk + * + * @return Z-coordinate + */ + public int getZ() { + return z; + } + + /** + * Gets name of the world containing this chunk + * + * @return Parent World Name + */ + public String getWorldName() { + return worldname; + } + + /** + * Get block type for block at corresponding coordinate in the chunk + * + * @param x 0-15 + * @param y 0-127 + * @param z 0-15 + * @return 0-255 + */ + public int getBlockTypeId(int x, int y, int z) { + return buf[x << 11 | z << 7 | y] & 255; + } + + /** + * Get block data for block at corresponding coordinate in the chunk + * + * @param x 0-15 + * @param y 0-127 + * @param z 0-15 + * @return 0-15 + */ + public int getBlockData(int x, int y, int z) { + int off = ((x << 10) | (z << 6) | (y >> 1)) + BLOCKDATA_OFF; + + return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF); + } + + /** + * Get sky light level for block at corresponding coordinate in the chunk + * + * @param x 0-15 + * @param y 0-127 + * @param z 0-15 + * @return 0-15 + */ + public int getBlockSkyLight(int x, int y, int z) { + int off = ((x << 10) | (z << 6) | (y >> 1)) + SKYLIGHT_OFF; + + return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF); + } + + /** + * Get light level emitted by block at corresponding coordinate in the chunk + * + * @param x 0-15 + * @param y 0-127 + * @param z 0-15 + * @return 0-15 + */ + public int getBlockEmittedLight(int x, int y, int z) { + int off = ((x << 10) | (z << 6) | (y >> 1)) + BLOCKLIGHT_OFF; + + return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF); + } + + /** + * Gets the highest non-air coordinate at the given coordinates + * + * @param x X-coordinate of the blocks + * @param z Z-coordinate of the blocks + * @return Y-coordinate of the highest non-air block + */ + public int getHighestBlockYAt(int x, int z) { + return hmap[z << 4 | x] & 255; + } + + /** + * Get world full time when chunk snapshot was captured + * @return time in ticks + */ + public long getCaptureFullTime() { + return capture_fulltime; + } +} |