Search in sources :

Example 11 with ListTag

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

the class ItemSpawnEgg method onActivate.

@Override
public boolean onActivate(Level level, Player player, Block block, Block target, BlockFace face, double fx, double fy, double fz) {
    FullChunk chunk = level.getChunk((int) block.getX() >> 4, (int) block.getZ() >> 4);
    if (chunk == null) {
        return false;
    }
    CompoundTag nbt = new CompoundTag().putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", block.getX() + 0.5)).add(new DoubleTag("", block.getY())).add(new DoubleTag("", block.getZ() + 0.5))).putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", 0)).add(new DoubleTag("", 0)).add(new DoubleTag("", 0))).putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("", new Random().nextFloat() * 360)).add(new FloatTag("", 0)));
    if (this.hasCustomName()) {
        nbt.putString("CustomName", this.getCustomName());
    }
    Entity entity = Entity.createEntity(this.meta, chunk, nbt);
    if (entity != null) {
        if (player.isSurvival()) {
            Item item = player.getInventory().getItemInHand();
            item.setCount(item.getCount() - 1);
            player.getInventory().setItemInHand(item);
        }
        entity.spawnToAll();
        return true;
    }
    return false;
}
Also used : Entity(cn.nukkit.entity.Entity) FullChunk(cn.nukkit.level.format.FullChunk) FloatTag(cn.nukkit.nbt.tag.FloatTag) Random(java.util.Random) DoubleTag(cn.nukkit.nbt.tag.DoubleTag) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 12 with ListTag

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

the class Chunk method toBinary.

@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();
    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());
    if (this.isGenerated()) {
        nbt.putByteArray("Blocks", this.getBlockIdArray());
        nbt.putByteArray("Data", this.getBlockDataArray());
        nbt.putByteArray("SkyLight", this.getBlockSkyLightArray());
        nbt.putByteArray("BlockLight", this.getBlockLightArray());
        nbt.putIntArray("BiomeColors", this.getBiomeColorArray());
        int[] heightInts = new int[256];
        byte[] heightBytes = this.getHeightMapArray();
        for (int i = 0; i < heightInts.length; i++) {
            heightInts[i] = heightBytes[i] & 0xFF;
        }
        nbt.putIntArray("HeightMap", heightInts);
    }
    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);
    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);
    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }
    nbt.putByteArray("ExtraData", extraData.getBuffer());
    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);
    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) Player(cn.nukkit.Player) ArrayList(java.util.ArrayList) BinaryStream(cn.nukkit.utils.BinaryStream) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 13 with ListTag

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

the class BaseFullChunk method initChunk.

public void initChunk() {
    if (this.getProvider() != null && !this.isInit) {
        boolean changed = false;
        if (this.NBTentities != null) {
            this.getProvider().getLevel().timings.syncChunkLoadEntitiesTimer.startTiming();
            for (CompoundTag nbt : NBTentities) {
                if (!nbt.contains("id")) {
                    this.setChanged();
                    continue;
                }
                ListTag pos = nbt.getList("Pos");
                if ((((NumberTag) pos.get(0)).getData().intValue() >> 4) != this.getX() || ((((NumberTag) pos.get(2)).getData().intValue() >> 4) != this.getZ())) {
                    changed = true;
                    continue;
                }
                Entity entity = Entity.createEntity(nbt.getString("id"), this, nbt);
                if (entity != null) {
                    changed = true;
                    continue;
                }
            }
            this.getProvider().getLevel().timings.syncChunkLoadEntitiesTimer.stopTiming();
            this.NBTentities = null;
        }
        if (this.NBTtiles != null) {
            this.getProvider().getLevel().timings.syncChunkLoadBlockEntitiesTimer.startTiming();
            for (CompoundTag nbt : NBTtiles) {
                if (nbt != null) {
                    if (!nbt.contains("id")) {
                        changed = true;
                        continue;
                    }
                    if ((nbt.getInt("x") >> 4) != this.getX() || ((nbt.getInt("z") >> 4) != this.getZ())) {
                        changed = true;
                        continue;
                    }
                    BlockEntity blockEntity = BlockEntity.createBlockEntity(nbt.getString("id"), this, nbt);
                    if (blockEntity == null) {
                        changed = true;
                        continue;
                    }
                }
            }
            this.getProvider().getLevel().timings.syncChunkLoadBlockEntitiesTimer.stopTiming();
            this.NBTtiles = null;
        }
        this.setChanged(changed);
        this.isInit = true;
    }
}
Also used : Entity(cn.nukkit.entity.Entity) BlockEntity(cn.nukkit.blockentity.BlockEntity) NumberTag(cn.nukkit.nbt.tag.NumberTag) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 14 with ListTag

use of cn.nukkit.nbt.tag.ListTag 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 15 with ListTag

use of cn.nukkit.nbt.tag.ListTag 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)27 ListTag (cn.nukkit.nbt.tag.ListTag)27 DoubleTag (cn.nukkit.nbt.tag.DoubleTag)13 FloatTag (cn.nukkit.nbt.tag.FloatTag)13 Entity (cn.nukkit.entity.Entity)5 StringTag (cn.nukkit.nbt.tag.StringTag)5 Tag (cn.nukkit.nbt.tag.Tag)5 BlockRail (cn.nukkit.block.BlockRail)4 BlockEntity (cn.nukkit.blockentity.BlockEntity)4 Rail (cn.nukkit.utils.Rail)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 Player (cn.nukkit.Player)2 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)2 EntityProjectile (cn.nukkit.entity.projectile.EntityProjectile)2 ProjectileLaunchEvent (cn.nukkit.event.entity.ProjectileLaunchEvent)2 FullChunk (cn.nukkit.level.format.FullChunk)2 Random (java.util.Random)2 BlockAir (cn.nukkit.block.BlockAir)1 BlockEntityBrewingStand (cn.nukkit.blockentity.BlockEntityBrewingStand)1