Search in sources :

Example 1 with GetMaterialStatsEvent

use of net.silentchaos512.gear.api.event.GetMaterialStatsEvent in project Silent-Gear by SilentChaos512.

the class MaterialInstance method getStatModifiers.

@Override
public Collection<StatInstance> getStatModifiers(PartType partType, StatGearKey key, ItemStack gear) {
    List<StatInstance> mods = new ArrayList<>(material.getStatModifiers(this, partType, key, gear));
    ItemStat stat = ItemStats.get(key.getStat());
    if (stat == null) {
        SilentGear.LOGGER.warn("Unknown item stat: {}", key.getStat().getStatId());
        SilentGear.LOGGER.catching(new NullPointerException());
        return mods;
    }
    // Material modifiers (grades, starcharged, etc.)
    for (IMaterialModifier materialModifier : getModifiers()) {
        mods = materialModifier.modifyStats(partType, key, mods);
    }
    GetMaterialStatsEvent event = new GetMaterialStatsEvent(this, stat, partType, mods);
    MinecraftForge.EVENT_BUS.post(event);
    return event.getModifiers();
}
Also used : StatInstance(net.silentchaos512.gear.api.stats.StatInstance) IMaterialModifier(net.silentchaos512.gear.api.material.modifier.IMaterialModifier) GetMaterialStatsEvent(net.silentchaos512.gear.api.event.GetMaterialStatsEvent) ItemStat(net.silentchaos512.gear.api.stats.ItemStat)

Example 2 with GetMaterialStatsEvent

use of net.silentchaos512.gear.api.event.GetMaterialStatsEvent in project Silent-Gear by SilentChaos512.

the class CompoundMaterial method getStatModifiers.

@SuppressWarnings("OverlyComplexMethod")
@Override
public Collection<StatInstance> getStatModifiers(IMaterialInstance material, PartType partType, StatGearKey key, ItemStack gear) {
    // Get the materials and all the stat modifiers they provide for this stat
    Collection<IMaterialInstance> materials = getMaterials(material);
    List<StatInstance> statMods = materials.stream().map(AbstractMaterial::removeEnhancements).flatMap(m -> m.getStatModifiers(partType, key).stream()).collect(Collectors.toList());
    ItemStat stat = key.getStat() instanceof ItemStat ? (ItemStat) key.getStat() : null;
    if (stat == null || statMods.isEmpty()) {
        // No modifiers for this stat, so doing anything else is pointless
        return statMods;
    }
    MaterialInstance matInst = material instanceof MaterialInstance ? (MaterialInstance) material : null;
    GetMaterialStatsEvent event = null;
    if (matInst != null) {
        // FIXME: Potentially bad cast, need to rework event
        event = new GetMaterialStatsEvent(matInst, stat, partType, statMods);
        MinecraftForge.EVENT_BUS.post(event);
    }
    // Average together all modifiers of the same op. This makes things like rods with varying
    // numbers of materials more "sane".
    List<StatInstance> ret = new ArrayList<>(event != null ? event.getModifiers() : Collections.emptyList());
    for (StatInstance.Operation op : StatInstance.Operation.values()) {
        Collection<StatInstance> modsForOp = ret.stream().filter(s -> s.getOp() == op).collect(Collectors.toList());
        if (modsForOp.size() > 1) {
            StatInstance mod = compressModifiers(modsForOp, op, key);
            ret.removeIf(inst -> inst.getOp() == op);
            ret.add(mod);
        }
    }
    // Synergy
    if (stat.doesSynergyApply() && matInst != null) {
        final float synergy = SynergyUtils.getSynergy(partType, new ArrayList<>(materials), getTraits(matInst, PartGearKey.ofAll(partType), gear));
        if (!MathUtils.floatsEqual(synergy, 1.0f)) {
            final float multi = synergy - 1f;
            for (int i = 0; i < ret.size(); ++i) {
                StatInstance oldMod = ret.get(i);
                float value = oldMod.getValue();
                // Taking the abs of value times multi makes negative mods become less negative
                StatInstance newMod = oldMod.copySetValue(value + Math.abs(value) * multi);
                ret.remove(i);
                ret.add(i, newMod);
            }
        }
    }
    return ret;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) SilentGear(net.silentchaos512.gear.SilentGear) java.util(java.util) ModResourceLocation(net.silentchaos512.gear.util.ModResourceLocation) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) PartGearKey(net.silentchaos512.gear.api.util.PartGearKey) GetMaterialStatsEvent(net.silentchaos512.gear.api.event.GetMaterialStatsEvent) Ingredient(net.minecraft.world.item.crafting.Ingredient) CompoundMaterialItem(net.silentchaos512.gear.item.CompoundMaterialItem) Container(net.minecraft.world.Container) GsonHelper(net.minecraft.util.GsonHelper) CompoundMaterialDisplay(net.silentchaos512.gear.client.material.CompoundMaterialDisplay) Nullable(javax.annotation.Nullable) StatInstance(net.silentchaos512.gear.api.stats.StatInstance) ItemStat(net.silentchaos512.gear.api.stats.ItemStat) Component(net.minecraft.network.chat.Component) StatGearKey(net.silentchaos512.gear.api.util.StatGearKey) SyncMaterialCraftingItemsPacket(net.silentchaos512.gear.network.SyncMaterialCraftingItemsPacket) MathUtils(net.silentchaos512.utils.MathUtils) SynergyUtils(net.silentchaos512.gear.util.SynergyUtils) Collectors(java.util.stream.Collectors) PartType(net.silentchaos512.gear.api.part.PartType) GearType(net.silentchaos512.gear.api.item.GearType) MinecraftForge(net.minecraftforge.common.MinecraftForge) Color(net.silentchaos512.utils.Color) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) ItemStack(net.minecraft.world.item.ItemStack) net.silentchaos512.gear.api.material(net.silentchaos512.gear.api.material) TraitHelper(net.silentchaos512.gear.util.TraitHelper) com.google.gson(com.google.gson) StatInstance(net.silentchaos512.gear.api.stats.StatInstance) ItemStat(net.silentchaos512.gear.api.stats.ItemStat) GetMaterialStatsEvent(net.silentchaos512.gear.api.event.GetMaterialStatsEvent)

Aggregations

GetMaterialStatsEvent (net.silentchaos512.gear.api.event.GetMaterialStatsEvent)2 ItemStat (net.silentchaos512.gear.api.stats.ItemStat)2 StatInstance (net.silentchaos512.gear.api.stats.StatInstance)2 com.google.gson (com.google.gson)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)1 Component (net.minecraft.network.chat.Component)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1 GsonHelper (net.minecraft.util.GsonHelper)1 Container (net.minecraft.world.Container)1 ItemStack (net.minecraft.world.item.ItemStack)1 Ingredient (net.minecraft.world.item.crafting.Ingredient)1 MinecraftForge (net.minecraftforge.common.MinecraftForge)1 SilentGear (net.silentchaos512.gear.SilentGear)1 GearType (net.silentchaos512.gear.api.item.GearType)1 net.silentchaos512.gear.api.material (net.silentchaos512.gear.api.material)1 IMaterialModifier (net.silentchaos512.gear.api.material.modifier.IMaterialModifier)1 PartType (net.silentchaos512.gear.api.part.PartType)1