AnvilChunkLoader .java

net.minecraft.world.chunk.storage.AnvilChunkLoader (class): members: pendingAnvilChunksCoordinates, checkedReadChunkFromNBT, addChunkToPending, chunkTick.

net.minecraft.world.chunk.storageclass484 linescommon17.8 KB

Implements

API Surface (8)

Chunk loadChunk(World worldIn, int x, int z)
Chunk checkedReadChunkFromNBT(World worldIn, int x, int z, NBTTagCompound p_75822_4_)
void saveChunk(World worldIn, Chunk chunkIn)
void addChunkToPending(ChunkCoordIntPair p_75824_1_, NBTTagCompound p_75824_2_)
boolean writeNextIO()
void saveExtraChunkData(World worldIn, Chunk chunkIn)
void chunkTick()
void saveExtraData()

Fields & Constants (5)

logger
chunksToRemove
pendingAnvilChunksCoordinates
chunkSaveLocation
field_183014_e

String literals (22)

"Level"
"Chunk file at "
" is missing level data, skipping"
"Sections"
" is missing block data, skipping"
", got "
"xPos"
"zPos"
"Failed to save chunk"
"LastUpdate"
"HeightMap"
"TerrainPopulated"
"LightPopulated"
"InhabitedTime"
"Blocks"
"BlockLight"
"SkyLight"
"Biomes"
"Entities"
"TileEntities"
"TileTicks"
"Riding"

Full Source

AnvilChunkLoader.java484 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.world.chunk.storage;
2
3
import java.io.DataInputStream;
4
import java.io.DataOutputStream;
5
import java.io.File;
6
import java.io.IOException;
7
import java.util.Collections;
8
import java.util.List;
9
import java.util.Map;
10
import java.util.Set;
11
import java.util.concurrent.ConcurrentHashMap;
12
import net.minecraft.block.Block;
13
import net.minecraft.entity.Entity;
14
import net.minecraft.entity.EntityList;
15
import net.minecraft.nbt.CompressedStreamTools;
16
import net.minecraft.nbt.NBTTagCompound;
17
import net.minecraft.nbt.NBTTagList;
18
import net.minecraft.tileentity.TileEntity;
19
import net.minecraft.util.BlockPos;
20
import net.minecraft.util.ResourceLocation;
21
import net.minecraft.world.ChunkCoordIntPair;
22
import net.minecraft.world.MinecraftException;
23
import net.minecraft.world.NextTickListEntry;
24
import net.minecraft.world.World;
25
import net.minecraft.world.chunk.Chunk;
26
import net.minecraft.world.chunk.NibbleArray;
27
import net.minecraft.world.storage.IThreadedFileIO;
28
import net.minecraft.world.storage.ThreadedFileIOBase;
29
import org.apache.logging.log4j.LogManager;
30
import org.apache.logging.log4j.Logger;
31
32
public class AnvilChunkLoader implements IChunkLoader, IThreadedFileIO
33
{
34
    private static final Logger logger = LogManager.getLogger();
35
    private Map<ChunkCoordIntPair, NBTTagCompound> chunksToRemove = new ConcurrentHashMap();
36
    private Set<ChunkCoordIntPair> pendingAnvilChunksCoordinates = Collections.<ChunkCoordIntPair>newSetFromMap(new ConcurrentHashMap());
37
38
    /** Save directory for chunks using the Anvil format */
39
    private final File chunkSaveLocation;
40
    private boolean field_183014_e = false;
41
42
    public AnvilChunkLoader(File chunkSaveLocationIn)
43
    {
44
        this.chunkSaveLocation = chunkSaveLocationIn;
45
    }
46
47
    /**
48
     * Loads the specified(XZ) chunk into the specified world.
49
     */
50
    public Chunk loadChunk(World worldIn, int x, int z) throws IOException
51
    {
52
        ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(x, z);
53
        NBTTagCompound nbttagcompound = (NBTTagCompound)this.chunksToRemove.get(chunkcoordintpair);
54
55
        if (nbttagcompound == null)
56
        {
57
            DataInputStream datainputstream = RegionFileCache.getChunkInputStream(this.chunkSaveLocation, x, z);
58
59
            if (datainputstream == null)
60
            {
61
                return null;
62
            }
63
64
            nbttagcompound = CompressedStreamTools.read(datainputstream);
65
        }
66
67
        return this.checkedReadChunkFromNBT(worldIn, x, z, nbttagcompound);
68
    }
69
70
    /**
71
     * Wraps readChunkFromNBT. Checks the coordinates and several NBT tags.
72
     */
73
    protected Chunk checkedReadChunkFromNBT(World worldIn, int x, int z, NBTTagCompound p_75822_4_)
74
    {
75
        if (!p_75822_4_.hasKey("Level", 10))
76
        {
77
            logger.error("Chunk file at " + x + "," + z + " is missing level data, skipping");
78
            return null;
79
        }
80
        else
81
        {
82
            NBTTagCompound nbttagcompound = p_75822_4_.getCompoundTag("Level");
83
84
            if (!nbttagcompound.hasKey("Sections", 9))
85
            {
86
                logger.error("Chunk file at " + x + "," + z + " is missing block data, skipping");
87
                return null;
88
            }
89
            else
90
            {
91
                Chunk chunk = this.readChunkFromNBT(worldIn, nbttagcompound);
92
93
                if (!chunk.isAtLocation(x, z))
94
                {
95
                    logger.error("Chunk file at " + x + "," + z + " is in the wrong location; relocating. (Expected " + x + ", " + z + ", got " + chunk.xPosition + ", " + chunk.zPosition + ")");
96
                    nbttagcompound.setInteger("xPos", x);
97
                    nbttagcompound.setInteger("zPos", z);
98
                    chunk = this.readChunkFromNBT(worldIn, nbttagcompound);
99
                }
100
101
                return chunk;
102
            }
103
        }
104
    }
105
106
    public void saveChunk(World worldIn, Chunk chunkIn) throws MinecraftException, IOException
107
    {
108
        worldIn.checkSessionLock();
109
110
        try
111
        {
112
            NBTTagCompound nbttagcompound = new NBTTagCompound();
113
            NBTTagCompound nbttagcompound1 = new NBTTagCompound();
114
            nbttagcompound.setTag("Level", nbttagcompound1);
115
            this.writeChunkToNBT(chunkIn, worldIn, nbttagcompound1);
116
            this.addChunkToPending(chunkIn.getChunkCoordIntPair(), nbttagcompound);
117
        }
118
        catch (Exception exception)
119
        {
120
            logger.error((String)"Failed to save chunk", (Throwable)exception);
121
        }
122
    }
123
124
    protected void addChunkToPending(ChunkCoordIntPair p_75824_1_, NBTTagCompound p_75824_2_)
125
    {
126
        if (!this.pendingAnvilChunksCoordinates.contains(p_75824_1_))
127
        {
128
            this.chunksToRemove.put(p_75824_1_, p_75824_2_);
129
        }
130
131
        ThreadedFileIOBase.getThreadedIOInstance().queueIO(this);
132
    }
133
134
    /**
135
     * Returns a boolean stating if the write was unsuccessful.
136
     */
137
    public boolean writeNextIO()
138
    {
139
        if (this.chunksToRemove.isEmpty())
140
        {
141
            if (this.field_183014_e)
142
            {
143
                logger.info("ThreadedAnvilChunkStorage ({}): All chunks are saved", new Object[] {this.chunkSaveLocation.getName()});
144
            }
145
146
            return false;
147
        }
148
        else
149
        {
150
            ChunkCoordIntPair chunkcoordintpair = (ChunkCoordIntPair)this.chunksToRemove.keySet().iterator().next();
151
            boolean lvt_3_1_;
152
153
            try
154
            {
155
                this.pendingAnvilChunksCoordinates.add(chunkcoordintpair);
156
                NBTTagCompound nbttagcompound = (NBTTagCompound)this.chunksToRemove.remove(chunkcoordintpair);
157
158
                if (nbttagcompound != null)
159
                {
160
                    try
161
                    {
162
                        this.func_183013_b(chunkcoordintpair, nbttagcompound);
163
                    }
164
                    catch (Exception exception)
165
                    {
166
                        logger.error((String)"Failed to save chunk", (Throwable)exception);
167
                    }
168
                }
169
170
                lvt_3_1_ = true;
171
            }
172
            finally
173
            {
174
                this.pendingAnvilChunksCoordinates.remove(chunkcoordintpair);
175
            }
176
177
            return lvt_3_1_;
178
        }
179
    }
180
181
    private void func_183013_b(ChunkCoordIntPair p_183013_1_, NBTTagCompound p_183013_2_) throws IOException
182
    {
183
        DataOutputStream dataoutputstream = RegionFileCache.getChunkOutputStream(this.chunkSaveLocation, p_183013_1_.chunkXPos, p_183013_1_.chunkZPos);
184
        CompressedStreamTools.write(p_183013_2_, dataoutputstream);
185
        dataoutputstream.close();
186
    }
187
188
    /**
189
     * Save extra data associated with this Chunk not normally saved during autosave, only during chunk unload.
190
     * Currently unused.
191
     */
192
    public void saveExtraChunkData(World worldIn, Chunk chunkIn) throws IOException
193
    {
194
    }
195
196
    /**
197
     * Called every World.tick()
198
     */
199
    public void chunkTick()
200
    {
201
    }
202
203
    /**
204
     * Save extra data not associated with any Chunk.  Not saved during autosave, only during world unload.  Currently
205
     * unused.
206
     */
207
    public void saveExtraData()
208
    {
209
        try
210
        {
211
            this.field_183014_e = true;
212
213
            while (true)
214
            {
215
                if (this.writeNextIO())
216
                {
217
                    continue;
218
                }
219
            }
220
        }
221
        finally
222
        {
223
            this.field_183014_e = false;
224
        }
225
    }
226
227
    /**
228
     * Writes the Chunk passed as an argument to the NBTTagCompound also passed, using the World argument to retrieve
229
     * the Chunk's last update time.
230
     */
231
    private void writeChunkToNBT(Chunk chunkIn, World worldIn, NBTTagCompound p_75820_3_)
232
    {
233
        p_75820_3_.setByte("V", (byte)1);
234
        p_75820_3_.setInteger("xPos", chunkIn.xPosition);
235
        p_75820_3_.setInteger("zPos", chunkIn.zPosition);
236
        p_75820_3_.setLong("LastUpdate", worldIn.getTotalWorldTime());
237
        p_75820_3_.setIntArray("HeightMap", chunkIn.getHeightMap());
238
        p_75820_3_.setBoolean("TerrainPopulated", chunkIn.isTerrainPopulated());
239
        p_75820_3_.setBoolean("LightPopulated", chunkIn.isLightPopulated());
240
        p_75820_3_.setLong("InhabitedTime", chunkIn.getInhabitedTime());
241
        ExtendedBlockStorage[] aextendedblockstorage = chunkIn.getBlockStorageArray();
242
        NBTTagList nbttaglist = new NBTTagList();
243
        boolean flag = !worldIn.provider.getHasNoSky();
244
245
        for (ExtendedBlockStorage extendedblockstorage : aextendedblockstorage)
246
        {
247
            if (extendedblockstorage != null)
248
            {
249
                NBTTagCompound nbttagcompound = new NBTTagCompound();
250
                nbttagcompound.setByte("Y", (byte)(extendedblockstorage.getYLocation() >> 4 & 255));
251
                byte[] abyte = new byte[extendedblockstorage.getData().length];
252
                NibbleArray nibblearray = new NibbleArray();
253
                NibbleArray nibblearray1 = null;
254
255
                for (int i = 0; i < extendedblockstorage.getData().length; ++i)
256
                {
257
                    char c0 = extendedblockstorage.getData()[i];
258
                    int j = i & 15;
259
                    int k = i >> 8 & 15;
260
                    int l = i >> 4 & 15;
261
262
                    if (c0 >> 12 != 0)
263
                    {
264
                        if (nibblearray1 == null)
265
                        {
266
                            nibblearray1 = new NibbleArray();
267
                        }
268
269
                        nibblearray1.set(j, k, l, c0 >> 12);
270
                    }
271
272
                    abyte[i] = (byte)(c0 >> 4 & 255);
273
                    nibblearray.set(j, k, l, c0 & 15);
274
                }
275
276
                nbttagcompound.setByteArray("Blocks", abyte);
277
                nbttagcompound.setByteArray("Data", nibblearray.getData());
278
279
                if (nibblearray1 != null)
280
                {
281
                    nbttagcompound.setByteArray("Add", nibblearray1.getData());
282
                }
283
284
                nbttagcompound.setByteArray("BlockLight", extendedblockstorage.getBlocklightArray().getData());
285
286
                if (flag)
287
                {
288
                    nbttagcompound.setByteArray("SkyLight", extendedblockstorage.getSkylightArray().getData());
289
                }
290
                else
291
                {
292
                    nbttagcompound.setByteArray("SkyLight", new byte[extendedblockstorage.getBlocklightArray().getData().length]);
293
                }
294
295
                nbttaglist.appendTag(nbttagcompound);
296
            }
297
        }
298
299
        p_75820_3_.setTag("Sections", nbttaglist);
300
        p_75820_3_.setByteArray("Biomes", chunkIn.getBiomeArray());
301
        chunkIn.setHasEntities(false);
302
        NBTTagList nbttaglist1 = new NBTTagList();
303
304
        for (int i1 = 0; i1 < chunkIn.getEntityLists().length; ++i1)
305
        {
306
            for (Entity entity : chunkIn.getEntityLists()[i1])
307
            {
308
                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
309
310
                if (entity.writeToNBTOptional(nbttagcompound1))
311
                {
312
                    chunkIn.setHasEntities(true);
313
                    nbttaglist1.appendTag(nbttagcompound1);
314
                }
315
            }
316
        }
317
318
        p_75820_3_.setTag("Entities", nbttaglist1);
319
        NBTTagList nbttaglist2 = new NBTTagList();
320
321
        for (TileEntity tileentity : chunkIn.getTileEntityMap().values())
322
        {
323
            NBTTagCompound nbttagcompound2 = new NBTTagCompound();
324
            tileentity.writeToNBT(nbttagcompound2);
325
            nbttaglist2.appendTag(nbttagcompound2);
326
        }
327
328
        p_75820_3_.setTag("TileEntities", nbttaglist2);
329
        List<NextTickListEntry> list = worldIn.getPendingBlockUpdates(chunkIn, false);
330
331
        if (list != null)
332
        {
333
            long j1 = worldIn.getTotalWorldTime();
334
            NBTTagList nbttaglist3 = new NBTTagList();
335
336
            for (NextTickListEntry nextticklistentry : list)
337
            {
338
                NBTTagCompound nbttagcompound3 = new NBTTagCompound();
339
                ResourceLocation resourcelocation = (ResourceLocation)Block.blockRegistry.getNameForObject(nextticklistentry.getBlock());
340
                nbttagcompound3.setString("i", resourcelocation == null ? "" : resourcelocation.toString());
341
                nbttagcompound3.setInteger("x", nextticklistentry.position.getX());
342
                nbttagcompound3.setInteger("y", nextticklistentry.position.getY());
343
                nbttagcompound3.setInteger("z", nextticklistentry.position.getZ());
344
                nbttagcompound3.setInteger("t", (int)(nextticklistentry.scheduledTime - j1));
345
                nbttagcompound3.setInteger("p", nextticklistentry.priority);
346
                nbttaglist3.appendTag(nbttagcompound3);
347
            }
348
349
            p_75820_3_.setTag("TileTicks", nbttaglist3);
350
        }
351
    }
352
353
    /**
354
     * Reads the data stored in the passed NBTTagCompound and creates a Chunk with that data in the passed World.
355
     * Returns the created Chunk.
356
     */
357
    private Chunk readChunkFromNBT(World worldIn, NBTTagCompound p_75823_2_)
358
    {
359
        int i = p_75823_2_.getInteger("xPos");
360
        int j = p_75823_2_.getInteger("zPos");
361
        Chunk chunk = new Chunk(worldIn, i, j);
362
        chunk.setHeightMap(p_75823_2_.getIntArray("HeightMap"));
363
        chunk.setTerrainPopulated(p_75823_2_.getBoolean("TerrainPopulated"));
364
        chunk.setLightPopulated(p_75823_2_.getBoolean("LightPopulated"));
365
        chunk.setInhabitedTime(p_75823_2_.getLong("InhabitedTime"));
366
        NBTTagList nbttaglist = p_75823_2_.getTagList("Sections", 10);
367
        int k = 16;
368
        ExtendedBlockStorage[] aextendedblockstorage = new ExtendedBlockStorage[k];
369
        boolean flag = !worldIn.provider.getHasNoSky();
370
371
        for (int l = 0; l < nbttaglist.tagCount(); ++l)
372
        {
373
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(l);
374
            int i1 = nbttagcompound.getByte("Y");
375
            ExtendedBlockStorage extendedblockstorage = new ExtendedBlockStorage(i1 << 4, flag);
376
            byte[] abyte = nbttagcompound.getByteArray("Blocks");
377
            NibbleArray nibblearray = new NibbleArray(nbttagcompound.getByteArray("Data"));
378
            NibbleArray nibblearray1 = nbttagcompound.hasKey("Add", 7) ? new NibbleArray(nbttagcompound.getByteArray("Add")) : null;
379
            char[] achar = new char[abyte.length];
380
381
            for (int j1 = 0; j1 < achar.length; ++j1)
382
            {
383
                int k1 = j1 & 15;
384
                int l1 = j1 >> 8 & 15;
385
                int i2 = j1 >> 4 & 15;
386
                int j2 = nibblearray1 != null ? nibblearray1.get(k1, l1, i2) : 0;
387
                achar[j1] = (char)(j2 << 12 | (abyte[j1] & 255) << 4 | nibblearray.get(k1, l1, i2));
388
            }
389
390
            extendedblockstorage.setData(achar);
391
            extendedblockstorage.setBlocklightArray(new NibbleArray(nbttagcompound.getByteArray("BlockLight")));
392
393
            if (flag)
394
            {
395
                extendedblockstorage.setSkylightArray(new NibbleArray(nbttagcompound.getByteArray("SkyLight")));
396
            }
397
398
            extendedblockstorage.removeInvalidBlocks();
399
            aextendedblockstorage[i1] = extendedblockstorage;
400
        }
401
402
        chunk.setStorageArrays(aextendedblockstorage);
403
404
        if (p_75823_2_.hasKey("Biomes", 7))
405
        {
406
            chunk.setBiomeArray(p_75823_2_.getByteArray("Biomes"));
407
        }
408
409
        NBTTagList nbttaglist1 = p_75823_2_.getTagList("Entities", 10);
410
411
        if (nbttaglist1 != null)
412
        {
413
            for (int k2 = 0; k2 < nbttaglist1.tagCount(); ++k2)
414
            {
415
                NBTTagCompound nbttagcompound1 = nbttaglist1.getCompoundTagAt(k2);
416
                Entity entity = EntityList.createEntityFromNBT(nbttagcompound1, worldIn);
417
                chunk.setHasEntities(true);
418
419
                if (entity != null)
420
                {
421
                    chunk.addEntity(entity);
422
                    Entity entity1 = entity;
423
424
                    for (NBTTagCompound nbttagcompound4 = nbttagcompound1; nbttagcompound4.hasKey("Riding", 10); nbttagcompound4 = nbttagcompound4.getCompoundTag("Riding"))
425
                    {
426
                        Entity entity2 = EntityList.createEntityFromNBT(nbttagcompound4.getCompoundTag("Riding"), worldIn);
427
428
                        if (entity2 != null)
429
                        {
430
                            chunk.addEntity(entity2);
431
                            entity1.mountEntity(entity2);
432
                        }
433
434
                        entity1 = entity2;
435
                    }
436
                }
437
            }
438
        }
439
440
        NBTTagList nbttaglist2 = p_75823_2_.getTagList("TileEntities", 10);
441
442
        if (nbttaglist2 != null)
443
        {
444
            for (int l2 = 0; l2 < nbttaglist2.tagCount(); ++l2)
445
            {
446
                NBTTagCompound nbttagcompound2 = nbttaglist2.getCompoundTagAt(l2);
447
                TileEntity tileentity = TileEntity.createAndLoadEntity(nbttagcompound2);
448
449
                if (tileentity != null)
450
                {
451
                    chunk.addTileEntity(tileentity);
452
                }
453
            }
454
        }
455
456
        if (p_75823_2_.hasKey("TileTicks", 9))
457
        {
458
            NBTTagList nbttaglist3 = p_75823_2_.getTagList("TileTicks", 10);
459
460
            if (nbttaglist3 != null)
461
            {
462
                for (int i3 = 0; i3 < nbttaglist3.tagCount(); ++i3)
463
                {
464
                    NBTTagCompound nbttagcompound3 = nbttaglist3.getCompoundTagAt(i3);
465
                    Block block;
466
467
                    if (nbttagcompound3.hasKey("i", 8))
468
                    {
469
                        block = Block.getBlockFromName(nbttagcompound3.getString("i"));
470
                    }
471
                    else
472
                    {
473
                        block = Block.getBlockById(nbttagcompound3.getInteger("i"));
474
                    }
475
476
                    worldIn.scheduleBlockUpdate(new BlockPos(nbttagcompound3.getInteger("x"), nbttagcompound3.getInteger("y"), nbttagcompound3.getInteger("z")), block, nbttagcompound3.getInteger("t"), nbttagcompound3.getInteger("p"));
477
                }
478
            }
479
        }
480
481
        return chunk;
482
    }
483
}
484

Deobfuscated MCP 1.8.9 source of AnvilChunkLoader from Marcelektro/MavenMCP-1.8.9 (net/minecraft/world/chunk/storage/AnvilChunkLoader.java), stored verbatim.