-
Notifications
You must be signed in to change notification settings - Fork 0
/
CraftChunk.java
290 lines (224 loc) · 9.6 KB
/
CraftChunk.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package org.bukkit.craftbukkit;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.entity.Entity;
import org.bukkit.ChunkSnapshot;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.BiomeProvider;
import net.minecraft.world.chunk.BlockStateContainer;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
public class CraftChunk implements Chunk {
private WeakReference<minecraft.world.chunk.Chunk> weakChunk;
private final WorldServer worldServer;
private final int x;
private final int z;
private static final byte[] emptyData = new byte[2048];
private static final BlockStateContainer<IBlockState> emptyBlockIDs = new ExtendedBlockStorage(0, false).getData();
private static final byte[] emptySkyLight = new byte[2048];
public CraftChunk(minecraft.world.chunk.Chunk chunk) {
this.weakChunk = new WeakReference<minecraft.world.chunk.Chunk>(chunk);
worldServer = (WorldServer) getHandle().world;
x = getHandle().x;
z = getHandle().z;
}
public World getWorld() {
return worldServer.getWorld();
}
public CraftWorld getCraftWorld() {
return (CraftWorld) getWorld();
}
public minecraft.world.chunk.Chunk getHandle() {
minecraft.world.chunk.Chunk c = weakChunk.get();
if (c == null) {
c = worldServer.getChunkAt(x, z);
weakChunk = new WeakReference<minecraft.world.chunk.Chunk>(c);
}
return c;
}
void breakLink() {
weakChunk.clear();
}
public int getX() {
return x;
}
public int getZ() {
return z;
}
@Override
public String toString() {
return "CraftChunk{" + "x=" + getX() + "z=" + getZ() + '}';
}
public Block getBlock(int x, int y, int z) {
return new CraftBlock(worldServer, new BlockPos((this.x << 4) | x, y, (this.z << 4) | z));
}
public Entity[] getEntities() {
int count = 0, index = 0;
minecraft.world.chunk.Chunk chunk = getHandle();
for (int i = 0; i < 16; i++) {
count += chunk.entityLists[i].size();
}
Entity[] entities = new Entity[count];
for (int i = 0; i < 16; i++) {
for (Object obj : chunk.entityLists[i].toArray()) {
if (!(obj instanceof net.minecraft.entity.Entity)) {
continue;
}
entities[index++] = ((net.minecraft.entity.Entity) obj).getBukkitEntity();
}
}
return entities;
}
public BlockState[] getTileEntities() {
int index = 0;
minecraft.world.chunk.Chunk chunk = getHandle();
BlockState[] entities = new BlockState[chunk.tileEntities.size()];
for (Object obj : chunk.tileEntities.keySet().toArray()) {
if (!(obj instanceof BlockPos)) {
continue;
}
BlockPos position = (BlockPos) obj;
entities[index++] = worldServer.getWorld().getBlockAt(position.getX(), position.getY(), position.getZ()).getState();
}
return entities;
}
public boolean isLoaded() {
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());
}
@Override
public boolean isSlimeChunk() {
// 987234911L is deterimined in EntitySlime when seeing if a slime can spawn in a chunk
return SeededRandom.a(getX(), getZ(), getWorld().getSeed(), 987234911L).nextInt(10) == 0;
}
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);
}
public ChunkSnapshot getChunkSnapshot() {
return getChunkSnapshot(true, false, false);
}
public ChunkSnapshot getChunkSnapshot(boolean includeMaxBlockY, boolean includeBiome, boolean includeBiomeTempRain) {
minecraft.world.chunk.Chunk chunk = getHandle();
ExtendedBlockStorage[] cs = chunk.getSections();
BlockStateContainer[] sectionBlockIDs = new BlockStateContainer[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;
sectionSkyLights[i] = emptySkyLight;
sectionEmitLights[i] = emptyData;
sectionEmpty[i] = true;
} else { // Not empty
NBTTagCompound data = new NBTTagCompound();
cs[i].getData().b(data, "Spigot", "Magic");
BlockStateContainer blockids = new BlockStateContainer<>(ExtendedBlockStorage.GLOBAL_PALETTE, net.minecraft.block.Block.BLOCK_STATE_IDS, NBTUtil::readBlockState, NBTUtil::a, Blocks.AIR.getDefaultState()); // TODO: snapshot whole ChunkSection
blockids.a(data, "Spigot", "Magic");
sectionBlockIDs[i] = blockids;
if (cs[i].getSkyLight() == null) {
sectionSkyLights[i] = emptySkyLight;
} else {
sectionSkyLights[i] = new byte[2048];
System.arraycopy(cs[i].getSkyLight().getData(), 0, sectionSkyLights[i], 0, 2048);
}
sectionEmitLights[i] = new byte[2048];
System.arraycopy(cs[i].getBlockLight().getData(), 0, sectionEmitLights[i], 0, 2048);
}
}
HeightMap hmap = null;
if (includeMaxBlockY) {
hmap = new HeightMap(null, HeightMap.Type.LIGHT_BLOCKING);
hmap.a(chunk.heightMap.get(HeightMap.Type.LIGHT_BLOCKING).b());
}
Biome[] biome = null;
double[] biomeTemp = null;
if (includeBiome || includeBiomeTempRain) {
BiomeProvider wcm = worldServer.getChunkProvider().getChunkGenerator().getWorldChunkManager();
if (includeBiome) {
biome = new Biome[256];
for (int i = 0; i < 256; i++) {
biome[i] = chunk.getBiome(new BlockPos(i & 0xF, 0, i >> 4));
}
}
if (includeBiomeTempRain) {
biomeTemp = new double[256];
float[] dat = getTemperatures(wcm, getX() << 4, getZ() << 4);
for (int i = 0; i < 256; i++) {
biomeTemp[i] = dat[i];
}
}
}
World world = getWorld();
return new CraftChunkSnapshot(getX(), getZ(), world.getName(), world.getFullTime(), sectionBlockIDs, sectionSkyLights, sectionEmitLights, sectionEmpty, hmap, biome, biomeTemp);
}
public static ChunkSnapshot getEmptyChunkSnapshot(int x, int z, CraftWorld world, boolean includeBiome, boolean includeBiomeTempRain) {
Biome[] biome = null;
double[] biomeTemp = null;
if (includeBiome || includeBiomeTempRain) {
BiomeProvider wcm = world.getHandle().getChunkProvider().getChunkGenerator().getWorldChunkManager();
if (includeBiome) {
biome = new Biome[256];
for (int i = 0; i < 256; i++) {
biome[i] = world.getHandle().getBiome(new BlockPos((x << 4) + (i & 0xF), 0, (z << 4) + (i >> 4)));
}
}
if (includeBiomeTempRain) {
biomeTemp = new double[256];
float[] dat = getTemperatures(wcm, x << 4, z << 4);
for (int i = 0; i < 256; i++) {
biomeTemp[i] = dat[i];
}
}
}
/* Fill with empty data */
int hSection = world.getMaxHeight() >> 4;
BlockStateContainer[] blockIDs = new BlockStateContainer[hSection];
byte[][] skyLight = new byte[hSection][];
byte[][] emitLight = new byte[hSection][];
boolean[] empty = new boolean[hSection];
for (int i = 0; i < hSection; i++) {
blockIDs[i] = emptyBlockIDs;
skyLight[i] = emptySkyLight;
emitLight[i] = emptyData;
empty[i] = true;
}
return new CraftChunkSnapshot(x, z, world.getName(), world.getFullTime(), blockIDs, skyLight, emitLight, empty, new HeightMap(null, HeightMap.Type.LIGHT_BLOCKING), biome, biomeTemp);
}
private static float[] getTemperatures(BiomeProvider chunkmanager, int chunkX, int chunkZ) {
Biome[] biomes = chunkmanager.getBiomes(chunkX, chunkZ, 16, 16);
float[] temps = new float[biomes.length];
for (int i = 0; i < biomes.length; i++) {
float temp = biomes[i].getDefaultTemperature(); // Vanilla of olde: ((int) biomes[i].temperature * 65536.0F) / 65536.0F
if (temp > 1F) {
temp = 1F;
}
temps[i] = temp;
}
return temps;
}
static {
Arrays.fill(emptySkyLight, (byte) 0xFF);
}
}