Search in sources :

Example 41 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class Chunk method fromBinary.

public static Chunk fromBinary(byte[] data, LevelProvider provider) {
    try {
        int chunkX = Binary.readLInt(new byte[] { data[0], data[1], data[2], data[3] });
        int chunkZ = Binary.readLInt(new byte[] { data[4], data[5], data[6], data[7] });
        byte[] chunkData = Binary.subBytes(data, 8, data.length - 1);
        int flags = data[data.length - 1];
        List<CompoundTag> entities = new ArrayList<>();
        List<CompoundTag> tiles = new ArrayList<>();
        Map<Integer, Integer> extraDataMap = new HashMap<>();
        if (provider instanceof LevelDB) {
            byte[] entityData = ((LevelDB) provider).getDatabase().get(EntitiesKey.create(chunkX, chunkZ).toArray());
            if (entityData != null && entityData.length > 0) {
                try (NBTInputStream nbtInputStream = new NBTInputStream(new ByteArrayInputStream(entityData), ByteOrder.LITTLE_ENDIAN)) {
                    while (nbtInputStream.available() > 0) {
                        Tag tag = Tag.readNamedTag(nbtInputStream);
                        if (!(tag instanceof CompoundTag)) {
                            throw new IOException("Root tag must be a named compound tag");
                        }
                        entities.add((CompoundTag) tag);
                    }
                }
            }
            byte[] tileData = ((LevelDB) provider).getDatabase().get(TilesKey.create(chunkX, chunkZ).toArray());
            if (tileData != null && tileData.length > 0) {
                try (NBTInputStream nbtInputStream = new NBTInputStream(new ByteArrayInputStream(tileData), ByteOrder.LITTLE_ENDIAN)) {
                    while (nbtInputStream.available() > 0) {
                        Tag tag = Tag.readNamedTag(nbtInputStream);
                        if (!(tag instanceof CompoundTag)) {
                            throw new IOException("Root tag must be a named compound tag");
                        }
                        tiles.add((CompoundTag) tag);
                    }
                }
            }
            byte[] extraData = ((LevelDB) provider).getDatabase().get(ExtraDataKey.create(chunkX, chunkZ).toArray());
            if (extraData != null && extraData.length > 0) {
                BinaryStream stream = new BinaryStream(tileData);
                int count = stream.getInt();
                for (int i = 0; i < count; ++i) {
                    int key = stream.getInt();
                    int value = stream.getShort();
                    extraDataMap.put(key, value);
                }
            }
            /*if (!entities.isEmpty() || !blockEntities.isEmpty()) {
                    CompoundTag ct = new CompoundTag();
                    ListTag<CompoundTag> entityList = new ListTag<>("entities");
                    ListTag<CompoundTag> tileList = new ListTag<>("blockEntities");

                    entityList.list = entities;
                    tileList.list = blockEntities;
                    ct.putList(entityList);
                    ct.putList(tileList);
                    NBTIO.write(ct, new File(Nukkit.DATA_PATH + chunkX + "_" + chunkZ + ".dat"));
                }*/
            Chunk chunk = new Chunk(provider, chunkX, chunkZ, chunkData, entities, tiles, extraDataMap);
            if ((flags & 0x01) > 0) {
                chunk.setGenerated();
            }
            if ((flags & 0x02) > 0) {
                chunk.setPopulated();
            }
            if ((flags & 0x04) > 0) {
                chunk.setLightPopulated();
            }
            return chunk;
        }
    } catch (Exception e) {
        Server.getInstance().getLogger().logException(e);
    }
    return null;
}
Also used : BinaryStream(cn.nukkit.utils.BinaryStream) IOException(java.io.IOException) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) NBTInputStream(cn.nukkit.nbt.stream.NBTInputStream) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 42 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class LevelDB method generate.

public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/db").exists()) {
        new File(path + "/db").mkdirs();
    }
    CompoundTag levelData = new CompoundTag("").putLong("currentTick", 0).putInt("DayCycleStopTime", -1).putInt("GameType", 0).putInt("Generator", Generator.getGeneratorType(generator)).putBoolean("hasBeenLoadedInCreative", false).putLong("LastPlayed", System.currentTimeMillis() / 1000).putString("LevelName", name).putFloat("lightningLevel", 0).putInt("lightningTime", new Random().nextInt()).putInt("limitedWorldOriginX", 128).putInt("limitedWorldOriginY", 70).putInt("limitedWorldOriginZ", 128).putInt("Platform", 0).putFloat("rainLevel", 0).putInt("rainTime", new Random().nextInt()).putLong("RandomSeed", seed).putByte("spawnMobs", 0).putInt("SpawnX", 128).putInt("SpawnY", 70).putInt("SpawnZ", 128).putInt("storageVersion", 4).putLong("Time", 0).putLong("worldStartCount", ((long) Integer.MAX_VALUE) & 0xffffffffL);
    byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(Binary.writeLInt(3));
    outputStream.write(Binary.writeLInt(data.length));
    outputStream.write(data);
    Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));
    DB db = Iq80DBFactory.factory.open(new File(path + "/db"), new Options().createIfMissing(true));
    db.close();
}
Also used : Options(org.iq80.leveldb.Options) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) DB(org.iq80.leveldb.DB)

Example 43 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class LevelDB method requestChunkTask.

@Override
public AsyncTask requestChunkTask(int x, int z) {
    Chunk chunk = this.getChunk(x, z, false);
    if (chunk == null) {
        throw new ChunkException("Invalid Chunk sent");
    }
    long timestamp = chunk.getChanges();
    byte[] tiles = new byte[0];
    if (!chunk.getBlockEntities().isEmpty()) {
        List<CompoundTag> tagList = new ArrayList<>();
        for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
            if (blockEntity instanceof BlockEntitySpawnable) {
                tagList.add(((BlockEntitySpawnable) blockEntity).getSpawnCompound());
            }
        }
        try {
            tiles = NBTIO.write(tagList, ByteOrder.LITTLE_ENDIAN);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    Map<Integer, Integer> extra = chunk.getBlockExtraDataArray();
    BinaryStream extraData;
    if (!extra.isEmpty()) {
        extraData = new BinaryStream();
        extraData.putLInt(extra.size());
        for (Integer key : extra.values()) {
            extraData.putLInt(key);
            extraData.putLShort(extra.get(key));
        }
    } else {
        extraData = null;
    }
    BinaryStream stream = new BinaryStream();
    stream.put(chunk.getBlockIdArray());
    stream.put(chunk.getBlockDataArray());
    stream.put(chunk.getBlockSkyLightArray());
    stream.put(chunk.getBlockLightArray());
    stream.put(chunk.getHeightMapArray());
    for (int color : chunk.getBiomeColorArray()) {
        stream.put(Binary.writeInt(color));
    }
    if (extraData != null) {
        stream.put(extraData.getBuffer());
    } else {
        stream.putLInt(0);
    }
    stream.put(tiles);
    this.getLevel().chunkRequestCallback(timestamp, x, z, stream.getBuffer());
    return null;
}
Also used : BlockEntitySpawnable(cn.nukkit.blockentity.BlockEntitySpawnable) BaseFullChunk(cn.nukkit.level.format.generic.BaseFullChunk) FullChunk(cn.nukkit.level.format.FullChunk) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 44 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class ProjectileItem method onClickAir.

public boolean onClickAir(Player player, Vector3 directionVector) {
    CompoundTag nbt = new CompoundTag().putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", player.x)).add(new DoubleTag("", player.y + player.getEyeHeight())).add(new DoubleTag("", player.z))).putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", directionVector.x)).add(new DoubleTag("", directionVector.y)).add(new DoubleTag("", directionVector.z))).putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("", (float) player.yaw)).add(new FloatTag("", (float) player.pitch)));
    this.correctNBT(nbt);
    Entity projectile = Entity.createEntity(this.getProjectileEntityType(), player.getLevel().getChunk(player.getFloorX() >> 4, player.getFloorZ() >> 4), nbt, player);
    if (projectile != null) {
        projectile.setMotion(projectile.getMotion().multiply(this.getThrowForce()));
        this.count--;
        if (projectile instanceof EntityProjectile) {
            ProjectileLaunchEvent ev = new ProjectileLaunchEvent((EntityProjectile) projectile);
            player.getServer().getPluginManager().callEvent(ev);
            if (ev.isCancelled()) {
                projectile.kill();
            } else {
                projectile.spawnToAll();
                player.getLevel().addSound(player, Sound.RANDOM_BOW, 1, 1, player.getViewers().values());
            }
        } else {
            projectile.spawnToAll();
        }
    } else {
        return false;
    }
    return true;
}
Also used : Entity(cn.nukkit.entity.Entity) FloatTag(cn.nukkit.nbt.tag.FloatTag) ProjectileLaunchEvent(cn.nukkit.event.entity.ProjectileLaunchEvent) DoubleTag(cn.nukkit.nbt.tag.DoubleTag) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) EntityProjectile(cn.nukkit.entity.projectile.EntityProjectile)

Example 45 with CompoundTag

use of cn.nukkit.nbt.tag.CompoundTag in project Nukkit by Nukkit.

the class BlockEntityChest method initBlockEntity.

@Override
protected void initBlockEntity() {
    this.inventory = new ChestInventory(this);
    if (!this.namedTag.contains("Items") || !(this.namedTag.get("Items") instanceof ListTag)) {
        this.namedTag.putList(new ListTag<CompoundTag>("Items"));
    }
    /* for (int i = 0; i < this.getSize(); i++) {
            this.inventory.setItem(i, this.getItem(i));
        } */
    ListTag<CompoundTag> list = (ListTag<CompoundTag>) this.namedTag.getList("Items");
    for (CompoundTag compound : list.getAll()) {
        Item item = NBTIO.getItemHelper(compound);
        this.inventory.slots.put(compound.getByte("Slot"), item);
    }
    super.initBlockEntity();
}
Also used : Item(cn.nukkit.item.Item) ChestInventory(cn.nukkit.inventory.ChestInventory) DoubleChestInventory(cn.nukkit.inventory.DoubleChestInventory) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Aggregations

CompoundTag (cn.nukkit.nbt.tag.CompoundTag)74 ListTag (cn.nukkit.nbt.tag.ListTag)28 BlockEntity (cn.nukkit.blockentity.BlockEntity)13 DoubleTag (cn.nukkit.nbt.tag.DoubleTag)13 FloatTag (cn.nukkit.nbt.tag.FloatTag)13 Tag (cn.nukkit.nbt.tag.Tag)13 StringTag (cn.nukkit.nbt.tag.StringTag)12 Map (java.util.Map)8 Entity (cn.nukkit.entity.Entity)7 IOException (java.io.IOException)7 Player (cn.nukkit.Player)5 BinaryStream (cn.nukkit.utils.BinaryStream)5 ArrayList (java.util.ArrayList)5 BlockRail (cn.nukkit.block.BlockRail)4 FullChunk (cn.nukkit.level.format.FullChunk)4 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)4 Rail (cn.nukkit.utils.Rail)4 File (java.io.File)4 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)3 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)3