cn.nukkit.nbt.NBTIO Java Examples

The following examples show how to use cn.nukkit.nbt.NBTIO. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BlockEntityShulkerBox.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #2
Source File: BaseLevelProvider.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public BaseLevelProvider(Level level, String path) throws IOException {
    this.level = level;
    this.path = path;
    File file_path = new File(this.path);
    if (!file_path.exists()) {
        file_path.mkdirs();
    }
    CompoundTag levelData = NBTIO.readCompressed(new FileInputStream(new File(this.getPath() + "level.dat")), ByteOrder.BIG_ENDIAN);
    if (levelData.get("Data") instanceof CompoundTag) {
        this.levelData = levelData.getCompound("Data");
    } else {
        throw new LevelException("Invalid level.dat");
    }

    if (!this.levelData.contains("generatorName")) {
        this.levelData.putString("generatorName", Generator.getGenerator("DEFAULT").getSimpleName().toLowerCase());
    }

    if (!this.levelData.contains("generatorOptions")) {
        this.levelData.putString("generatorOptions", "");
    }

    this.spawn = new Vector3(this.levelData.getInt("SpawnX"), this.levelData.getInt("SpawnY"), this.levelData.getInt("SpawnZ"));
}
 
Example #3
Source File: BaseLevelProvider.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public BaseLevelProvider(Level level, String path) throws IOException {
    this.level = level;
    this.path = path;
    File file_path = new File(this.path);
    if (!file_path.exists()) {
        file_path.mkdirs();
    }
    CompoundTag levelData = NBTIO.readCompressed(new FileInputStream(new File(this.getPath() + "level.dat")), ByteOrder.BIG_ENDIAN);
    if (levelData.get("Data") instanceof CompoundTag) {
        this.levelData = levelData.getCompound("Data");
    } else {
        throw new LevelException("Invalid level.dat");
    }

    if (!this.levelData.contains("generatorName")) {
        this.levelData.putString("generatorName", Generator.getGenerator("DEFAULT").getSimpleName().toLowerCase());
    }

    if (!this.levelData.contains("generatorOptions")) {
        this.levelData.putString("generatorOptions", "");
    }

    this.spawn = new Vector3(this.levelData.getInt("SpawnX"), this.levelData.getInt("SpawnY"), this.levelData.getInt("SpawnZ"));
}
 
Example #4
Source File: BlockEntityChest.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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();
}
 
Example #5
Source File: EntityMinecartHopper.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void initEntity() {
    super.initEntity();

    this.inventory = new MinecartHopperInventory(this);
    if (this.namedTag.contains("Items") && this.namedTag.get("Items") instanceof ListTag) {
        ListTag<CompoundTag> inventoryList = this.namedTag.getList("Items", CompoundTag.class);
        for (CompoundTag item : inventoryList.getAll()) {
            this.inventory.setItem(item.getByte("Slot"), NBTIO.getItemHelper(item));
        }
    }

    this.dataProperties
            .putByte(DATA_CONTAINER_TYPE, 11)
            .putInt(DATA_CONTAINER_BASE_SIZE, this.inventory.getSize())
            .putInt(DATA_CONTAINER_EXTRA_SLOTS_PER_STRENGTH, 0);
}
 
Example #6
Source File: EntityFirework.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public EntityFirework(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);

    this.fireworkAge = 0;
    Random rand = new Random();
    this.lifetime = 30 + rand.nextInt(6) + rand.nextInt(7);

    this.motionX = rand.nextGaussian() * 0.001D;
    this.motionZ = rand.nextGaussian() * 0.001D;
    this.motionY = 0.05D;

    if (nbt.contains("FireworkItem")) {
        firework = NBTIO.getItemHelper(nbt.getCompound("FireworkItem"));
    } else {
        firework = new ItemFirework();
    }

    this.setDataProperty(new NBTEntityData(Entity.DATA_DISPLAY_ITEM, firework.getNamedTag()));
    this.setDataProperty(new IntEntityData(Entity.DATA_DISPLAY_OFFSET, 1));
    this.setDataProperty(new ByteEntityData(Entity.DATA_HAS_DISPLAY, 1));
}
 
Example #7
Source File: EntityItem.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void saveNBT() {
    super.saveNBT();
    if (this.item != null) { // Yes, a item can be null... I don't know what causes this, but it can happen.
        this.namedTag.putCompound("Item", NBTIO.putItemHelper(this.item, -1));
        this.namedTag.putShort("Health", (int) this.getHealth());
        this.namedTag.putShort("Age", this.age);
        this.namedTag.putShort("PickupDelay", this.pickupDelay);
        if (this.owner != null) {
            this.namedTag.putString("Owner", this.owner);
        }

        if (this.thrower != null) {
            this.namedTag.putString("Thrower", this.thrower);
        }
    }
}
 
Example #8
Source File: BlockEntityShulkerBox.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void initBlockEntity() {
    this.inventory = new ShulkerBoxInventory(this);

    if (!this.namedTag.contains("Items") || !(this.namedTag.get("Items") instanceof ListTag)) {
        this.namedTag.putList(new ListTag<CompoundTag>("Items"));
    }

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

    if (!this.namedTag.contains("facing")) {
        this.namedTag.putByte("facing", 0);
    }

    super.initBlockEntity();
}
 
Example #9
Source File: BlockEntityHopper.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #10
Source File: BlockEntityHopper.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #11
Source File: BlockEntityItemFrame.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public CompoundTag getSpawnCompound() {
    if (!this.namedTag.contains("Item")) {
        this.setItem(new ItemBlock(new BlockAir()), false);
    }
    CompoundTag NBTItem = namedTag.getCompound("Item").copy();
    NBTItem.setName("Item");
    boolean item = NBTItem.getShort("id") == Item.AIR;
    return new CompoundTag()
            .putString("id", BlockEntity.ITEM_FRAME)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z)
            .putCompound("Item", item ? NBTIO.putItemHelper(new ItemBlock(new BlockAir())) : NBTItem)
            .putByte("ItemRotation", item ? 0 : this.getItemRotation());
    // TODO: This crashes the client, why?
    // .putFloat("ItemDropChance", this.getItemDropChance());
}
 
Example #12
Source File: EntityItem.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void saveNBT() {
    super.saveNBT();
    if (this.item != null) { // Yes, a item can be null... I don't know what causes this, but it can happen.
        this.namedTag.putCompound("Item", NBTIO.putItemHelper(this.item, -1));
        this.namedTag.putShort("Health", (int) this.getHealth());
        this.namedTag.putShort("Age", this.age);
        this.namedTag.putShort("PickupDelay", this.pickupDelay);
        if (this.owner != null) {
            this.namedTag.putString("Owner", this.owner);
        }

        if (this.thrower != null) {
            this.namedTag.putString("Thrower", this.thrower);
        }
    }
}
 
Example #13
Source File: BlockEntityHopper.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #14
Source File: BlockEntityDropper.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #15
Source File: BlockEntityChest.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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();
}
 
Example #16
Source File: BlockEntitySpawnable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public void spawnTo(Player player) {
    if (this.closed) {
        return;
    }

    CompoundTag tag = this.getSpawnCompound();
    BlockEntityDataPacket pk = new BlockEntityDataPacket();
    pk.x = (int) this.x;
    pk.y = (int) this.y;
    pk.z = (int) this.z;
    try {
        pk.namedTag = NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    player.dataPacket(pk);
}
 
Example #17
Source File: BlockEntityBrewingStand.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #18
Source File: BlockEntityItemFrame.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void initBlockEntity() {
    if (!namedTag.contains("Item")) {
        namedTag.putCompound("Item", NBTIO.putItemHelper(new ItemBlock(Block.get(BlockID.AIR))));
    }
    if (!namedTag.contains("ItemRotation")) {
        namedTag.putByte("ItemRotation", 0);
    }
    if (!namedTag.contains("ItemDropChance")) {
        namedTag.putFloat("ItemDropChance", 1.0f);
    }

    this.level.updateComparatorOutputLevel(this);

    super.initBlockEntity();
}
 
Example #19
Source File: ChunkRequestTask.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public ChunkRequestTask(Level level, Chunk chunk) {
    this.levelId = level.getId();
    this.chunk = chunk.toFastBinary();
    this.chunkX = chunk.getX();
    this.chunkZ = chunk.getZ();

    byte[] buffer = new byte[0];

    for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
        if (blockEntity instanceof BlockEntitySpawnable) {
            try {
                buffer = Binary.appendBytes(buffer, NBTIO.write(((BlockEntitySpawnable) blockEntity).getSpawnCompound(), ByteOrder.BIG_ENDIAN, true));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    this.blockEntities = buffer;
}
 
Example #20
Source File: BlockEntityFurnace.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void setItem(int index, Item item) {
    int i = this.getSlotIndex(index);

    CompoundTag d = NBTIO.putItemHelper(item, index);

    if (item.getId() == Item.AIR || item.getCount() <= 0) {
        if (i >= 0) {
            this.namedTag.getList("Items").getAll().remove(i);
        }
    } else if (i < 0) {
        (this.namedTag.getList("Items", CompoundTag.class)).add(d);
    } else {
        (this.namedTag.getList("Items", CompoundTag.class)).add(i, d);
    }
}
 
Example #21
Source File: Level.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void dropItem(Vector3 source, Item item, Vector3 motion, boolean dropAround, int delay) {
    if (motion == null) {
        if (dropAround) {
            float f = ThreadLocalRandom.current().nextFloat() * 0.5f;
            float f1 = ThreadLocalRandom.current().nextFloat() * ((float) Math.PI * 2);

            motion = new Vector3(-MathHelper.sin(f1) * f, 0.20000000298023224, MathHelper.cos(f1) * f);
        } else {
            motion = new Vector3(new java.util.Random().nextDouble() * 0.2 - 0.1, 0.2,
                    new java.util.Random().nextDouble() * 0.2 - 0.1);
        }
    }

    CompoundTag itemTag = NBTIO.putItemHelper(item);
    itemTag.setName("Item");

    if (item.getId() > 0 && item.getCount() > 0) {
        EntityItem itemEntity = new EntityItem(
                this.getChunk((int) source.getX() >> 4, (int) source.getZ() >> 4, true),
                new CompoundTag().putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("", source.getX()))
                        .add(new DoubleTag("", source.getY())).add(new DoubleTag("", source.getZ())))

                        .putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("", motion.x))
                                .add(new DoubleTag("", motion.y)).add(new DoubleTag("", motion.z)))

                        .putList(new ListTag<FloatTag>("Rotation")
                                .add(new FloatTag("", new java.util.Random().nextFloat() * 360))
                                .add(new FloatTag("", 0)))

                        .putShort("Health", 5).putCompound("Item", itemTag).putShort("PickupDelay", delay));

        itemEntity.spawnToAll();
    }
}
 
Example #22
Source File: McRegion.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("Data")
            .putCompound("GameRules", new CompoundTag())

            .putLong("DayTime", 0)
            .putInt("GameType", 0)
            .putString("generatorName", Generator.getGeneratorName(generator))
            .putString("generatorOptions", options.getOrDefault("preset", ""))
            .putInt("generatorVersion", 1)
            .putBoolean("hardcore", false)
            .putBoolean("initialized", true)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putBoolean("raining", false)
            .putInt("rainTime", 0)
            .putLong("RandomSeed", seed)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putBoolean("thundering", false)
            .putInt("thunderTime", 0)
            .putInt("version", 19133)
            .putLong("Time", 0)
            .putLong("SizeOnDisk", 0);

    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
 
Example #23
Source File: BlockEntityItemFrame.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public BlockEntityItemFrame(FullChunk chunk, CompoundTag nbt) {
    super(chunk, nbt);
    if (!nbt.contains("Item")) {
        nbt.putCompound("Item", NBTIO.putItemHelper(new ItemBlock(new BlockAir())));
    }
    if (!nbt.contains("ItemRotation")) {
        nbt.putByte("ItemRotation", 0);
    }
    if (!nbt.contains("ItemDropChance")) {
        nbt.putFloat("ItemDropChance", 1.0f);
    }

    this.level.updateComparatorOutputLevel(this);
}
 
Example #24
Source File: BlockEntityItemFrame.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void setItem(Item item, boolean setChanged) {
    this.namedTag.putCompound("Item", NBTIO.putItemHelper(item));
    if (setChanged) {
        this.setChanged();
    }

    this.level.updateComparatorOutputLevel(this);
}
 
Example #25
Source File: EntityItem.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void initEntity() {
    super.initEntity();

    this.setMaxHealth(5);
    this.setHealth(this.namedTag.getShort("Health"));

    if (this.namedTag.contains("Age")) {
        this.age = this.namedTag.getShort("Age");
    }

    if (this.namedTag.contains("PickupDelay")) {
        this.pickupDelay = this.namedTag.getShort("PickupDelay");
    }

    if (this.namedTag.contains("Owner")) {
        this.owner = this.namedTag.getString("Owner");
    }

    if (this.namedTag.contains("Thrower")) {
        this.thrower = this.namedTag.getString("Thrower");
    }

    if (!this.namedTag.contains("Item")) {
        this.close();
        return;
    }

    this.item = NBTIO.getItemHelper(this.namedTag.getCompound("Item"));
    this.setDataFlag(DATA_FLAGS, DATA_FLAG_IMMOBILE, true); 

    this.server.getPluginManager().callEvent(new ItemSpawnEvent(this));
}
 
Example #26
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static Chunk fromBinary(byte[] data, LevelProvider provider) {
    try {
        CompoundTag chunk = NBTIO.read(new ByteArrayInputStream(Zlib.inflate(data)), ByteOrder.BIG_ENDIAN);

        if (!chunk.contains("Level") || !(chunk.get("Level") instanceof CompoundTag)) {
            return null;
        }
        return new Chunk(provider != null ? provider : McRegion.class.newInstance(), chunk.getCompound("Level"));
    } catch (Exception e) {
        return null;
    }
}
 
Example #27
Source File: BaseLevelProvider.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void saveLevelData() {
    try {
        NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", this.levelData), new FileOutputStream(this.getPath() + "level.dat"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #28
Source File: Item.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public byte[] writeCompoundTag(CompoundTag tag) {
    try {
        tag.setName("");
        return NBTIO.write(tag, ByteOrder.LITTLE_ENDIAN);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #29
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void saveLevelData() {
    try {
        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()));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #30
Source File: BlockEntityHopper.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item getItem(int index) {
    int i = this.getSlotIndex(index);
    if (i < 0) {
        return new ItemBlock(Block.get(BlockID.AIR), 0, 0);
    } else {
        CompoundTag data = (CompoundTag) this.namedTag.getList("Items").get(i);
        return NBTIO.getItemHelper(data);
    }
}