com.nukkitx.network.util.Preconditions Java Examples

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

    String name = BedrockUtils.readString(buffer);
    int type = VarInts.readUnsignedInt(buffer);

    switch (type) {
        case 1:
            return new GameRuleData<>(name, buffer.readBoolean());
        case 2:
            return new GameRuleData<>(name, VarInts.readUnsignedInt(buffer));
        case 3:
            return new GameRuleData<>(name, buffer.readFloatLE());
    }
    throw new IllegalStateException("Invalid gamerule type received");
}
 
Example #4
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static void writeInventorySource(ByteBuf buffer, InventorySource inventorySource) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(inventorySource, "inventorySource");

    VarInts.writeUnsignedInt(buffer, inventorySource.getType().id());

    switch (inventorySource.getType()) {
        case CONTAINER:
        case UNTRACKED_INTERACTION_UI:
        case NON_IMPLEMENTED_TODO:
            VarInts.writeInt(buffer, inventorySource.getContainerId());
            break;
        case WORLD_INTERACTION:
            VarInts.writeUnsignedInt(buffer, inventorySource.getFlag().ordinal());
            break;
    }
}
 
Example #5
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static GameRuleData readGameRule(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    String name = BedrockUtils.readString(buffer);
    int type = VarInts.readUnsignedInt(buffer);

    switch (type) {
        case 1:
            return new GameRuleData<>(name, buffer.readBoolean());
        case 2:
            return new GameRuleData<>(name, VarInts.readUnsignedInt(buffer));
        case 3:
            return new GameRuleData<>(name, buffer.readFloatLE());
    }
    throw new IllegalStateException("Invalid gamerule type received");
}
 
Example #6
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static void writeInventorySource(ByteBuf buffer, InventorySource inventorySource) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(inventorySource, "inventorySource");

    VarInts.writeUnsignedInt(buffer, inventorySource.getType().id());

    switch (inventorySource.getType()) {
        case CONTAINER:
        case UNTRACKED_INTERACTION_UI:
        case NON_IMPLEMENTED_TODO:
            VarInts.writeInt(buffer, inventorySource.getContainerId());
            break;
        case WORLD_INTERACTION:
            VarInts.writeUnsignedInt(buffer, inventorySource.getFlag().ordinal());
            break;
    }
}
 
Example #7
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);
        entries.add(new ResourcePacksInfoPacket.Entry(packId, packVersion, packSize, encryptionKey, subpackName, contentId, false));
    }
    return entries;
}
 
Example #8
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);
        entries.add(new ResourcePacksInfoPacket.Entry(packId, packVersion, packSize, encryptionKey, subpackName, contentId, false));
    }
    return entries;
}
 
Example #9
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 6 votes vote down vote up
public static GameRuleData readGameRule(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    String name = BedrockUtils.readString(buffer);
    int type = VarInts.readUnsignedInt(buffer);

    switch (type) {
        case 1:
            return new GameRuleData<>(name, buffer.readBoolean());
        case 2:
            return new GameRuleData<>(name, VarInts.readUnsignedInt(buffer));
        case 3:
            return new GameRuleData<>(name, buffer.readFloatLE());
    }
    throw new IllegalStateException("Invalid gamerule type received");
}
 
Example #10
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 #11
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 #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: 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 #14
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 #15
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 #16
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeStructureSettings(ByteBuf buffer, StructureSettings structureSettings) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(structureSettings, "structureSettings");

    BedrockUtils.writeString(buffer, structureSettings.getPaletteName());
    buffer.writeBoolean(structureSettings.isIgnoreEntities());
    buffer.writeBoolean(structureSettings.isIgnoreBlocks());
    BedrockUtils.writeBlockPosition(buffer, structureSettings.getStructureSize());
    BedrockUtils.writeBlockPosition(buffer, structureSettings.getStructureOffset());
    VarInts.writeLong(buffer, structureSettings.getLastTouchedByEntityId());
    buffer.writeByte(structureSettings.getRotation());
    buffer.writeByte(structureSettings.getMirror());
    buffer.writeFloatLE(structureSettings.getIntegrityValue());
    VarInts.writeUnsignedInt(buffer, structureSettings.getIntegritySeed());
}
 
Example #17
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static AsciiString readVarIntAsciiString(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    int length = VarInts.readUnsignedInt(buffer);
    byte[] bytes = new byte[length];
    buffer.readBytes(bytes);
    return new AsciiString(bytes);
}
 
Example #18
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());
    }
}
 
Example #19
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeEntityAttribute(ByteBuf buffer, Attribute attribute) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(attribute, "attribute");

    BedrockUtils.writeString(buffer, attribute.getName());
    buffer.writeFloatLE(attribute.getMinimum());
    buffer.writeFloatLE(attribute.getMaximum());
    buffer.writeFloatLE(attribute.getValue());
}
 
Example #20
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 #21
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static EntityLink readEntityLink(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    long from = VarInts.readLong(buffer);
    long to = VarInts.readLong(buffer);
    int type = buffer.readUnsignedByte();
    boolean immediate = buffer.readBoolean();

    return new EntityLink(from, to, EntityLink.Type.values()[type], immediate);
}
 
Example #22
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static AsciiString readVarIntAsciiString(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    int length = VarInts.readUnsignedInt(buffer);
    byte[] bytes = new byte[length];
    buffer.readBytes(bytes);
    return new AsciiString(bytes);
}
 
Example #23
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 #24
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeVector3i(ByteBuf buffer, Vector3i vector3i) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(vector3i, "vector3i");
    VarInts.writeInt(buffer, vector3i.getX());
    VarInts.writeInt(buffer, vector3i.getY());
    VarInts.writeInt(buffer, vector3i.getZ());
}
 
Example #25
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 #26
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeVector3f(ByteBuf buffer, Vector3f vector3f) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(vector3f, "vector3f");
    buffer.writeFloatLE(vector3f.getX());
    buffer.writeFloatLE(vector3f.getY());
    buffer.writeFloatLE(vector3f.getZ());
}
 
Example #27
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writeCommandOriginData(ByteBuf buffer, CommandOriginData originData) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(originData, "commandOriginData");
    VarInts.writeUnsignedInt(buffer, originData.getOrigin().ordinal());
    writeUuid(buffer, originData.getUuid());
    writeString(buffer, originData.getRequestId());
    if (originData.getOrigin() == CommandOriginData.Origin.DEV_CONSOLE || originData.getOrigin() == CommandOriginData.Origin.TEST) {
        VarInts.writeLong(buffer, originData.getEvent());
    }
}
 
Example #28
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static void writePackInstanceEntry(ByteBuf buffer, ResourcePackStackPacket.Entry packInstanceEntry) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkNotNull(packInstanceEntry, "packInstanceEntry");

    writeString(buffer, packInstanceEntry.getPackId().toString());
    writeString(buffer, packInstanceEntry.getPackVersion());
    writeString(buffer, packInstanceEntry.getSubpackName());
}
 
Example #29
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 #30
Source File: BedrockUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static AsciiString readVarIntAsciiString(ByteBuf buffer) {
    Preconditions.checkNotNull(buffer, "buffer");

    int length = VarInts.readUnsignedInt(buffer);
    byte[] bytes = new byte[length];
    buffer.readBytes(bytes);
    return new AsciiString(bytes);
}