Search in sources :

Example 16 with StatInstance

use of net.silentchaos512.gear.api.stats.StatInstance in project Silent-Gear by SilentChaos512.

the class MaterialBuilder method stat.

public MaterialBuilder stat(PartType partType, IItemStat stat, GearType gearType, float value, StatInstance.Operation operation) {
    StatGearKey key = StatGearKey.of(stat, gearType);
    StatInstance mod = StatInstance.of(value, operation, key);
    StatModifierMap map = stats.computeIfAbsent(partType, pt -> new StatModifierMap());
    map.put(stat, gearType, mod);
    return this;
}
Also used : StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Example 17 with StatInstance

use of net.silentchaos512.gear.api.stats.StatInstance in project Silent-Gear by SilentChaos512.

the class ItemStat method computeForDisplay.

public StatInstance computeForDisplay(float baseValue, GearType gearType, Collection<StatInstance> modifiers) {
    if (modifiers.isEmpty()) {
        return StatInstance.of(baseValue, Operation.AVG, StatInstance.DEFAULT_KEY);
    }
    int add = 1;
    for (StatInstance inst : modifiers) {
        Operation op = inst.getOp();
        if (op == Operation.AVG || op == Operation.ADD || op == Operation.MAX) {
            add = 0;
            break;
        }
    }
    float value = compute(baseValue + add, false, gearType, modifiers) - add;
    Operation op = modifiers.iterator().next().getOp();
    return StatInstance.of(value, op, StatInstance.DEFAULT_KEY);
}
Also used : Operation(net.silentchaos512.gear.api.stats.StatInstance.Operation)

Example 18 with StatInstance

use of net.silentchaos512.gear.api.stats.StatInstance in project Silent-Gear by SilentChaos512.

the class StatInstance method getWeightedAverageMod.

public static StatInstance getWeightedAverageMod(Collection<StatInstance> modifiers, Operation op) {
    float value = ItemStat.getWeightedAverage(modifiers, op);
    StatGearKey key = getMostSpecificKey(modifiers);
    return new StatInstance(value, op, key);
}
Also used : StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Example 19 with StatInstance

use of net.silentchaos512.gear.api.stats.StatInstance in project Silent-Gear by SilentChaos512.

the class StatInstance method getMostSpecificKey.

private static StatGearKey getMostSpecificKey(Collection<StatInstance> modifiers) {
    // Gets the key furthest down the gear type hierarchy (key with most parents)
    Set<StatGearKey> found = new HashSet<>();
    for (StatInstance mod : modifiers) {
        found.add(mod.key);
    }
    StatGearKey ret = null;
    int best = 0;
    for (StatGearKey key : found) {
        int parents = 0;
        StatGearKey parent = key.getParent();
        while (parent != null) {
            parent = parent.getParent();
            ++parents;
        }
        if (parents > best || ret == null) {
            best = parents;
            ret = key;
        }
    }
    return ret != null ? ret : DEFAULT_KEY;
}
Also used : StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Example 20 with StatInstance

use of net.silentchaos512.gear.api.stats.StatInstance in project Silent-Gear by SilentChaos512.

the class StatModifierMap method deserialize.

public static StatModifierMap deserialize(JsonElement json) {
    StatModifierMap map = new StatModifierMap();
    if (json.isJsonObject()) {
        for (Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            StatGearKey key = StatGearKey.read(entry.getKey());
            if (key != null) {
                JsonElement value = entry.getValue();
                if (value.isJsonArray()) {
                    for (JsonElement je : value.getAsJsonArray()) {
                        StatInstance mod = StatInstance.read(key, je);
                        map.put(key, mod);
                    }
                } else {
                    map.put(key, StatInstance.read(key, value));
                }
            }
        }
    } else if (json.isJsonArray()) {
        for (JsonElement element : json.getAsJsonArray()) {
            JsonObject jsonObj = element.getAsJsonObject();
            StatGearKey key = StatGearKey.read(GsonHelper.getAsString(jsonObj, "name"));
            if (key != null) {
                map.put(key, StatInstance.read(key, element));
            }
        }
    } else {
        throw new JsonParseException("Expected object or array");
    }
    return map;
}
Also used : JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParseException(com.google.gson.JsonParseException) StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Aggregations

StatInstance (net.silentchaos512.gear.api.stats.StatInstance)18 StatGearKey (net.silentchaos512.gear.api.util.StatGearKey)15 ItemStat (net.silentchaos512.gear.api.stats.ItemStat)11 GearType (net.silentchaos512.gear.api.item.GearType)7 Component (net.minecraft.network.chat.Component)5 MutableComponent (net.minecraft.network.chat.MutableComponent)5 Color (net.silentchaos512.utils.Color)5 JsonObject (com.google.gson.JsonObject)4 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)4 ItemStack (net.minecraft.world.item.ItemStack)4 JsonParseException (com.google.gson.JsonParseException)3 java.util (java.util)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 Nullable (javax.annotation.Nullable)3 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)3 ResourceLocation (net.minecraft.resources.ResourceLocation)3 GsonHelper (net.minecraft.util.GsonHelper)3 PartType (net.silentchaos512.gear.api.part.PartType)3 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)3