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;
}
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);
}
}
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;
}
}
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;
}
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();
}
Aggregations