uniham/init.lua

140 lines
4.0 KiB
Lua
Raw Normal View History

2018-09-22 01:15:44 +02:00
local replacements = {
-- default sandstone to sand
['default:sandstone'] = 'default:sand',
['default:desert_sandstone'] = 'default:desert_sand',
['default:silver_sandstone'] = 'default:silver_sand',
-- stone crushing
['default:obsidian'] = 'default:stone',
['default:stone'] = 'default:cobble',
['default:cobble'] = 'default:gravel',
['default:gravel'] = 'default:sand'
}
-- Hammer use function
--
-- The function is called when using the hammer. It either converts the node to
-- its replacement or does nothing. When converting the modified (added wear)
-- itemstack is returned.
--
-- @param itemstack The `itemstack` as per on_use definition
-- @param player The `player` as per on_use definition
-- @param pointed_thing The `pointed_thing` as per on_use definition
-- @see <https://dev.minetest.net/on_use#on_use>
-- @return itemstack|void
local use_hammer = function (itemstack, player, pointed_thing)
if pointed_thing.type == 'nothing' then return end
local node_pos = minetest.get_pointed_thing_position(pointed_thing)
local node_name = minetest.get_node(node_pos).name
local node_sounds = minetest.registered_nodes[node_name].sounds
if not replacements[node_name] then
minetest.sound_play(node_sounds.dug.name, {
pos = node_pos,
max_hear_distance = 8,
gain = node_sounds.dug.gain,
})
return
end
local replacement = replacements[node_name]
local replacement_sounds = minetest.registered_nodes[replacement].sounds
local uses = minetest.registered_tools[itemstack:get_name()]._uniham.uses
minetest.sound_play(replacement_sounds.dug.name, {
pos = node_pos,
max_hear_distance = 8,
gain = replacement_sounds.dug.gain,
})
minetest.set_node(node_pos, { name = replacement })
minetest.check_for_falling(node_pos)
itemstack:add_wear(math.ceil(65535 / uses) + 1)
return itemstack
end
-- Register a new hammer
--
-- definition = {
-- name = 'Name of the hammer',
-- head = 'head_material_texture.png',
-- craft = 'crafting:material',
-- uses = 123,
-- }
--
-- @param short_id Unprefixed ID for the new hammer
-- @param definition Definition table as described
-- @return void
local register_hammer = function (short_id, definition)
local texture = '([combine:16x16:-1,1=+s)^((+h^+m)^[makealpha:0,0,0)'
texture = texture:gsub('+.', {
['+s'] = 'default_stick.png',
['+h'] = definition.head,
['+m'] = 'uniham_head_mask.png'
})
minetest.register_tool('uniham:'..short_id, {
description = definition.name,
inventory_image = texture,
_uniham = { uses = definition.uses },
on_use = function(itemstack, player, pointed_thing)
return use_hammer(itemstack, player, pointed_thing)
end
})
minetest.register_craft({
output = 'uniham:'..short_id,
recipe = {
{ '', definition.craft, '' },
{ '', 'default:stick', definition.craft },
{ 'default:stick', '', '' }
}
})
end
register_hammer('hammer_bronze', {
name = 'Bronze Hammer',
head = 'default_bronze_block.png',
craft = 'default:bronze_ingot',
uses = 100
})
register_hammer('hammer_diamond', {
name = 'Diamond Hammer',
head = 'default_diamond_block.png',
craft = 'default:diamond',
uses = 200
})
register_hammer('hammer_mese', {
name = 'Mese Hammer',
head = 'default_mese_block.png',
craft = 'default:mese_crystal',
uses = 300
})
register_hammer('hammer_steel', {
name = 'Steel Hammer',
head = 'default_steel_block.png',
craft = 'default:iron_ingot',
uses = 150
})
register_hammer('hammer_stone', {
name = 'Stone Hammer',
head = 'default_stone.png',
craft = 'default:cobble',
uses = 50
})
register_hammer('hammer_wood', {
name = 'Wooden Hammer',
head = 'default_wood.png',
craft = 'default:wood',
uses = 10
})