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;
}
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);
}
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;
}
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);
}
Aggregations