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

The following examples show how to use com.nukkitx.network.util.Preconditions#checkArgument() . 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: PaddedBitArray.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void set(int index, int value) {
    Preconditions.checkElementIndex(index, this.size);
    Preconditions.checkArgument(value >= 0 && value <= this.version.maxEntryValue, "Invalid value");
    int arrayIndex = index / this.version.entriesPerWord;
    int offset = (index % this.version.entriesPerWord) * this.version.bits;

    this.words[arrayIndex] = this.words[arrayIndex] & ~(this.version.maxEntryValue << offset) | (value & this.version.maxEntryValue) << offset;
}
 
Example 2
Source File: Pow2BitArray.java    From Geyser with MIT License 5 votes vote down vote up
/**
 * Sets the entry at the given location to the given value
 */
public void set(int index, int value) {
    Preconditions.checkElementIndex(index, this.size);
    Preconditions.checkArgument(value >= 0 && value <= this.version.maxEntryValue, "Invalid value %s", value);
    int bitIndex = index * this.version.bits;
    int arrayIndex = bitIndex >> 5;
    int offset = bitIndex & 31;
    this.words[arrayIndex] = this.words[arrayIndex] & ~(this.version.maxEntryValue << offset) | (value & this.version.maxEntryValue) << offset;
}
 
Example 3
Source File: NibbleArray.java    From Geyser with MIT License 5 votes vote down vote up
public void set(int index, byte value) {
    Preconditions.checkArgument(value >= 0 && value < 16, "Nibbles must have a value between 0 and 15.");
    Preconditions.checkElementIndex(index, data.length * 2);
    value &= 0xf;
    int half = index / 2;
    byte previous = data[half];
    if ((index & 1) == 0) {
        data[half] = (byte) (previous & 0xf0 | value);
    } else {
        data[half] = (byte) (previous & 0x0f | value << 4);
    }
}
 
Example 4
Source File: NibbleArray.java    From Geyser with MIT License 5 votes vote down vote up
public void fill(byte value) {
    Preconditions.checkArgument(value >= 0 && value < 16, "Nibbles must have a value between 0 and 15.");
    value &= 0xf;
    for (int i = 0; i < data.length; i++) {
        data[i] = (byte) ((value << 4) | value);
    }
}
 
Example 5
Source File: ChunkSection.java    From Geyser with MIT License 5 votes vote down vote up
public ChunkSection(BlockStorage[] storage, byte[] blockLight, byte[] skyLight) {
    Preconditions.checkNotNull(storage, "storage");
    Preconditions.checkArgument(storage.length > 1, "Block storage length must be at least 2");
    for (BlockStorage blockStorage : storage) {
        Preconditions.checkNotNull(blockStorage, "storage");
    }

    this.storage = storage;
    this.blockLight = new NibbleArray(blockLight);
    this.skyLight = new NibbleArray(skyLight);
}
 
Example 6
Source File: BedrockPacketCodec.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public <T extends BedrockPacket> Builder registerPacket(Class<T> packetClass, PacketSerializer<T> packetSerializer, @Nonnegative int id) {
    Preconditions.checkArgument(id >= 0, "id cannot be negative");
    Class<BedrockPacket> clazz = (Class<BedrockPacket>) packetClass;
    PacketSerializer<BedrockPacket> serializer = (PacketSerializer<BedrockPacket>) packetSerializer;
    Preconditions.checkArgument(!idBiMap.containsKey(id), "Packet id already registered");
    Preconditions.checkArgument(!idBiMap.containsValue(clazz), "Packet class already registered");

    serializers.put(id, serializer);
    idBiMap.put(id, clazz);
    return this;
}
 
Example 7
Source File: ScoreInfo.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public ScoreInfo(long scoreboardId, String objectiveId, int score, ScorerType type, long entityId) {
    Preconditions.checkArgument(type == ScorerType.ENTITY || type == ScorerType.PLAYER, "Must be player or entity");
    this.scoreboardId = scoreboardId;
    this.objectiveId = objectiveId;
    this.score = score;
    this.type = type;
    this.entityId = entityId;
}
 
Example 8
Source File: ItemData.java    From Protocol with Apache License 2.0 5 votes vote down vote up
public static ItemData of(int id, short damage, int count, CompoundTag tag, String[] canPlace, String[] canBreak, long blockingTicks) {
    if (id == 0) {
        return AIR;
    }
    Preconditions.checkNotNull(canPlace, "canPlace");
    Preconditions.checkNotNull(canBreak, "canBreak");
    Preconditions.checkArgument(count < 256, "count exceeds maximum of 255");
    return new ItemData(id, damage, count, tag, canPlace, canBreak, blockingTicks);
}
 
Example 9
Source File: EncryptionUtils.java    From Protocol with Apache License 2.0 5 votes vote down vote up
/**
 * Verify the validity of the login chain data from the {@link com.nukkitx.protocol.bedrock.packet.LoginPacket}
 *
 * @param chain array of JWS objects
 * @return chain validity
 * @throws JOSEException            invalid JWS algorithm used
 * @throws ParseException           invalid JWS object
 * @throws InvalidKeySpecException  invalid EC key provided
 * @throws NoSuchAlgorithmException runtime does not support EC spec
 */
public static boolean verifyChain(JSONArray chain) throws JOSEException, ParseException, InvalidKeySpecException, NoSuchAlgorithmException {
    ECPublicKey lastKey = null;
    boolean validChain = false;
    for (Object node : chain) {
        Preconditions.checkArgument(node instanceof String, "Chain node is not a string");
        JWSObject jwt = JWSObject.parse((String) node);

        if (lastKey == null) {
            validChain = verifyJwt(jwt, MOJANG_PUBLIC_KEY);
        } else {
            validChain = verifyJwt(jwt, lastKey);
        }

        if (!validChain) {
            break;
        }

        Object payload = JSONValue.parse(jwt.getPayload().toString());
        Preconditions.checkArgument(payload instanceof JSONObject, "Payload is not a object");

        Object identityPublicKey = ((JSONObject) payload).get("identityPublicKey");
        Preconditions.checkArgument(identityPublicKey instanceof String, "identityPublicKey node is missing in chain");
        lastKey = generateKey((String) identityPublicKey);
    }
    return validChain;
}
 
Example 10
Source File: NibbleArray.java    From Geyser with MIT License 4 votes vote down vote up
public void copyFrom(byte[] bytes) {
    Preconditions.checkNotNull(bytes, "bytes");
    Preconditions.checkArgument(bytes.length == data.length, "length of provided byte array is %s but expected %s", bytes.length,
            data.length);
    System.arraycopy(bytes, 0, data, 0, data.length);
}
 
Example 11
Source File: ChunkSection.java    From Geyser with MIT License 4 votes vote down vote up
private static void checkBounds(int x, int y, int z) {
    Preconditions.checkArgument(x >= 0 && x < 16, "x (%s) is not between 0 and 15", x);
    Preconditions.checkArgument(y >= 0 && y < 16, "y (%s) is not between 0 and 15", y);
    Preconditions.checkArgument(z >= 0 && z < 16, "z (%s) is not between 0 and 15", z);
}