Java Code Examples for com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket#setData()

The following examples show how to use com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket#setData() . 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: ItemFrameEntity.java    From Geyser with MIT License 6 votes vote down vote up
/**
 * Updates the item frame as a block
 * @param session GeyserSession.
 */
public void updateBlock(GeyserSession session) {
    UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
    updateBlockPacket.setDataLayer(0);
    updateBlockPacket.setBlockPosition(bedrockPosition);
    updateBlockPacket.setRuntimeId(bedrockRuntimeId);
    updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
    updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NONE);
    updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
    session.sendUpstreamPacket(updateBlockPacket);

    BlockEntityDataPacket blockEntityDataPacket = new BlockEntityDataPacket();
    blockEntityDataPacket.setBlockPosition(bedrockPosition);
    if (cachedTag != null) {
        blockEntityDataPacket.setData(cachedTag);
    } else {
        blockEntityDataPacket.setData(getDefaultTag());
    }

    session.sendUpstreamPacket(blockEntityDataPacket);
}
 
Example 2
Source File: BlockInventoryHolder.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void prepareInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
    Vector3i position = session.getPlayerEntity().getPosition().toInt();
    position = position.add(Vector3i.UP);
    UpdateBlockPacket blockPacket = new UpdateBlockPacket();
    blockPacket.setDataLayer(0);
    blockPacket.setBlockPosition(position);
    blockPacket.setRuntimeId(blockId);
    blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
    session.sendUpstreamPacket(blockPacket);
    inventory.setHolderPosition(position);

    CompoundTag tag = CompoundTag.builder()
            .intTag("x", position.getX())
            .intTag("y", position.getY())
            .intTag("z", position.getZ())
            .stringTag("CustomName", LocaleUtils.getLocaleString(inventory.getTitle(), session.getClientData().getLanguageCode())).buildRootTag();
    BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
    dataPacket.setData(tag);
    dataPacket.setBlockPosition(position);
    session.sendUpstreamPacket(dataPacket);
}
 
Example 3
Source File: JavaBlockValueTranslator.java    From Geyser with MIT License 5 votes vote down vote up
/**
 * Emulating a piston extending
 * @param session GeyserSession
 * @param position Block position
 * @param progress How far the piston is
 * @param lastProgress How far the piston last was
 */
private void extendPiston(GeyserSession session, Vector3i position, float progress, float lastProgress) {
    BlockEntityDataPacket blockEntityDataPacket = new BlockEntityDataPacket();
    blockEntityDataPacket.setBlockPosition(position);
    byte state = (byte) ((progress == 1.0f && lastProgress == 1.0f) ? 2 : 1);
    blockEntityDataPacket.setData(buildPistonTag(position, progress, lastProgress, state));
    session.sendUpstreamPacket(blockEntityDataPacket);
    if (lastProgress != 1.0f) {
        session.getConnector().getGeneralThreadPool().schedule(() ->
                        extendPiston(session, position, (progress >= 1.0f) ? 1.0f : progress + 0.5f, progress),
                20, TimeUnit.MILLISECONDS);
    }
}
 
Example 4
Source File: JavaBlockValueTranslator.java    From Geyser with MIT License 5 votes vote down vote up
/**
 * Emulate a piston retracting.
 * @param session GeyserSession
 * @param position Block position
 * @param progress Current progress of piston
 * @param lastProgress Last progress of piston
 */
private void retractPiston(GeyserSession session, Vector3i position, float progress, float lastProgress) {
    BlockEntityDataPacket blockEntityDataPacket = new BlockEntityDataPacket();
    blockEntityDataPacket.setBlockPosition(position);
    byte state = (byte) ((progress == 0.0f && lastProgress == 0.0f) ? 0 : 3);
    blockEntityDataPacket.setData(buildPistonTag(position, progress, lastProgress, state));
    session.sendUpstreamPacket(blockEntityDataPacket);
    if (lastProgress != 0.0f) {
        session.getConnector().getGeneralThreadPool().schedule(() ->
                        retractPiston(session, position, (progress <= 0.0f) ? 0.0f : progress - 0.5f, progress),
                20, TimeUnit.MILLISECONDS);
    }
}
 
Example 5
Source File: BlockEntityDataSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setData(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: BlockEntityDataSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setData(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: BlockEntityDataSerializer_v340.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setData(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: BlockEntityDataSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setData(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: BlockEntityDataSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setData(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: BlockEntityDataSerializer_v332.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setData(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 11
Source File: BlockEntityDataSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    try (NBTInputStream reader = NbtUtils.createNetworkReader(new ByteBufInputStream(buffer))) {
        packet.setData(reader.readTag());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 12
Source File: BlockEntityUtils.java    From Geyser with MIT License 4 votes vote down vote up
public static void updateBlockEntity(GeyserSession session, com.nukkitx.nbt.tag.CompoundTag blockEntity, Vector3i position) {
    BlockEntityDataPacket blockEntityPacket = new BlockEntityDataPacket();
    blockEntityPacket.setBlockPosition(position);
    blockEntityPacket.setData(blockEntity);
    session.sendUpstreamPacket(blockEntityPacket);
}