Search in sources :

Example 66 with CompoundTag

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

the class BlockChest method place.

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    BlockEntityChest chest = null;
    int[] faces = { 2, 5, 3, 4 };
    this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
    for (int side = 2; side <= 5; ++side) {
        if ((this.getDamage() == 4 || this.getDamage() == 5) && (side == 4 || side == 5)) {
            continue;
        } else if ((this.getDamage() == 3 || this.getDamage() == 2) && (side == 2 || side == 3)) {
            continue;
        }
        Block c = this.getSide(BlockFace.fromIndex(side));
        if (c instanceof BlockChest && c.getDamage() == this.getDamage()) {
            BlockEntity blockEntity = this.getLevel().getBlockEntity(c);
            if (blockEntity instanceof BlockEntityChest && !((BlockEntityChest) blockEntity).isPaired()) {
                chest = (BlockEntityChest) blockEntity;
                break;
            }
        }
    }
    this.getLevel().setBlock(block, this, true, true);
    CompoundTag nbt = new CompoundTag("").putList(new ListTag<>("Items")).putString("id", BlockEntity.CHEST).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 blockEntity = new BlockEntityChest(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
    if (chest != null) {
        chest.pairWith(((BlockEntityChest) blockEntity));
        ((BlockEntityChest) blockEntity).pairWith(chest);
    }
    return true;
}
Also used : BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) StringTag(cn.nukkit.nbt.tag.StringTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) Tag(cn.nukkit.nbt.tag.Tag) ListTag(cn.nukkit.nbt.tag.ListTag) Map(java.util.Map) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 67 with CompoundTag

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

the class BlockEnchantingTable method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    if (player != null) {
        BlockEntity t = this.getLevel().getBlockEntity(this);
        BlockEntityEnchantTable enchantTable;
        if (t instanceof BlockEntityEnchantTable) {
            enchantTable = (BlockEntityEnchantTable) t;
        } else {
            CompoundTag nbt = new CompoundTag().putList(new ListTag<>("Items")).putString("id", BlockEntity.ENCHANT_TABLE).putInt("x", (int) this.x).putInt("y", (int) this.y).putInt("z", (int) this.z);
            enchantTable = new BlockEntityEnchantTable(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
        }
        if (enchantTable.namedTag.contains("Lock") && enchantTable.namedTag.get("Lock") instanceof StringTag) {
            if (!enchantTable.namedTag.getString("Lock").equals(item.getCustomName())) {
                return true;
            }
        }
        player.addWindow(new EnchantInventory(this.getLocation()), Player.ENCHANT_WINDOW_ID);
    }
    return true;
}
Also used : StringTag(cn.nukkit.nbt.tag.StringTag) BlockEntityEnchantTable(cn.nukkit.blockentity.BlockEntityEnchantTable) EnchantInventory(cn.nukkit.inventory.EnchantInventory) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 68 with CompoundTag

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

the class Server method getOfflinePlayerData.

public CompoundTag getOfflinePlayerData(String name) {
    name = name.toLowerCase();
    String path = this.getDataPath() + "players/";
    File file = new File(path + name + ".dat");
    if (this.shouldSavePlayerData() && file.exists()) {
        try {
            return NBTIO.readCompressed(new FileInputStream(file));
        } catch (Exception e) {
            file.renameTo(new File(path + name + ".dat.bak"));
            this.logger.notice(this.getLanguage().translateString("nukkit.data.playerCorrupted", name));
        }
    } else {
        this.logger.notice(this.getLanguage().translateString("nukkit.data.playerNotFound", name));
    }
    Position spawn = this.getDefaultLevel().getSafeSpawn();
    CompoundTag nbt = new CompoundTag().putLong("firstPlayed", System.currentTimeMillis() / 1000).putLong("lastPlayed", System.currentTimeMillis() / 1000).putList(new ListTag<DoubleTag>("Pos").add(new DoubleTag("0", spawn.x)).add(new DoubleTag("1", spawn.y)).add(new DoubleTag("2", spawn.z))).putString("Level", this.getDefaultLevel().getName()).putList(new ListTag<>("Inventory")).putCompound("Achievements", new CompoundTag()).putInt("playerGameType", this.getGamemode()).putList(new ListTag<DoubleTag>("Motion").add(new DoubleTag("0", 0)).add(new DoubleTag("1", 0)).add(new DoubleTag("2", 0))).putList(new ListTag<FloatTag>("Rotation").add(new FloatTag("0", 0)).add(new FloatTag("1", 0))).putFloat("FallDistance", 0).putShort("Fire", 0).putShort("Air", 300).putBoolean("OnGround", true).putBoolean("Invulnerable", false).putString("NameTag", name);
    this.saveOfflinePlayerData(name, nbt);
    return nbt;
}
Also used : FloatTag(cn.nukkit.nbt.tag.FloatTag) Position(cn.nukkit.level.Position) DoubleTag(cn.nukkit.nbt.tag.DoubleTag) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 69 with CompoundTag

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

the class EntityHuman method initEntity.

@Override
protected void initEntity() {
    this.setDataFlag(DATA_PLAYER_FLAGS, DATA_PLAYER_FLAG_SLEEP, false);
    this.setDataFlag(DATA_FLAGS, DATA_FLAG_GRAVITY);
    this.setDataProperty(new IntPositionEntityData(DATA_PLAYER_BED_POSITION, 0, 0, 0), false);
    if (!(this instanceof Player)) {
        if (this.namedTag.contains("NameTag")) {
            this.setNameTag(this.namedTag.getString("NameTag"));
        }
        if (this.namedTag.contains("Skin") && this.namedTag.get("Skin") instanceof CompoundTag) {
            if (!this.namedTag.getCompound("Skin").contains("Transparent")) {
                this.namedTag.getCompound("Skin").putBoolean("Transparent", false);
            }
            this.setSkin(new Skin(this.namedTag.getCompound("Skin").getByteArray("Data"), this.namedTag.getCompound("Skin").getString("ModelId")));
        }
        this.uuid = Utils.dataToUUID(String.valueOf(this.getId()).getBytes(StandardCharsets.UTF_8), this.getSkin().getData(), this.getNameTag().getBytes(StandardCharsets.UTF_8));
    }
    super.initEntity();
}
Also used : Player(cn.nukkit.Player) IntPositionEntityData(cn.nukkit.entity.data.IntPositionEntityData) Skin(cn.nukkit.entity.data.Skin) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Example 70 with CompoundTag

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

the class EntityHumanType method initEntity.

@Override
protected void initEntity() {
    this.inventory = new PlayerInventory(this);
    if (this.namedTag.contains("Inventory") && this.namedTag.get("Inventory") instanceof ListTag) {
        ListTag<CompoundTag> inventoryList = this.namedTag.getList("Inventory", CompoundTag.class);
        for (CompoundTag item : inventoryList.getAll()) {
            int slot = item.getByte("Slot");
            if (slot >= 0 && slot < 9) {
                // hotbar
                // Old hotbar saving stuff, remove it (useless now)
                inventoryList.remove(item);
            } else if (slot >= 100 && slot < 104) {
                this.inventory.setItem(this.inventory.getSize() + slot - 100, NBTIO.getItemHelper(item));
            } else {
                this.inventory.setItem(slot - 9, NBTIO.getItemHelper(item));
            }
        }
    }
    this.enderChestInventory = new PlayerEnderChestInventory(this);
    if (this.namedTag.contains("EnderItems") && this.namedTag.get("EnderItems") instanceof ListTag) {
        ListTag<CompoundTag> inventoryList = this.namedTag.getList("EnderItems", CompoundTag.class);
        for (CompoundTag item : inventoryList.getAll()) {
            this.enderChestInventory.setItem(item.getByte("Slot"), NBTIO.getItemHelper(item));
        }
    }
    super.initEntity();
}
Also used : PlayerEnderChestInventory(cn.nukkit.inventory.PlayerEnderChestInventory) PlayerInventory(cn.nukkit.inventory.PlayerInventory) ListTag(cn.nukkit.nbt.tag.ListTag) CompoundTag(cn.nukkit.nbt.tag.CompoundTag)

Aggregations

CompoundTag (cn.nukkit.nbt.tag.CompoundTag)74 ListTag (cn.nukkit.nbt.tag.ListTag)28 BlockEntity (cn.nukkit.blockentity.BlockEntity)13 DoubleTag (cn.nukkit.nbt.tag.DoubleTag)13 FloatTag (cn.nukkit.nbt.tag.FloatTag)13 Tag (cn.nukkit.nbt.tag.Tag)13 StringTag (cn.nukkit.nbt.tag.StringTag)12 Map (java.util.Map)8 Entity (cn.nukkit.entity.Entity)7 IOException (java.io.IOException)7 Player (cn.nukkit.Player)5 BinaryStream (cn.nukkit.utils.BinaryStream)5 ArrayList (java.util.ArrayList)5 BlockRail (cn.nukkit.block.BlockRail)4 FullChunk (cn.nukkit.level.format.FullChunk)4 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)4 Rail (cn.nukkit.utils.Rail)4 File (java.io.File)4 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)3 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)3