### Initialize Mod Setup Function Source: https://wiki.melvoridle.com/w/Mod_Creation/Getting_Started The setup function must be exported from setup.mjs to be accessible by the Mod Manager. ```javascript export function setup() { console.log('Hello From My Mod!'); } ``` -------------------------------- ### Define a setup entry-point module Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials The setup function is required for the Mod Manager to initialize the mod. It receives the context object upon loading. ```javascript // setup.mjs export function setup(ctx) { console.log('Hello World!'); } ``` -------------------------------- ### Accessing Context from a Module Setup Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Use the exported setup function in your module to receive the context object as a parameter. ```javascript export function setup(ctx) { // ... } ``` -------------------------------- ### Define legacy extension manifest.json Source: https://wiki.melvoridle.com/w/Mod_Creation/Migrating_from_Scripts_and_Extensions Example of a previous extension manifest structure before migration. ```json { "icons": { "48": "icons/my-icon-48.png" }, "content_scripts": [ { "js": ["sources/contentScript.js"], "css": ["styles/mainStyle.css"] } ] } ``` -------------------------------- ### Define Mod Manifest Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Configure the mod namespace and setup entry point in the manifest file. ```json // manifest.json { "namespace": "helloWorld", "setup": "setup.mjs" } ``` -------------------------------- ### Expose Mod API Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Use the setup function to register methods to the mod API, making them accessible to other mods. ```javascript // setup.mjs export function setup({ api }) { api({ greet: name => console.log(`Hello, ${name!}`); }); } ``` -------------------------------- ### Register Game Data Packages Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Methods for registering data packages via manifest or setup script. ```json // manifest.json { "namespace": "helloWorld", "load": ["path-to-your-data.json"] } ``` ```javascript // setup.mjs export async function setup({ gameData }) { await gameData.addPackage('path-to-your-data.json'); } ``` -------------------------------- ### Create a helper module Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Helper modules export functions that can be imported and used by the main setup module. ```javascript // helper.mjs export function greet(name) { console.log(`Hello, ${name}!`); } ``` -------------------------------- ### Create a PetiteVue component instance Source: https://wiki.melvoridle.com/w/Mod_Creation/Reusable_Components_with_PetiteVue Defines a template, manifest entry, and setup function to mount a component to the DOM. ```html ``` ```json // manifest.json { "load": ["templates.html"] } ``` ```javascript // setup.mjs function Counter(props) { return { $template: '#counter-component', count: props.count, inc() { this.count++; } }; } export function setup({ onInterfaceReady }) { onInterfaceReady(() => { // Create and append a Counter component to the bottom of the Woodcutting page ui.create(Counter({ count: 0 }), document.getElementById('woodcutting-container')); }); } ``` -------------------------------- ### Implementing SettingTypeConfig functions Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Define the render, get, and set logic for a custom setting type. ```javascript // The render function for a simple text box function render(name, onChange, config) { const input = document.createElement('input'); input.id = name; input.type = 'text'; input.name = name; input.addEventListener('change', () => onChange()); const label = document.createElement('label'); label.for = name; label.textContent = config.label; if (config.hint) { const hint = document.createElement('small'); hint.textContent = config.hint; label.appendChild(hint); } const validation = document.createElement('small'); validation.classList.add('text-danger', 'validation-message'); const root = document.createElement('div'); root.appendChild(input); root.appendChild(label); root.appendChild(validation); return root; } ``` ```javascript // get function for simple text input defined by above render function get(root) { return root.querySelector('input').value; } ``` ```javascript // set function for simple text setting defined above function set(root, value) { root.querySelector('input').value = value; } ``` -------------------------------- ### Pass context to a helper module Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Explicitly pass the context object from the setup function to helper modules that require it. ```javascript // configService.mjs export function init(ctx) { // Perform actions using the context object here... } // setup.mjs export async function setup(ctx) { const configService = await ctx.loadModule('configService.mjs'); configService.init(ctx); } ``` -------------------------------- ### Define Mod Metadata Source: https://wiki.melvoridle.com/w/Mod_Creation/Getting_Started The manifest.json file provides essential metadata to the game, specifically pointing to the setup script. ```json { "setup": "setup.mjs" } ``` -------------------------------- ### Define a method patch Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Initialize a patch for a specific class method within the setup function. ```javascript // setup.mjs export function setup({ patch }) { const xpPatch = patch(Skill, 'addXP'); } ``` -------------------------------- ### Get and Set Setting Values Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Access or modify setting values at runtime using the get and set methods on a section object. ```javascript // elsewhere.mjs const { settings } = mod.getContext(import.meta); const generalSettings = settings.section('General'); generalSettings.set('pick-a-number', 1); console.log(generalSettings.get('pick-a-number')); // 1 ``` -------------------------------- ### Get and Set Setting Values Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Retrieves or updates the value of a specific setting by its name. ```javascript // Assuming the player has typed "1" into the setting ctx.settings.section('Other').get('pick-a-number'); // 1 ``` ```javascript ctx.settings.section('Other').set('pick-a-number', 5); ``` -------------------------------- ### Define an Item via JSON Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Example of defining a weapon item within a JSON data package. ```json { "$schema": "https://melvoridle.com/assets/schema/gameData.json", "data": { "items": [{ "id": "Wooden_Dagger", "name": "Wooden Dagger", "category": "Combat", "type": "Weapon", "media": "wooden-dagger.png", "ignoreCompletion": false, "obtainFromItemLog": false, "golbinRaidExclusive": false, "sellsFor": 0, "tier": "wooden", "validSlots": ["Weapon"], "occupiesSlots": [], "equipRequirements": [ { "type": "SkillLevel", "skillID": "melvorD:Attack", "level": 1 } ], "equipmentStats": [ { "key": "attackSpeed", "value": 2200 }, { "key": "stabAttackBonus", "value": 4 }, { "key": "slashAttackBonus", "value": 1 }, { "key": "blockAttackBonus", "value": 4 }, { "key": "meleeStrengthBonus", "value": 1 } ], "itemType": "Weapon", "attackType": "melee" }] } } ``` -------------------------------- ### Registering a custom setting type with implementation Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Finalize the registration of a custom setting type by providing the required render, get, and set functions. ```javascript // Use functions defined in above examples as reference ctx.settings.type('simpleText', { render: render, get: get, set: set }); ``` -------------------------------- ### Get account storage item Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Retrieves the value associated with a key, returning undefined if the key does not exist. ```javascript ctx.accountStorage.getItem('coolThings'); // returns ['rocks'] ``` -------------------------------- ### sidebar.category(id, config, builder) Source: https://wiki.melvoridle.com/w/Mod_Creation/Sidebar_API_Reference Gets or creates a category by its ID, with optional configuration and a builder callback for adding items. ```APIDOC ## sidebar.category(id: string, config?: CategoryConfig, builder?: (category: Category) => void): Category ### Description Gets or creates a category by its unique identifier. Optionally configures the category and executes a builder function to add items. ### Parameters - **id** (string) - Required - The id of the category to get or create. - **config** (CategoryConfig) - Optional - Configuration object for the category. - **builder** (function) - Optional - Callback that receives the category instance for further modification. ### Returns - **Category** - The category object. ``` -------------------------------- ### PropertyPatch.replace(getter?: (o: () => any) => any, setter?: (o: (value: any) => void, value: any) => void) Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference An alias for calling both get and set methods simultaneously on a property. ```APIDOC ## PropertyPatch.replace(getter?: (o: () => any) => any, setter?: (o: (value: any) => void, value: any) => void) ### Description Alias for calling `get` and `set` at the same time. ### Parameters - **getter** ((o: () => any) => any) - Optional - The getter function to be executed. - **setter** ((o: (value: any) => void, value: any) => void) - Optional - The setter function to be executed. ``` -------------------------------- ### subitem(id: string, config?: SubitemConfig, builder?: (subitem: Subitem) => void): Subitem Source: https://wiki.melvoridle.com/w/Mod_Creation/Sidebar_API_Reference Gets or creates a subitem by its ID, with optional configuration and builder callback. ```APIDOC ## subitem(id: string, config?: SubitemConfig, builder?: (subitem: Subitem) => void): Subitem ### Description Gets or creates then gets a subitem by its id. ### Parameters - **id** (string) - Required - The id of the subitem to get or create - **config** (SubitemConfig) - Optional - An optional configuration object that may be used to configure either a new or existing subitem. - **builder** ((subitem: Subitem) => void) - Optional - An optional callback that receives the subitem as a parameter ### Returns - **Subitem** - The subitem object ### Example ``` // Move Pets above Skills in the Completion Log sidebar.category('General').item('Completion Log').subitem('Pets', { before: 'Skills' }); // Add a shortcut to Alt. Magic beneath Magic sidebar.category('Combat').item('melvorD:Magic').subitem('Alt. Magic', { onClick() { sidebar.category('Non-Combat').item('melvorD:Magic').click(); } }); ``` ``` -------------------------------- ### Render Component into UI Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Use the ui.create helper to instantiate and mount the component to a DOM element. ```javascript ui.create(Counter({ count: 0 }), document.getElementById('woodcutting-container')); ``` -------------------------------- ### Build Data Packages at Runtime Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Dynamic registration of game objects using the mod context API. ```javascript // setup.mjs export function setup({ gameData }) { gameData.buildPackage((p) => { // use the `p` object to add game objects }).add(); } ``` ```javascript // setup.mjs export function setup({ gameData }) { gameData.buildPackage((p) => { p.items.add({ id: 'Wooden_Dagger', name: 'Wooden Dagger', category: 'Combat', type: 'Weapon', media: 'wooden-dagger.png', ignoreCompletion: false, obtainFromItemLog: false, golbinRaidExclusive: false, sellsFor: 0, tier: 'wooden', validSlots: ['Weapon'], occupiesSlots: [], equipRequirements: [{ type: 'SkillLevel', skillID: 'melvorD:Attack', level: 1 }], equipmentStats: [ { key: 'attackSpeed', value: 2200 }, { key: 'stabAttackBonus', value: 4 }, { key: 'slashAttackBonus', value: 1 }, { key: 'blockAttackBonus', value: 4 }, { key: 'meleeStrengthBonus', value: 1 } ], itemType: 'Weapon', attackType: 'melee' }); }).add(); } ``` -------------------------------- ### Patching Getter Properties Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Use the get method to intercept and modify the return value of a getter property. ```javascript // Effectively double available Township resources ctx.patch(TownshipResource, 'amount').get(function (o) { return o() * 2; }); // Or more practically, make resources unlimited ctx.patch(TownshipResource, 'amount').get(function () { return 999999; }); ``` -------------------------------- ### ui.create(props: ComponentProps, host: HTMLElement) Source: https://wiki.melvoridle.com/w/Mod_Creation/Reusable_Components_with_PetiteVue Creates an instance of a PetiteVue component and mounts it within the specified HTML element. ```APIDOC ## ui.create(props, host) ### Description Creates an instance of a component and mounts it within the HTML. ### Parameters - **props** (ComponentProps) - Required - The PetiteVue component function that you want to instantiate. - **host** (HTMLElement) - Required - The element that the component should be appended to. ### Returns - **HTMLElement** - The host element. ``` -------------------------------- ### ui.createStatic(template, host) Source: https://wiki.melvoridle.com/w/Mod_Creation/Reusable_Components_with_PetiteVue Creates an instance of a static component from a template and mounts it to a specified host element. ```APIDOC ## ui.createStatic(template: string, host: HTMLElement) ### Description Creates an instance of a static component (no PetiteVue bindings) and mounts it within the HTML. This helper function should be preferred if you only need to create a reusable static piece of HTML. ### Parameters - **template** (string) - Required - The selector string for the template you want to clone (e.g., '#my-static-component'). - **host** (HTMLElement) - Required - The element that the component should be appended to. ### Returns - **HTMLElement** - The host element. ### Example ```javascript // Create the static component and place it at the bottom of the Woodcutting page ui.createStatic('#my-static-component', document.getElementById('woodcutting-container')); ``` ``` -------------------------------- ### Define Modifier in Data Package Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Example of registering a new modifier within a game data package. ```json { "$schema": "https://melvoridle.com/assets/schema/gameData.json", "namespace": "myNamespace", "data": { "modifiers": [ { "id": "flatSkillInterval", "inverted": true, "modifyValue": "value/1000", "allowedScopes": [ { "scopes": {}, "descriptions": [ { "text": "${value}s Global Non-Combat Skill Interval", "lang": "MODIFIER_DATA_flatSkillInterval" } ] } ] } ] } } ``` -------------------------------- ### Create Mod Settings Sections Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Initializes settings sections which determine the display order in the mod settings window. ```javascript ctx.settings.section('General'); ctx.settings.section('Other'); // Sections will be displayed in the settings window in this order // 1. General // 2. Other ``` -------------------------------- ### Import HTML Templates Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Import templates via manifest configuration or the loadTemplates helper function. ```json { "load": "templates.html" } ``` ```javascript export function setup({ loadTemplates }) { loadTemplates('templates.html'); } ``` -------------------------------- ### Create a Component Instance Source: https://wiki.melvoridle.com/w/Mod_Creation/Reusable_Components_with_PetiteVue Mounts the defined Block component into a specific DOM element. ```javascript ui.create(Block({ header: { title: 'My Block Component' }, content: { lines: ['My first paragraph.', 'My second paragraph.'] } }), document.getElementById('woodcutting-container')); ``` -------------------------------- ### Build and Register a Game Data Package Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Constructs a package using the builder API and registers it. ```javascript ctx.gameData.buildPackage((p) => { // data registration here }); ``` ```javascript const pkg = ctx.gameData.buildPackage((p) => { /* ... */ }); pkg.add(); ``` -------------------------------- ### Create Mod Setting Sections Source: https://wiki.melvoridle.com/w/Mod_Creation/Essentials Use the section method to initialize or retrieve a settings section header. ```javascript // setup.mjs export function setup({ settings }) { // Creates a section labeled "General" settings.section('General'); // Future calls to that section will not create a new "General" section, but instead return the already existing one settings.section('General'); } ``` -------------------------------- ### ui.createStore(props: Record) Source: https://wiki.melvoridle.com/w/Mod_Creation/Reusable_Components_with_PetiteVue Creates a PetiteVue store for sharing state amongst multiple components. ```APIDOC ## ui.createStore(props) ### Description Creates a PetiteVue store for sharing state amongst components. ### Parameters - **props** (Record) - Required - The props that the store should contain. ### Returns - **ComponentStore** - The PetiteVue store that can be shared between components. ``` -------------------------------- ### Get character storage item Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Retrieves a previously stored value by its key, returning undefined if the key does not exist. ```javascript ctx.characterStorage.getItem('coolThings'); // returns ['rocks'] ``` -------------------------------- ### Execute code on interface ready Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Register a callback to run after offline progress is calculated and the UI is fully initialized. ```javascript ctx.onInterfaceReady(async (ctx) => { // ... }); ``` -------------------------------- ### Configure and build sidebar categories Source: https://wiki.melvoridle.com/w/Mod_Creation/Sidebar_API_Reference Use category to retrieve or create a category, optionally applying configuration or a builder callback to populate items. ```javascript // Move COMBAT skills to be below NON-COMBAT sidebar.category('Combat', { after: 'Non-Combat' }); // Create a new category "GREETINGS" and add an item beneath it sidebar.category('Greetings', { toggleable: true }, (greetings) => { greetings.item('Hello'); }); ``` -------------------------------- ### Execute code on interface available Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Register a callback to run after the game interface is injected but before character loading. ```javascript ctx.onInterfaceAvailable(async (ctx) => { // ... }); ``` -------------------------------- ### Initialize Patching Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Entry point for creating MethodPatch or PropertyPatch objects for specific classes. ```javascript ctx.patch(Skill, 'addXP'); // Returns a MethodPatch ctx.patch(Skill, 'level'); // Returns a PropertyPatch ``` -------------------------------- ### loadStylesheet(path: string): void Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Injects a CSS stylesheet into the page. ```APIDOC ## loadStylesheet(path: string): void ### Description Injects a CSS stylesheet into the page. ### Parameters - **path** (string) - Required - The relative path to the stylesheet resource. ``` -------------------------------- ### settings.section Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Accesses or creates a settings section for the mod. ```APIDOC ## section(name: string) ### Description Gets or creates a settings section. The order that sections are created determines the display order in the mod's settings window. ### Parameters - **name** (string) - Required - The name of the section, displayed as a header. ### Returns - **Section** - The section object used to add, set, or get settings. ### Example ```javascript ctx.settings.section('General'); ``` ``` -------------------------------- ### loadTemplates(path: string): Promise Source: https://wiki.melvoridle.com/w/Mod_Creation/Mod_Context_API_Reference Inject all