uniham/system/register_hammer.lua

96 lines
3.5 KiB
Lua
Raw Normal View History

-- Localize
local crush_nodes = uniham.crush_nodes
-- Register a new hammer
--
-- definition = {
-- name = 'Name of the hammer',
-- head = 'head_material_texture.png',
2018-09-30 19:14:41 +02:00
-- texture = 'hammer_texture.png',
-- craft = 'crafting:material',
-- on_use = on_use_function,
-- uses = 123,
-- }
--
-- The `name` will be shown as tooltip on hovering the item in the inventory
-- and will be used as it is.
--
-- `head` is a texture that is used as base for the hammer head texture mask.
-- To change the mask textture packs need to add `uniham_head_mask.png` with
-- another file. All black pixels will become transparent.
--
2018-09-30 19:14:41 +02:00
-- Instead of `head` the entry `texture` can be used. This does not auto-create
-- The hammer texture but uses the given texture file as hammer texture. If
-- both `head` and `texture` are given `texture` gets precedence.
--
-- With `craft` the material to craft the hammer from is defined. Hammers are
-- crafted all the same with the provided material for x and a stick for s
--
-- [ ][x][ ]
-- [ ][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
-- modify and return the itemstack as needed.
--
-- If no function is provided the default function (replace nodes by the table
-- provided as `uniham.crush_nodes` in mod loading state).
--
-- `uses` is added as `_uniham.uses` to the hammers item definition and can
-- be used by the custom `on_use` function. The built-in function (replacing
-- nodes) calculates the `add_wear` value from this.
--
-- @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 crush_nodes
2018-09-30 19:14:41 +02:00
local autotexture = '([combine:16x16:-1,1=+s)^((+h^+m)^[makealpha:0,0,0)'
local hammer_id = minetest.get_current_modname()..':'..short_id
2018-09-30 19:14:41 +02:00
local texture = ''
if definition.head and not definition.texture then
texture = autotexture:gsub('+.', {
['+s'] = 'default_stick.png',
['+h'] = definition.head,
['+m'] = 'uniham_head_mask.png'
})
end
if definition.texture then
texture = definition.texture
end
minetest.register_tool(hammer_id, {
description = definition.name,
inventory_image = texture,
_uniham = { uses = definition.uses },
on_use = function(itemstack, player, pointed_thing)
return on_use(itemstack, player, pointed_thing)
end
})
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