Search in sources :

Example 1 with IMaterialInstance

use of net.silentchaos512.gear.api.material.IMaterialInstance in project Silent-Gear by SilentChaos512.

the class CompoundPart method getStatModifiers.

@Override
public Collection<StatInstance> getStatModifiers(IPartData part, PartType partType, StatGearKey key, ItemStack gear) {
    // Get the materials and all the stat modifiers they provide for this stat
    List<IMaterialInstance> materials = getMaterials(part);
    List<StatInstance> statMods = materials.stream().flatMap(m -> m.getStatModifiers(partType, key).stream()).collect(Collectors.toList());
    // Get any base modifiers for this part (could be none)
    statMods.addAll(this.stats.get(key));
    if (statMods.isEmpty()) {
        // No modifiers for this stat, so doing anything else is pointless
        return statMods;
    }
    GetStatModifierEvent event = new GetStatModifierEvent((PartData) part, (ItemStat) key.getStat(), 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.getModifiers());
    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 (key.getStat().doesSynergyApply()) {
        final float synergy = SynergyUtils.getSynergy(this.partType, materials, getTraits(part, PartGearKey.of(gearType, 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 : JsonParseException(com.google.gson.JsonParseException) ResourceLocation(net.minecraft.resources.ResourceLocation) SilentGear(net.silentchaos512.gear.SilentGear) JsonObject(com.google.gson.JsonObject) MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) GearHelper(net.silentchaos512.gear.util.GearHelper) MaterialManager(net.silentchaos512.gear.gear.material.MaterialManager) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) PartGearKey(net.silentchaos512.gear.api.util.PartGearKey) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) IPartData(net.silentchaos512.gear.api.part.IPartData) GsonHelper(net.minecraft.util.GsonHelper) ColorUtils(net.silentchaos512.gear.client.util.ColorUtils) 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) MathUtils(net.silentchaos512.utils.MathUtils) Collection(java.util.Collection) 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) IMaterial(net.silentchaos512.gear.api.material.IMaterial) MinecraftForge(net.minecraftforge.common.MinecraftForge) List(java.util.List) IPartSerializer(net.silentchaos512.gear.api.part.IPartSerializer) IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance) MaterialList(net.silentchaos512.gear.api.material.MaterialList) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) GetStatModifierEvent(net.silentchaos512.gear.api.event.GetStatModifierEvent) ItemStack(net.minecraft.world.item.ItemStack) TraitHelper(net.silentchaos512.gear.util.TraitHelper) CompoundPartItem(net.silentchaos512.gear.item.CompoundPartItem) Collections(java.util.Collections) GetStatModifierEvent(net.silentchaos512.gear.api.event.GetStatModifierEvent) IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance) ArrayList(java.util.ArrayList) StatInstance(net.silentchaos512.gear.api.stats.StatInstance)

Example 2 with IMaterialInstance

use of net.silentchaos512.gear.api.material.IMaterialInstance in project Silent-Gear by SilentChaos512.

the class CompoundPart method getTraits.

@Override
public Collection<TraitInstance> getTraits(IPartData part, PartGearKey partKey, ItemStack gear) {
    List<TraitInstance> ret = new ArrayList<>(super.getTraits(part, partKey, gear));
    List<IMaterialInstance> materials = getMaterials(part);
    for (TraitInstance inst : TraitHelper.getTraits(materials, partKey, gear)) {
        if (inst.conditionsMatch(partKey, gear, materials)) {
            ret.add(inst);
        }
    }
    return ret;
}
Also used : IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) ArrayList(java.util.ArrayList)

Example 3 with IMaterialInstance

use of net.silentchaos512.gear.api.material.IMaterialInstance in project Silent-Gear by SilentChaos512.

the class AbstractMaterial method removeEnhancements.

public static IMaterialInstance removeEnhancements(IMaterialInstance material) {
    ItemStack stack = material.getItem().copy();
    for (IMaterialModifierType modifierType : MaterialModifiers.getTypes()) {
        modifierType.removeModifier(stack);
    }
    EnchantmentHelper.setEnchantments(Collections.emptyMap(), stack);
    IMaterial iMaterial = material.get();
    if (iMaterial != null) {
        return MaterialInstance.of(iMaterial, stack);
    } else {
        return material;
    }
}
Also used : IMaterialModifierType(net.silentchaos512.gear.api.material.modifier.IMaterialModifierType) ItemStack(net.minecraft.world.item.ItemStack)

Example 4 with IMaterialInstance

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

Example 5 with IMaterialInstance

use of net.silentchaos512.gear.api.material.IMaterialInstance in project Silent-Gear by SilentChaos512.

the class CraftedMaterial method getCategories.

@Override
public Collection<IMaterialCategory> getCategories(IMaterialInstance material) {
    Collection<IMaterialCategory> set = super.getCategories(material);
    IMaterialInstance base = getBaseMaterial(material);
    set.addAll(base.getCategories());
    return set;
}
Also used : IMaterialCategory(net.silentchaos512.gear.api.material.IMaterialCategory) IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance)

Aggregations

IMaterialInstance (net.silentchaos512.gear.api.material.IMaterialInstance)24 ItemStack (net.minecraft.world.item.ItemStack)10 PartType (net.silentchaos512.gear.api.part.PartType)6 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)6 ResourceLocation (net.minecraft.resources.ResourceLocation)5 IMaterial (net.silentchaos512.gear.api.material.IMaterial)5 IMaterialDisplay (net.silentchaos512.gear.api.material.IMaterialDisplay)5 Nullable (javax.annotation.Nullable)4 StatGearKey (net.silentchaos512.gear.api.util.StatGearKey)4 ArrayList (java.util.ArrayList)3 Collectors (java.util.stream.Collectors)3 Component (net.minecraft.network.chat.Component)3 SilentGear (net.silentchaos512.gear.SilentGear)3 StatInstance (net.silentchaos512.gear.api.stats.StatInstance)3 JsonObject (com.google.gson.JsonObject)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)2 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)2