Search in sources :

Example 16 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class AnvilChunkIoService method read.

/**
     * Reads a chunk from its region file.
     *
     * @param chunk The GlowChunk to read into.
     * @return Whether the
     * @throws IOException if an I/O error occurs.
     */
@Override
public boolean read(GlowChunk chunk) throws IOException {
    int x = chunk.getX(), 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();
        levelTag = root.getCompound("Level");
    }
    // read the vertical sections
    List<CompoundTag> sectionList = levelTag.getCompoundList("Sections");
    ChunkSection[] sections = new ChunkSection[GlowChunk.SEC_COUNT];
    for (CompoundTag sectionTag : sectionList) {
        int y = sectionTag.getByte("Y");
        if (sections[y] != null) {
            GlowServer.logger.log(Level.WARNING, "Multiple chunk sections at y " + y + " in " + chunk + "!");
            continue;
        }
        if (y < 0 || y > GlowChunk.SEC_COUNT) {
            GlowServer.logger.log(Level.WARNING, "Out of bounds chunk section at y " + y + " in " + chunk + "!");
            continue;
        }
        sections[y] = ChunkSection.fromNBT(sectionTag);
    }
    // initialize the chunk
    chunk.initializeSections(sections);
    chunk.setPopulated(levelTag.getBool("TerrainPopulated"));
    // read biomes
    if (levelTag.isByteArray("Biomes")) {
        chunk.setBiomes(levelTag.getByteArray("Biomes"));
    }
    // read height map
    if (levelTag.isIntArray("HeightMap")) {
        chunk.setHeightMap(levelTag.getIntArray("HeightMap"));
    } else {
        chunk.automaticHeightMap();
    }
    // read entities
    if (levelTag.isList("Entities", TagType.COMPOUND)) {
        for (CompoundTag entityTag : levelTag.getCompoundList("Entities")) {
            try {
                // note that creating the entity is sufficient to add it to the world
                EntityStorage.loadEntity(chunk.getWorld(), entityTag);
            } catch (Exception e) {
                String id = entityTag.isString("id") ? entityTag.getString("id") : "<missing>";
                if (e.getMessage() != null && e.getMessage().startsWith("Unknown entity type to load:")) {
                    GlowServer.logger.warning("Unknown entity in " + chunk + ": " + id);
                } else {
                    GlowServer.logger.log(Level.WARNING, "Error loading entity in " + chunk + ": " + id, e);
                }
            }
        }
    }
    // read block entities
    List<CompoundTag> storedBlockEntities = levelTag.getCompoundList("TileEntities");
    BlockEntity blockEntity;
    for (CompoundTag blockEntityTag : storedBlockEntities) {
        int tx = blockEntityTag.getInt("x");
        int ty = blockEntityTag.getInt("y");
        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) {
                String id = blockEntityTag.isString("id") ? blockEntityTag.getString("id") : "<missing>";
                GlowServer.logger.log(Level.SEVERE, "Error loading block entity at " + blockEntity.getBlock() + ": " + id, ex);
            }
        } else {
            String id = blockEntityTag.isString("id") ? blockEntityTag.getString("id") : "<missing>";
            GlowServer.logger.warning("Unknown block entity at " + chunk.getWorld().getName() + "," + tx + "," + ty + "," + tz + ": " + id);
        }
    }
    if (levelTag.isList("TileTicks", TagType.COMPOUND)) {
        List<CompoundTag> tileTicks = levelTag.getCompoundList("TileTicks");
        for (CompoundTag tileTick : tileTicks) {
            int tileX = tileTick.getInt("x");
            int tileY = tileTick.getInt("y");
            int tileZ = tileTick.getInt("z");
            String id = tileTick.getString("i");
            if (id.startsWith("minecraft:")) {
                id = id.replace("minecraft:", "");
                if (id.startsWith("flowing_")) {
                    id = id.replace("flowing_", "");
                } else if (id.equals("water") || id.equals("lava")) {
                    id = "stationary_" + id;
                }
            }
            Material material = Material.getMaterial(id.toUpperCase());
            GlowBlock block = chunk.getBlock(tileX, tileY, tileZ);
            if (material != block.getType()) {
                continue;
            }
            // TODO tick delay: tileTick.getInt("t");
            // TODO ordering: tileTick.getInt("p");
            BlockType type = ItemTable.instance().getBlock(material);
            if (type == null) {
                continue;
            }
            block.getWorld().requestPulse(block);
        }
    }
    return true;
}
Also used : Material(org.bukkit.Material) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException) GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) NBTInputStream(net.glowstone.util.nbt.NBTInputStream) ChunkSection(net.glowstone.chunk.ChunkSection) CompoundTag(net.glowstone.util.nbt.CompoundTag) BlockEntity(net.glowstone.block.entity.BlockEntity)

Example 17 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class PlayerStore method save.

@Override
public void save(GlowPlayer entity, CompoundTag tag) {
    super.save(entity, tag);
    // players have no id tag
    tag.remove("id");
    // experience
    tag.putInt("XpLevel", entity.getLevel());
    tag.putFloat("XpP", entity.getExp());
    tag.putInt("XpTotal", entity.getTotalExperience());
    // food
    tag.putInt("foodLevel", entity.getFoodLevel());
    tag.putFloat("foodSaturationLevel", entity.getSaturation());
    tag.putFloat("foodExhaustionLevel", entity.getExhaustion());
    // spawn location
    Location bed = entity.getBedSpawnLocation();
    if (bed != null) {
        tag.putInt("SpawnX", bed.getBlockX());
        tag.putInt("SpawnY", bed.getBlockY());
        tag.putInt("SpawnZ", bed.getBlockZ());
        tag.putBool("SpawnForced", entity.isBedSpawnForced());
    }
    // abilities
    CompoundTag abilities = new CompoundTag();
    abilities.putFloat("walkSpeed", entity.getWalkSpeed() / 2f);
    abilities.putFloat("flySpeed", entity.getFlySpeed() / 2f);
    abilities.putBool("mayfly", entity.getAllowFlight());
    abilities.putBool("flying", entity.isFlying());
    // for now, base these on the game mode value
    abilities.putBool("invulnerable", entity.getGameMode() == GameMode.CREATIVE);
    abilities.putBool("mayBuild", entity.getGameMode() != GameMode.ADVENTURE);
    abilities.putBool("instabuild", entity.getGameMode() == GameMode.CREATIVE);
    tag.putCompound("abilities", abilities);
    // bukkit
    CompoundTag bukkit = new CompoundTag();
    bukkit.putLong("firstPlayed", entity.getFirstPlayed() == 0 ? entity.getJoinTime() : entity.getFirstPlayed());
    bukkit.putLong("lastPlayed", entity.getJoinTime());
    bukkit.putString("lastKnownName", entity.getName());
    tag.putCompound("bukkit", bukkit);
}
Also used : CompoundTag(net.glowstone.util.nbt.CompoundTag) Location(org.bukkit.Location)

Example 18 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class NbtPlayerDataService method writeData.

@Override
public void writeData(GlowPlayer player) {
    File playerFile = getPlayerFile(player.getUniqueId());
    CompoundTag tag = new CompoundTag();
    EntityStorage.save(player, tag);
    try (NBTOutputStream out = new NBTOutputStream(new FileOutputStream(playerFile))) {
        out.writeTag(tag);
    } catch (IOException e) {
        player.kickPlayer("Failed to save player data!");
        server.getLogger().log(Level.SEVERE, "Failed to write data for " + player.getName() + ": " + playerFile, e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) NBTOutputStream(net.glowstone.util.nbt.NBTOutputStream) CompoundTag(net.glowstone.util.nbt.CompoundTag)

Example 19 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class NbtPlayerDataService method readData.

@Override
public void readData(GlowPlayer player) {
    File playerFile = getPlayerFile(player.getUniqueId());
    CompoundTag playerTag = new CompoundTag();
    if (playerFile.exists()) {
        try (NBTInputStream in = new NBTInputStream(new FileInputStream(playerFile))) {
            playerTag = in.readCompound();
        } catch (IOException e) {
            player.kickPlayer("Failed to read player data!");
            server.getLogger().log(Level.SEVERE, "Failed to read data for " + player.getName() + ": " + playerFile, e);
        }
    }
    readDataImpl(player, playerTag);
}
Also used : NBTInputStream(net.glowstone.util.nbt.NBTInputStream) IOException(java.io.IOException) File(java.io.File) CompoundTag(net.glowstone.util.nbt.CompoundTag) FileInputStream(java.io.FileInputStream)

Example 20 with CompoundTag

use of net.glowstone.util.nbt.CompoundTag in project Glowstone by GlowstoneMC.

the class NbtStructureDataService method readStructuresData.

@Override
public Map<Integer, GlowStructure> readStructuresData() {
    Map<Integer, GlowStructure> structures = new HashMap<>();
    for (StructureStore<?> store : StructureStorage.getStructureStores()) {
        File structureFile = new File(structureDir, store.getId() + ".dat");
        if (structureFile.exists()) {
            try (NBTInputStream in = new NBTInputStream(new FileInputStream(structureFile))) {
                CompoundTag data = new CompoundTag();
                data = in.readCompound();
                if (data.isCompound("data")) {
                    data = data.getCompound("data");
                    if (data.isCompound("Features")) {
                        CompoundTag features = data.getCompound("Features");
                        features.getValue().keySet().stream().filter(features::isCompound).forEach(key -> {
                            GlowStructure structure = StructureStorage.loadStructure(world, features.getCompound(key));
                            structures.put(new Key(structure.getChunkX(), structure.getChunkZ()).hashCode(), structure);
                        });
                    }
                } else {
                    server.getLogger().log(Level.SEVERE, "No data tag in " + structureFile);
                }
            } catch (IOException e) {
                server.getLogger().log(Level.SEVERE, "Failed to read structure data from " + structureFile, e);
            }
        }
    }
    return structures;
}
Also used : GlowStructure(net.glowstone.generator.structures.GlowStructure) HashMap(java.util.HashMap) NBTInputStream(net.glowstone.util.nbt.NBTInputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) CompoundTag(net.glowstone.util.nbt.CompoundTag) Key(net.glowstone.chunk.GlowChunk.Key)

Aggregations

CompoundTag (net.glowstone.util.nbt.CompoundTag)58 ArrayList (java.util.ArrayList)9 NBTInputStream (net.glowstone.util.nbt.NBTInputStream)7 Location (org.bukkit.Location)7 IOException (java.io.IOException)6 NBTOutputStream (net.glowstone.util.nbt.NBTOutputStream)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)3 BlockEntity (net.glowstone.block.entity.BlockEntity)3 GlowEntity (net.glowstone.entity.GlowEntity)3 ByteBuf (io.netty.buffer.ByteBuf)2 FileOutputStream (java.io.FileOutputStream)2 UUID (java.util.UUID)2 ChunkSection (net.glowstone.chunk.ChunkSection)2 AttributeManager (net.glowstone.entity.AttributeManager)2 Modifier (net.glowstone.entity.AttributeManager.Modifier)2 GlowStructure (net.glowstone.generator.structures.GlowStructure)2 StructureBoundingBox (net.glowstone.generator.structures.util.StructureBoundingBox)2 MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)2 Material (org.bukkit.Material)2