Search in sources :

Example 1 with NBTHolder

use of arekkuusu.solar.api.entanglement.quantum.data.INBTData.NBTHolder in project Solar by ArekkuusuJerii.

the class WorldQuantumData method init.

public static void init(ASMDataTable table) {
    Stopwatch stopwatch = Stopwatch.createStarted();
    List<ASMDataTable.ASMData> loaders = Lists.newArrayList(table.getAll(NBTHolder.class.getName()));
    loaders.sort((l, r) -> l.getObjectName().compareToIgnoreCase(r.getClassName()));
    for (ASMDataTable.ASMData loader : loaders) {
        try {
            Class<?> data = Class.forName(loader.getClassName());
            if (INBTData.class.isAssignableFrom(data)) {
                if (Stream.of(data.getConstructors()).anyMatch(c -> c.getParameterCount() == 0)) {
                    NBTHolder nbtData = data.getAnnotation(NBTHolder.class);
                    String modId = nbtData.modId();
                    String name = nbtData.name();
                    ResourceLocation location = new ResourceLocation(modId, name);
                    // noinspection unchecked
                    DATA_MAP.put(location, (Class<INBTData<?>>) data);
                } else {
                    Solar.LOG.error("[WorldQuantumData] - Class {} has no empty constructor", data.getName());
                }
            } else {
                Solar.LOG.error("[WorldQuantumData] - Class {} is annotated with @NBTHolder but is not an INBTData", data.getName());
            }
        } catch (ClassNotFoundException e) {
            Solar.LOG.error("[WorldQuantumData] - Failed to find class {}", loader.getClassName());
            e.printStackTrace();
        }
    }
    Solar.LOG.info("[Discovered {} NBT data holder(s) in {}]", loaders.size(), stopwatch.stop());
}
Also used : NBTHolder(arekkuusu.solar.api.entanglement.quantum.data.INBTData.NBTHolder) ASMDataTable(net.minecraftforge.fml.common.discovery.ASMDataTable) ResourceLocation(net.minecraft.util.ResourceLocation) Stopwatch(com.google.common.base.Stopwatch) INBTData(arekkuusu.solar.api.entanglement.quantum.data.INBTData)

Example 2 with NBTHolder

use of arekkuusu.solar.api.entanglement.quantum.data.INBTData.NBTHolder in project Solar by ArekkuusuJerii.

the class WorldQuantumData method writeToNBT.

@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
    NBTTagList list = new NBTTagList();
    saved.forEach((k, v) -> {
        // noinspection SuspiciousMethodCalls
        if (v.save() && DATA_MAP.containsValue(v.getClass())) {
            NBTHolder nbtData = v.getClass().getAnnotation(NBTHolder.class);
            NBTTagCompound tag = new NBTTagCompound();
            tag.setString("modId", nbtData.modId());
            tag.setString("name", nbtData.name());
            tag.setUniqueId("key", k);
            tag.setTag("data", v.write());
            list.appendTag(tag);
        }
    });
    compound.setTag(EntangledIItemHandler.NBT_TAG, list);
    return compound;
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTHolder(arekkuusu.solar.api.entanglement.quantum.data.INBTData.NBTHolder) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Aggregations

NBTHolder (arekkuusu.solar.api.entanglement.quantum.data.INBTData.NBTHolder)2 INBTData (arekkuusu.solar.api.entanglement.quantum.data.INBTData)1 Stopwatch (com.google.common.base.Stopwatch)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 NBTTagList (net.minecraft.nbt.NBTTagList)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 ASMDataTable (net.minecraftforge.fml.common.discovery.ASMDataTable)1