Skip to content
Merlord edited this page Oct 3, 2023 · 7 revisions

The Crafting Framework

Create a Crafting System

The Crafting Framework allows you to easily create a simple crafting system with minimal configs, while providing plenty of customisability for more advanced systems. Creating a crafting system generally requires the following three steps: registering Materials, creating a list of recipes, and registering your Menu Activator.

Step 1: Register your Materials and Tools

Registering Materials is only required if your recipes will use generic materials, where you either have multiple objects that are valid for that material, or you want other mods to be able to add valid ids for that material for cross-mod compatibility. If so, register your Materials before you register your Menu Activator.

See the Material section for how to register materials.

Tools are like materials except they are not used up when crafting (although equipped tools can have their condition degraded with each use). You can also require that a tool actually be equipped in order to be valid.

See the Tool section for how to register tools.

Step 2: Create your Recipes

Before creating your Menu Activator you will need a list of crafting recipes for it to register with.

See the Recipe section for how to create them.

Step 3: Register a Menu Activator

The Menu Activator is the starting point of your crafting system. It determines what your crafting menu will look like, what recipes it will have, and how/when it is opened. Once you have your materials and recipes, register your MenuActivator with the list of recipes added to it.

See the MenuActivator section for how to register your Menu Activator.


Integrate with an Existing Crafting Menu

After a Menu Activator is Registered, it fires an event. Register a callback to this event to add recipes to the menu.

Example:

local recipes = {
   --add standard crafting framework recipe configs here
}

-- Replace [MENU_ACTIVATOR_ID] with the (case-sensitive) id of the menu activator
event.register("[MENU_ACTIVATOR_ID]:Registered", function(e)
    e.menuActivator:registerRecipes(recipes)
end)

Example of a Fully Configured Crafting System

--Get the Crafting Framework API and check that it exists
local CraftingFramework = include("CraftingFramework")
if not CraftingFramework then return end

--Register your materials
local materials = {
    {
        id = "wood",
        name = "Wood",
        ids = {
            "misc_firewood",
            "misc_oak_wood_01"
        }
    },
    {
        id = "metal",
        name = "Metal",
        ids = {
            "misc_iron_01",
            "misc_steel_01",
            "ingred_scrap_metal_01"
        }
    }
}
CraftingFramework.Material:registerMaterials(materials)

--Create List of Recipes
local recipes = {
    {
        id = "crafted_item_01",
        materials = {
            { 
                material = "wood",
                count = 1,
            },
            { 
                material = "metal",
                count = 3,
            },
        }
    },
    {
        id = "crafted_item_02",
        materials = {
            { 
                material = "someItemId_01",
                count = 1,
            },
            { 
                material = "wood",
                count = 2,
            },
        }
    },
}

--Register your MenuActivator
CraftingFramework.MenuActivator:new{
    id = "My_activator_01",
    type = "activate",
    recipes = recipes
}
  翻译: