Search in sources :

Example 21 with StatInstance

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

the class MaterialInstance method getEnchantmentModifiedStats.

@Deprecated
private void getEnchantmentModifiedStats(List<StatInstance> mods, StatGearKey key) {
    if (key.getStat() == ItemStats.CHARGEABILITY) {
        return;
    }
    // Search for materials that stats
    for (Map.Entry<Enchantment, Integer> entry : EnchantmentHelper.getEnchantments(this.item).entrySet()) {
        Enchantment enchantment = entry.getKey();
        Integer level = entry.getValue();
        if (enchantment instanceof IStatModifierEnchantment) {
            IStatModifierEnchantment statModifierEnchantment = (IStatModifierEnchantment) enchantment;
            ChargedProperties charge = new ChargedProperties(level, getChargeability());
            // Replace modifiers with updated ones (if provided)
            for (int i = 0; i < mods.size(); i++) {
                StatInstance mod = mods.get(i);
                StatInstance newMod = statModifierEnchantment.modifyStat(key, mod, charge);
                if (newMod != null) {
                    mods.remove(i);
                    mods.add(i, newMod);
                }
            }
        }
    }
}
Also used : ChargedProperties(net.silentchaos512.gear.api.stats.ChargedProperties) IStatModifierEnchantment(net.silentchaos512.gear.api.enchantment.IStatModifierEnchantment) StatInstance(net.silentchaos512.gear.api.stats.StatInstance) Enchantment(net.minecraft.world.item.enchantment.Enchantment) IStatModifierEnchantment(net.silentchaos512.gear.api.enchantment.IStatModifierEnchantment)

Example 22 with StatInstance

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

the class StatsCommand method runInfo.

private static int runInfo(CommandContext<CommandSourceStack> context, ServerPlayer player) {
    ItemStack stack = player.getMainHandItem();
    if (!GearHelper.isGear(stack)) {
        context.getSource().sendFailure(TextUtil.translate("command", "invalidItemType", stack.getHoverName()));
        return 0;
    }
    context.getSource().sendSuccess(TextUtil.translate("command", "stats.info.header", player.getName(), stack.getHoverName()).withStyle(ChatFormatting.BOLD), true);
    ICoreItem item = (ICoreItem) stack.getItem();
    PartDataList parts = GearData.getConstructionParts(stack);
    StatModifierMap stats = GearData.getStatModifiers(stack, item, parts);
    for (ItemStat stat : ItemStats.allStatsOrderedExcluding((item).getExcludedStats(stack))) {
        StatGearKey key = StatGearKey.of(stat, item.getGearType());
        Collection<StatInstance> mods = stats.get(key);
        if (!mods.isEmpty()) {
            Component name = TextUtil.withColor(stat.getDisplayName(), stat.getNameColor());
            Component modsText = StatModifierMap.formatText(mods, stat, 5, true);
            float statValue = stat.compute(0f, true, item.getGearType(), mods);
            Component valueText = TextUtil.withColor(StatInstance.of(statValue, StatInstance.Operation.AVG, key).getFormattedText(stat, 5, false), ChatFormatting.YELLOW);
            context.getSource().sendSuccess(TextUtil.translate("command", "stats.info.format", name, modsText, valueText), true);
            for (PartData part : parts) {
                Collection<StatInstance> partMods = part.getStatModifiers(key, stack);
                if (!partMods.isEmpty()) {
                    Component partName = part.getDisplayName(stack);
                    Component partModsText = StatModifierMap.formatText(partMods, stat, 5, true);
                    context.getSource().sendSuccess(TextUtil.translate("command", "stats.info.formatPart", partName, partModsText), true);
                }
            }
        }
    }
    return 1;
}
Also used : PartDataList(net.silentchaos512.gear.api.part.PartDataList) PartData(net.silentchaos512.gear.gear.part.PartData) StatInstance(net.silentchaos512.gear.api.stats.StatInstance) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) ItemStack(net.minecraft.world.item.ItemStack) Component(net.minecraft.network.chat.Component) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) StatModifierMap(net.silentchaos512.gear.api.stats.StatModifierMap) ItemStat(net.silentchaos512.gear.api.stats.ItemStat) StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Example 23 with StatInstance

use of net.silentchaos512.gear.api.stats.StatInstance 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 24 with StatInstance

use of net.silentchaos512.gear.api.stats.StatInstance 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 25 with StatInstance

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

the class GearClientHelper method addStatsInfo.

public static void addStatsInfo(ItemStack stack, List<Component> tooltip, GearTooltipFlag flag, ICoreItem item) {
    if (KeyTracker.isDisplayStatsDown() && flag.showStats) {
        tooltip.add(TextUtil.withColor(misc("tooltip.stats"), Color.GOLD));
        tooltip.add(TextUtil.withColor(misc("tier", GearData.getTier(stack)), Color.DEEPSKYBLUE));
        // Display only stats relevant to the item class
        Collection<ItemStat> relevantStats = item.getRelevantStats(stack);
        Collection<ItemStat> displayStats = flag.isAdvanced() && SilentGear.isDevBuild() ? ItemStats.allStatsOrdered() : relevantStats;
        TextListBuilder builder = new TextListBuilder();
        for (ItemStat stat : displayStats) {
            if (stat == ItemStats.ENCHANTABILITY && !Config.Common.allowEnchanting.get()) {
                // Enchanting not allowed, so hide the stat
                continue;
            }
            float statValue = GearData.getStat(stack, stat);
            StatInstance inst = StatInstance.of(statValue, StatInstance.Operation.AVG, StatInstance.DEFAULT_KEY);
            Color nameColor = relevantStats.contains(stat) ? stat.getNameColor() : TooltipHandler.MC_DARK_GRAY;
            Component textName = TextUtil.withColor(stat.getDisplayName(), nameColor);
            MutableComponent textStat = inst.getFormattedText(stat, stat.isDisplayAsInt() ? 0 : 2, false);
            // TODO: The stats should probably handle this instead
            if (stat == ItemStats.DURABILITY) {
                int durabilityLeft = stack.getMaxDamage() - stack.getDamageValue();
                int durabilityMax = stack.getMaxDamage();
                textStat = statText("durabilityFormat", durabilityLeft, durabilityMax);
            } else if (stat == ItemStats.HARVEST_LEVEL) {
                textStat = TooltipHandler.harvestLevelWithHint(textStat, statValue);
            }
            builder.add(statText("displayFormat", textName, textStat));
        }
        tooltip.addAll(builder.build());
    } else if (flag.showStats) {
        tooltip.add(TextUtil.withColor(TextUtil.misc("tooltip.stats"), Color.GOLD).append(new TextComponent(" ").append(TextUtil.withColor(TextUtil.keyBinding(KeyTracker.DISPLAY_STATS), ChatFormatting.GRAY))));
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) MutableComponent(net.minecraft.network.chat.MutableComponent) Color(net.silentchaos512.utils.Color) StatInstance(net.silentchaos512.gear.api.stats.StatInstance) MutableComponent(net.minecraft.network.chat.MutableComponent) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent) ItemStat(net.silentchaos512.gear.api.stats.ItemStat)

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