cn.nukkit.blockentity.BlockEntity Java Examples

The following examples show how to use cn.nukkit.blockentity.BlockEntity. 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: BlockEnderChest.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) {
    int[] faces = {4, 2, 5, 3};
    this.meta = 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());
        }
    }

    new BlockEntityEnderChest(this.getLevel().getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);
    return true;
}
 
Example #2
Source File: BlockBanner.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item toItem() {
    BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
    Item item = Item.get(Item.BANNER);
    if (blockEntity instanceof BlockEntityBanner) {
        BlockEntityBanner banner = (BlockEntityBanner) blockEntity;
        item.setDamage(banner.getBaseColor() & 0xf);
        item.setNamedTag((item.hasCompoundTag() ? item.getNamedTag() : new CompoundTag())
                .putInt("Base", banner.getBaseColor() & 0xf));
        int type = banner.namedTag.getInt("Type");
        if (type > 0) {
            item.setNamedTag((item.hasCompoundTag() ? item.getNamedTag() : new CompoundTag())
                    .putInt("Type", type));
        }
        ListTag<CompoundTag> patterns = banner.namedTag.getList("Patterns", CompoundTag.class);
        if (patterns.size() > 0) {
            item.setNamedTag((item.hasCompoundTag() ? item.getNamedTag() : new CompoundTag())
                    .putList(patterns));
        }
    }
    return item;
}
 
Example #3
Source File: BlockFlowerPot.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    boolean dropInside = false;
    int insideID = 0;
    int insideMeta = 0;
    BlockEntity blockEntity = getLevel().getBlockEntity(this);
    if (blockEntity instanceof BlockEntityFlowerPot) {
        dropInside = true;
        insideID = blockEntity.namedTag.getShort("item");
        insideMeta = blockEntity.namedTag.getInt("data");
    }

    if (dropInside) {
        return new Item[]{
                new ItemFlowerPot(),
                Item.get(insideID, insideMeta, 1)
        };
    } else {
        return new Item[]{
                new ItemFlowerPot()
        };
    }
}
 
Example #4
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 #5
Source File: BlockFlowerPot.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) {
    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;
}
 
Example #6
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 #7
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 #8
Source File: BlockPistonBase.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int onUpdate(int type) {
    if (type != 6 && type != 1) {
        return 0;
    } else {
        BlockEntity blockEntity = this.level.getBlockEntity(this);
        if (blockEntity instanceof BlockEntityPistonArm) {
            BlockEntityPistonArm arm = (BlockEntityPistonArm) blockEntity;
            boolean powered = this.isPowered();
            if (arm.powered != powered) {
                arm.powered = !arm.powered;
                if (arm.chunk != null) {
                    arm.chunk.setChanged();
                }
            }
        }

        return type;
    }
}
 
Example #9
Source File: BlockFlowerPot.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Item[] getDrops(Item item) {
    boolean dropInside = false;
    int insideID = 0;
    int insideMeta = 0;
    BlockEntity blockEntity = getLevel().getBlockEntity(this);
    if (blockEntity instanceof BlockEntityFlowerPot) {
        dropInside = true;
        insideID = blockEntity.namedTag.getShort("item");
        insideMeta = blockEntity.namedTag.getInt("data");
    }

    if (dropInside) {
        return new Item[]{
                new ItemFlowerPot(),
                Item.get(insideID, insideMeta, 1)
        };
    } else {
        return new Item[]{
                new ItemFlowerPot()
        };
    }
}
 
Example #10
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 #11
Source File: BlockPistonBase.java    From Jupiter 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.meta = BlockFace.UP.getIndex();
        } else if (this.y - y > 0) {
            this.meta = BlockFace.DOWN.getIndex();
        } else {
            this.meta = player.getHorizontalFacing().getIndex();
        }
    } else {
        this.meta = 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 = new BlockEntityPistonArm(this.level.getChunk((int) this.x >> 4, (int) this.z >> 4), nbt);

    //this.checkState();
    return true;
}
 
Example #12
Source File: BlockBed.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public DyeColor getDyeColor() {
    BlockEntity blockEntity = this.level.getBlockEntity(this);

    if (blockEntity instanceof BlockEntityBed) {
        return ((BlockEntityBed) blockEntity).getDyeColor();
    }
    return DyeColor.WHITE;
}
 
Example #13
Source File: BlockChest.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@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;
}
 
Example #14
Source File: BlockChest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onBreak(Item item) {
    BlockEntity t = this.getLevel().getBlockEntity(this);
    if (t instanceof BlockEntityChest) {
        ((BlockEntityChest) t).unpair();
    }
    this.getLevel().setBlock(this, Block.get(BlockID.AIR), true, true);

    return true;
}
 
Example #15
Source File: BlockBrewingStand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getComparatorInputOverride() {
    BlockEntity blockEntity = this.level.getBlockEntity(this);

    if (blockEntity instanceof BlockEntityBrewingStand) {
        return ContainerInventory.calculateRedstone(((BlockEntityBrewingStand) blockEntity).getInventory());
    }

    return super.getComparatorInputOverride();
}
 
Example #16
Source File: BlockSkull.java    From Jupiter 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.meta = 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);
        }
    }

    new BlockEntitySkull(getLevel().getChunk((int) block.x >> 4, (int) block.z >> 4), nbt);

    return true;
}
 
Example #17
Source File: BlockHopper.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getComparatorInputOverride() {
    BlockEntity blockEntity = this.level.getBlockEntity(this);

    if (blockEntity instanceof BlockEntityHopper) {
        return ContainerInventory.calculateRedstone(((BlockEntityHopper) blockEntity).getInventory());
    }

    return super.getComparatorInputOverride();
}
 
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: BaseFullChunk.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void removeBlockEntity(BlockEntity blockEntity) {
    this.tiles.remove(blockEntity.getId());
    int index = ((blockEntity.getFloorZ() & 0x0f) << 12) | ((blockEntity.getFloorX() & 0x0f) << 8) | (blockEntity.getFloorY() & 0xff);
    this.tileList.remove(index);
    if (this.isInit) {
        this.hasChanged = true;
    }
}
 
Example #20
Source File: BlockChest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public int getComparatorInputOverride() {
    BlockEntity blockEntity = this.level.getBlockEntity(this);

    if (blockEntity instanceof BlockEntityChest) {
        return ContainerInventory.calculateRedstone(((BlockEntityChest) blockEntity).getInventory());
    }

    return super.getComparatorInputOverride();
}
 
Example #21
Source File: BlockBrewingStand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getComparatorInputOverride() {
    BlockEntity blockEntity = this.level.getBlockEntity(this);

    if (blockEntity instanceof BlockEntityBrewingStand) {
        return ContainerInventory.calculateRedstone(((BlockEntityBrewingStand) blockEntity).getInventory());
    }

    return super.getComparatorInputOverride();
}
 
Example #22
Source File: BlockFurnaceBurning.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getComparatorInputOverride() {
    BlockEntity blockEntity = this.level.getBlockEntity(this);

    if (blockEntity instanceof BlockEntityFurnace) {
        return ContainerInventory.calculateRedstone(((BlockEntityFurnace) blockEntity).getInventory());
    }

    return super.getComparatorInputOverride();
}
 
Example #23
Source File: BlockBannerStanding.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Item toItem() {
	BlockEntity blockEntity = this.level.getBlockEntity(this);
	if (blockEntity != null && blockEntity instanceof BlockEntityBanner) {
		int id = ((BlockEntityBanner) blockEntity).getBase();
		ListTag<CompoundTag> tag = ((BlockEntityBanner) blockEntity).getPatterns();
		ItemBanner item = new ItemBanner(id, 1);
		for (CompoundTag nbt : tag.getAll()) {
			item.addPatterns(nbt);
		}
		return item;
	} else {
		return new ItemBanner();
	}
}
 
Example #24
Source File: BlockNoteblock.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private BlockEntityMusic getBlockEntity() {
    BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
    if (blockEntity instanceof BlockEntityMusic) {
        return (BlockEntityMusic) blockEntity;
    }
    return null;
}
 
Example #25
Source File: BlockMobSpawner.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    if (item.getId() == Item.SPAWN_EGG) {
        BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
        if (blockEntity instanceof BlockEntityMobSpawner) {
            this.setDamage(item.getDamage());
            ((BlockEntityMobSpawner) blockEntity).setNetworkId(item.getDamage());
        }

        return true;
    }

    return false;
}
 
Example #26
Source File: BlockFlowerPot.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = getLevel().getBlockEntity(this);
    if (!(blockEntity instanceof BlockEntityFlowerPot)) return false;
    if (blockEntity.namedTag.getShort("item") != 0 || blockEntity.namedTag.getInt("mData") != 0) return false;
    int itemID;
    int itemMeta;
    if (!canPlaceIntoFlowerPot(item.getId())) {
        if (!canPlaceIntoFlowerPot(item.getBlock().getId())) {
            return true;
        }
        itemID = item.getBlock().getId();
        itemMeta = item.getDamage();
    } else {
        itemID = item.getId();
        itemMeta = item.getDamage();
    }
    blockEntity.namedTag.putShort("item", itemID);
    blockEntity.namedTag.putInt("data", itemMeta);

    this.setDamage(1);
    this.getLevel().setBlock(this, this, true);
    ((BlockEntityFlowerPot) blockEntity).spawnToAll();

    if (player.isSurvival()) {
        item.setCount(item.getCount() - 1);
        player.getInventory().setItemInHand(item.getCount() > 0 ? item : Item.get(Item.AIR));
    }
    return true;
}
 
Example #27
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean setItem(int index, Item item, boolean send) {
    item = item.clone();
    if (index < 0 || index >= this.size) {
        return false;
    } else if (item.getId() == 0 || item.getCount() <= 0) {
        return this.clear(index, send);
    }

    InventoryHolder holder = this.getHolder();
    if (holder instanceof Entity) {
        EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, this.getItem(index), item, index);
        Server.getInstance().getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            this.sendSlot(index, this.getViewers());
            return false;
        }

        item = ev.getNewItem();
    }

    if (holder instanceof BlockEntity) {
        ((BlockEntity) holder).setDirty();
    }

    Item old = this.getItem(index);
    this.slots.put(index, item.clone());
    this.onSlotChange(index, old, send);

    return true;
}
 
Example #28
Source File: BlockBrewingStand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int getComparatorInputOverride() {
    BlockEntity blockEntity = this.level.getBlockEntity(this);

    if (blockEntity instanceof BlockEntityBrewingStand) {
        return ContainerInventory.calculateRedstone(((BlockEntityBrewingStand) blockEntity).getInventory());
    }

    return super.getComparatorInputOverride();
}
 
Example #29
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 #30
Source File: BlockJukebox.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onBreak(Item item) {
    if (super.onBreak(item)) {
        BlockEntity blockEntity = this.level.getBlockEntity(this);

        if (blockEntity instanceof BlockEntityJukebox) {
            ((BlockEntityJukebox) blockEntity).dropItem();
        }
        return true;
    }

    return false;
}