Search in sources :

Example 1 with CollectionNBT

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

the class CCArgumentWrapper method wrapNBT.

@Nullable
private static Object wrapNBT(@Nullable INBT nbt) {
    if (nbt == null) {
        return null;
    }
    switch(nbt.getId()) {
        case Constants.NBT.TAG_BYTE:
        case Constants.NBT.TAG_SHORT:
        case Constants.NBT.TAG_INT:
        case Constants.NBT.TAG_LONG:
        case Constants.NBT.TAG_FLOAT:
        case Constants.NBT.TAG_DOUBLE:
        case Constants.NBT.TAG_ANY_NUMERIC:
            return ((NumberNBT) nbt).getAsNumber();
        case Constants.NBT.TAG_STRING:
        case // Tag End is highly unlikely to ever be used outside of networking but handle it anyway
        Constants.NBT.TAG_END:
            return nbt.getAsString();
        case Constants.NBT.TAG_BYTE_ARRAY:
        case Constants.NBT.TAG_INT_ARRAY:
        case Constants.NBT.TAG_LONG_ARRAY:
        case Constants.NBT.TAG_LIST:
            CollectionNBT<?> collectionNBT = (CollectionNBT<?>) nbt;
            int size = collectionNBT.size();
            Map<Integer, Object> wrappedCollection = new HashMap<>(size);
            for (int i = 0; i < size; i++) {
                wrappedCollection.put(i, wrapNBT(collectionNBT.get(i)));
            }
            return wrappedCollection;
        case Constants.NBT.TAG_COMPOUND:
            CompoundNBT compound = (CompoundNBT) nbt;
            Map<String, Object> wrappedCompound = new HashMap<>(compound.size());
            for (String key : compound.getAllKeys()) {
                Object value = wrapNBT(compound.get(key));
                if (value != null) {
                    wrappedCompound.put(key, value);
                }
            }
            return wrappedCompound;
    }
    return null;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) HashMap(java.util.HashMap) CollectionNBT(net.minecraft.nbt.CollectionNBT) NumberNBT(net.minecraft.nbt.NumberNBT) Nullable(javax.annotation.Nullable)

Example 2 with CollectionNBT

use of net.minecraft.nbt.CollectionNBT 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 3 with CollectionNBT

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

the class CCArgumentWrapperTestHelper method wrapNBT.

/**
 * Basically a copy of {@code CCArgumentWrapper#wrapNBT} except slightly modified to do the conversion that would happen passing it through lua of converting all
 * numbers to doubles, and also added support for including NBT hints.
 */
@Nullable
static Object wrapNBT(@Nullable INBT nbt, boolean includeHints) {
    if (nbt == null) {
        return null;
    }
    byte id = nbt.getId();
    switch(id) {
        case Constants.NBT.TAG_BYTE:
        case Constants.NBT.TAG_SHORT:
        case Constants.NBT.TAG_INT:
        case Constants.NBT.TAG_LONG:
        case Constants.NBT.TAG_FLOAT:
        case Constants.NBT.TAG_DOUBLE:
        case Constants.NBT.TAG_ANY_NUMERIC:
            // Get it as a double instead of a number as we will only get it back as a double from CC
            return addNBTHint(id, ((NumberNBT) nbt).getAsDouble(), includeHints);
        case Constants.NBT.TAG_STRING:
        case // Tag End is highly unlikely to ever be used outside of networking but handle it anyway
        Constants.NBT.TAG_END:
            return addNBTHint(id, nbt.getAsString(), includeHints);
        case Constants.NBT.TAG_BYTE_ARRAY:
        case Constants.NBT.TAG_INT_ARRAY:
        case Constants.NBT.TAG_LONG_ARRAY:
        case Constants.NBT.TAG_LIST:
            CollectionNBT<?> collectionNBT = (CollectionNBT<?>) nbt;
            int size = collectionNBT.size();
            Map<Double, Object> wrappedCollection = new HashMap<>(size);
            for (int i = 0; i < size; i++) {
                wrappedCollection.put((double) i, wrapNBT(collectionNBT.get(i), includeHints));
            }
            return addNBTHint(id, wrappedCollection, includeHints);
        case Constants.NBT.TAG_COMPOUND:
            CompoundNBT compound = (CompoundNBT) nbt;
            Map<String, Object> wrappedCompound = new HashMap<>(compound.size());
            for (String key : compound.getAllKeys()) {
                Object value = wrapNBT(compound.get(key), includeHints);
                if (value != null) {
                    wrappedCompound.put(key, value);
                }
            }
            return addNBTHint(id, wrappedCompound, includeHints);
    }
    return null;
}
Also used : CompoundNBT(net.minecraft.nbt.CompoundNBT) HashMap(java.util.HashMap) CollectionNBT(net.minecraft.nbt.CollectionNBT) Nullable(javax.annotation.Nullable)

Example 4 with CollectionNBT

use of net.minecraft.nbt.CollectionNBT 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)

Aggregations

Nullable (javax.annotation.Nullable)4 CollectionNBT (net.minecraft.nbt.CollectionNBT)4 CompoundNBT (net.minecraft.nbt.CompoundNBT)4 NumberNBT (net.minecraft.nbt.NumberNBT)3 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ByteArrayNBT (net.minecraft.nbt.ByteArrayNBT)2 ByteNBT (net.minecraft.nbt.ByteNBT)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 ListNBT (net.minecraft.nbt.ListNBT)2 LongArrayNBT (net.minecraft.nbt.LongArrayNBT)2 LongNBT (net.minecraft.nbt.LongNBT)2 ShortNBT (net.minecraft.nbt.ShortNBT)2 StringNBT (net.minecraft.nbt.StringNBT)2