Search in sources :

Example 31 with GearType

use of net.silentchaos512.gear.api.item.GearType in project Silent-Gear by SilentChaos512.

the class TargetEffectTraitBuilder method serialize.

@Override
public JsonObject serialize() {
    if (this.potions.isEmpty()) {
        throw new IllegalStateException("Target effect trait '" + this.traitId + "' has no effects");
    }
    JsonObject json = super.serialize();
    JsonObject effectsJson = new JsonObject();
    this.potions.forEach((gearType, map) -> {
        TargetEffectTrait.EffectMap effectMap = new TargetEffectTrait.EffectMap(map);
        effectsJson.add(gearType.getName(), effectMap.serialize());
    });
    json.add("effects", effectsJson);
    return json;
}
Also used : JsonObject(com.google.gson.JsonObject) TargetEffectTrait(net.silentchaos512.gear.gear.trait.TargetEffectTrait)

Example 32 with GearType

use of net.silentchaos512.gear.api.item.GearType in project Silent-Gear by SilentChaos512.

the class CoatingSmithingRecipe method applyUpgrade.

@Override
protected ItemStack applyUpgrade(ItemStack gear, ItemStack upgradeItem) {
    MaterialInstance material = MaterialInstance.from(upgradeItem);
    if (material != null) {
        GearType gearType = GearHelper.getType(gear);
        if (gearType.isGear()) {
            ItemStack result = gear.copy();
            PartType.COATING.getCompoundPartItem(gearType).ifPresent(cpi -> {
                ItemStack partItem = cpi.create(material, 1);
                // Unfortunately this deletes the old part; can't get a player here
                GearData.addOrReplacePart(result, Objects.requireNonNull(PartData.from(partItem)));
            });
            result.setDamageValue(0);
            GearData.removeExcessParts(result, PartType.COATING);
            // Crafting player is always null?
            GearData.recalculateStats(result, ForgeHooks.getCraftingPlayer());
            return result;
        }
    }
    return ItemStack.EMPTY;
}
Also used : GearType(net.silentchaos512.gear.api.item.GearType) MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) ItemStack(net.minecraft.world.item.ItemStack)

Example 33 with GearType

use of net.silentchaos512.gear.api.item.GearType in project Silent-Gear by SilentChaos512.

the class GearData method printStatsForDebugging.

private static void printStatsForDebugging(ItemStack stack, StatModifierMap stats, @Nullable Map<ItemStat, Float> oldStats) {
    // Prints stats that have changed for debugging purposes
    if (oldStats != null && SilentGear.LOGGER.isDebugEnabled()) {
        GearType gearType = GearHelper.getType(stack);
        Map<ItemStat, Float> newStats = getCurrentStatsForDebugging(stack);
        assert newStats != null;
        for (ItemStat stat : stats.getStats()) {
            float oldValue = oldStats.get(stat);
            float newValue = newStats.get(stat);
            float change = newValue - oldValue;
            SilentGear.LOGGER.debug(" - {}: {} -> {} ({}) - mods: [{}]", stat.getDisplayName().getString(), oldValue, newValue, change < 0 ? change : "+" + change, StatModifierMap.formatText(stats.get(stat, gearType), stat, 5).getString());
        }
    }
}
Also used : GearType(net.silentchaos512.gear.api.item.GearType)

Example 34 with GearType

use of net.silentchaos512.gear.api.item.GearType 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 35 with GearType

use of net.silentchaos512.gear.api.item.GearType in project Silent-Gear by SilentChaos512.

the class GearModelOverrideList method addSimplePartLayers.

private static void addSimplePartLayers(List<MaterialLayer> list, PartData part, ItemStack stack) {
    IPartDisplay model = GearDisplayManager.get(part.get());
    if (model != null) {
        GearType gearType = GearHelper.getType(stack);
        List<MaterialLayer> layers = model.getLayers(gearType, part).getLayers();
        addColorBlendedLayers(list, part, stack, layers);
    }
}
Also used : GearType(net.silentchaos512.gear.api.item.GearType) IPartDisplay(net.silentchaos512.gear.api.part.IPartDisplay) MaterialLayer(net.silentchaos512.gear.api.material.MaterialLayer)

Aggregations

GearType (net.silentchaos512.gear.api.item.GearType)25 ItemStack (net.minecraft.world.item.ItemStack)9 PartType (net.silentchaos512.gear.api.part.PartType)9 ArrayList (java.util.ArrayList)8 ResourceLocation (net.minecraft.resources.ResourceLocation)8 JsonObject (com.google.gson.JsonObject)6 StatGearKey (net.silentchaos512.gear.api.util.StatGearKey)6 MaterialInstance (net.silentchaos512.gear.gear.material.MaterialInstance)6 JsonArray (com.google.gson.JsonArray)5 List (java.util.List)5 ICoreItem (net.silentchaos512.gear.api.item.ICoreItem)5 PartGearKey (net.silentchaos512.gear.api.util.PartGearKey)5 JsonParseException (com.google.gson.JsonParseException)4 IMaterial (net.silentchaos512.gear.api.material.IMaterial)4 MaterialLayer (net.silentchaos512.gear.api.material.MaterialLayer)4 IPartData (net.silentchaos512.gear.api.part.IPartData)4 ItemStat (net.silentchaos512.gear.api.stats.ItemStat)4 StatInstance (net.silentchaos512.gear.api.stats.StatInstance)4 ITrait (net.silentchaos512.gear.api.traits.ITrait)4 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)4