Search in sources :

Example 1 with IToolStat

use of slimeknights.tconstruct.library.tools.stat.IToolStat in project TinkersConstruct by SlimeKnights.

the class StatsNBT method readFromNBT.

/**
 * Reads the stat from NBT
 */
public static StatsNBT readFromNBT(@Nullable Tag inbt) {
    if (inbt == null || inbt.getId() != Tag.TAG_COMPOUND) {
        return EMPTY;
    }
    ImmutableMap.Builder<IToolStat<?>, Object> builder = ImmutableMap.builder();
    // simply try each key as a tool stat
    CompoundTag nbt = (CompoundTag) inbt;
    for (String key : nbt.getAllKeys()) {
        Tag tag = nbt.get(key);
        if (tag != null) {
            IToolStat<?> stat = readStatIdFromNBT(key);
            if (stat != null) {
                Object value = stat.read(tag);
                if (value != null) {
                    builder.put(stat, value);
                }
            }
        }
    }
    return new StatsNBT(builder.build());
}
Also used : IToolStat(slimeknights.tconstruct.library.tools.stat.IToolStat) JsonObject(com.google.gson.JsonObject) ToString(lombok.ToString) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) ImmutableMap(com.google.common.collect.ImmutableMap) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with IToolStat

use of slimeknights.tconstruct.library.tools.stat.IToolStat in project TinkersConstruct by SlimeKnights.

the class StatsNBT method serializeToNBT.

/**
 * Writes these stats to NBT
 */
public CompoundTag serializeToNBT() {
    CompoundTag nbt = new CompoundTag();
    for (Entry<IToolStat<?>, Object> entry : stats.entrySet()) {
        IToolStat<?> stat = entry.getKey();
        Tag serialized = serialize(stat, entry.getValue());
        if (serialized != null) {
            nbt.put(stat.getName().toString(), serialized);
        }
    }
    return nbt;
}
Also used : IToolStat(slimeknights.tconstruct.library.tools.stat.IToolStat) JsonObject(com.google.gson.JsonObject) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

JsonObject (com.google.gson.JsonObject)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 Tag (net.minecraft.nbt.Tag)2 IToolStat (slimeknights.tconstruct.library.tools.stat.IToolStat)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ToString (lombok.ToString)1