Search in sources :

Example 1 with UnknownEntityTypeException

use of net.glowstone.io.entity.UnknownEntityTypeException in project Glowstone by GlowstoneMC.

the class AnvilChunkIoService method read.

@Override
public boolean read(GlowChunk chunk) throws IOException {
    int x = chunk.getX();
    int z = chunk.getZ();
    RegionFile region = cache.getRegionFile(x, z);
    int regionX = x & REGION_SIZE - 1;
    int regionZ = z & REGION_SIZE - 1;
    if (!region.hasChunk(regionX, regionZ)) {
        return false;
    }
    DataInputStream in = region.getChunkDataInputStream(regionX, regionZ);
    CompoundTag levelTag;
    try (NbtInputStream nbt = new NbtInputStream(in, false)) {
        CompoundTag root = nbt.readCompound();
        // NON-NLS
        levelTag = root.getCompound("Level");
    }
    // read the vertical sections
    // NON-NLS
    List<CompoundTag> sectionList = levelTag.getCompoundList("Sections");
    ChunkSection[] sections = new ChunkSection[GlowChunk.SEC_COUNT];
    for (CompoundTag sectionTag : sectionList) {
        // NON-NLS
        int y = sectionTag.getByte("Y");
        if (y < 0 || y > GlowChunk.SEC_COUNT) {
            ConsoleMessages.Warn.Chunk.SECTION_OOB.log(y, chunk);
            continue;
        }
        if (sections[y] != null) {
            ConsoleMessages.Warn.Chunk.SECTION_DUP.log(y, chunk);
            continue;
        }
        sections[y] = ChunkSection.fromNbt(sectionTag);
    }
    // initialize the chunk
    chunk.initializeSections(sections);
    // NON-NLS
    chunk.setPopulated(levelTag.getBoolean("TerrainPopulated", false));
    levelTag.readLong("InhabitedTime", chunk::setInhabitedTime);
    // read biomes
    // NON-NLS
    levelTag.readByteArray("Biomes", chunk::setBiomes);
    // read height map
    if (!levelTag.readIntArray("HeightMap", chunk::setHeightMap)) {
        // NON-NLS
        chunk.automaticHeightMap();
    }
    // read slime chunk
    // NON-NLS
    levelTag.readByte("isSlimeChunk", chunk::setIsSlimeChunk);
    // read entities
    levelTag.iterateCompoundList("Entities", entityTag -> {
        // NON-NLS
        try {
            // note that creating the entity is sufficient to add it to the world
            EntityStorage.loadEntity(chunk.getWorld(), entityTag);
        } catch (UnknownEntityTypeException e) {
            ConsoleMessages.Warn.Entity.UNKNOWN.log(chunk, e.getIdOrTag());
        } catch (Exception e) {
            ConsoleMessages.Warn.Entity.LOAD_FAILED.log(e, chunk);
        }
    });
    // read block entities
    // NON-NLS
    List<CompoundTag> storedBlockEntities = levelTag.getCompoundList("TileEntities");
    BlockEntity blockEntity;
    for (CompoundTag blockEntityTag : storedBlockEntities) {
        // NON-NLS
        int tx = blockEntityTag.getInt("x");
        // NON-NLS
        int ty = blockEntityTag.getInt("y");
        // NON-NLS
        int tz = blockEntityTag.getInt("z");
        blockEntity = chunk.createEntity(tx & 0xf, ty, tz & 0xf, chunk.getType(tx & 0xf, tz & 0xf, ty));
        if (blockEntity != null) {
            try {
                blockEntity.loadNbt(blockEntityTag);
            } catch (Exception ex) {
                // NON-NLS
                String id = blockEntityTag.tryGetString("id").orElse("<missing>");
                ConsoleMessages.Error.BlockEntity.LOAD_FAILED.log(ex, blockEntity.getBlock(), id);
            }
        } else {
            String id = // NON-NLS
            blockEntityTag.tryGetString("id").orElse("<missing>");
            ConsoleMessages.Warn.BlockEntity.UNKNOWN.log(chunk.getWorld().getName(), tx, ty, tz, id);
        }
    }
    levelTag.iterateCompoundList("TileTicks", tileTick -> {
        // NON-NLS
        // NON-NLS
        int tileX = tileTick.getInt("x");
        // NON-NLS
        int tileY = tileTick.getInt("y");
        // NON-NLS
        int tileZ = tileTick.getInt("z");
        // NON-NLS
        String id = tileTick.getString("i");
        Material material = ItemIds.getBlock(id);
        if (material == null) {
            ConsoleMessages.Warn.Chunk.UNKNOWN_BLOCK_TO_TICK.log(id);
            return;
        }
        GlowBlock block = chunk.getBlock(tileX, tileY, tileZ);
        if (material != block.getType()) {
            return;
        }
        // TODO tick delay: tileTick.getInt("t");
        // TODO ordering: tileTick.getInt("p");
        BlockType type = ItemTable.instance().getBlock(material);
        if (type == null) {
            return;
        }
        block.getWorld().requestPulse(block);
    });
    return true;
}
Also used : UnknownEntityTypeException(net.glowstone.io.entity.UnknownEntityTypeException) Material(org.bukkit.Material) DataInputStream(java.io.DataInputStream) NbtInputStream(net.glowstone.util.nbt.NbtInputStream) UnknownEntityTypeException(net.glowstone.io.entity.UnknownEntityTypeException) IOException(java.io.IOException) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) ChunkSection(net.glowstone.chunk.ChunkSection) CompoundTag(net.glowstone.util.nbt.CompoundTag) BlockEntity(net.glowstone.block.entity.BlockEntity)

Aggregations

DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 GlowBlock (net.glowstone.block.GlowBlock)1 BlockType (net.glowstone.block.blocktype.BlockType)1 BlockEntity (net.glowstone.block.entity.BlockEntity)1 ChunkSection (net.glowstone.chunk.ChunkSection)1 UnknownEntityTypeException (net.glowstone.io.entity.UnknownEntityTypeException)1 CompoundTag (net.glowstone.util.nbt.CompoundTag)1 NbtInputStream (net.glowstone.util.nbt.NbtInputStream)1 Material (org.bukkit.Material)1