allow for recipe tables instead of only item IDs

This commit is contained in:
Dirk Sohler 2018-09-30 19:30:28 +02:00
parent 145208ade5
commit 0bc60d396f
No known key found for this signature in database
GPG Key ID: B9751241BD7D4E1A
2 changed files with 21 additions and 11 deletions

@ -58,7 +58,7 @@ This registers a hammer with the name “My Cool Hammer” that has the ID `mymo
If instead of `head` the value `texture` is provided then the automatic texture generation is disabled and the hammer will get the texture defined by the `texture` value. If both values are added then the `texture` value is given precedence.
The hammer is crafted like the provided hammers with two sticks and two of the items provided for `craft`.
The hammer is crafted like the provided hammers with two sticks and two of the items provided for `craft`. Alternative: When providing a [recipe table](https://dev.minetest.net/minetest.register_craft) instead of an item ID then the recipe table is used for registering the crafting recipe for the hammer (the `output` value gets automatically set by the API and can be omitted).
`uses` defines how often the hammer can be used. The value also gets stored in the tools definition as `_uniham.uses` that can be used by a custom `on_use` function for example

@ -31,6 +31,10 @@ local use_hammer = uniham.use_hammer
-- [ ][s][x]
-- [s][ ][ ]
--
-- If for `craft` a table is provided then the mod tries to register the hammer
-- with the given table as recipe table. The `output` value gets overwritten
-- by the mod but all other values are taken as they are.
--
-- The function provided via `on_use` gets three parameters when called by the
-- hammers on_use action: `itemstack`, `player`, and `pointed_thing`. All of
-- those three parameters are defined by Minetests API. The function has to
@ -46,10 +50,12 @@ local use_hammer = uniham.use_hammer
-- @param short_id Unprefixed ID for the new hammer
-- @param definition Definition table as described
-- @see <https://dev.minetest.net/on_use#on_use>
-- @see <https://dev.minetest.net/minetest.register_craft>
-- @return void
uniham.register_hammer = function (short_id, definition)
local on_use = definition.on_use or use_hammer
local autotexture = '([combine:16x16:-1,1=+s)^((+h^+m)^[makealpha:0,0,0)'
local hammer_id = minetest.get_current_modname()..':'..short_id
local texture = ''
if definition.head and not definition.texture then
@ -64,8 +70,7 @@ uniham.register_hammer = function (short_id, definition)
texture = definition.texture
end
minetest.register_tool('uniham:'..short_id, {
minetest.register_tool(hammer_id, {
description = definition.name,
inventory_image = texture,
_uniham = { uses = definition.uses },
@ -74,12 +79,17 @@ uniham.register_hammer = function (short_id, definition)
end
})
minetest.register_craft({
output = minetest.get_current_modname()..':'..short_id,
recipe = {
{ '', definition.craft, '' },
{ '', 'default:stick', definition.craft },
{ 'default:stick', '', '' }
}
})
if type(definition.craft) == 'string' then
minetest.register_craft({
output = hammer_id,
recipe = {
{ '', definition.craft, '' },
{ '', 'default:stick', definition.craft },
{ 'default:stick', '', '' }
}
})
elseif type(definition.craft) == 'table' then
definition.craft['output'] = hammer_id
minetest.register_craft(definition.craft)
end
end