Java Code Examples for cn.nukkit.blockentity.BlockEntity#createBlockEntity()

The following examples show how to use cn.nukkit.blockentity.BlockEntity#createBlockEntity() . 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: BlockEnchantingTable.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 2
Source File: BlockEnchantingTable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: BlockUndyedShulkerBox.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    if (player != null) {
        BlockEntity t = this.getLevel().getBlockEntity(this);
        BlockEntityShulkerBox box;
        if (t instanceof BlockEntityShulkerBox) {
            box = (BlockEntityShulkerBox) t;
        } else {
            CompoundTag nbt = BlockEntity.getDefaultCompound(this, BlockEntity.SHULKER_BOX);
            box = (BlockEntityShulkerBox) BlockEntity.createBlockEntity(BlockEntity.SHULKER_BOX, this.getLevel().getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), nbt);
            if (box == null) {
                return false;
            }
        }

        Block block = this.getSide(BlockFace.fromIndex(box.namedTag.getByte("facing")));
        if (!(block instanceof BlockAir) && !(block instanceof BlockLiquid) && !(block instanceof BlockFlowable)) {
            return true;
        }

        player.addWindow(box.getInventory());
    }

    return true;
}
 
Example 4
Source File: BlockItemFrame.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (face.getIndex() > 1 && target.isSolid() && (!block.isSolid() || block.canBeReplaced())) {
        this.setDamage(FACING[face.getIndex()]);
        this.getLevel().setBlock(block, this, true, true);
        CompoundTag nbt = new CompoundTag()
                .putString("id", BlockEntity.ITEM_FRAME)
                .putInt("x", (int) block.x)
                .putInt("y", (int) block.y)
                .putInt("z", (int) block.z)
                .putByte("ItemRotation", 0)
                .putFloat("ItemDropChance", 1.0f);
        if (item.hasCustomBlockData()) {
            for (Tag aTag : item.getCustomBlockData().getAllTags()) {
                nbt.put(aTag.getName(), aTag);
            }
        }
        BlockEntityItemFrame frame = (BlockEntityItemFrame) BlockEntity.createBlockEntity(BlockEntity.ITEM_FRAME, this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
        if (frame == null) {
            return false;
        }
        this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_PLACE);
        return true;
    }
    return false;
}
 
Example 5
Source File: BlockEnchantingTable.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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());
        }
    }

    BlockEntityEnchantTable enchantTable = (BlockEntityEnchantTable) BlockEntity.createBlockEntity(BlockEntity.ENCHANT_TABLE, getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    return enchantTable != null;
}
 
Example 6
Source File: BlockRedstoneComparator.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (super.place(item, block, target, face, fx, fy, fz, player)) {
        CompoundTag nbt = new CompoundTag()
                .putList(new ListTag<>("Items"))
                .putString("id", BlockEntity.COMPARATOR)
                .putInt("x", (int) this.x)
                .putInt("y", (int) this.y)
                .putInt("z", (int) this.z);
        BlockEntityComparator comparator = (BlockEntityComparator) BlockEntity.createBlockEntity(BlockEntity.COMPARATOR, this.level.getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
        if (comparator == null) {
            return false;
        }
        onUpdate(Level.BLOCK_UPDATE_REDSTONE);
        return true;
    }

    return false;
}
 
Example 7
Source File: BlockBeacon.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    boolean blockSuccess = super.place(item, block, target, face, fx, fy, fz, player);

    if (blockSuccess) {
        CompoundTag nbt = new CompoundTag("")
                .putString("id", BlockEntity.BEACON)
                .putInt("x", (int) this.x)
                .putInt("y", (int) this.y)
                .putInt("z", (int) this.z);
        BlockEntityBeacon beacon = (BlockEntityBeacon) BlockEntity.createBlockEntity(BlockEntity.BEACON, this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
        if (beacon == null) {
            return false;
        }
    }

    return blockSuccess;
}
 
Example 8
Source File: BlockFurnaceBurning.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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());
        }
    }

    BlockEntityFurnace furnace = (BlockEntityFurnace) BlockEntity.createBlockEntity(BlockEntity.FURNACE, this.getLevel().getChunk((int) (this.x) >> 4, (int) (this.z) >> 4), nbt);
    return furnace != null;
}
 
Example 9
Source File: BlockEnderChest.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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("")
            .putString("id", BlockEntity.ENDER_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());
        }
    }

    BlockEntityEnderChest ender = (BlockEntityEnderChest) BlockEntity.createBlockEntity(BlockEntity.ENDER_CHEST, this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    return ender != null;
}
 
Example 10
Source File: BlockCauldron.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    CompoundTag nbt = new CompoundTag("")
            .putString("id", BlockEntity.CAULDRON)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z)
            .putShort("PotionId", 0xffff)
            .putByte("SplashPotion", 0);

    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }

    BlockEntityCauldron cauldron = (BlockEntityCauldron) BlockEntity.createBlockEntity(BlockEntity.CAULDRON, this.level.getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    if (cauldron == null) {
        return false;
    }
    this.getLevel().setBlock(block, this, true, true);
    return true;
}
 
Example 11
Source File: BlockUndyedShulkerBox.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@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);
    CompoundTag nbt = BlockEntity.getDefaultCompound(this, BlockEntity.SHULKER_BOX)
            .putByte("facing", face.getIndex());

    if (item.hasCustomName()) {
        nbt.putString("CustomName", item.getCustomName());
    }

    CompoundTag t = item.getNamedTag();

    // This code gets executed when the player has broken the shulker box and placed it back (©Kevims 2020)
    if (t != null && t.contains("Items")) {
        nbt.putList(t.getList("Items"));
    }

    // This code gets executed when the player has copied the shulker box in creative mode (©Kevims 2020)
    if (item.hasCustomBlockData()) {
        Map<String, Tag> customData = item.getCustomBlockData().getTags();
        for (Map.Entry<String, Tag> tag : customData.entrySet()) {
            nbt.put(tag.getKey(), tag.getValue());
        }
    }

    BlockEntityShulkerBox box = (BlockEntityShulkerBox) BlockEntity.createBlockEntity(BlockEntity.SHULKER_BOX, this.getLevel().getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), nbt);
    return box != null;
}
 
Example 12
Source File: BlockJukebox.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private BlockEntity createBlockEntity() {
    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<>("Items"))
            .putString("id", BlockEntity.JUKEBOX)
            .putInt("x", getFloorX())
            .putInt("y", getFloorY())
            .putInt("z", getFloorZ());

    return BlockEntity.createBlockEntity(BlockEntity.JUKEBOX, this.level.getChunk(getFloorX() >> 4, getFloorZ() >> 4), nbt);
}
 
Example 13
Source File: BlockHopper.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    BlockFace facing = face.getOpposite();

    if (facing == BlockFace.UP) {
        facing = BlockFace.DOWN;
    }

    this.setDamage(facing.getIndex());

    boolean powered = this.level.isBlockPowered(this.getLocation());

    if (powered == this.isEnabled()) {
        this.setEnabled(!powered);
    }

    this.level.setBlock(this, this);

    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<>("Items"))
            .putString("id", BlockEntity.HOPPER)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z);

    BlockEntityHopper hopper = (BlockEntityHopper) BlockEntity.createBlockEntity(BlockEntity.HOPPER, this.level.getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), nbt);
    return hopper != null;
}
 
Example 14
Source File: BlockBanner.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (face != BlockFace.DOWN) {
        if (face == BlockFace.UP) {
            this.setDamage(NukkitMath.floorDouble(((player.yaw + 180) * 16 / 360) + 0.5) & 0x0f);
            this.getLevel().setBlock(block, this, true);
        } else {
            this.setDamage(face.getIndex());
            this.getLevel().setBlock(block, Block.get(BlockID.WALL_BANNER, this.getDamage()), true);
        }

        CompoundTag nbt = BlockEntity.getDefaultCompound(this, BlockEntity.BANNER)
                .putInt("Base", item.getDamage() & 0xf);

        Tag type = item.getNamedTagEntry("Type");
        if (type instanceof IntTag) {
            nbt.put("Type", type);
        }
        Tag patterns = item.getNamedTagEntry("Patterns");
        if (patterns instanceof ListTag) {
            nbt.put("Patterns", patterns);
        }

        BlockEntityBanner banner = (BlockEntityBanner) BlockEntity.createBlockEntity(BlockEntity.BANNER, this.getChunk(), nbt);
        return banner != null;
    }
    return false;
}
 
Example 15
Source File: BlockPistonBase.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    if (Math.abs(player.x - this.x) < 2 && Math.abs(player.z - this.z) < 2) {
        double y = player.y + player.getEyeHeight();

        if (y - this.y > 2) {
            this.setDamage(BlockFace.UP.getIndex());
        } else if (this.y - y > 0) {
            this.setDamage(BlockFace.DOWN.getIndex());
        } else {
            this.setDamage(player.getHorizontalFacing().getIndex());
        }
    } else {
        this.setDamage(player.getHorizontalFacing().getIndex());
    }
    this.level.setBlock(block, this, true, false);

    CompoundTag nbt = new CompoundTag("")
            .putString("id", BlockEntity.PISTON_ARM)
            .putInt("x", (int) this.x)
            .putInt("y", (int) this.y)
            .putInt("z", (int) this.z)
            .putBoolean("Sticky", this.sticky);

    BlockEntityPistonArm be = (BlockEntityPistonArm) BlockEntity.createBlockEntity(BlockEntity.PISTON_ARM, this.level.getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);

    if (be == null) return false;
    //this.checkState();
    return true;
}
 
Example 16
Source File: BlockSkull.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
    switch (face) {
        case NORTH:
        case SOUTH:
        case EAST:
        case WEST:
        case UP:
            this.setDamage(face.getIndex());
            break;
        case DOWN:
        default:
            return false;
    }
    this.getLevel().setBlock(block, this, true, true);

    CompoundTag nbt = new CompoundTag()
            .putString("id", BlockEntity.SKULL)
            .putByte("SkullType", item.getDamage())
            .putInt("x", block.getFloorX())
            .putInt("y", block.getFloorY())
            .putInt("z", block.getFloorZ())
            .putByte("Rot", (int) Math.floor((player.yaw * 16 / 360) + 0.5) & 0x0f);
    if (item.hasCustomBlockData()) {
        for (Tag aTag : item.getCustomBlockData().getAllTags()) {
            nbt.put(aTag.getName(), aTag);
        }
    }
    BlockEntitySkull skull = (BlockEntitySkull) BlockEntity.createBlockEntity(BlockEntity.SKULL, getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);
    if (skull == null) {
        return false;
    }

    // TODO: 2016/2/3 SPAWN WITHER

    return true;
}
 
Example 17
Source File: BlockJukebox.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private BlockEntity createBlockEntity() {
    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<>("Items"))
            .putString("id", BlockEntity.JUKEBOX)
            .putInt("x", getFloorX())
            .putInt("y", getFloorY())
            .putInt("z", getFloorZ());

    return BlockEntity.createBlockEntity(BlockEntity.JUKEBOX, this.level.getChunk(getFloorX() >> 4, getFloorZ() >> 4), nbt);
}
 
Example 18
Source File: BlockJukebox.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private BlockEntity createBlockEntity() {
    CompoundTag nbt = new CompoundTag()
            .putList(new ListTag<>("Items"))
            .putString("id", BlockEntity.JUKEBOX)
            .putInt("x", getFloorX())
            .putInt("y", getFloorY())
            .putInt("z", getFloorZ());

    return BlockEntity.createBlockEntity(BlockEntity.JUKEBOX, this.level.getChunk(getFloorX() >> 4, getFloorZ() >> 4), nbt);
}
 
Example 19
Source File: BlockNoteblock.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
private BlockEntityMusic createBlockEntity() {
    return (BlockEntityMusic) BlockEntity.createBlockEntity(BlockEntity.MUSIC, this.getLevel().getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4),
                                    BlockEntity.getDefaultCompound(this, BlockEntity.MUSIC));
}
 
Example 20
Source File: BlockBed.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
private void createBlockEntity(Vector3 pos, int color) {
    CompoundTag nbt = BlockEntity.getDefaultCompound(pos, BlockEntity.BED);
    nbt.putByte("color", color);

    BlockEntity.createBlockEntity(BlockEntity.BED, this.level.getChunk(pos.getFloorX() >> 4, pos.getFloorZ() >> 4), nbt);
}