Search in sources :

Example 21 with ItemStat

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

use of net.silentchaos512.gear.api.stats.ItemStat 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)

Example 23 with ItemStat

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

the class MaterialsCommand method makeTsvLine.

private static String makeTsvLine(MaterialInstance material, PartType partType) {
    StringBuilder builder = new StringBuilder();
    appendTsv(builder, material.get().getPackName());
    appendTsv(builder, material.getDisplayName(partType).getString());
    int tier = material.getTier(partType);
    // appendTsv(builder, partType.getDisplayName(tier).getFormattedText());
    appendTsv(builder, partType.getDisplayName(0).getString());
    appendTsv(builder, material.getId().toString());
    appendTsv(builder, getParentId(material.get()));
    // Traits
    appendTsv(builder, material.getTraits(partType).stream().map(t -> t.getTrait().getDisplayName(t.getLevel()).getString()).collect(Collectors.joining(", ")));
    appendTsv(builder, tier);
    // Stats
    for (ItemStat stat : ItemStats.allStatsOrdered()) {
        Collection<StatInstance> statModifiers = material.getStatModifiers(partType, StatGearKey.of(stat, GearType.ALL));
        appendTsv(builder, FORMAT_CODES.matcher(StatModifierMap.formatText(statModifiers, stat, 5).getString()).replaceAll(""));
    }
    return builder.toString();
}
Also used : StatInstance(net.silentchaos512.gear.api.stats.StatInstance) ItemStat(net.silentchaos512.gear.api.stats.ItemStat)

Example 24 with ItemStat

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

the class PartsCommand method partToTsvLine.

private static String partToTsvLine(IGearPart part) {
    StringBuilder builder = new StringBuilder();
    PartData partData = PartData.of(part);
    appendTsv(builder, part.getDisplayName(partData, ItemStack.EMPTY).getString());
    appendTsv(builder, part.getId().toString());
    appendTsv(builder, part.getType().getName());
    appendTsv(builder, partData.getTier());
    // Stats
    for (ItemStat stat : ItemStats.allStatsOrdered()) {
        Collection<StatInstance> statModifiers = partData.getStatModifiers(StatGearKey.of(stat, GearType.ALL), ItemStack.EMPTY);
        appendTsv(builder, FORMAT_CODES.matcher(StatModifierMap.formatText(statModifiers, stat, 5).getString()).replaceAll(""));
    }
    // Traits
    appendTsv(builder, partData.getTraits().stream().map(t -> t.getTrait().getDisplayName(t.getLevel()).getString()).collect(Collectors.joining(", ")));
    return builder.toString();
}
Also used : PartData(net.silentchaos512.gear.gear.part.PartData) StatInstance(net.silentchaos512.gear.api.stats.StatInstance) ItemStat(net.silentchaos512.gear.api.stats.ItemStat)

Example 25 with ItemStat

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

the class TooltipHandler method getMaterialStatLines.

private static void getMaterialStatLines(ItemTooltipEvent event, PartType partType, MaterialInstance material) {
    TextListBuilder builder = new TextListBuilder();
    for (ItemStat stat : ItemStats.allStatsOrdered()) {
        if (stat.isVisible()) {
            getMaterialStatModLines(event, partType, material, builder, stat);
        }
    }
    event.getToolTip().addAll(builder.build());
}
Also used : TextListBuilder(net.silentchaos512.gear.client.util.TextListBuilder) ItemStat(net.silentchaos512.gear.api.stats.ItemStat)

Aggregations

ItemStat (net.silentchaos512.gear.api.stats.ItemStat)16 StatInstance (net.silentchaos512.gear.api.stats.StatInstance)13 GearType (net.silentchaos512.gear.api.item.GearType)9 Color (net.silentchaos512.utils.Color)9 StatGearKey (net.silentchaos512.gear.api.util.StatGearKey)8 TextComponent (net.minecraft.network.chat.TextComponent)7 Component (net.minecraft.network.chat.Component)5 MutableComponent (net.minecraft.network.chat.MutableComponent)5 Nullable (javax.annotation.Nullable)4 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)4 ItemStack (net.minecraft.world.item.ItemStack)4 IItemStat (net.silentchaos512.gear.api.stats.IItemStat)4 JsonObject (com.google.gson.JsonObject)3 JsonParseException (com.google.gson.JsonParseException)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)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