use of net.silentchaos512.gear.api.traits.ITrait in project Silent-Gear by SilentChaos512.
the class TraitManager method handleTraitSyncPacket.
public static void handleTraitSyncPacket(SyncTraitsPacket packet, Supplier<NetworkEvent.Context> context) {
synchronized (MAP) {
Map<ResourceLocation, ITrait> oldTraits = ImmutableMap.copyOf(MAP);
MAP.clear();
for (ITrait trait : packet.getTraits()) {
trait.retainData(oldTraits.get(trait.getId()));
MAP.put(trait.getId(), trait);
}
SilentGear.LOGGER.info("Read {} traits from server", MAP.size());
}
context.get().setPacketHandled(true);
}
use of net.silentchaos512.gear.api.traits.ITrait in project Silent-Gear by SilentChaos512.
the class GearHelper method onItemUse.
public static InteractionResult onItemUse(UseOnContext context) {
InteractionResult ret = InteractionResult.PASS;
Map<ITrait, Integer> traits = TraitHelper.getCachedTraits(context.getItemInHand());
for (Map.Entry<ITrait, Integer> entry : traits.entrySet()) {
InteractionResult result = entry.getKey().onItemUse(context, entry.getValue());
if (result != InteractionResult.PASS) {
ret = result;
}
}
return ret;
}
use of net.silentchaos512.gear.api.traits.ITrait 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.ITrait in project Silent-Gear by SilentChaos512.
the class TraitHelper method activateTraits.
/**
* An easy way to activate an item's traits from anywhere. <strong>Use with care!</strong>
* Calling this frequently (like every render tick) causes FPS to tank.
* <p>
* This implementation pulls the item's traits straight from NBT to minimize object creation.
* The {@link TraitFunction} is applied to every trait.
*
* @param gear The {@link net.silentchaos512.gear.api.item.ICoreItem} affected
* @param inputValue The base value to have the traits act on.
* @param action The specific action to apply to each trait. This is {@code (trait, level,
* value) -> modifiedInputValue}, where 'value' is the currently calculated
* result.
* @return The {@code inputValue} modified by traits.
*/
public static float activateTraits(ItemStack gear, final float inputValue, TraitFunction action) {
if (!GearHelper.isGear(gear)) {
SilentGear.LOGGER.error("Called activateTraits on non-gear item, {}", gear);
SilentGear.LOGGER.catching(new IllegalArgumentException());
return inputValue;
}
ListTag tagList = GearData.getPropertiesData(gear).getList("Traits", Tag.TAG_COMPOUND);
float value = inputValue;
for (Tag nbt : tagList) {
if (nbt instanceof CompoundTag) {
CompoundTag tagCompound = (CompoundTag) nbt;
String regName = tagCompound.getString("Name");
ITrait trait = TraitManager.get(regName);
if (trait != null) {
int level = tagCompound.getByte("Level");
value = action.apply(trait, level, value);
}
}
}
return value;
}
use of net.silentchaos512.gear.api.traits.ITrait in project Silent-Gear by SilentChaos512.
the class TraitHelper method tickTraits.
static void tickTraits(Level world, @Nullable Player player, ItemStack gear, boolean isEquipped) {
ListTag tagList = GearData.getPropertiesData(gear).getList("Traits", Tag.TAG_COMPOUND);
for (int i = 0; i < tagList.size(); ++i) {
CompoundTag tagCompound = tagList.getCompound(i);
String regName = tagCompound.getString("Name");
ITrait trait = TraitManager.get(regName);
if (trait != null) {
int level = tagCompound.getByte("Level");
TraitActionContext context = new TraitActionContext(player, level, gear);
trait.onUpdate(context, isEquipped);
}
}
}
Aggregations