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

The following examples show how to use com.nukkitx.network.VarInts#readInt() . 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: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static InventorySource readInventorySource(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    InventorySource.Type type = InventorySource.Type.byId(VarInts.readUnsignedInt(buffer));

    switch (type) {
        case CONTAINER:
            int containerId = VarInts.readInt(buffer);
            return InventorySource.fromContainerWindowId(containerId);
        case GLOBAL:
            return InventorySource.fromGlobalInventory();
        case WORLD_INTERACTION:
            InventorySource.Flag flag = InventorySource.Flag.values()[VarInts.readUnsignedInt(buffer)];
            return InventorySource.fromWorldInteraction(flag);
        case CREATIVE:
            return InventorySource.fromCreativeInventory();
        case UNTRACKED_INTERACTION_UI:
            containerId = VarInts.readInt(buffer);
            return InventorySource.fromUntrackedInteractionUI(containerId);
        case NON_IMPLEMENTED_TODO:
            containerId = VarInts.readInt(buffer);
            return InventorySource.fromNonImplementedTodo(containerId);
        default:
            return InventorySource.fromInvalid();
    }
}
 
Example 2
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static InventorySource readInventorySource(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    InventorySource.Type type = InventorySource.Type.byId(VarInts.readUnsignedInt(buffer));

    switch (type) {
        case CONTAINER:
            int containerId = VarInts.readInt(buffer);
            return InventorySource.fromContainerWindowId(containerId);
        case GLOBAL:
            return InventorySource.fromGlobalInventory();
        case WORLD_INTERACTION:
            InventorySource.Flag flag = InventorySource.Flag.values()[VarInts.readUnsignedInt(buffer)];
            return InventorySource.fromWorldInteraction(flag);
        case CREATIVE:
            return InventorySource.fromCreativeInventory();
        case UNTRACKED_INTERACTION_UI:
            containerId = VarInts.readInt(buffer);
            return InventorySource.fromUntrackedInteractionUI(containerId);
        case NON_IMPLEMENTED_TODO:
            containerId = VarInts.readInt(buffer);
            return InventorySource.fromNonImplementedTodo(containerId);
        default:
            return InventorySource.fromInvalid();
    }
}
 
Example 3
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static InventorySource readInventorySource(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    InventorySource.Type type = InventorySource.Type.byId(VarInts.readUnsignedInt(buffer));

    switch (type) {
        case CONTAINER:
            int containerId = VarInts.readInt(buffer);
            return InventorySource.fromContainerWindowId(containerId);
        case GLOBAL:
            return InventorySource.fromGlobalInventory();
        case WORLD_INTERACTION:
            InventorySource.Flag flag = InventorySource.Flag.values()[VarInts.readUnsignedInt(buffer)];
            return InventorySource.fromWorldInteraction(flag);
        case CREATIVE:
            return InventorySource.fromCreativeInventory();
        case UNTRACKED_INTERACTION_UI:
            containerId = VarInts.readInt(buffer);
            return InventorySource.fromUntrackedInteractionUI(containerId);
        case NON_IMPLEMENTED_TODO:
            containerId = VarInts.readInt(buffer);
            return InventorySource.fromNonImplementedTodo(containerId);
        default:
            return InventorySource.fromInvalid();
    }
}
 
Example 4
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 5
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3i readVector3i(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    int x = VarInts.readInt(buffer);
    int y = VarInts.readInt(buffer);
    int z = VarInts.readInt(buffer);

    return Vector3i.from(x, y, z);
}
 
Example 6
Source File: MoveEntityDeltaSerializer_v340.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, MoveEntityDeltaPacket packet) {
    packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
    byte flags = buffer.readByte();
    int x = 0, y = 0, z = 0;
    float pitch = 0, yaw = 0, roll = 0;
    if ((flags & HAS_X) != 0) {
        x = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Y) != 0) {
        y = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Z) != 0) {
        z = VarInts.readInt(buffer);
    }
    if ((flags & HAS_PITCH) != 0) {
        pitch = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_YAW) != 0) {
        yaw = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_ROLL) != 0) {
        roll = BedrockUtils.readByteAngle(buffer);
    }
    packet.setMovementDelta(Vector3i.from(x, y, z));
    packet.setRotationDelta(Vector3f.from(pitch, yaw, roll));
}
 
Example 7
Source File: MoveEntityDeltaSerializer_v332.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, MoveEntityDeltaPacket packet) {
    packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
    byte flags = buffer.readByte();
    int x = 0, y = 0, z = 0;
    float pitch = 0, yaw = 0, roll = 0;
    if ((flags & HAS_X) != 0) {
        x = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Y) != 0) {
        y = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Z) != 0) {
        z = VarInts.readInt(buffer);
    }
    if ((flags & HAS_PITCH) != 0) {
        pitch = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_YAW) != 0) {
        yaw = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_ROLL) != 0) {
        roll = BedrockUtils.readByteAngle(buffer);
    }
    packet.setMovementDelta(Vector3i.from(x, y, z));
    packet.setRotationDelta(Vector3f.from(pitch, yaw, roll));
}
 
Example 8
Source File: LevelEventSerializer_v291.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, LevelEventPacket packet) {
    int eventId = VarInts.readInt(buffer);
    packet.setType(events.get(eventId));
    if (packet.getType() == null) {
        log.debug("Unknown Level Event {} received", eventId);
    }
    packet.setPosition(BedrockUtils.readVector3f(buffer));
    packet.setData(VarInts.readInt(buffer));
}
 
Example 9
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 10
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3i readVector3i(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    int x = VarInts.readInt(buffer);
    int y = VarInts.readInt(buffer);
    int z = VarInts.readInt(buffer);

    return Vector3i.from(x, y, z);
}
 
Example 11
Source File: MoveEntityDeltaSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, MoveEntityDeltaPacket packet) {
    packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
    short flags = buffer.readShortLE();
    int x = 0, y = 0, z = 0;
    float pitch = 0, yaw = 0, roll = 0;
    if ((flags & HAS_X) != 0) {
        x = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Y) != 0) {
        y = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Z) != 0) {
        z = VarInts.readInt(buffer);
    }
    if ((flags & HAS_PITCH) != 0) {
        pitch = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_YAW) != 0) {
        yaw = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_ROLL) != 0) {
        roll = BedrockUtils.readByteAngle(buffer);
    }
    packet.setMovementDelta(Vector3i.from(x, y, z));
    packet.setRotationDelta(Vector3f.from(pitch, yaw, roll));
}
 
Example 12
Source File: LevelEventSerializer_v388.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, LevelEventPacket packet) {
    int eventId = VarInts.readInt(buffer);
    packet.setType(events.get(eventId));
    if (packet.getType() == null) {
        log.debug("Unknown Level Event {} received", eventId);
    }
    packet.setPosition(BedrockUtils.readVector3f(buffer));
    packet.setData(VarInts.readInt(buffer));
}
 
Example 13
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static ItemData readRecipeIngredient(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    int id = VarInts.readInt(buffer);
    int auxValue = 0;
    int stackSize = 0;

    if (id != 0) {
        auxValue = VarInts.readInt(buffer);
        if (auxValue == 0x7fff) auxValue = -1;
        stackSize = VarInts.readInt(buffer);
    }

    return ItemData.of(id, (short) auxValue, stackSize);
}
 
Example 14
Source File: LevelEventSerializer_v313.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, LevelEventPacket packet) {
    int eventId = VarInts.readInt(buffer);
    packet.setType(events.get(eventId));
    if (packet.getType() == null) {
        log.debug("Unknown Level Event {} received", eventId);
    }
    packet.setPosition(BedrockUtils.readVector3f(buffer));
    packet.setData(VarInts.readInt(buffer));
}
 
Example 15
Source File: MoveEntityDeltaSerializer_v354.java    From Protocol with Apache License 2.0 5 votes vote down vote up
@Override
public void deserialize(ByteBuf buffer, MoveEntityDeltaPacket packet) {
    packet.setRuntimeEntityId(VarInts.readUnsignedLong(buffer));
    byte flags = buffer.readByte();
    int x = 0, y = 0, z = 0;
    float pitch = 0, yaw = 0, roll = 0;
    if ((flags & HAS_X) != 0) {
        x = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Y) != 0) {
        y = VarInts.readInt(buffer);
    }
    if ((flags & HAS_Z) != 0) {
        z = VarInts.readInt(buffer);
    }
    if ((flags & HAS_PITCH) != 0) {
        pitch = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_YAW) != 0) {
        yaw = BedrockUtils.readByteAngle(buffer);
    }
    if ((flags & HAS_ROLL) != 0) {
        roll = BedrockUtils.readByteAngle(buffer);
    }
    packet.setMovementDelta(Vector3i.from(x, y, z));
    packet.setRotationDelta(Vector3f.from(pitch, yaw, roll));
}
 
Example 16
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 17
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static ItemData readRecipeIngredient(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    int id = VarInts.readInt(buffer);
    int auxValue = 0;
    int stackSize = 0;

    if (id != 0) {
        auxValue = VarInts.readInt(buffer);
        if (auxValue == 0x7fff) auxValue = -1;
        stackSize = VarInts.readInt(buffer);
    }

    return ItemData.of(id, (short) auxValue, stackSize);
}
 
Example 18
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 19
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);
        }
    }
}
 
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);
        }
    }
}