Search in sources :

Example 1 with ByteArrayNBT

use of net.minecraft.nbt.ByteArrayNBT in project Mekanism by mekanism.

the class CCArgumentWrapperPropertyTest method checkSameIntArray.

// ===================
// Integer Arrays
// ===================
private boolean checkSameIntArray(List<Integer> ints, @Nullable Class<? extends INBT> targetClass) {
    IntArrayNBT nbt = new IntArrayNBT(ints);
    Object sanitized = CCArgumentWrapperTestHelper.wrapAndSanitize(nbt, targetClass, false);
    if (ints.isEmpty()) {
        if (targetClass == INBT.class) {
            return sanitized instanceof CompoundNBT && ((CompoundNBT) sanitized).isEmpty();
        }
        // CollectionNBT
        return sanitized instanceof ListNBT && ((ListNBT) sanitized).isEmpty();
    } else if (ints.stream().allMatch(value -> value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE)) {
        ByteArrayNBT expected = new ByteArrayNBT(ints.stream().map(Integer::byteValue).collect(Collectors.toList()));
        return expected.equals(sanitized);
    } else if (ints.stream().allMatch(value -> value >= Short.MIN_VALUE && value <= Short.MAX_VALUE)) {
        ListNBT expected = ints.stream().map(i -> ShortNBT.valueOf(i.shortValue())).collect(Collectors.toCollection(ListNBT::new));
        return expected.equals(sanitized);
    }
    return nbt.equals(sanitized);
}
Also used : ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) FloatNBT(net.minecraft.nbt.FloatNBT) Gen(org.quicktheories.core.Gen) NumberNBT(net.minecraft.nbt.NumberNBT) Nullable(javax.annotation.Nullable) INBT(net.minecraft.nbt.INBT) LongArrayNBT(net.minecraft.nbt.LongArrayNBT) ByteNBT(net.minecraft.nbt.ByteNBT) ListNBT(net.minecraft.nbt.ListNBT) StringNBT(net.minecraft.nbt.StringNBT) DoubleNBT(net.minecraft.nbt.DoubleNBT) CollectionNBT(net.minecraft.nbt.CollectionNBT) ShortNBT(net.minecraft.nbt.ShortNBT) IntArrayNBT(net.minecraft.nbt.IntArrayNBT) Collectors(java.util.stream.Collectors) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) IntNBT(net.minecraft.nbt.IntNBT) List(java.util.List) Constraint(org.quicktheories.impl.Constraint) LongNBT(net.minecraft.nbt.LongNBT) WithQuickTheories(org.quicktheories.WithQuickTheories) Collections(java.util.Collections) QuickTheory(org.quicktheories.QuickTheory) ListNBT(net.minecraft.nbt.ListNBT) IntArrayNBT(net.minecraft.nbt.IntArrayNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT)

Example 2 with ByteArrayNBT

use of net.minecraft.nbt.ByteArrayNBT in project Mekanism by mekanism.

the class CCArgumentWrapperTest method testInvalidListCompoundByteArray.

@Test
@DisplayName("Test making sure we fail to create a list out of invalid elements (non empty compound + byte array)")
void testInvalidListCompoundByteArray() {
    CompoundNBT nbt = new CompoundNBT();
    nbt.putString("key", "value");
    assertMismatchedList(nbt, new ByteArrayNBT(Collections.singletonList((byte) 0)));
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with ByteArrayNBT

use of net.minecraft.nbt.ByteArrayNBT in project Mekanism by mekanism.

the class CCArgumentWrapperTest method testInvalidHintCollection.

@Test
@DisplayName("Test hint mismatch expected type using arrays as actual")
void testInvalidHintCollection() {
    ByteArrayNBT nbt = new ByteArrayNBT(new byte[0]);
    Object withHint = addInvalidHint(nbt, Constants.NBT.TAG_FLOAT);
    Assertions.assertNotEquals(nbt, CCArgumentWrapperTestHelper.sanitize(nbt.getClass(), withHint));
}
Also used : ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with ByteArrayNBT

use of net.minecraft.nbt.ByteArrayNBT in project Mekanism by mekanism.

the class CCArgumentWrapperPropertyTest method checkSameLongArray.

// ===================
// Long Arrays
// ===================
private boolean checkSameLongArray(List<Long> longs, @Nullable Class<? extends INBT> targetClass) {
    LongArrayNBT nbt = new LongArrayNBT(longs);
    Object sanitized = CCArgumentWrapperTestHelper.wrapAndSanitize(nbt, targetClass, false);
    if (longs.isEmpty()) {
        if (targetClass == INBT.class) {
            return sanitized instanceof CompoundNBT && ((CompoundNBT) sanitized).isEmpty();
        }
        // CollectionNBT
        return sanitized instanceof ListNBT && ((ListNBT) sanitized).isEmpty();
    } else if (longs.stream().allMatch(value -> value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE)) {
        ByteArrayNBT expected = new ByteArrayNBT(longs.stream().map(Long::byteValue).collect(Collectors.toList()));
        return expected.equals(sanitized);
    } else if (longs.stream().allMatch(value -> value >= Short.MIN_VALUE && value <= Short.MAX_VALUE)) {
        ListNBT expected = longs.stream().map(i -> ShortNBT.valueOf(i.shortValue())).collect(Collectors.toCollection(ListNBT::new));
        return expected.equals(sanitized);
    } else if (longs.stream().allMatch(value -> value >= Integer.MIN_VALUE && value <= Integer.MAX_VALUE)) {
        IntArrayNBT expected = new IntArrayNBT(longs.stream().map(Long::intValue).collect(Collectors.toList()));
        return expected.equals(sanitized);
    }
    return getExpected(longs).equals(sanitized);
}
Also used : ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) FloatNBT(net.minecraft.nbt.FloatNBT) Gen(org.quicktheories.core.Gen) NumberNBT(net.minecraft.nbt.NumberNBT) Nullable(javax.annotation.Nullable) INBT(net.minecraft.nbt.INBT) LongArrayNBT(net.minecraft.nbt.LongArrayNBT) ByteNBT(net.minecraft.nbt.ByteNBT) ListNBT(net.minecraft.nbt.ListNBT) StringNBT(net.minecraft.nbt.StringNBT) DoubleNBT(net.minecraft.nbt.DoubleNBT) CollectionNBT(net.minecraft.nbt.CollectionNBT) ShortNBT(net.minecraft.nbt.ShortNBT) IntArrayNBT(net.minecraft.nbt.IntArrayNBT) Collectors(java.util.stream.Collectors) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) IntNBT(net.minecraft.nbt.IntNBT) List(java.util.List) Constraint(org.quicktheories.impl.Constraint) LongNBT(net.minecraft.nbt.LongNBT) WithQuickTheories(org.quicktheories.WithQuickTheories) Collections(java.util.Collections) QuickTheory(org.quicktheories.QuickTheory) ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) IntArrayNBT(net.minecraft.nbt.IntArrayNBT) ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT) LongArrayNBT(net.minecraft.nbt.LongArrayNBT)

Example 5 with ByteArrayNBT

use of net.minecraft.nbt.ByteArrayNBT in project Mekanism by mekanism.

the class CCArgumentWrapperPropertyTest method testListByteArrayPlusEmpty.

@Test
@DisplayName("Test serializing and deserializing lists of lists that would create the inner ones as byte arrays plus an empty list")
void testListByteArrayPlusEmpty() {
    qt().forAll(lists().of(allBytes()).ofSizeBetween(1, 15)).check(bytes -> {
        ListNBT nbt = fromArray(fromBytes(bytes), new ListNBT());
        ListNBT expected = fromArray(new ByteArrayNBT(bytes), new ByteArrayNBT(new byte[0]));
        return expected.equals(CCArgumentWrapperTestHelper.wrapAndSanitize(nbt, null, false));
    });
}
Also used : ListNBT(net.minecraft.nbt.ListNBT) ByteArrayNBT(net.minecraft.nbt.ByteArrayNBT) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

ByteArrayNBT (net.minecraft.nbt.ByteArrayNBT)6 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 ListNBT (net.minecraft.nbt.ListNBT)4 CompoundNBT (net.minecraft.nbt.CompoundNBT)3 Collections (java.util.Collections)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 ByteNBT (net.minecraft.nbt.ByteNBT)2 CollectionNBT (net.minecraft.nbt.CollectionNBT)2 DoubleNBT (net.minecraft.nbt.DoubleNBT)2 FloatNBT (net.minecraft.nbt.FloatNBT)2 INBT (net.minecraft.nbt.INBT)2 IntArrayNBT (net.minecraft.nbt.IntArrayNBT)2 IntNBT (net.minecraft.nbt.IntNBT)2 LongArrayNBT (net.minecraft.nbt.LongArrayNBT)2 LongNBT (net.minecraft.nbt.LongNBT)2 NumberNBT (net.minecraft.nbt.NumberNBT)2 ShortNBT (net.minecraft.nbt.ShortNBT)2