Search in sources :

Example 6 with UpdateBlockPacket

use of com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket in project Geyser by GeyserMC.

the class DoubleChestInventoryTranslator method prepareInventory.

@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
    // See BlockInventoryHolder - same concept there except we're also dealing with a specific block state
    if (session.getLastInteractionPlayerPosition().equals(session.getPlayerEntity().getPosition())) {
        int javaBlockId = session.getGeyser().getWorldManager().getBlockAt(session, session.getLastInteractionBlockPosition());
        String[] javaBlockString = BlockRegistries.JAVA_IDENTIFIERS.get().getOrDefault(javaBlockId, "minecraft:air").split("\\[");
        if (javaBlockString.length > 1 && (javaBlockString[0].equals("minecraft:chest") || javaBlockString[0].equals("minecraft:trapped_chest")) && !javaBlockString[1].contains("type=single")) {
            inventory.setHolderPosition(session.getLastInteractionBlockPosition());
            ((Container) inventory).setUsingRealBlock(true, javaBlockString[0]);
            NbtMapBuilder tag = NbtMap.builder().putString("id", "Chest").putInt("x", session.getLastInteractionBlockPosition().getX()).putInt("y", session.getLastInteractionBlockPosition().getY()).putInt("z", session.getLastInteractionBlockPosition().getZ()).putString("CustomName", inventory.getTitle()).putString("id", "Chest");
            DoubleChestValue chestValue = BlockStateValues.getDoubleChestValues().get(javaBlockId);
            DoubleChestBlockEntityTranslator.translateChestValue(tag, chestValue, session.getLastInteractionBlockPosition().getX(), session.getLastInteractionBlockPosition().getZ());
            BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
            dataPacket.setData(tag.build());
            dataPacket.setBlockPosition(session.getLastInteractionBlockPosition());
            session.sendUpstreamPacket(dataPacket);
            return;
        }
    }
    Vector3i position = session.getPlayerEntity().getPosition().toInt().add(Vector3i.UP);
    Vector3i pairPosition = position.add(Vector3i.UNIT_X);
    int bedrockBlockId = session.getBlockMappings().getBedrockBlockId(defaultJavaBlockState);
    UpdateBlockPacket blockPacket = new UpdateBlockPacket();
    blockPacket.setDataLayer(0);
    blockPacket.setBlockPosition(position);
    blockPacket.setRuntimeId(bedrockBlockId);
    blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
    session.sendUpstreamPacket(blockPacket);
    NbtMap tag = NbtMap.builder().putString("id", "Chest").putInt("x", position.getX()).putInt("y", position.getY()).putInt("z", position.getZ()).putInt("pairx", pairPosition.getX()).putInt("pairz", pairPosition.getZ()).putString("CustomName", inventory.getTitle()).build();
    BlockEntityDataPacket dataPacket = new BlockEntityDataPacket();
    dataPacket.setData(tag);
    dataPacket.setBlockPosition(position);
    session.sendUpstreamPacket(dataPacket);
    blockPacket = new UpdateBlockPacket();
    blockPacket.setDataLayer(0);
    blockPacket.setBlockPosition(pairPosition);
    blockPacket.setRuntimeId(bedrockBlockId);
    blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
    session.sendUpstreamPacket(blockPacket);
    tag = NbtMap.builder().putString("id", "Chest").putInt("x", pairPosition.getX()).putInt("y", pairPosition.getY()).putInt("z", pairPosition.getZ()).putInt("pairx", position.getX()).putInt("pairz", position.getZ()).putString("CustomName", inventory.getTitle()).build();
    dataPacket = new BlockEntityDataPacket();
    dataPacket.setData(tag);
    dataPacket.setBlockPosition(pairPosition);
    session.sendUpstreamPacket(dataPacket);
    inventory.setHolderPosition(position);
}
Also used : Container(org.geysermc.geyser.inventory.Container) Vector3i(com.nukkitx.math.vector.Vector3i) NbtMap(com.nukkitx.nbt.NbtMap) DoubleChestValue(org.geysermc.geyser.level.block.DoubleChestValue) BlockEntityDataPacket(com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder) UpdateBlockPacket(com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)

Example 7 with UpdateBlockPacket

use of com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket in project Geyser by GeyserMC.

the class ItemFrameEntity method despawnEntity.

@Override
public boolean despawnEntity() {
    UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
    updateBlockPacket.setDataLayer(0);
    updateBlockPacket.setBlockPosition(bedrockPosition);
    // TODO maybe set this to the world block or another item frame?
    updateBlockPacket.setRuntimeId(session.getBlockMappings().getBedrockAirId());
    updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.PRIORITY);
    updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NETWORK);
    updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
    session.sendUpstreamPacket(updateBlockPacket);
    session.getItemFrameCache().remove(bedrockPosition, this);
    valid = false;
    return true;
}
Also used : UpdateBlockPacket(com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)

Example 8 with UpdateBlockPacket

use of com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket in project Geyser by GeyserMC.

the class BlockInventoryHolder method closeInventory.

@Override
public void closeInventory(InventoryTranslator translator, GeyserSession session, Inventory inventory) {
    if (((Container) inventory).isUsingRealBlock()) {
        // No need to reset a block since we didn't change any blocks
        // But send a container close packet because we aren't destroying the original.
        ContainerClosePacket packet = new ContainerClosePacket();
        packet.setId((byte) inventory.getId());
        // TODO needs to be changed in Protocol to "server-side" or something
        packet.setUnknownBool0(true);
        session.sendUpstreamPacket(packet);
        return;
    }
    Vector3i holderPos = inventory.getHolderPosition();
    Position pos = new Position(holderPos.getX(), holderPos.getY(), holderPos.getZ());
    int realBlock = session.getGeyser().getWorldManager().getBlockAt(session, pos.getX(), pos.getY(), pos.getZ());
    UpdateBlockPacket blockPacket = new UpdateBlockPacket();
    blockPacket.setDataLayer(0);
    blockPacket.setBlockPosition(holderPos);
    blockPacket.setRuntimeId(session.getBlockMappings().getBedrockBlockId(realBlock));
    blockPacket.getFlags().addAll(UpdateBlockPacket.FLAG_ALL_PRIORITY);
    session.sendUpstreamPacket(blockPacket);
}
Also used : Container(org.geysermc.geyser.inventory.Container) Position(com.github.steveice10.mc.protocol.data.game.entity.metadata.Position) ContainerClosePacket(com.nukkitx.protocol.bedrock.packet.ContainerClosePacket) Vector3i(com.nukkitx.math.vector.Vector3i) UpdateBlockPacket(com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)

Example 9 with UpdateBlockPacket

use of com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket in project Geyser by GeyserMC.

the class ChunkUtils method updateBlock.

/**
 * Sends a block update to the Bedrock client. If chunk caching is enabled and the platform is not Spigot, this also
 * adds that block to the cache.
 * @param session the Bedrock session to send/register the block to
 * @param blockState the Java block state of the block
 * @param position the position of the block
 */
public static void updateBlock(GeyserSession session, int blockState, Vector3i position) {
    // Checks for item frames so they aren't tripped up and removed
    ItemFrameEntity itemFrameEntity = ItemFrameEntity.getItemFrameEntity(session, position);
    if (itemFrameEntity != null) {
        if (blockState == JAVA_AIR_ID) {
            // Item frame is still present and no block overrides that; refresh it
            itemFrameEntity.updateBlock(true);
            // Still update the chunk cache with the new block
            session.getChunkCache().updateBlock(position.getX(), position.getY(), position.getZ(), blockState);
            return;
        }
    // Otherwise, let's still store our reference to the item frame, but let the new block take precedence for now
    }
    SkullPlayerEntity skull = session.getSkullCache().get(position);
    if (skull != null && blockState != skull.getBlockState()) {
        // Skull is gone
        skull.despawnEntity(position);
    }
    // It's used for extending piston heads, but it isn't needed on Bedrock and causes pistons to flicker
    if (!BlockStateValues.isMovingPiston(blockState)) {
        int blockId = session.getBlockMappings().getBedrockBlockId(blockState);
        UpdateBlockPacket updateBlockPacket = new UpdateBlockPacket();
        updateBlockPacket.setDataLayer(0);
        updateBlockPacket.setBlockPosition(position);
        updateBlockPacket.setRuntimeId(blockId);
        updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NEIGHBORS);
        updateBlockPacket.getFlags().add(UpdateBlockPacket.Flag.NETWORK);
        session.sendUpstreamPacket(updateBlockPacket);
        UpdateBlockPacket waterPacket = new UpdateBlockPacket();
        waterPacket.setDataLayer(1);
        waterPacket.setBlockPosition(position);
        if (BlockRegistries.WATERLOGGED.get().contains(blockState)) {
            waterPacket.setRuntimeId(session.getBlockMappings().getBedrockWaterId());
        } else {
            waterPacket.setRuntimeId(session.getBlockMappings().getBedrockAirId());
        }
        session.sendUpstreamPacket(waterPacket);
    }
    BlockStateValues.getLecternBookStates().handleBlockChange(session, blockState, position);
    // needs to be sent
    for (BedrockOnlyBlockEntity bedrockOnlyBlockEntity : BlockEntityUtils.BEDROCK_ONLY_BLOCK_ENTITIES) {
        if (bedrockOnlyBlockEntity.isBlock(blockState)) {
            // Flower pots are block entities only in Bedrock and are not updated anywhere else like note blocks
            bedrockOnlyBlockEntity.updateBlock(session, blockState, position);
            // No block will be a part of two classes
            break;
        }
    }
    session.getChunkCache().updateBlock(position.getX(), position.getY(), position.getZ(), blockState);
}
Also used : ItemFrameEntity(org.geysermc.geyser.entity.type.ItemFrameEntity) SkullPlayerEntity(org.geysermc.geyser.entity.type.player.SkullPlayerEntity) BedrockOnlyBlockEntity(org.geysermc.geyser.translator.level.block.entity.BedrockOnlyBlockEntity) UpdateBlockPacket(com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)

Aggregations

UpdateBlockPacket (com.nukkitx.protocol.bedrock.packet.UpdateBlockPacket)9 Vector3i (com.nukkitx.math.vector.Vector3i)5 Container (org.geysermc.geyser.inventory.Container)4 NbtMap (com.nukkitx.nbt.NbtMap)2 BlockEntityDataPacket (com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket)2 ContainerClosePacket (com.nukkitx.protocol.bedrock.packet.ContainerClosePacket)2 Position (com.github.steveice10.mc.protocol.data.game.entity.metadata.Position)1 NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)1 LevelChunkPacket (com.nukkitx.protocol.bedrock.packet.LevelChunkPacket)1 ItemFrameEntity (org.geysermc.geyser.entity.type.ItemFrameEntity)1 SkullPlayerEntity (org.geysermc.geyser.entity.type.player.SkullPlayerEntity)1 DoubleChestValue (org.geysermc.geyser.level.block.DoubleChestValue)1 BedrockOnlyBlockEntity (org.geysermc.geyser.translator.level.block.entity.BedrockOnlyBlockEntity)1