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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations