Skip to content

LunaSnippets

Lukas04 edited this page Mar 19, 2023 · 5 revisions

Snippets are an alternative to console commands, instead of being executed through a Console, they are listed cards in an UI window. The benefits are that new Snippets are easier to discover and that the output window can be used to display any TooltipMakerAPI UI elements. The downside is that its a Campaign-based UI element, meaning unlike Console Commands, this doesnt suit Combat content.

A Snippet can be made by extending the LunaSnippet class.

public class ModListSnippet extends LunaSnippet {

    //The name displayed on top of the Snippet.
    @Override
    public String getName() {
        return "Copy modlist to the clipboard";
    }

    //The Description of the Snippet. The length of the Description slightly increases the size of the Snippet, but not by a lot.
    //If your Snippet has a long description and not many parameters, use the builder.addSpace(); method in addParameters to increase the size of the Snippet.
    @Override
    public String getDescription() {
        return "Copies a list of all loaded mods on to the clipboard. Can be toggled in to a more discord-friendly format.";
    }

    //Required to display which mod the Snippet is from.
    @Override
    public String getModId() {
        return "lunalib";
    }

    //Represents the tags on the left side of the Snippet Menu. The SnippetTags Enum provides the main preset Tags available, but returning a Unique String will
    //create a new Tag and add it to the list.
    @Override
    public List<String> getTags() {
        List<String> tags = new ArrayList<>();
        tags.add(SnippetTags.Debug.toString());
        return tags;
    }

    //Called when the Snippet is being created. It allows you to add Parameters to the Snippet. The "Key" String can be received in the execute method.
    @Override
    public void addParameters(SnippetBuilder builder) {
        builder.addBooleanParameter("Discord Format", "Discord", false);
    }

    //Called when the "Execute" has been pressed.
    //The parameters variable contains the value of all parameters added from addParameters, getabble through their key String.
    //The output variable allows to add paragraphs, images, etc to the output panel.
    @Override
    public void execute(Map<String, Object> parameters, TooltipMakerAPI output) {
        Boolean discord = (Boolean) parameters.get("Discord");

        List<ModSpecAPI> mods = Global.getSettings().getModManager().getEnabledModsCopy();
        StringBuilder modList = new StringBuilder();

        //Creating a new Paragraph at the top of the Output Window.
        output.addPara("Succesfully copied to clipboard!", 0f, Misc.getPositiveHighlightColor(), Misc.getHighlightColor());
        output.addSpacer(5f);
        for (ModSpecAPI mod : mods)
        {
            if (discord)
            {
                modList.append("**" + mod.getName() + ":** ``" + mod.getVersion() + "``\n");
            }
            else
            {
                modList.append(mod.getName() + ":" + mod.getVersion() + "\n");
            }
            //Creating a new Paragraph in the output window for each line, and then highlighting the Mod name.
            LabelAPI paragraph = output.addPara(mod.getName() + ":" + mod.getVersion(), 0f, Misc.getBasePlayerColor(), Misc.getHighlightColor());
            paragraph.setHighlight(mod.getName());
        }

        StringSelection stringSelection = new StringSelection(modList.toString());
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, null);
    }
}

To have the snippet show up in the menu, you need to call the LunaDebug.addSnippet() method in your ModPlugins OnApplicationLoad() method and input the Snippets Class. If Lunalib isnt a hard-dependency for your mod, make sure to check if LunaLib is active.

public class ExamplePlugin extends BaseModPlugin {

    @Override
    public void onApplicationLoad()
    {
        if (Global.getSettings().getModManager().isModEnabled("lunalib"))
        {
            LunaDebug.addSnippet(new ModListSnippet());
        }
    }
}