Search in sources :

Example 16 with ITrait

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

the class TraitsCommand method getPartsWithTrait.

private static String getPartsWithTrait(ITrait trait) {
    StringBuilder str = new StringBuilder();
    boolean foundAny = false;
    for (IGearPart part : PartManager.getValues()) {
        PartData partData = PartData.of(part);
        for (TraitInstance inst : partData.getTraits()) {
            if (inst.getTrait().equals(trait) && part.isVisible()) {
                if (foundAny) {
                    str.append(", ");
                }
                foundAny = true;
                str.append("**").append(partData.getDisplayName(ItemStack.EMPTY).getString()).append("**");
            }
        }
    }
    return str.toString();
}
Also used : IGearPart(net.silentchaos512.gear.api.part.IGearPart) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) PartData(net.silentchaos512.gear.gear.part.PartData)

Example 17 with ITrait

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

the class CuriosCompat method getHighestTraitLevel.

public static int getHighestTraitLevel(LivingEntity entity, DataResource<ITrait> trait) {
    LazyOptional<IItemHandlerModifiable> lazy = CuriosApi.getCuriosHelper().getEquippedCurios(entity);
    int max = 0;
    if (lazy.isPresent()) {
        IItemHandlerModifiable handler = lazy.orElseThrow(IllegalStateException::new);
        for (int i = 0; i < handler.getSlots(); ++i) {
            ItemStack stack = handler.getStackInSlot(i);
            if (stack.getItem() instanceof ICoreItem) {
                max = Math.max(max, TraitHelper.getTraitLevel(stack, trait));
            }
        }
    }
    return max;
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) ItemStack(net.minecraft.world.item.ItemStack)

Example 18 with ITrait

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

the class MaterialBuilder method trait.

public MaterialBuilder trait(PartType partType, DataResource<ITrait> trait, int level, ITraitCondition... conditions) {
    ITraitInstance inst = TraitInstance.of(trait, level, conditions);
    List<ITraitInstance> list = traits.computeIfAbsent(partType, pt -> new ArrayList<>());
    list.add(inst);
    return this;
}
Also used : ITraitInstance(net.silentchaos512.gear.api.traits.ITraitInstance)

Example 19 with ITrait

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

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

the class TraitHelper method getCachedTraits.

public static Map<ITrait, Integer> getCachedTraits(ItemStack gear) {
    if (!GearHelper.isGear(gear))
        return ImmutableMap.of();
    Map<ITrait, Integer> result = new LinkedHashMap<>();
    ListTag tagList = GearData.getPropertiesData(gear).getList("Traits", Tag.TAG_COMPOUND);
    for (Tag nbt : tagList) {
        if (nbt instanceof CompoundTag) {
            CompoundTag tagCompound = (CompoundTag) nbt;
            String name = tagCompound.getString("Name");
            ITrait trait = TraitManager.get(name);
            int level = tagCompound.getByte("Level");
            if (trait != null && level > 0) {
                result.put(trait, level);
            }
        }
    }
    return result;
}
Also used : ITrait(net.silentchaos512.gear.api.traits.ITrait) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ITrait (net.silentchaos512.gear.api.traits.ITrait)14 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)6 ResourceLocation (net.minecraft.resources.ResourceLocation)5 CompoundTag (net.minecraft.nbt.CompoundTag)4 ListTag (net.minecraft.nbt.ListTag)4 TextComponent (net.minecraft.network.chat.TextComponent)4 PartData (net.silentchaos512.gear.gear.part.PartData)4 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)3 ItemStack (net.minecraft.world.item.ItemStack)3 IModInfo (net.minecraftforge.forgespi.language.IModInfo)3 IGearPart (net.silentchaos512.gear.api.part.IGearPart)3 CommandDispatcher (com.mojang.brigadier.CommandDispatcher)2 CommandContext (com.mojang.brigadier.context.CommandContext)2 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)2 SuggestionProvider (com.mojang.brigadier.suggestion.SuggestionProvider)2 java.io (java.io)2 StandardCharsets (java.nio.charset.StandardCharsets)2 LocalDateTime (java.time.LocalDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 java.util (java.util)2