Game Api
Game Api
=================
GitHub Repo: https://github.com/minetest/minetest_game
Introduction
------------
The Minetest Game game offers multiple new possibilities in addition to the
Minetest engine's built-in API,
allowing you to add new plants to farming mod, buckets for new liquids, new stairs
and custom panes.
For information on the Minetest API, visit
https://github.com/minetest/minetest/blob/master/doc/lua_api.txt
Please note:
Bucket API
----------
The bucket API allows registering new types of buckets for non-default liquids.
bucket.register_liquid(
"default:lava_source", -- name of the source node
"default:lava_flowing", -- name of the flowing node
"bucket:bucket_lava", -- name of the new bucket item (or nil if
liquid is not takeable)
"bucket_lava.png", -- texture of the new bucket item (ignored if
itemname == nil)
"Lava Bucket", -- text description of the bucket item
{lava_bucket = 1}, -- groups of the bucket item, OPTIONAL
false -- force-renew, OPTIONAL. Force the liquid
source to renew if it has
-- a source neighbour, even if defined as
'liquid_renewable = false'.
-- Needed to avoid creating holes in sloping
rivers.
)
The filled bucket item is returned to the player that uses an empty bucket pointing
to the given liquid source.
When punching with an empty bucket pointing to an entity or a non-liquid node, the
on_punch of the entity or node will be triggered.
Beds API
--------
beds.register_bed(
"beds:bed", -- Bed name
def -- See [#Bed definition]
)
{
description = "Simple Bed",
inventory_image = "beds_bed.png",
wield_image = "beds_bed.png",
tiles = {
bottom = {'Tile definition'}, -- the tiles of the bottom part of
the bed.
top = {Tile definition} -- the tiles of the bottom part of the
bed.
},
nodebox = {
bottom = 'regular nodebox', -- bottom part of bed (see [Node
boxes])
top = 'regular nodebox', -- top part of bed (see [Node
boxes])
},
selectionbox = 'regular nodebox', -- for both nodeboxes (see [Node
boxes])
recipe = { -- Craft recipe
{"group:wool", "group:wool", "group:wool"},
{"group:wood", "group:wood", "group:wood"}
}
}
Bones API
---------
Creative API
------------
is used to show all tools. Name is used in the sfinv page name, title is the
human readable title.
Group overrides can be used for any registered item, node or tool. Use one of
the groups stated below to pick which category it will appear in.
Chests API
----------
The chests API allows the creation of chests, which have their own inventories for
holding items.
`default.chest.get_chest_formspec(pos)`
`default.chest.chest_lid_obstructed(pos)`
* Returns a boolean depending on whether or not a chest has its top obstructed by
a solid node.
* `pos` Location of the chest node, e.g `{x = 1, y = 1, z = 1}`
`default.chest.chest_lid_close(pn)`
`default.chest.open_chests`
* A table indexed by player name to keep track of who opened what chest.
* Key: The name of the player.
* Value: A table containing information about the chest the player is looking at.
e.g `{ pos = {1, 1, 1}, sound = null, swap = "default:chest" }`
`default.chest.register_chest(name, def)`
description = "Chest",
tiles = {
"default_chest_top.png",
"default_chest_top.png",
"default_chest_side.png",
"default_chest_side.png",
"default_chest_front.png",
"default_chest_inside.png"
}, -- Textures which are applied to the chest model.
sounds = default.node_sound_wood_defaults(),
sound_open = "default_chest_open",
sound_close = "default_chest_close",
groups = {choppy = 2, oddly_breakable_by_hand = 2},
protected = false, -- If true, only placer can modify chest.
Doors API
---------
The doors mod allows modders to register custom doors and trapdoors.
`doors.register_door(name, def)`
`doors.register_trapdoor(name, def)`
`doors.register_fencegate(name, def)`
`doors.get(pos)`
### Methods
The mod that places chests with loot in dungeons provides an API to register
additional loot.
`dungeon_loot.register(def)`
`dungeon_loot.registered_loot`
name = "item:name",
chance = 0.5,
-- ^ chance value from 0.0 to 1.0 that the item will appear in the chest when
chosen
-- Due to an extra step in the selection process, 0.5 does not(!) mean that
-- on average every second chest will have this item
count = {1, 4},
-- ^ table with minimum and maximum amounts of this item
-- optional, defaults to always single item
y = {-32768, -512},
-- ^ table with minimum and maximum heights this item can be found at
-- optional, defaults to no height restrictions
types = {"desert"},
-- ^ table with types of dungeons this item can be found in
-- supported types: "normal" (the cobble/mossycobble one), "sandstone"
-- "desert" and "ice"
-- optional, defaults to no type restrictions
Fence API
---------
Registers a new fence. Custom fields texture and material are required, as
are name and description. The rest is optional. You can pass most normal
nodedef fields here except drawtype. The fence group will always be added
for this node.
Walls API
---------
The walls API allows easy addition of stone auto-connecting wall nodes.
Farming API
-----------
The farming API allows you to easily register plants and hoes.
`farming.registered_plants[name] = definition`
* Table of registered plants, indexed by plant name
{
description = "", -- Description for tooltip
inventory_image = "unknown_item.png", -- Image to be used as wield-
and inventory image
max_uses = 30, -- Uses until destroyed
material = "", -- Material for recipes
recipe = { -- Craft recipe, if material
isn't used
{"air", "air", "air"},
{"", "group:stick"},
{"", "group:stick"},
}
}
{
description = "", -- Description of seed item
harvest_description = "", -- Description of harvest item
-- (optional, derived
automatically if not provided)
inventory_image = "unknown_item.png", -- Image to be used as seed's
wield- and inventory image
steps = 8, -- How many steps the plant has
to grow, until it can be harvested
-- ^ Always provide a plant texture for each step, format:
modname_plantname_i.png (i = stepnumber)
minlight = 13, -- Minimum light to grow
maxlight = default.LIGHT_MAX -- Maximum light to grow
}
Fire API
--------
Add group flammable when registering a node to make fire seek for it.
Add it to an item to make it burn up when dropped in lava or fire.
New node def property:
`on_burn(pos)`
`on_ignite(pos, igniter)`
* Called when Flint and steel (or a mod defined ignitor) is used on a node.
Defining it may prevent the default action (spawning flames) from triggering.
* `pos` Position of the ignited node.
* `igniter` Player that used the tool, when available.
`give_initial_stuff.give(player)`
`give_initial_stuff.add(stack)`
`give_initial_stuff.clear()`
`give_initial_stuff.get_list()`
`give_initial_stuff.set_list(list)`
`give_initial_stuff.add_from_csv(str)`
^ str is a comma separated list of initial stuff
^ Adds items to the list of items to be given
Player API
----------
The player API can register player models and update the player's appearance.
* `player_api.globalstep(dtime, ...)`
* The function called by the globalstep that controls player animations.
You can override this to replace the globalstep with your own
implementation.
* Receives all args that minetest.register_globalstep() passes
* `player_api.register_model(name, def)`
* Register a new model to be used by players
* `name`: model filename such as "character.x", "foo.b3d", etc.
* `def`: see [#Model definition]
* Saved to player_api.registered_models
* `player_api.registered_models[name]`
* Get a model's definition
* `name`: model filename
* See [#Model definition]
* `player_api.set_model(player, model_name)`
* Change a player's model
* `player`: PlayerRef
* `model_name`: model registered with `player_api.register_model`
* `player_api.set_textures(player, textures)`
* Sets player textures
* `player`: PlayerRef
* `textures`: array of textures. If nil, the default from the model def is
used
* `player_api.get_animation(player)`
* Returns a table containing fields `model`, `textures` and `animation`
* Any of the fields of the returned table may be nil
* `player`: PlayerRef
* `player_api.player_attached`
* A table that maps a player name to a boolean
* If the value for a given player is set to true, the default player
animations
(walking, digging, ...) will no longer be updated, and knockback from
damage is
prevented for that player
* Example of usage: A mod sets a player's value to true when attached to a
vehicle
{
animation_speed = 30, -- Default animation speed, in
keyframes per second
textures = {"character.png"}, -- Default array of textures
animations = {
-- [anim_name] = {
-- x = <start_frame>,
-- y = <end_frame>,
-- collisionbox = <model collisionbox>, -- (optional)
-- eye_height = <model eye height>, -- (optional)
-- -- suspend client side animations while this one is active
(optional)
-- override_local = <true/false>
-- },
stand = ..., lay = ..., walk = ..., mine = ..., walk_mine = ...,
-- required animations
sit = ... -- used by boats and other MTG mods
},
-- Default object properties, see lua_api.txt
visual_size = {x = 1, y = 1},
collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3},
stepheight = 0.6,
eye_height = 1.47
}
TNT API
-------
`tnt.register_tnt(definition)`
^ Create an explosion.
`tnt.burn(position, [nodename])`
^ Ignite node at position, triggering its `on_ignite` callback (see fire mod).
If no such callback exists, fallback to turn tnt group nodes to their
"_burning" variant.
nodename isn't required unless already known.
To make dropping items from node inventories easier, you can use the
following helper function from 'default':
The function returns no values. The drops are returned in the `drops`
parameter, and drops is not reinitialized so you can call it several
times in a row to add more inventory items to it.
`on_blast` callbacks:
`nodedef.on_blast(pos, intensity)`
^ Allow drop and node removal overriding
* `pos` - node position
* `intensity` - TNT explosion measure. larger or equal to 1.0
^ Should return a list of drops (e.g. {"default:stone"})
^ Should perform node removal itself. If callback exists in the nodedef
^ then the TNT code will not destroy this node.
`entitydef.on_blast(luaobj, damage)`
^ Allow TNT effects on entities to be overridden
* `luaobj` - LuaEntityRef of the entity
* `damage` - suggested HP damage value
^ Should return a list of (bool do_damage, bool do_knockback, table drops)
* `do_damage` - if true then TNT mod wil damage the entity
* `do_knockback` - if true then TNT mod will knock the entity away
* `drops` - a list of drops, e.g. {"wool:red"}
Screwdriver API
---------------
The screwdriver API allows you to control a node's behaviour when a screwdriver is
used on it.
To use it, add the `on_screwdriver` function to the node definition.
Sethome API
-----------
The sethome API adds three global functions to allow mods to read a players home
position,
set a players home position and teleport a player to home position.
`sethome.get(name)`
`sethome.set(name, pos)`
`sethome.go(name)`
Sfinv API
---------
It is recommended that you read this link for a good introduction to the
sfinv API by its author:
https://rubenwardy.com/minetest_modding_book/en/chapters/sfinv.html
**Pages**
**Contexts**
**Theming**
### Context
### sfinv.register_page
sfinv.register_page(name, def)
Stairs API
----------
The stairs API lets you register stairs and slabs and ensures that they are
registered the same way as those
delivered with Minetest Game, to keep them compatible with other mods.
* Registers a stair
* `subname`: Basically the material name (e.g. cobble) used for the stair name.
Nodename pattern: "stairs:stair_subname"
* `recipeitem`: Item used in the craft recipe, e.g. "default:cobble", may be `nil`
* `groups`: See [Known damage and digging time defining groups]
* `images`: See [Tile definition]
* `description`: Used for the description field in the stair's definition
* `sounds`: See [#Default sounds]
* `worldaligntex`: A bool to set all textures world-aligned. Default false. See
[Tile definition]
* Registers a slab
* `subname`: Basically the material name (e.g. cobble) used for the slab name.
Nodename pattern: "stairs:slab_subname"
* `recipeitem`: Item used in the craft recipe, e.g. "default:cobble"
* `groups`: See [Known damage and digging time defining groups]
* `images`: See [Tile definition]
* `description`: Used for the description field in the slab's definition
* `sounds`: See [#Default sounds]
* `worldaligntex`: A bool to set all textures world-aligned. Default false. See
[Tile definition]
```
stairs.register_stair_and_slab(subname, recipeitem, groups, images, desc_stair,
desc_slab,
sounds, worldaligntex, desc_stair_inner, desc_stair_outer)
```
Xpanes API
----------
`xpanes.register_pane(subname, def)`
{
textures = {
"texture for front and back",
(unused),
"texture for the 4 edges"
}, -- More tiles aren't supported
groups = {group = rating}, -- Uses the known node groups, see [Known
damage and digging time defining groups]
sounds = SoundSpec, -- See [#Default sounds]
recipe = {{"","","","","","","","",""}}, -- Recipe field only
use_texture_alpha = true, -- Optional boolean (default: `false`) for
colored glass panes
}
Raillike definitions
--------------------
The following nodes use the group `connect_to_raillike` and will only connect to
raillike nodes within this group and the same group value.
Use `minetest.raillike_group(<Name>)` to get the group value.
Example:
If you want to add a new rail type and want it to connect with default:rail,
add `connect_to_raillike=minetest.raillike_group("rail")` into the `groups` table
of your node.
Default sounds
--------------
Sounds inside the default table can be used within the sounds field of node
definitions.
* `default.node_sound_defaults()`
* `default.node_sound_stone_defaults()`
* `default.node_sound_dirt_defaults()`
* `default.node_sound_sand_defaults()`
* `default.node_sound_wood_defaults()`
* `default.node_sound_leaves_defaults()`
* `default.node_sound_glass_defaults()`
* `default.node_sound_metal_defaults()`
Default constants
-----------------
`default.get_hotbar_bg(x, y)`
`default.gui_bg`
`default.gui_bg_img`
`default.gui_slots`
`default.gui_survival_form`
`default.get_furnace_active_formspec(fuel_percent, item_percent)`
* Get the active furnace formspec using the defined GUI elements
* fuel_percent: Percent of how much the fuel is used
* item_percent: Percent of how much the item is cooked
`default.get_furnace_inactive_formspec()`
* Get the inactive furnace formspec using the defined GUI elements
Leafdecay
---------
To enable leaf decay for leaves when a tree is cut down by a player,
register the tree with the default.register_leafdecay(leafdecaydef)
function.
`default.register_leafdecay(leafdecaydef)`
Dyes
----
To make recipes that will work with dyes from many mods, define them using the
dye group and the color groups.
* `color_white`
* `color_grey`
* `color_dark_grey`
* `color_black`
* `color_red`
* `color_pink`
* `color_orange`
* `color_brown`
* `color_yellow`
* `color_green`
* `color_dark_green`
* `color_blue`
* `color_cyan`
* `color_violet`
* `color_magenta`
Example of one shapeless recipe using the dye group and a color group:
minetest.register_craft({
type = "shapeless",
output = "<mod>:item_yellow",
recipe = {"<mod>:item_no_color", "group:dye,color_yellow"},
})
Trees
-----
* `default.grow_tree(pos, is_apple_tree)`
* Grows a mgv6 tree or apple tree at pos
* `default.grow_jungle_tree(pos)`
* Grows a mgv6 jungletree at pos
* `default.grow_pine_tree(pos)`
* Grows a mgv6 pinetree at pos
* `default.grow_new_apple_tree(pos)`
* Grows a new design apple tree at pos
* `default.grow_new_jungle_tree(pos)`
* Grows a new design jungle tree at pos
* `default.grow_new_pine_tree(pos)`
* Grows a new design pine tree at pos
* `default.grow_new_snowy_pine_tree(pos)`
* Grows a new design snowy pine tree at pos
* `default.grow_new_acacia_tree(pos)`
* Grows a new design acacia tree at pos
* `default.grow_new_aspen_tree(pos)`
* Grows a new design aspen tree at pos
* `default.grow_bush(pos)`
* Grows a bush at pos
* `default.grow_acacia_bush(pos)`
* Grows an acaia bush at pos
* `default.grow_pine_bush(pos)`
* Grows a pine bush at pos
* `default.grow_blueberry_bush(pos)`
* Grows a blueberry bush at pos
Carts
-----
carts.register_rail(
"mycarts:myrail", -- Rail name
nodedef, -- standard nodedef
railparams -- rail parameter struct (optional)
)
railparams = {
on_step(obj, dtime), -- Event handler called when
-- cart is on rail
acceleration, -- integer acceleration factor (negative
-- values to brake)
}
Key API
-------
The key API allows mods to add key functionality to nodes that have
ownership or specific permissions. Using the API will make it so
that a node owner can use skeleton keys on their nodes to create keys
for that node in that location, and give that key to other players,
allowing them some sort of access that they otherwise would not have
due to node protection.
To make your new nodes work with the key API, you need to register
two callback functions in each nodedef:
`on_key_use(pos, player)`
* Is called when a player right-clicks (uses) a normal key on your
* node.
* `pos` - position of the node
* `player` - PlayerRef
* return value: none, ignored
Aside from the secret value, the function should retun a descriptive
name for the node and the owner name. The return values are all
encoded in the key that will be given to the player in replacement
for the wielded skeleton key.
`default.register_craft_metadata_copy(ingredient, result)`
----------------------------------------------------------
Log API
-------
`default.log_player_action(player, ...)`
`default.set_inventory_action_loggers(def, name)`
* sets the callbacks `on_metadata_inventory_move`,
`on_metadata_inventory_put` and `on_metadata_inventory_take`
that log corresponding actions
* `def` See [Node definition]
* `name` Description of the node in the log message