Java Code Examples for com.nukkitx.network.util.Preconditions#checkNotNull()

The following examples show how to use com.nukkitx.network.util.Preconditions#checkNotNull() . 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 List<ResourcePacksInfoPacket.Entry> readPacksInfoEntries(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    List<ResourcePacksInfoPacket.Entry> entries = new ObjectArrayList<>();
    int length = buffer.readUnsignedShortLE();
    for (int i = 0; i < length; i++) {
        String packId = readString(buffer);
        String packVersion = readString(buffer);
        long packSize = buffer.readLongLE();
        String encryptionKey = readString(buffer);
        String subpackName = readString(buffer);
        String contentId = readString(buffer);
        boolean unknownBool = buffer.readBoolean();
        entries.add(new ResourcePacksInfoPacket.Entry(packId, packVersion, packSize, encryptionKey, subpackName, contentId, unknownBool));
    }
    return entries;
}
 
Example 2
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readByteRotation(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float pitch = readByteAngle(buffer);
    float yaw = readByteAngle(buffer);
    float roll = readByteAngle(buffer);
    return Vector3f.from(pitch, yaw, roll);
}
 
Example 3
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 4
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Attribute readEntityAttribute(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    String name = BedrockUtils.readString(buffer);
    float min = buffer.readFloatLE();
    float max = buffer.readFloatLE();
    float val = buffer.readFloatLE();

    return new Attribute(name, min, max, val, max);
}
 
Example 5
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeEntityLink(ByteBuf buffer, EntityLink entityLink) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(entityLink, "entityLink");

    VarInts.writeLong(buffer, entityLink.getFrom());
    VarInts.writeLong(buffer, entityLink.getTo());
    buffer.writeByte(entityLink.getType().ordinal());
    buffer.writeBoolean(entityLink.isImmediate());
}
 
Example 6
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeByteRotation(ByteBuf buffer, Vector3f rotation) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(rotation, "rotation");
    writeByteAngle(buffer, rotation.getX());
    writeByteAngle(buffer, rotation.getY());
    writeByteAngle(buffer, rotation.getZ());
}
 
Example 7
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 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: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writePacksInfoEntries(ByteBuf buffer, Collection<ResourcePacksInfoPacket.Entry> packInfoEntries) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(packInfoEntries, "packInfoEntries");
    buffer.writeShortLE(packInfoEntries.size());
    for (ResourcePacksInfoPacket.Entry packInfoEntry : packInfoEntries) {
        writeString(buffer, packInfoEntry.getPackId());
        writeString(buffer, packInfoEntry.getPackVersion());
        buffer.writeLongLE(packInfoEntry.getPackSize());
        writeString(buffer, packInfoEntry.getEncryptionKey());
        writeString(buffer, packInfoEntry.getSubpackName());
        writeString(buffer, packInfoEntry.getContentId());
        buffer.writeBoolean(packInfoEntry.isScripting());
    }
}
 
Example 10
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeCommandEnumData(ByteBuf buffer, CommandEnumData enumData) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(enumData, "enumData");

    BedrockUtils.writeString(buffer, enumData.getName());

    String[] values = enumData.getValues();
    VarInts.writeUnsignedInt(buffer, values.length);
    for (String value : values) {
        BedrockUtils.writeString(buffer, value);
    }
}
 
Example 11
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readVector3f(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float x = buffer.readFloatLE();
    float y = buffer.readFloatLE();
    float z = buffer.readFloatLE();
    return Vector3f.from(x, y, z);
}
 
Example 12
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static Vector3f readVector3f(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    float x = buffer.readFloatLE();
    float y = buffer.readFloatLE();
    float z = buffer.readFloatLE();
    return Vector3f.from(x, y, z);
}
 
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: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static ResourcePackStackPacket.Entry readPackInstanceEntry(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    String packId = readString(buffer);
    String packVersion = readString(buffer);
    String subpackName = readString(buffer);
    return new ResourcePackStackPacket.Entry(packId, packVersion, subpackName);
}
 
Example 15
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static String readString(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    return new String(readByteArray(buffer), StandardCharsets.UTF_8);
}
 
Example 16
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 17
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeByteAngle(ByteBuf buffer, float angle) {
    Preconditions.checkNotNull(buffer, "buffer");
    buffer.writeByte((byte) (angle / (360f / 256f)));
}
 
Example 18
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeLEAsciiString(ByteBuf buffer, AsciiString string) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(string, "string");
    buffer.writeIntLE(string.length());
    buffer.writeBytes(string.toByteArray());
}
 
Example 19
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static float readByteAngle(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    return buffer.readByte() * (360f / 256f);
}
 
Example 20
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 4 votes vote down vote up
public static void writeEntityData(ByteBuf buffer, EntityDataMap entityDataMap) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(entityDataMap, "entityDataDictionary");

    VarInts.writeUnsignedInt(buffer, entityDataMap.size());

    for (Map.Entry<EntityData, Object> entry : entityDataMap.entrySet()) {
        int index = buffer.writerIndex();
        VarInts.writeUnsignedInt(buffer, METADATAS.get(entry.getKey()));
        Object object = entry.getValue();
        EntityData.Type type = EntityData.Type.from(object);
        VarInts.writeUnsignedInt(buffer, METADATA_TYPES.get(type));

        switch (type) {
            case BYTE:
                buffer.writeByte((byte) object);
                break;
            case SHORT:
                buffer.writeShortLE((short) object);
                break;
            case INT:
                VarInts.writeInt(buffer, (int) object);
                break;
            case FLOAT:
                buffer.writeFloatLE((float) object);
                break;
            case STRING:
                BedrockUtils.writeString(buffer, (String) object);
                break;
            case NBT:
                ItemData item;
                if (object instanceof CompoundTag) {
                    item = ItemData.of(1, (short) 0, 1, (CompoundTag) object);
                } else {
                    item = (ItemData) object;
                }
                BedrockUtils.writeItemData(buffer, item);
                break;
            case VECTOR3I:
                BedrockUtils.writeVector3i(buffer, (Vector3i) object);
                break;
            case FLAGS:
                int flagsIndex = entry.getKey() == FLAGS_2 ? 1 : 0;
                object = ((EntityFlags) object).get(flagsIndex, METADATA_FLAGS);
            case LONG:
                VarInts.writeLong(buffer, (long) object);
                break;
            case VECTOR3F:
                BedrockUtils.writeVector3f(buffer, (Vector3f) object);
                break;
            default:
                buffer.writerIndex(index);
                break;
        }
    }
}