Search in sources :

Example 6 with TraitInstance

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

the class TraitHelper method getTraits.

public static List<TraitInstance> getTraits(List<? extends IGearComponentInstance<?>> components, PartGearKey partKey, ItemStack gear) {
    if (components.isEmpty()) {
        return Collections.emptyList();
    }
    Map<ITrait, Integer> map = new LinkedHashMap<>();
    Map<ITrait, Integer> countMatsWithTrait = new HashMap<>();
    for (IGearComponentInstance<?> comp : components) {
        for (TraitInstance inst : comp.getTraits(partKey, gear)) {
            if (inst.conditionsMatch(partKey, gear, components)) {
                map.merge(inst.getTrait(), inst.getLevel(), Integer::sum);
                countMatsWithTrait.merge(inst.getTrait(), 1, Integer::sum);
            }
        }
    }
    ITrait[] keys = map.keySet().toArray(new ITrait[0]);
    for (ITrait trait : keys) {
        final int matsWithTrait = countMatsWithTrait.get(trait);
        final float divisor = Math.max(components.size() / 2f, matsWithTrait);
        final int value = Math.round(map.get(trait) / divisor);
        map.put(trait, Mth.clamp(value, 1, trait.getMaxLevel()));
    }
    cancelTraits(map, keys);
    // FIXME
    // MinecraftForge.EVENT_BUS.post(new GetTraitsEvent(gear, materials, result));
    List<TraitInstance> ret = new ArrayList<>();
    map.forEach((trait, level) -> ret.add(TraitInstance.of(trait, level)));
    return ret;
}
Also used : ITrait(net.silentchaos512.gear.api.traits.ITrait) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance)

Example 7 with TraitInstance

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

the class TraitHelper method getTraits.

/**
 * Gets a Map of Traits and levels from the parts, used to calculate trait levels and should not
 * be used in most cases. Consider using {@link #getTraitLevel(ItemStack, ResourceLocation)} or
 * {@link #hasTrait(ItemStack, ResourceLocation)} when appropriate.
 *
 * @param gear     The item
 * @param gearType The gear type
 * @param parts    The list of all parts used in constructing the gear.
 * @return A Map of Traits to their levels
 */
public static Map<ITrait, Integer> getTraits(ItemStack gear, GearType gearType, PartDataList parts) {
    if (parts.isEmpty() || (!gear.isEmpty() && GearHelper.isBroken(gear)))
        return ImmutableMap.of();
    Map<ITrait, Integer> result = new LinkedHashMap<>();
    for (PartData part : parts) {
        PartGearKey key = PartGearKey.of(gearType, part);
        for (TraitInstance inst : part.getTraits(key, gear)) {
            if (inst.conditionsMatch(key, gear, parts)) {
                ITrait trait = inst.getTrait();
                // Get the highest value in any part
                result.merge(trait, inst.getLevel(), Integer::max);
            }
        }
    }
    ITrait[] keys = result.keySet().toArray(new ITrait[0]);
    cancelTraits(result, keys);
    MinecraftForge.EVENT_BUS.post(new GetTraitsEvent(gear, parts, result));
    return result;
}
Also used : PartGearKey(net.silentchaos512.gear.api.util.PartGearKey) GetTraitsEvent(net.silentchaos512.gear.api.event.GetTraitsEvent) ITrait(net.silentchaos512.gear.api.traits.ITrait) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) PartData(net.silentchaos512.gear.gear.part.PartData)

Example 8 with TraitInstance

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

the class CompoundMaterial method getTraits.

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

Example 9 with TraitInstance

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

the class CraftedMaterial method getTraits.

@Override
public Collection<TraitInstance> getTraits(IMaterialInstance material, PartGearKey partKey, ItemStack gear) {
    Collection<TraitInstance> ret = super.getTraits(material, partKey, gear);
    IMaterialInstance base = getBaseMaterial(material);
    ret.addAll(base.getTraits(partKey, gear));
    return ret;
}
Also used : IMaterialInstance(net.silentchaos512.gear.api.material.IMaterialInstance) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance)

Example 10 with TraitInstance

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

the class MaterialRatioTraitCondition method matches.

@Override
public boolean matches(ITrait trait, PartGearKey key, ItemStack gear, List<? extends IGearComponentInstance<?>> components) {
    int count = 0;
    for (IGearComponentInstance<?> comp : components) {
        Collection<TraitInstance> traits = comp.getTraits(key, gear);
        for (TraitInstance inst : traits) {
            if (inst.getTrait() == trait) {
                ++count;
                break;
            }
        }
    }
    float ratio = (float) count / components.size();
    return ratio >= this.requiredRatio;
}
Also used : TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance)

Aggregations

TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)12 TextComponent (net.minecraft.network.chat.TextComponent)3 IMaterialInstance (net.silentchaos512.gear.api.material.IMaterialInstance)3 ITrait (net.silentchaos512.gear.api.traits.ITrait)3 PartData (net.silentchaos512.gear.gear.part.PartData)3 Component (net.minecraft.network.chat.Component)2 MutableComponent (net.minecraft.network.chat.MutableComponent)2 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)2 IGearPart (net.silentchaos512.gear.api.part.IGearPart)2 PartType (net.silentchaos512.gear.api.part.PartType)2 CommandDispatcher (com.mojang.brigadier.CommandDispatcher)1 CommandContext (com.mojang.brigadier.context.CommandContext)1 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 SuggestionProvider (com.mojang.brigadier.suggestion.SuggestionProvider)1 java.io (java.io)1 StandardCharsets (java.nio.charset.StandardCharsets)1 LocalDateTime (java.time.LocalDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1