Search in sources :

Example 1 with net.silentchaos512.gear.api.material

use of net.silentchaos512.gear.api.material 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 2 with net.silentchaos512.gear.api.material

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

the class TraitsCommand method runDumpMdClient.

public static void runDumpMdClient() {
    Player player = SilentGear.PROXY.getClientPlayer();
    if (player == null) {
        SilentGear.LOGGER.error("TraitsCommand#runDumpMcClient: player is null?");
        return;
    }
    String fileName = "traits_list.md";
    String dirPath = "output/silentgear";
    File output = new File(dirPath, fileName);
    File directory = output.getParentFile();
    if (!directory.exists() && !directory.mkdirs()) {
        player.sendMessage(new TextComponent("Could not create directory: " + output.getParent()), Util.NIL_UUID);
        return;
    }
    try (Writer writer = new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8)) {
        writer.write("# Traits\n\n");
        writer.write("Generated in-game by `sgear_traits dump_md` command on " + getCurrentDateTime() + "\n\n");
        writer.write("This data may or may not be accurate depending on the mod pack you are playing and the mods or data packs installed.\n\n");
        writer.write("## Data Sources\n\n");
        writer.write("The following mods and data packs have added traits to the output. Running the dump command yourself may produce different results.\n\n");
        writer.write(getDataSources() + "\n");
        writer.write("## Trait Types\n\n");
        writer.write("These are trait serializers. You can define custom instances of these types using data packs.\n");
        writer.write("Code for traits and their serializers can be found in `net.silentchaos512.gear.gear.trait`.\n\n");
        writer.write("Note that \"simple\" traits are often used where custom code is required.\n");
        writer.write("They are not especially useful when just defined by a data pack.\n\n");
        for (ITraitSerializer<?> serializer : TraitSerializers.getSerializers()) {
            String typeName = serializer instanceof SimpleTrait.Serializer ? ((SimpleTrait.Serializer) serializer).getTypeName() : "";
            writer.write("- `" + serializer.getName() + "`");
            if (!typeName.isEmpty()) {
                writer.write(" _(" + typeName + ")_");
            }
            writer.write("\n");
        }
        writer.write("\n## List of Traits");
        List<ResourceLocation> ids = new ArrayList<>(TraitManager.getKeys());
        ids.sort(Comparator.comparing(id -> Objects.requireNonNull(TraitManager.get(id)).getDisplayName(0).getString()));
        for (ResourceLocation id : ids) {
            ITrait trait = TraitManager.get(id);
            assert trait != null;
            writer.write("\n");
            writer.write("### " + getLinkToBuiltinTraitJson(id, trait.getDisplayName(0).getString()) + "\n");
            writer.write("- " + trait.getDescription(0).getString() + "\n");
            String materialsWithTrait = getMaterialsWithTrait(trait);
            writer.write("- Found On:\n  - Materials: " + (materialsWithTrait.isEmpty() ? "Nothing" : materialsWithTrait) + "\n");
            String partsWithTrait = getPartsWithTrait(trait);
            if (!partsWithTrait.isEmpty()) {
                writer.write("  - Parts: " + partsWithTrait + "\n");
            }
            if (!trait.getConditions().isEmpty()) {
                // Just wrap all of them inside an AND condition, since that's how the logic works anyway
                AndTraitCondition condition = new AndTraitCondition(trait.getConditions().toArray(new ITraitCondition[0]));
                writer.write("- Conditions: " + condition.getDisplayText().getString() + "\n");
            }
            writer.write("- ID: `" + id + "`\n");
            writer.write("- Type: `" + trait.getSerializer().getName() + "`\n");
            writer.write("- Max Level: " + trait.getMaxLevel() + "\n");
            Collection<String> cancelsWithSet = trait.getCancelsWithSet().stream().map(s -> "`" + s + "`").collect(Collectors.toList());
            if (!cancelsWithSet.isEmpty()) {
                writer.write("- Cancels With: " + String.join(", ", cancelsWithSet) + "\n");
            }
            Collection<String> wikiLines = trait.getExtraWikiLines();
            if (!wikiLines.isEmpty()) {
                writer.write("- Extra Info:\n");
                for (String line : wikiLines) {
                    writer.write(line + "\n");
                }
            }
        }
        writer.write("\n");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        Component fileNameText = (new TextComponent(output.getAbsolutePath())).withStyle(ChatFormatting.UNDERLINE).withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, output.getAbsolutePath())));
        player.sendMessage(new TextComponent("Wrote to ").append(fileNameText), Util.NIL_UUID);
    }
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ResourceLocation(net.minecraft.resources.ResourceLocation) SilentGear(net.silentchaos512.gear.SilentGear) ModContainer(net.minecraftforge.fml.ModContainer) java.util(java.util) CommandDispatcher(com.mojang.brigadier.CommandDispatcher) SharedSuggestionProvider(net.minecraft.commands.SharedSuggestionProvider) CommandSourceStack(net.minecraft.commands.CommandSourceStack) MaterialInstance(net.silentchaos512.gear.gear.material.MaterialInstance) LocalDateTime(java.time.LocalDateTime) MaterialManager(net.silentchaos512.gear.gear.material.MaterialManager) NetworkDirection(net.minecraftforge.network.NetworkDirection) ResourceLocationArgument(net.minecraft.commands.arguments.ResourceLocationArgument) ServerPlayer(net.minecraft.server.level.ServerPlayer) PartData(net.silentchaos512.gear.gear.part.PartData) ITraitCondition(net.silentchaos512.gear.api.traits.ITraitCondition) ChatFormatting(net.minecraft.ChatFormatting) ITraitSerializer(net.silentchaos512.gear.api.traits.ITraitSerializer) PartManager(net.silentchaos512.gear.gear.part.PartManager) ClientOutputCommandPacket(net.silentchaos512.gear.network.ClientOutputCommandPacket) SuggestionProvider(com.mojang.brigadier.suggestion.SuggestionProvider) IGearPart(net.silentchaos512.gear.api.part.IGearPart) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Component(net.minecraft.network.chat.Component) TraitManager(net.silentchaos512.gear.gear.trait.TraitManager) CommandContext(com.mojang.brigadier.context.CommandContext) SimpleTrait(net.silentchaos512.gear.gear.trait.SimpleTrait) Commands(net.minecraft.commands.Commands) ModList(net.minecraftforge.fml.ModList) AndTraitCondition(net.silentchaos512.gear.gear.trait.condition.AndTraitCondition) IModInfo(net.minecraftforge.forgespi.language.IModInfo) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Player(net.minecraft.world.entity.player.Player) PartType(net.silentchaos512.gear.api.part.PartType) IMaterial(net.silentchaos512.gear.api.material.IMaterial) Util(net.minecraft.Util) TextComponent(net.minecraft.network.chat.TextComponent) ITrait(net.silentchaos512.gear.api.traits.ITrait) java.io(java.io) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) DateTimeFormatter(java.time.format.DateTimeFormatter) TraitSerializers(net.silentchaos512.gear.gear.trait.TraitSerializers) Network(net.silentchaos512.gear.network.Network) ItemStack(net.minecraft.world.item.ItemStack) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) ClickEvent(net.minecraft.network.chat.ClickEvent) ServerPlayer(net.minecraft.server.level.ServerPlayer) Player(net.minecraft.world.entity.player.Player) ITrait(net.silentchaos512.gear.api.traits.ITrait) ITraitCondition(net.silentchaos512.gear.api.traits.ITraitCondition) SimpleTrait(net.silentchaos512.gear.gear.trait.SimpleTrait) AndTraitCondition(net.silentchaos512.gear.gear.trait.condition.AndTraitCondition) ClickEvent(net.minecraft.network.chat.ClickEvent) ResourceLocation(net.minecraft.resources.ResourceLocation) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent)

Aggregations

ITrait (net.silentchaos512.gear.api.traits.ITrait)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 Collectors (java.util.stream.Collectors)1 ChatFormatting (net.minecraft.ChatFormatting)1 Util (net.minecraft.Util)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 Commands (net.minecraft.commands.Commands)1 SharedSuggestionProvider (net.minecraft.commands.SharedSuggestionProvider)1 ResourceLocationArgument (net.minecraft.commands.arguments.ResourceLocationArgument)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ListTag (net.minecraft.nbt.ListTag)1 Tag (net.minecraft.nbt.Tag)1