Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.craftbukkit;
2
3import net.minecraft.server.BiomeBase;
4import org.bukkit.ChunkSnapshot;
5import org.bukkit.block.Biome;
6import org.bukkit.craftbukkit.block.CraftBlock;
7
8/**
9 * Represents a static, thread-safe snapshot of chunk of blocks
10 * 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)
11 */
12public class CraftChunkSnapshot implements ChunkSnapshot {
13 private final int x, z;
14 private final String worldname;
15 private final byte[] buf; // Flat buffer in uncompressed chunk file format
16 private final byte[] hmap; // Height map
17 private final long captureFulltime;
18 private final BiomeBase[] biome;
19 private final double[] biomeTemp;
20 private final double[] biomeRain;
21
22 private static final int BLOCKDATA_OFF = 32768;
23 private static final int BLOCKLIGHT_OFF = BLOCKDATA_OFF + 16384;
24 private static final int SKYLIGHT_OFF = BLOCKLIGHT_OFF + 16384;
25
26 /**
27 * Constructor
28 */
29 CraftChunkSnapshot(int x, int z, String wname, long wtime, byte[] buf, byte[] hmap, BiomeBase[] biome, double[] biomeTemp, double[] biomeRain) {
30 this.x = x;
31 this.z = z;
32 this.worldname = wname;
33 this.captureFulltime = wtime;
34 this.buf = buf;
35 this.hmap = hmap;
36 this.biome = biome;
37 this.biomeTemp = biomeTemp;
38 this.biomeRain = biomeRain;
39 }
40
41 /**
42 * Gets the X-coordinate of this chunk
43 *
44 * @return X-coordinate
45 */
46 public int getX() {
47 return x;
48 }
49
50 /**
51 * Gets the Z-coordinate of this chunk
52 *
53 * @return Z-coordinate
54 */
55 public int getZ() {
56 return z;
57 }
58
59 /**
60 * Gets name of the world containing this chunk
61 *
62 * @return Parent World Name
63 */
64 public String getWorldName() {
65 return worldname;
66 }
67
68 /**
69 * Get block type for block at corresponding coordinate in the chunk
70 *
71 * @param x 0-15
72 * @param y 0-127
73 * @param z 0-15
74 * @return 0-255
75 */
76 public int getBlockTypeId(int x, int y, int z) {
77 return buf[x << 11 | z << 7 | y] & 255;
78 }
79
80 /**
81 * Get block data for block at corresponding coordinate in the chunk
82 *
83 * @param x 0-15
84 * @param y 0-127
85 * @param z 0-15
86 * @return 0-15
87 */
88 public int getBlockData(int x, int y, int z) {
89 int off = ((x << 10) | (z << 6) | (y >> 1)) + BLOCKDATA_OFF;
90
91 return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF);
92 }
93
94 /**
95 * Get sky light level for block at corresponding coordinate in the chunk
96 *
97 * @param x 0-15
98 * @param y 0-127
99 * @param z 0-15
100 * @return 0-15
101 */
102 public int getBlockSkyLight(int x, int y, int z) {
103 int off = ((x << 10) | (z << 6) | (y >> 1)) + SKYLIGHT_OFF;
104
105 return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF);
106 }
107
108 /**
109 * Get light level emitted by block at corresponding coordinate in the chunk
110 *
111 * @param x 0-15
112 * @param y 0-127
113 * @param z 0-15
114 * @return 0-15
115 */
116 public int getBlockEmittedLight(int x, int y, int z) {
117 int off = ((x << 10) | (z << 6) | (y >> 1)) + BLOCKLIGHT_OFF;
118
119 return ((y & 1) == 0) ? (buf[off] & 0xF) : ((buf[off] >> 4) & 0xF);
120 }
121
122 /**
123 * Gets the highest non-air coordinate at the given coordinates
124 *
125 * @param x X-coordinate of the blocks
126 * @param z Z-coordinate of the blocks
127 * @return Y-coordinate of the highest non-air block
128 */
129 public int getHighestBlockYAt(int x, int z) {
130 return hmap[z << 4 | x] & 255;
131 }
132
133 /**
134 * Get biome at given coordinates
135 *
136 * @param x X-coordinate
137 * @param z Z-coordinate
138 * @return Biome at given coordinate
139 */
140 public Biome getBiome(int x, int z) {
141 return CraftBlock.biomeBaseToBiome(biome[x << 4 | z]);
142 }
143
144 /**
145 * Get raw biome temperature (0.0-1.0) at given coordinate
146 *
147 * @param x X-coordinate
148 * @param z Z-coordinate
149 * @return temperature at given coordinate
150 */
151 public double getRawBiomeTemperature(int x, int z) {
152 return biomeTemp[x << 4 | z];
153 }
154
155 /**
156 * Get raw biome rainfall (0.0-1.0) at given coordinate
157 *
158 * @param x X-coordinate
159 * @param z Z-coordinate
160 * @return rainfall at given coordinate
161 */
162 public double getRawBiomeRainfall(int x, int z) {
163 return biomeRain[x << 4 | z];
164 }
165
166 /**
167 * Get world full time when chunk snapshot was captured
168 *
169 * @return time in ticks
170 */
171 public long getCaptureFullTime() {
172 return captureFulltime;
173 }
174}