/** * Attempt to cast this NBT tag as a list. * * @param tag - the NBT tag to cast. * @return This instance as a list. * @throws UnsupportedOperationException If this is not a list. */ public static NbtList<?> asList(NbtBase<?> tag) { if (tag instanceof NbtList) return (NbtList<?>) tag; else if (tag != null) throw new UnsupportedOperationException( "Cannot cast a " + tag.getClass() + "( " + tag.getType() + ") to TAG_LIST."); else throw new IllegalArgumentException("Tag cannot be NULL."); }
/** * Attempt to cast this NBT tag as a compund. * * @param tag - the NBT tag to cast. * @return This instance as a compound. * @throws UnsupportedOperationException If this is not a compound. */ public static NbtCompound asCompound(NbtBase<?> tag) { if (tag instanceof NbtCompound) return (NbtCompound) tag; else if (tag != null) throw new UnsupportedOperationException( "Cannot cast a " + tag.getClass() + "( " + tag.getType() + ") to TAG_COMPUND."); else throw new IllegalArgumentException("Tag cannot be NULL."); }
/** * Get a NBT wrapper from a NBT base. * * <p>This may clone the content if the NbtBase is not a NbtWrapper. * * @param <T> Type * @param base - the base class. * @return A NBT wrapper. */ @SuppressWarnings("unchecked") public static <T> NbtWrapper<T> fromBase(NbtBase<T> base) { if (base instanceof NbtWrapper) { return (NbtWrapper<T>) base; } else { if (base.getType() == NbtType.TAG_COMPOUND) { // Load into a NBT-backed wrapper WrappedCompound copy = WrappedCompound.fromName(base.getName()); T value = base.getValue(); copy.setValue((Map<String, NbtBase<?>>) value); return (NbtWrapper<T>) copy; } else if (base.getType() == NbtType.TAG_LIST) { // As above NbtList<T> copy = WrappedList.fromName(base.getName()); copy.setValue((List<NbtBase<T>>) base.getValue()); return (NbtWrapper<T>) copy; } else { // Copy directly NbtWrapper<T> copy = ofWrapper(base.getType(), base.getName()); copy.setValue(base.getValue()); return copy; } } }