Search in sources :

Example 6 with TraitActionContext

use of net.silentchaos512.gear.api.traits.TraitActionContext in project Silent-Gear by SilentChaos512.

the class TraitHelper method tickTraits.

static void tickTraits(Level world, @Nullable Player player, ItemStack gear, boolean isEquipped) {
    ListTag tagList = GearData.getPropertiesData(gear).getList("Traits", Tag.TAG_COMPOUND);
    for (int i = 0; i < tagList.size(); ++i) {
        CompoundTag tagCompound = tagList.getCompound(i);
        String regName = tagCompound.getString("Name");
        ITrait trait = TraitManager.get(regName);
        if (trait != null) {
            int level = tagCompound.getByte("Level");
            TraitActionContext context = new TraitActionContext(player, level, gear);
            trait.onUpdate(context, isEquipped);
        }
    }
}
Also used : TraitActionContext(net.silentchaos512.gear.api.traits.TraitActionContext) ITrait(net.silentchaos512.gear.api.traits.ITrait) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 7 with TraitActionContext

use of net.silentchaos512.gear.api.traits.TraitActionContext in project Silent-Gear by SilentChaos512.

the class ICoreItem method construct.

// region Item properties and construction
default ItemStack construct(Collection<? extends IPartData> parts) {
    ItemStack result = new ItemStack(this);
    GearData.writeConstructionParts(result, parts);
    parts.forEach(p -> p.onAddToGear(result));
    GearData.recalculateStats(result, null);
    // Allow traits to make any needed changes (must be done after a recalculate)
    TraitHelper.activateTraits(result, 0, (trait, level, nothing) -> {
        trait.onGearCrafted(new TraitActionContext(null, level, result));
        return 0;
    });
    return result;
}
Also used : TraitActionContext(net.silentchaos512.gear.api.traits.TraitActionContext) ItemStack(net.minecraft.world.item.ItemStack)

Example 8 with TraitActionContext

use of net.silentchaos512.gear.api.traits.TraitActionContext in project Silent-Gear by SilentChaos512.

the class WielderEffectTrait method onUpdate.

@Override
public void onUpdate(TraitActionContext context, boolean isEquipped) {
    if (!isEquipped || context.getPlayer() == null || context.getPlayer().tickCount % 10 != 0)
        return;
    GearType gearType = ((ICoreItem) context.getGear().getItem()).getGearType();
    for (Map.Entry<String, List<PotionData>> entry : potions.entrySet()) {
        String type = entry.getKey();
        List<PotionData> list = entry.getValue();
        applyEffects(context, gearType, type, list);
    }
}
Also used : GearType(net.silentchaos512.gear.api.item.GearType) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem)

Example 9 with TraitActionContext

use of net.silentchaos512.gear.api.traits.TraitActionContext 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 10 with TraitActionContext

use of net.silentchaos512.gear.api.traits.TraitActionContext in project Silent-Gear by SilentChaos512.

the class BonusDropsTraitLootModifier method doApply.

@Nonnull
@Override
protected List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
    List<ItemStack> ret = new ArrayList<>(generatedLoot);
    ItemStack tool = context.getParamOrNull(LootContextParams.TOOL);
    if (tool != null && GearHelper.isGear(tool)) {
        // noinspection OverlyLongLambda
        TraitHelper.activateTraits(tool, 0, (trait, level, value) -> {
            generatedLoot.forEach(lootStack -> {
                ItemStack stack = trait.addLootDrops(new TraitActionContext(null, level, tool), lootStack);
                if (!stack.isEmpty()) {
                    ret.add(stack);
                }
            });
            return 0;
        });
    }
    return ret;
}
Also used : TraitActionContext(net.silentchaos512.gear.api.traits.TraitActionContext) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.world.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Aggregations

TraitActionContext (net.silentchaos512.gear.api.traits.TraitActionContext)8 GearType (net.silentchaos512.gear.api.item.GearType)5 ItemStack (net.minecraft.world.item.ItemStack)4 CompoundTag (net.minecraft.nbt.CompoundTag)3 ResourceLocation (net.minecraft.resources.ResourceLocation)3 ITrait (net.silentchaos512.gear.api.traits.ITrait)3 ArrayList (java.util.ArrayList)2 ListTag (net.minecraft.nbt.ListTag)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 AttributeModifier (net.minecraft.world.entity.ai.attributes.AttributeModifier)2 Player (net.minecraft.world.entity.player.Player)2 SilentGear (net.silentchaos512.gear.SilentGear)2 ICoreItem (net.silentchaos512.gear.api.item.ICoreItem)2 LinkedHashMultimap (com.google.common.collect.LinkedHashMultimap)1 Multimap (com.google.common.collect.Multimap)1 Sets (com.google.common.collect.Sets)1 com.google.gson (com.google.gson)1 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 java.util (java.util)1 HashMap (java.util.HashMap)1