Search in sources :

Example 1 with BlockEntityChest

use of cn.nukkit.blockentity.BlockEntityChest in project Nukkit by Nukkit.

the class BlockChest method onBreak.

@Override
public boolean onBreak(Item item) {
    BlockEntity t = this.getLevel().getBlockEntity(this);
    if (t instanceof BlockEntityChest) {
        ((BlockEntityChest) t).unpair();
    }
    this.getLevel().setBlock(this, new BlockAir(), true, true);
    return true;
}
Also used : BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 2 with BlockEntityChest

use of cn.nukkit.blockentity.BlockEntityChest in project Nukkit by Nukkit.

the class BlockChest method onActivate.

@Override
public boolean onActivate(Item item, Player player) {
    if (player != null) {
        Block top = up();
        if (!top.isTransparent()) {
            return true;
        }
        BlockEntity t = this.getLevel().getBlockEntity(this);
        BlockEntityChest chest;
        if (t instanceof BlockEntityChest) {
            chest = (BlockEntityChest) t;
        } else {
            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);
            chest = new BlockEntityChest(this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
        }
        if (chest.namedTag.contains("Lock") && chest.namedTag.get("Lock") instanceof StringTag) {
            if (!chest.namedTag.getString("Lock").equals(item.getCustomName())) {
                return true;
            }
        }
        player.addWindow(chest.getInventory());
    }
    return true;
}
Also used : StringTag(cn.nukkit.nbt.tag.StringTag) BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) CompoundTag(cn.nukkit.nbt.tag.CompoundTag) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 3 with BlockEntityChest

use of cn.nukkit.blockentity.BlockEntityChest in project Nukkit by Nukkit.

the class BlockTrappedChest method getWeakPower.

@Override
public int getWeakPower(BlockFace face) {
    int playerCount = 0;
    BlockEntity blockEntity = this.level.getBlockEntity(this);
    if (blockEntity instanceof BlockEntityChest) {
        playerCount = ((BlockEntityChest) blockEntity).getInventory().getViewers().size();
    }
    return playerCount < 0 ? 0 : playerCount > 15 ? 15 : playerCount;
}
Also used : BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) BlockEntity(cn.nukkit.blockentity.BlockEntity)

Example 4 with BlockEntityChest

use of cn.nukkit.blockentity.BlockEntityChest in project Nukkit by Nukkit.

the class BlockTrappedChest 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 };
    BlockEntityChest chest = null;
    this.setDamage(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
    for (BlockFace side : Plane.HORIZONTAL) {
        if ((this.getDamage() == 4 || this.getDamage() == 5) && (side == BlockFace.WEST || side == BlockFace.EAST)) {
            continue;
        } else if ((this.getDamage() == 3 || this.getDamage() == 2) && (side == BlockFace.NORTH || side == BlockFace.SOUTH)) {
            continue;
        }
        Block c = this.getSide(side);
        if (c instanceof BlockTrappedChest && 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 : BlockFace(cn.nukkit.math.BlockFace) BlockEntityChest(cn.nukkit.blockentity.BlockEntityChest) 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 5 with BlockEntityChest

use of cn.nukkit.blockentity.BlockEntityChest 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)

Aggregations

BlockEntity (cn.nukkit.blockentity.BlockEntity)6 BlockEntityChest (cn.nukkit.blockentity.BlockEntityChest)6 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)3 ListTag (cn.nukkit.nbt.tag.ListTag)2 StringTag (cn.nukkit.nbt.tag.StringTag)2 Tag (cn.nukkit.nbt.tag.Tag)2 Map (java.util.Map)2 Player (cn.nukkit.Player)1 Block (cn.nukkit.block.Block)1 BlockAir (cn.nukkit.block.BlockAir)1 EntityItem (cn.nukkit.entity.item.EntityItem)1 BlockBreakEvent (cn.nukkit.event.block.BlockBreakEvent)1 InventoryHolder (cn.nukkit.inventory.InventoryHolder)1 Item (cn.nukkit.item.Item)1 ItemBlock (cn.nukkit.item.ItemBlock)1 Enchantment (cn.nukkit.item.enchantment.Enchantment)1 DestroyBlockParticle (cn.nukkit.level.particle.DestroyBlockParticle)1 BlockFace (cn.nukkit.math.BlockFace)1