Search in sources :

Example 6 with ITrait

use of net.silentchaos512.gear.api.traits.ITrait 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;
}
Also used : PartGearKey(net.silentchaos512.gear.api.util.PartGearKey) GetTraitsEvent(net.silentchaos512.gear.api.event.GetTraitsEvent) ITrait(net.silentchaos512.gear.api.traits.ITrait) TraitInstance(net.silentchaos512.gear.api.traits.TraitInstance) PartData(net.silentchaos512.gear.gear.part.PartData)

Example 7 with ITrait

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

the class TraitHelper method cancelTraits.

private static void cancelTraits(Map<ITrait, Integer> mapToModify, ITrait[] keys) {
    for (int i = 0; i < keys.length; ++i) {
        ITrait t1 = keys[i];
        if (mapToModify.containsKey(t1)) {
            for (int j = i + 1; j < keys.length; ++j) {
                ITrait t2 = keys[j];
                if (mapToModify.containsKey(t2) && t1.willCancelWith(t2)) {
                    final int level = mapToModify.get(t1);
                    final int otherLevel = mapToModify.get(t2);
                    final int cancelLevel = t1.getCanceledLevel(level, t2, otherLevel);
                    if (cancelLevel > 0) {
                        mapToModify.put(t1, cancelLevel);
                        mapToModify.remove(t2);
                    } else if (cancelLevel < 0) {
                        mapToModify.put(t2, -cancelLevel);
                        mapToModify.remove(t1);
                        break;
                    } else {
                        mapToModify.remove(t1);
                        mapToModify.remove(t2);
                        break;
                    }
                }
            }
        }
    }
}
Also used : ITrait(net.silentchaos512.gear.api.traits.ITrait)

Example 8 with ITrait

use of net.silentchaos512.gear.api.traits.ITrait 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)

Example 9 with ITrait

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

the class TraitsCommand method runDescribe.

private static int runDescribe(CommandContext<CommandSourceStack> context, ResourceLocation traitId) {
    ITrait trait = TraitManager.get(traitId);
    if (trait == null) {
        context.getSource().sendFailure(new TranslatableComponent("command.silentgear.traits.traitNotFound", traitId));
        return 0;
    }
    context.getSource().sendSuccess(trait.getDisplayName(0), true);
    context.getSource().sendSuccess(trait.getDescription(1), true);
    context.getSource().sendSuccess(new TranslatableComponent("command.silentgear.traits.maxLevel", trait.getMaxLevel()), true);
    context.getSource().sendSuccess(new TextComponent("Object: " + trait), true);
    context.getSource().sendSuccess(new TextComponent("Serializer: " + trait.getSerializer()), true);
    return 1;
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ITrait(net.silentchaos512.gear.api.traits.ITrait) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent)

Example 10 with ITrait

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

the class TraitsCommand method getDataSources.

private static String getDataSources() {
    Set<String> sourceSet = new LinkedHashSet<>();
    for (ITrait trait : TraitManager.getValues()) {
        sourceSet.add(trait.getId().getNamespace());
    }
    StringBuilder ret = new StringBuilder();
    for (String id : sourceSet) {
        ret.append("- ");
        Optional<? extends ModContainer> container = ModList.get().getModContainerById(id);
        if (container.isPresent()) {
            IModInfo modInfo = container.get().getModInfo();
            ret.append(modInfo.getDisplayName()).append(" (").append(id).append(") ").append(modInfo.getVersion()).append("\n");
        } else {
            ret.append(id);
        }
    }
    return ret.toString();
}
Also used : ITrait(net.silentchaos512.gear.api.traits.ITrait) IModInfo(net.minecraftforge.forgespi.language.IModInfo)

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