Search in sources :

Example 21 with StatGearKey

use of net.silentchaos512.gear.api.util.StatGearKey in project Silent-Gear by SilentChaos512.

the class PartBuilder method stat.

public PartBuilder stat(IItemStat stat, float value, StatInstance.Operation operation) {
    StatGearKey key = StatGearKey.of(stat, GearType.ALL);
    StatInstance mod = StatInstance.of(value, operation, key);
    this.stats.put(stat, GearType.ALL, mod);
    return this;
}
Also used : StatInstance(net.silentchaos512.gear.api.stats.StatInstance) StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Example 22 with StatGearKey

use of net.silentchaos512.gear.api.util.StatGearKey in project Silent-Gear by SilentChaos512.

the class GearData method tryRecalculateStats.

@SuppressWarnings({ "OverlyLongMethod", "OverlyComplexMethod" })
private static void tryRecalculateStats(ItemStack gear, @Nullable Player player) {
    if (checkNonGearItem(gear, "recalculateStats"))
        return;
    getUUID(gear);
    TraitHelper.activateTraits(gear, 0f, (trait, level, value) -> {
        trait.onRecalculatePre(new TraitActionContext(player, level, gear));
        return 0f;
    });
    ICoreItem item = (ICoreItem) gear.getItem();
    PartDataList parts = getConstructionParts(gear);
    CompoundTag propertiesCompound = getData(gear, NBT_ROOT_PROPERTIES);
    if (!propertiesCompound.contains(NBT_LOCK_STATS))
        propertiesCompound.putBoolean(NBT_LOCK_STATS, false);
    String playerName = player != null ? player.getScoreboardName() : "somebody";
    String playersItemText = String.format("%s's %s", playerName, gear.getHoverName().getString());
    final boolean statsUnlocked = !propertiesCompound.getBoolean(NBT_LOCK_STATS);
    final boolean partsListValid = !parts.isEmpty() && !parts.getMains().isEmpty();
    if (statsUnlocked && partsListValid) {
        // We should recalculate the item's stats!
        if (player != null) {
            SilentGear.LOGGER.debug("Recalculating for {}", playersItemText);
        }
        clearCachedData(gear);
        propertiesCompound.putString("ModVersion", SilentGear.getVersion());
        Map<ITrait, Integer> traits = TraitHelper.getTraits(gear, item.getGearType(), parts);
        // Get all stat modifiers from all parts and item class modifiers
        StatModifierMap stats = getStatModifiers(gear, item, parts);
        // For debugging
        Map<ItemStat, Float> oldStatValues = getCurrentStatsForDebugging(gear);
        // Cache traits in properties compound as well
        ListTag traitList = new ListTag();
        traits.forEach((trait, level) -> traitList.add(trait.write(level)));
        propertiesCompound.put("Traits", traitList);
        propertiesCompound.remove(NBT_SYNERGY);
        // Calculate and write stats
        int maxDamage = gear.getMaxDamage() > 0 ? gear.getMaxDamage() : 1;
        final float damageRatio = Mth.clamp((float) gear.getDamageValue() / maxDamage, 0f, 1f);
        CompoundTag statsCompound = new CompoundTag();
        for (ItemStat stat : ItemStats.allStatsOrderedExcluding(item.getExcludedStats(gear))) {
            StatGearKey key = StatGearKey.of(stat, item.getGearType());
            Collection<StatInstance> modifiers = stats.get(key);
            GearType statGearType = stats.getMostSpecificKey(key).getGearType();
            final float initialValue = stat.compute(stat.getBaseValue(), true, item.getGearType(), statGearType, modifiers);
            // Allow traits to modify stat
            final float withTraits = TraitHelper.activateTraits(gear, initialValue, (trait, level, val) -> {
                TraitActionContext context = new TraitActionContext(player, level, gear);
                return trait.onGetStat(context, stat, val, damageRatio);
            });
            final float value = Config.Common.getStatWithMultiplier(stat, withTraits);
            if (!Mth.equal(value, 0f) || stats.containsKey(key)) {
                ResourceLocation statId = Objects.requireNonNull(stat.getRegistryName());
                // Remove old keys
                propertiesCompound.remove(statId.getPath());
                statsCompound.putFloat(statId.toString(), stat.clampValue(value));
            }
        }
        // Put missing relevant stats in the map to avoid recalculate stats packet spam
        for (ItemStat stat : item.getRelevantStats(gear)) {
            String statKey = stat.getStatId().toString();
            if (!statsCompound.contains(statKey)) {
                statsCompound.putFloat(statKey, stat.getDefaultValue());
            }
        }
        propertiesCompound.put(NBT_STATS, statsCompound);
        if (player != null) {
            printStatsForDebugging(gear, stats, oldStatValues);
        }
        // Remove enchantments if mod is configured to. Must be done before traits add enchantments!
        if (gear.getOrCreateTag().contains("Enchantments") && Config.Common.forceRemoveEnchantments.get()) {
            SilentGear.LOGGER.debug("Forcibly removing all enchantments from {} as per config settings", playersItemText);
            gear.removeTagKey("Enchantments");
        }
        // Remove trait-added enchantments then let traits re-add them
        EnchantmentTrait.removeTraitEnchantments(gear);
        TraitHelper.activateTraits(gear, 0f, (trait, level, value) -> {
            trait.onRecalculatePost(new TraitActionContext(player, level, gear));
            return 0f;
        });
    } else {
        SilentGear.LOGGER.debug("Not recalculating stats for {}", playersItemText);
    }
    // Update rendering info even if we didn't update stats
    updateRenderingInfo(gear, parts);
}
Also used : PartDataList(net.silentchaos512.gear.api.part.PartDataList) ITrait(net.silentchaos512.gear.api.traits.ITrait) ListTag(net.minecraft.nbt.ListTag) TraitActionContext(net.silentchaos512.gear.api.traits.TraitActionContext) GearType(net.silentchaos512.gear.api.item.GearType) ResourceLocation(net.minecraft.resources.ResourceLocation) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) CompoundTag(net.minecraft.nbt.CompoundTag) StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Example 23 with StatGearKey

use of net.silentchaos512.gear.api.util.StatGearKey in project Silent-Gear by SilentChaos512.

the class TooltipHandler method getMaterialStatModLines.

private static void getMaterialStatModLines(ItemTooltipEvent event, PartType partType, MaterialInstance material, TextListBuilder builder, ItemStat stat) {
    Collection<StatInstance> modsAll = material.getStatModifiers(partType, StatGearKey.of(stat, GearType.ALL));
    Optional<MutableComponent> head = getStatTooltipLine(event, partType, stat, modsAll);
    builder.add(head.orElseGet(() -> TextUtil.withColor(stat.getDisplayName(), stat.getNameColor())));
    builder.indent();
    int subCount = 0;
    List<StatGearKey> keysForStat = material.get().getStatKeys(material, partType).stream().filter(key -> key.getStat().equals(stat)).collect(Collectors.toList());
    for (StatGearKey key : keysForStat) {
        if (key.getGearType() != GearType.ALL) {
            ItemStat stat1 = ItemStats.get(key.getStat());
            if (stat1 != null) {
                Collection<StatInstance> mods = material.getStatModifiers(partType, key);
                Optional<MutableComponent> line = getSubStatTooltipLine(event, partType, stat1, key.getGearType(), mods);
                if (line.isPresent()) {
                    builder.add(line.get());
                    ++subCount;
                }
            }
        }
    }
    if (subCount == 0 && !head.isPresent()) {
        builder.removeLast();
    }
    builder.unindent();
}
Also used : GraderTileEntity(net.silentchaos512.gear.block.grader.GraderTileEntity) java.util(java.util) ChargerTileEntity(net.silentchaos512.gear.block.charger.ChargerTileEntity) MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) StatModifierMap(net.silentchaos512.gear.api.stats.StatModifierMap) IMaterialModifier(net.silentchaos512.gear.api.material.modifier.IMaterialModifier) MutableComponent(net.minecraft.network.chat.MutableComponent) PartData(net.silentchaos512.gear.gear.part.PartData) I18n(net.minecraft.client.resources.language.I18n) AbstractGearPart(net.silentchaos512.gear.gear.part.AbstractGearPart) Config(net.silentchaos512.gear.config.Config) ChatFormatting(net.minecraft.ChatFormatting) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) IMaterialCategory(net.silentchaos512.gear.api.material.IMaterialCategory) StatInstance(net.silentchaos512.gear.api.stats.StatInstance) KeyTracker(net.silentchaos512.gear.client.KeyTracker) ItemStat(net.silentchaos512.gear.api.stats.ItemStat) TextUtil(net.silentchaos512.gear.util.TextUtil) ItemStats(net.silentchaos512.gear.api.stats.ItemStats) Component(net.minecraft.network.chat.Component) StatGearKey(net.silentchaos512.gear.api.util.StatGearKey) ModTags(net.silentchaos512.gear.init.ModTags) Collectors(java.util.stream.Collectors) PartType(net.silentchaos512.gear.api.part.PartType) GearType(net.silentchaos512.gear.api.item.GearType) TextComponent(net.minecraft.network.chat.TextComponent) Color(net.silentchaos512.utils.Color) ItemTooltipEvent(net.minecraftforge.event.entity.player.ItemTooltipEvent) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) ItemStack(net.minecraft.world.item.ItemStack) ClientTicks(net.silentchaos512.lib.event.ClientTicks) TagUtils(net.silentchaos512.lib.util.TagUtils) TextListBuilder(net.silentchaos512.gear.client.util.TextListBuilder) CompoundPartItem(net.silentchaos512.gear.item.CompoundPartItem) MutableComponent(net.minecraft.network.chat.MutableComponent) StatInstance(net.silentchaos512.gear.api.stats.StatInstance) ItemStat(net.silentchaos512.gear.api.stats.ItemStat) StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Example 24 with StatGearKey

use of net.silentchaos512.gear.api.util.StatGearKey in project Silent-Gear by SilentChaos512.

the class IStatModProvider method getStat.

default float getStat(D instance, PartType partType, StatGearKey key, ItemStack gear) {
    ItemStat stat = ItemStats.get(key.getStat());
    if (stat == null)
        return key.getStat().getDefaultValue();
    Collection<StatInstance> mods = getStatModifiers(instance, partType, key, gear);
    return stat.compute(mods);
}
Also used : StatInstance(net.silentchaos512.gear.api.stats.StatInstance) ItemStat(net.silentchaos512.gear.api.stats.ItemStat)

Example 25 with StatGearKey

use of net.silentchaos512.gear.api.util.StatGearKey in project Silent-Gear by SilentChaos512.

the class StatGearKey method read.

@Nullable
public static StatGearKey read(String key) {
    String[] parts = key.split("/");
    if (parts.length > 2) {
        throw new JsonParseException("invalid key: " + key);
    }
    ItemStat stat = ItemStats.getRegistry().getValue(SilentGear.getIdWithDefaultNamespace(parts[0]));
    if (stat == null) {
        return null;
    }
    GearType gearType;
    if (parts.length > 1) {
        gearType = GearType.get(parts[1]);
        if (gearType.isInvalid()) {
            throw new JsonParseException("Unknown gear type: " + parts[1]);
        }
    } else {
        gearType = GearType.ALL;
    }
    return new StatGearKey(stat, gearType);
}
Also used : GearType(net.silentchaos512.gear.api.item.GearType) JsonParseException(com.google.gson.JsonParseException) ItemStat(net.silentchaos512.gear.api.stats.ItemStat) IItemStat(net.silentchaos512.gear.api.stats.IItemStat) Nullable(javax.annotation.Nullable)

Aggregations

StatGearKey (net.silentchaos512.gear.api.util.StatGearKey)16 StatInstance (net.silentchaos512.gear.api.stats.StatInstance)12 ItemStat (net.silentchaos512.gear.api.stats.ItemStat)8 GearType (net.silentchaos512.gear.api.item.GearType)6 Component (net.minecraft.network.chat.Component)4 ItemStack (net.minecraft.world.item.ItemStack)4 JsonObject (com.google.gson.JsonObject)3 JsonParseException (com.google.gson.JsonParseException)3 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 Nullable (javax.annotation.Nullable)3 ResourceLocation (net.minecraft.resources.ResourceLocation)3 ICoreItem (net.silentchaos512.gear.api.item.ICoreItem)3 IMaterialInstance (net.silentchaos512.gear.api.material.IMaterialInstance)3 PartType (net.silentchaos512.gear.api.part.PartType)3 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)3 java.util (java.util)2 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)2 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)2 GsonHelper (net.minecraft.util.GsonHelper)2