use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.
the class BlockBrewingStand method place.
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (!block.down().isTransparent()) {
getLevel().setBlock(block, this, true, true);
CompoundTag nbt = new CompoundTag().putList(new ListTag<>("Items")).putString("id", BlockEntity.BREWING_STAND).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
if (item.hasCustomName()) {
nbt.putString("CustomName", item.getCustomName());
}
if (item.hasCustomBlockData()) {
Map<String, Tag> customData = item.getCustomBlockData().getTags();
for (Map.Entry<String, Tag> tag : customData.entrySet()) {
nbt.put(tag.getKey(), tag.getValue());
}
}
new BlockEntityBrewingStand(getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
return true;
}
return false;
}
use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.
the class BlockEnchantingTable method place.
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
this.getLevel().setBlock(block, this, true, true);
CompoundTag nbt = new CompoundTag().putString("id", BlockEntity.ENCHANT_TABLE).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
if (item.hasCustomName()) {
nbt.putString("CustomName", item.getCustomName());
}
if (item.hasCustomBlockData()) {
Map<String, Tag> customData = item.getCustomBlockData().getTags();
for (Map.Entry<String, Tag> tag : customData.entrySet()) {
nbt.put(tag.getKey(), tag.getValue());
}
}
BlockEntity.createBlockEntity(BlockEntity.ENCHANT_TABLE, getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
return true;
}
use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.
the class BlockFlowerPot method place.
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (face != BlockFace.UP)
return false;
CompoundTag nbt = new CompoundTag().putString("id", BlockEntity.FLOWER_POT).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z).putShort("item", 0).putInt("data", 0);
if (item.hasCustomBlockData()) {
for (Tag aTag : item.getCustomBlockData().getAllTags()) {
nbt.put(aTag.getName(), aTag);
}
}
new BlockEntityFlowerPot(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);
this.getLevel().setBlock(block, this, true, true);
return true;
}
use of cn.nukkit.nbt.tag.Tag in project Nukkit by Nukkit.
the class BlockFurnaceBurning method place.
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
int[] faces = { 2, 5, 3, 4 };
this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
this.getLevel().setBlock(block, this, true, true);
CompoundTag nbt = new CompoundTag().putList(new ListTag<>("Items")).putString("id", BlockEntity.FURNACE).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
if (item.hasCustomName()) {
nbt.putString("CustomName", item.getCustomName());
}
if (item.hasCustomBlockData()) {
Map<String, Tag> customData = item.getCustomBlockData().getTags();
for (Map.Entry<String, Tag> tag : customData.entrySet()) {
nbt.put(tag.getKey(), tag.getValue());
}
}
new BlockEntityFurnace(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
return true;
}
use of cn.nukkit.nbt.tag.Tag 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;
}
Aggregations