Search in sources :

Example 1 with ITrait

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);
}
Also used : ITrait(net.silentchaos512.gear.api.traits.ITrait) ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 2 with ITrait

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;
}
Also used : InteractionResult(net.minecraft.world.InteractionResult) ITrait(net.silentchaos512.gear.api.traits.ITrait)

Example 3 with ITrait

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;
}
Also used : ITrait(net.silentchaos512.gear.api.traits.ITrait) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance)

Example 4 with ITrait

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;
}
Also used : ITrait(net.silentchaos512.gear.api.traits.ITrait) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 5 with ITrait

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);
        }
    }
}
Also used : TraitActionContext(net.silentchaos512.gear.api.traits.TraitActionContext) ITrait(net.silentchaos512.gear.api.traits.ITrait) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ITrait (net.silentchaos512.gear.api.traits.ITrait)14 TraitInstance (net.silentchaos512.gear.api.traits.TraitInstance)6 ResourceLocation (net.minecraft.resources.ResourceLocation)5 CompoundTag (net.minecraft.nbt.CompoundTag)4 ListTag (net.minecraft.nbt.ListTag)4 TextComponent (net.minecraft.network.chat.TextComponent)4 PartData (net.silentchaos512.gear.gear.part.PartData)4 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)3 ItemStack (net.minecraft.world.item.ItemStack)3 IModInfo (net.minecraftforge.forgespi.language.IModInfo)3 IGearPart (net.silentchaos512.gear.api.part.IGearPart)3 CommandDispatcher (com.mojang.brigadier.CommandDispatcher)2 CommandContext (com.mojang.brigadier.context.CommandContext)2 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)2 SuggestionProvider (com.mojang.brigadier.suggestion.SuggestionProvider)2 java.io (java.io)2 StandardCharsets (java.nio.charset.StandardCharsets)2 LocalDateTime (java.time.LocalDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 java.util (java.util)2