Java Code Examples for com.nukkitx.network.VarInts#readUnsignedInt()

The following examples show how to use com.nukkitx.network.VarInts#readUnsignedInt() . 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: PlayerListSerializer_v332.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, PlayerListPacket packet) {
    Action action = Action.values()[buffer.readUnsignedByte()];
    packet.setAction(action);
    int length = VarInts.readUnsignedInt(buffer);

    for (int i = 0; i < length; i++) {
        Entry entry = new Entry(BedrockUtils.readUuid(buffer));

        if (action == Action.ADD) {
            entry.setEntityId(VarInts.readLong(buffer));
            entry.setName(BedrockUtils.readString(buffer));
            String skinId = BedrockUtils.readString(buffer);
            ImageData skinData = ImageData.of(BedrockUtils.readByteArray(buffer));
            ImageData capeData = ImageData.of(64, 32, BedrockUtils.readByteArray(buffer));
            String geometryName = BedrockUtils.readString(buffer);
            String geometryData = BedrockUtils.readString(buffer);
            entry.setSkin(SerializedSkin.of(skinId, skinData, capeData, geometryName, geometryData, false));
            entry.setXuid(BedrockUtils.readString(buffer));
            entry.setPlatformChatId(BedrockUtils.readString(buffer));
        }
        packet.getEntries().add(entry);
    }
}
 
Example 2
Source File: AdventureSettingsSerializer_v332.java    From Protocol with Apache License 2.0 6 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, AdventureSettingsPacket packet) {
    int flags1 = VarInts.readUnsignedInt(buffer);
    packet.setCommandPermission(COMMAND_PERMISSIONS[VarInts.readUnsignedInt(buffer)]);
    int flags2 = VarInts.readUnsignedInt(buffer);
    packet.setPlayerPermission(PLAYER_PERMISSIONS[VarInts.readUnsignedInt(buffer)]);
    VarInts.readUnsignedInt(buffer); // useless
    packet.setUniqueEntityId(buffer.readLongLE());

    Set<AdventureSettingsPacket.Flag> flags = packet.getFlags();
    for (int i = 0; i < FLAGS_1.length; i++) {
        if ((flags1 & (1 << i)) != 0) {
            flags.add(FLAGS_1[i]);
        }
    }

    for (int i = 0; i < FLAGS_2.length; i++) {
        if ((flags2 & (1 << i)) != 0) {
            flags.add(FLAGS_2[i]);
        }
    }
}
 
Example 3
Source File: PlayerListSerializer_v390.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, PlayerListPacket packet) {
    Action action = Action.values()[buffer.readUnsignedByte()];
    packet.setAction(action);
    int length = VarInts.readUnsignedInt(buffer);

    if (action == Action.ADD) {
        for (int i = 0; i < length; i++) {
            Entry entry = new Entry(BedrockUtils.readUuid(buffer));
            entry.setEntityId(VarInts.readLong(buffer));
            entry.setName(BedrockUtils.readString(buffer));
            entry.setXuid(BedrockUtils.readString(buffer));
            entry.setPlatformChatId(BedrockUtils.readString(buffer));
            entry.setBuildPlatform(buffer.readIntLE());
            entry.setSkin(BedrockUtils_v390.readSkin(buffer));
            entry.setTeacher(buffer.readBoolean());
            entry.setHost(buffer.readBoolean());
            packet.getEntries().add(entry);
        }

        for (int i = 0; i < length; i++) {
            packet.getEntries().get(i).setTrustedSkin(buffer.readBoolean());
        }
    } else {
        for (int i = 0; i < length; i++) {
            packet.getEntries().add(new Entry(BedrockUtils.readUuid(buffer)));
        }
    }
}
 
Example 4
Source File: StructureBlockUpdateSerializer_v340.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, StructureBlockUpdatePacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    int structureType = VarInts.readUnsignedInt(buffer);
    // Structure Editor Data start
    String name = BedrockUtils.readString(buffer);
    String dataField = BedrockUtils.readString(buffer);
    Vector3i offset = BedrockUtils.readBlockPosition(buffer);
    Vector3i size = BedrockUtils.readBlockPosition(buffer);
    buffer.readBoolean(); // include entities
    boolean ignoreBlocks = !buffer.readBoolean();
    boolean includePlayers = buffer.readBoolean();
    buffer.readBoolean(); // show air
    // Structure Settings start
    float structureIntegrity = buffer.readFloatLE();
    int integritySeed = VarInts.readUnsignedInt(buffer);
    int mirror = VarInts.readUnsignedInt(buffer);
    int rotation = VarInts.readUnsignedInt(buffer);
    boolean ignoreEntities = buffer.readBoolean();
    buffer.readBoolean(); // ignore structure bocks
    BedrockUtils.readVector3i(buffer); // bounding box min
    BedrockUtils.readVector3i(buffer); // bounding box max
    // Structure Settings end
    // Structure Editor Data end
    boolean boundingBoxVisible = buffer.readBoolean();

    StructureSettings settings = new StructureSettings("", ignoreEntities, ignoreBlocks, size, offset,
            -1, (byte) rotation, (byte) mirror, structureIntegrity, integritySeed);
    StructureEditorData editorData = new StructureEditorData(name, dataField, includePlayers, boundingBoxVisible,
            structureType, settings);

    packet.setEditorData(editorData);
    packet.setPowered(buffer.readBoolean());
}
 
Example 5
Source File: UpdateBlockSyncedSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateBlockSyncedPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    packet.setRuntimeId(VarInts.readUnsignedInt(buffer));
    int flagValue = VarInts.readUnsignedInt(buffer);
    Set<Flag> flags = packet.getFlags();
    for (Flag flag : Flag.values()) {
        if ((flagValue & (1 << flag.ordinal())) != 0) {
            flags.add(flag);
        }
    }
    packet.setDataLayer(VarInts.readUnsignedInt(buffer));
    packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
    packet.setUnknownLong1(VarInts.readUnsignedLong(buffer));
}
 
Example 6
Source File: StructureBlockUpdateSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, StructureBlockUpdatePacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    int structureType = VarInts.readUnsignedInt(buffer);
    // Structure Editor Data start
    String name = BedrockUtils.readString(buffer);
    String dataField = BedrockUtils.readString(buffer);
    Vector3i offset = BedrockUtils.readBlockPosition(buffer);
    Vector3i size = BedrockUtils.readBlockPosition(buffer);
    buffer.readBoolean(); // include entities
    boolean ignoreBlocks = !buffer.readBoolean();
    boolean includePlayers = buffer.readBoolean();
    buffer.readBoolean(); // show air
    // Structure Settings start
    float structureIntegrity = buffer.readFloatLE();
    int integritySeed = VarInts.readUnsignedInt(buffer);
    int mirror = VarInts.readUnsignedInt(buffer);
    int rotation = VarInts.readUnsignedInt(buffer);
    boolean ignoreEntities = buffer.readBoolean();
    buffer.readBoolean(); // ignore structure bocks
    BedrockUtils.readVector3i(buffer); // bounding box min
    BedrockUtils.readVector3i(buffer); // bounding box max
    // Structure Settings end
    // Structure Editor Data end
    boolean boundingBoxVisible = buffer.readBoolean();

    StructureSettings settings = new StructureSettings("", ignoreEntities, ignoreBlocks, size, offset,
            -1, (byte) rotation, (byte) mirror, structureIntegrity, integritySeed);
    StructureEditorData editorData = new StructureEditorData(name, dataField, includePlayers, boundingBoxVisible,
            structureType, settings);

    packet.setEditorData(editorData);
    packet.setPowered(buffer.readBoolean());
}
 
Example 7
Source File: BossEventSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, BossEventPacket packet) {
    packet.setBossUniqueEntityId(VarInts.readInt(buffer));
    BossEventPacket.Action action = BossEventPacket.Action.values()[VarInts.readUnsignedInt(buffer)];
    packet.setAction(action);

    switch (action) {
        case REGISTER_PLAYER:
        case UNREGISTER_PLAYER:
            packet.setPlayerUniqueEntityId(VarInts.readLong(buffer));
            break;
        case SHOW:
            packet.setTitle(BedrockUtils.readString(buffer));
            packet.setHealthPercentage(buffer.readFloatLE());
            // fall through
        case DARKEN_SKY:
            packet.setDarkenSky(buffer.readUnsignedShortLE());
            // fall through
        case OVERLAY:
            packet.setColor(VarInts.readUnsignedInt(buffer));
            packet.setOverlay(VarInts.readUnsignedInt(buffer));
            break;
        case HEALTH_PERCENTAGE:
            packet.setHealthPercentage(buffer.readFloatLE());
            break;
        case TITLE:
            packet.setTitle(BedrockUtils.readString(buffer));
            break;
    }
}
 
Example 8
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static CommandOriginData readCommandOriginData(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    CommandOriginData.Origin origin = CommandOriginData.Origin.values()[VarInts.readUnsignedInt(buffer)];
    UUID uuid = readUuid(buffer);
    String requestId = readString(buffer);
    long varLong = -1;
    if (origin == CommandOriginData.Origin.DEV_CONSOLE || origin == CommandOriginData.Origin.TEST) {
        varLong = VarInts.readLong(buffer);
    }
    return new CommandOriginData(origin, uuid, requestId, varLong);
}
 
Example 9
Source File: InventoryTransactionSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, InventoryTransactionPacket packet) {
    Type transactionType = Type.values()[VarInts.readUnsignedInt(buffer)];
    packet.setTransactionType(transactionType);

    BedrockUtils.readArray(buffer, packet.getActions(), BedrockUtils::readInventoryAction);

    switch (transactionType) {
        case ITEM_USE:
            packet.setActionType(VarInts.readUnsignedInt(buffer));
            packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
            packet.setFace(VarInts.readInt(buffer));
            packet.setHotbarSlot(VarInts.readInt(buffer));
            packet.setItemInHand(BedrockUtils.readItemData(buffer));
            packet.setPlayerPosition(BedrockUtils.readVector3f(buffer));
            packet.setClickPosition(BedrockUtils.readVector3f(buffer));
            packet.setBlockRuntimeId(VarInts.readUnsignedInt(buffer));
            break;
        case ITEM_USE_ON_ENTITY:
            packet.setRuntimeEntityId(VarInts.readUnsignedInt(buffer));
            packet.setActionType(VarInts.readUnsignedInt(buffer));
            packet.setHotbarSlot(VarInts.readInt(buffer));
            packet.setItemInHand(BedrockUtils.readItemData(buffer));
            packet.setPlayerPosition(BedrockUtils.readVector3f(buffer));
            packet.setClickPosition(BedrockUtils.readVector3f(buffer));
            break;
        case ITEM_RELEASE:
            packet.setActionType(VarInts.readUnsignedInt(buffer));
            packet.setHotbarSlot(VarInts.readInt(buffer));
            packet.setItemInHand(BedrockUtils.readItemData(buffer));
            packet.setHeadPosition(BedrockUtils.readVector3f(buffer));
    }
}
 
Example 10
Source File: PacketHeaderSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, PacketHeader packet) {
    int header = VarInts.readUnsignedInt(buffer);
    packet.setPacketId(header & 0x3ff);
    packet.setSenderId((header >>> 10) & 3);
    packet.setClientId((header >>> 12) & 3);
}
 
Example 11
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static CommandOutputMessage readCommandOutputMessage(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    boolean internal = buffer.readBoolean();
    String messageId = readString(buffer);
    String[] parameters = new String[VarInts.readUnsignedInt(buffer)];
    for (int i = 0; i < parameters.length; i++) {
        parameters[i] = BedrockUtils.readString(buffer);
    }
    return new CommandOutputMessage(internal, messageId, parameters);
}
 
Example 12
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3i readBlockPosition(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    int x = VarInts.readInt(buffer);
    int y = VarInts.readUnsignedInt(buffer);
    int z = VarInts.readInt(buffer);

    return Vector3i.from(x, y, z);
}
 
Example 13
Source File: FullChunkDataSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, LevelChunkPacket packet) {
    packet.setChunkX(VarInts.readInt(buffer));
    packet.setChunkZ(VarInts.readInt(buffer));
    byte[] data = new byte[VarInts.readUnsignedInt(buffer)];
    buffer.readBytes(data);
    packet.setData(data);
}
 
Example 14
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static CommandEnumData readCommandEnumData(ByteBuf buffer, boolean soft) {
    Preconditions.checkNotNull(buffer, "buffer");

    String name = BedrockUtils.readString(buffer);

    String[] values = new String[VarInts.readUnsignedInt(buffer)];
    for (int i = 0; i < values.length; i++) {
        values[i] = BedrockUtils.readString(buffer);
    }
    return new CommandEnumData(name, values, soft);
}
 
Example 15
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static CommandEnumData readCommandEnumData(ByteBuf buffer, boolean soft) {
    Preconditions.checkNotNull(buffer, "buffer");

    String name = BedrockUtils.readString(buffer);

    String[] values = new String[VarInts.readUnsignedInt(buffer)];
    for (int i = 0; i < values.length; i++) {
        values[i] = BedrockUtils.readString(buffer);
    }
    return new CommandEnumData(name, values, soft);
}
 
Example 16
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static <T> void readArray(ByteBuf buffer, Collection<T> array, Function<ByteBuf, T> function) {
    int length = VarInts.readUnsignedInt(buffer);


    for (int i = 0; i < length; i++) {
        array.add(function.apply(buffer));
    }
}
 
Example 17
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static CommandOriginData readCommandOriginData(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    CommandOriginData.Origin origin = CommandOriginData.Origin.values()[VarInts.readUnsignedInt(buffer)];
    UUID uuid = readUuid(buffer);
    String requestId = readString(buffer);
    long varLong = -1;
    if (origin == CommandOriginData.Origin.DEV_CONSOLE || origin == CommandOriginData.Origin.TEST) {
        varLong = VarInts.readLong(buffer);
    }
    return new CommandOriginData(origin, uuid, requestId, varLong);
}
 
Example 18
Source File: UpdateBlockSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, UpdateBlockPacket packet) {
    packet.setBlockPosition(BedrockUtils.readBlockPosition(buffer));
    packet.setRuntimeId(VarInts.readUnsignedInt(buffer));
    int flagValue = VarInts.readUnsignedInt(buffer);
    Set<Flag> flags = packet.getFlags();
    for (Flag flag : Flag.values()) {
        if ((flagValue & (1 << flag.ordinal())) != 0) {
            flags.add(flag);
        }
    }
    packet.setDataLayer(VarInts.readUnsignedInt(buffer));
}
 
Example 19
Source File: FullChunkDataSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, LevelChunkPacket packet) {
    packet.setChunkX(VarInts.readInt(buffer));
    packet.setChunkZ(VarInts.readInt(buffer));
    byte[] data = new byte[VarInts.readUnsignedInt(buffer)];
    buffer.readBytes(data);
    packet.setData(data);
}
 
Example 20
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void readEntityData(ByteBuf buffer, EntityDataMap entityDataMap) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(entityDataMap, "entityDataDictionary");

    int length = VarInts.readUnsignedInt(buffer);

    for (int i = 0; i < length; i++) {
        int metadataInt = VarInts.readUnsignedInt(buffer);
        EntityData entityData = METADATAS.get(metadataInt);
        EntityData.Type type = METADATA_TYPES.get(VarInts.readUnsignedInt(buffer));
        if (entityData != null && entityData.isFlags()) {
            if (type != Type.LONG) {
                throw new IllegalArgumentException("Expected long value for flags, got " + type.name());
            }
            type = Type.FLAGS;
        }

        Object object;
        switch (type) {
            case BYTE:
                object = buffer.readByte();
                break;
            case SHORT:
                object = buffer.readShortLE();
                break;
            case INT:
                object = VarInts.readInt(buffer);
                break;
            case FLOAT:
                object = buffer.readFloatLE();
                break;
            case STRING:
                object = BedrockUtils.readString(buffer);
                break;
            case NBT:
                object = BedrockUtils.readItemData(buffer);
                break;
            case VECTOR3I:
                object = BedrockUtils.readVector3i(buffer);
                break;
            case FLAGS:
                int index = entityData == FLAGS_2 ? 1 : 0;
                entityDataMap.getOrCreateFlags().set(VarInts.readLong(buffer), index, METADATA_FLAGS);
                continue;
            case LONG:
                object = VarInts.readLong(buffer);
                break;
            case VECTOR3F:
                object = BedrockUtils.readVector3f(buffer);
                break;
            default:
                throw new IllegalArgumentException("Unknown entity data type received");
        }
        if (entityData != null) {
            entityDataMap.put(entityData, object);
        } else {
            log.debug("Unknown entity data: {} type {} value {}", metadataInt, type, object);
        }
    }
}