use of net.silentchaos512.gear.api.event.GetTraitsEvent 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.event.GetTraitsEvent 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;
}
Aggregations