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