Search in sources :

Example 1 with NbtList

use of com.nukkitx.nbt.NbtList in project BlockStateUpdater by CloudburstMC.

the class TagUtils method toMutable.

public static Object toMutable(Object immutable) {
    if (immutable instanceof NbtMap) {
        NbtMap map = (NbtMap) immutable;
        Map<String, Object> mutable = new LinkedHashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            mutable.put(entry.getKey(), toMutable(entry.getValue()));
        }
        return mutable;
    } else if (immutable instanceof NbtList) {
        List<Object> list = new ArrayList<>();
        for (Object value : (List<?>) immutable) {
            list.add(toMutable(value));
        }
        return list;
    }
    return immutable;
}
Also used : NbtMap(com.nukkitx.nbt.NbtMap) NbtList(com.nukkitx.nbt.NbtList) List(java.util.List) NbtList(com.nukkitx.nbt.NbtList) ArrayList(java.util.ArrayList) NbtMap(com.nukkitx.nbt.NbtMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with NbtList

use of com.nukkitx.nbt.NbtList in project BlockStateUpdater by CloudburstMC.

the class TagUtils method toImmutable.

public static Object toImmutable(Object mutable) {
    if (mutable instanceof Map) {
        Map<String, Object> map = (Map<String, Object>) mutable;
        NbtMapBuilder immutable = NbtMap.builder();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            immutable.put(entry.getKey(), toImmutable(entry.getValue()));
        }
        return immutable.build();
    } else if (mutable instanceof List) {
        List<Object> list = new ArrayList<>();
        NbtType<?> type = NbtType.END;
        for (Object value : (List<?>) mutable) {
            if (type == NbtType.END) {
                type = NbtType.byClass(value.getClass());
            }
            list.add(toImmutable(value));
        }
        return new NbtList(type, list);
    }
    return mutable;
}
Also used : NbtType(com.nukkitx.nbt.NbtType) NbtList(com.nukkitx.nbt.NbtList) List(java.util.List) NbtList(com.nukkitx.nbt.NbtList) ArrayList(java.util.ArrayList) NbtMapBuilder(com.nukkitx.nbt.NbtMapBuilder) NbtMap(com.nukkitx.nbt.NbtMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

NbtList (com.nukkitx.nbt.NbtList)2 NbtMap (com.nukkitx.nbt.NbtMap)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 NbtMapBuilder (com.nukkitx.nbt.NbtMapBuilder)1 NbtType (com.nukkitx.nbt.NbtType)1