From: octacian Date: Thu, 16 Mar 2017 05:22:09 +0000 (-0700) Subject: Keys: Move skeleton key to craftitems.lua X-Git-Tag: 0.4.16~26 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=c54a7e882f161155641267e63dfb4365f94b4ac1;p=oweals%2Fminetest_game.git Keys: Move skeleton key to craftitems.lua Commit 73d61cbb103415c9a3970eaf2c48aeadabfff828 makes skeleton keys craftitems rather than tools, warranting the move from tools.lua to craftitems.lua. --- diff --git a/mods/default/craftitems.lua b/mods/default/craftitems.lua index aa8f56bd..a0db6554 100644 --- a/mods/default/craftitems.lua +++ b/mods/default/craftitems.lua @@ -186,6 +186,62 @@ minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv craft_inv:set_stack("craft", index, original) end) +minetest.register_craftitem("default:skeleton_key", { + description = "Skeleton Key", + inventory_image = "default_key_skeleton.png", + groups = {key = 1}, + on_use = function(itemstack, user, pointed_thing) + if pointed_thing.type ~= "node" then + return itemstack + end + + local pos = pointed_thing.under + local node = minetest.get_node(pos) + + if not node then + return itemstack + end + + local on_skeleton_key_use = minetest.registered_nodes[node.name].on_skeleton_key_use + if not on_skeleton_key_use then + return itemstack + end + + -- make a new key secret in case the node callback needs it + local random = math.random + local newsecret = string.format( + "%04x%04x%04x%04x", + random(2^16) - 1, random(2^16) - 1, + random(2^16) - 1, random(2^16) - 1) + + local secret, _, _ = on_skeleton_key_use(pos, user, newsecret) + + if secret then + local inv = minetest.get_inventory({type="player", name=user:get_player_name()}) + + -- update original itemstack + itemstack:take_item() + + -- finish and return the new key + local new_stack = ItemStack("default:key") + local meta = new_stack:get_meta() + meta:set_string("secret", secret) + meta:set_string("description", "Key to "..user:get_player_name().."'s " + ..minetest.registered_nodes[node.name].description) + + if itemstack:get_count() == 0 then + itemstack = new_stack + else + if inv:add_item("main", new_stack):get_count() > 0 then + minetest.add_item(user:getpos(), new_stack) + end -- else: added to inventory successfully + end + + return itemstack + end + end +}) + minetest.register_craftitem("default:coal_lump", { description = "Coal Lump", inventory_image = "default_coal_lump.png", diff --git a/mods/default/tools.lua b/mods/default/tools.lua index f105a8fb..45c779f1 100644 --- a/mods/default/tools.lua +++ b/mods/default/tools.lua @@ -379,60 +379,6 @@ minetest.register_tool("default:sword_diamond", { sound = {breaks = "default_tool_breaks"}, }) -minetest.register_craftitem("default:skeleton_key", { - description = "Skeleton Key", - inventory_image = "default_key_skeleton.png", - groups = {key = 1}, - on_use = function(itemstack, user, pointed_thing) - if pointed_thing.type ~= "node" then - return itemstack - end - - local pos = pointed_thing.under - local node = minetest.get_node(pos) - - if not node then - return itemstack - end - - local on_skeleton_key_use = minetest.registered_nodes[node.name].on_skeleton_key_use - if on_skeleton_key_use then - -- make a new key secret in case the node callback needs it - local random = math.random - local newsecret = string.format( - "%04x%04x%04x%04x", - random(2^16) - 1, random(2^16) - 1, - random(2^16) - 1, random(2^16) - 1) - - local secret, _, _ = on_skeleton_key_use(pos, user, newsecret) - - if secret then - -- update original itemstack - itemstack:take_item() - - -- finish and return the new key - local new_stack = ItemStack("default:key") - local meta = new_stack:get_meta() - local inv = minetest.get_inventory({type="player", name=user:get_player_name()}) - meta:set_string("secret", secret) - meta:set_string("description", "Key to "..user:get_player_name().."'s " - ..minetest.registered_nodes[node.name].description) - - if itemstack:get_count() == 0 then - itemstack = new_stack - else - if inv:add_item("main", new_stack):get_count() > 0 then - minetest.add_item(user:getpos(), new_stack) - end - end - - return itemstack - end - end - return nil - end -}) - minetest.register_tool("default:key", { description = "Key", inventory_image = "default_key.png",