com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket Java Examples

The following examples show how to use com.nukkitx.protocol.bedrock.packet.BlockEntityDataPacket. 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: 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 #2
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 #3
Source File: BlockEntityDataSerializer_v340.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #4
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 #5
Source File: BlockEntityDataSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #6
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 #7
Source File: BlockEntityDataSerializer_v332.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #8
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 #9
Source File: BlockEntityDataSerializer_v361.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #10
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 #11
Source File: BlockEntityDataSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #12
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 #13
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 #14
Source File: BlockEntityDataSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #15
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 #16
Source File: BlockEntityDataSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ByteBuf buffer, BlockEntityDataPacket packet) {
    BedrockUtils.writeBlockPosition(buffer, packet.getBlockPosition());
    try (NBTOutputStream writer = NbtUtils.createNetworkWriter(new ByteBufOutputStream(buffer))) {
        writer.write(packet.getData());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #17
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 #18
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 #19
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);
}
 
Example #20
Source File: BedrockBlockEntityDataTranslator.java    From Geyser with MIT License 4 votes vote down vote up
@Override
public void translate(BlockEntityDataPacket packet, GeyserSession session) {
    if (packet.getData() instanceof CompoundTag) {
        CompoundTag tag = (CompoundTag) packet.getData();
        if (tag.getString("id").equals("Sign")) {
            // This is the reason why this all works - Bedrock sends packets every time you update the sign, Java only wants the final packet
            // But Bedrock sends one final packet when you're done editing the sign, which should be equal to the last message since there's no edits
            // So if the latest update does not match the last cached update then it's still being edited
            Position pos = new Position(tag.getInt("x"), tag.getInt("y"), tag.getInt("z"));
            if (!tag.getString("Text").equals(lastMessages.get(pos))) {
                lastMessages.put(pos, tag.getString("Text"));
                return;
            }
            // Otherwise the two messages are identical and we can get to work deconstructing
            StringBuilder newMessage = new StringBuilder();
            // While Bedrock's sign lines are one string, Java's is an array of each line
            // (Initialized all with empty strings because it complains about null)
            String[] lines = new String[] {"", "", "", ""};
            int iterator = 0;
            // This converts the message into the array'd message Java wants
            for (char character : tag.getString("Text").toCharArray()) {
                // If we get a return in Bedrock, that signals to use the next line.
                if (character == '\n') {
                    lines[iterator] = newMessage.toString();
                    iterator++;
                    // Bedrock, for whatever reason, can hold a message out of bounds
                    // We don't care about that so we discard that
                    if (iterator > lines.length - 1) {
                        break;
                    }
                    newMessage = new StringBuilder();
                } else newMessage.append(character);
            }
            // Put the final line on since it isn't done in the for loop
            if (iterator < lines.length) lines[iterator] = newMessage.toString();
            ClientUpdateSignPacket clientUpdateSignPacket = new ClientUpdateSignPacket(pos, lines);
            session.sendDownstreamPacket(clientUpdateSignPacket);
            //TODO (potentially): originally I was going to update the sign blocks so Bedrock and Java users would match visually
            // However Java can still store a lot per-line and visuals are still messed up so that doesn't work

            // We remove the sign position from map to indicate there is no work-in-progress sign
            lastMessages.remove(pos);
        }
    }

}