From 839f5932bfa127f229ba961c01240c1287a7bdc4 Mon Sep 17 00:00:00 2001 From: cora Date: Sun, 30 Jun 2024 03:49:45 +0200 Subject: [PATCH] Move vaults API to separate file --- .luacheckrc | 1 + mods/ITEMS/mcl_vaults/api.lua | 57 ++++++++++++++++++++++ mods/ITEMS/mcl_vaults/init.lua | 88 +++++----------------------------- 3 files changed, 70 insertions(+), 76 deletions(-) create mode 100644 mods/ITEMS/mcl_vaults/api.lua diff --git a/.luacheckrc b/.luacheckrc index 7ab87f535..34ea72bb0 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -186,6 +186,7 @@ globals = { "mcl_buttons", "mcl_tt", "mcl_util", + "mcl_vaults", "mcl_villages", "mcl_void_damage", "mcl_walls", diff --git a/mods/ITEMS/mcl_vaults/api.lua b/mods/ITEMS/mcl_vaults/api.lua new file mode 100644 index 000000000..75dc83cc0 --- /dev/null +++ b/mods/ITEMS/mcl_vaults/api.lua @@ -0,0 +1,57 @@ +local modname = minetest.get_current_modname() +local S = minetest.get_translator(modname) + +local function can_open(pos, player) + return true +end + +local function eject_items(pos, name, list) + if not list or #list == 0 then + local node = minetest.get_node(pos) + node.name = "mcl_vaults:"..name + minetest.swap_node(pos, node) + return + end + minetest.add_item(vector.offset(pos, 0, 0.5, 0), table.remove(list)) + minetest.after(0.5, eject_items, pos, name, list) +end + +minetest.register_craftitem("mcl_vaults:trial_key", { + inventory_image = "mcl_vaults_trial_key.png", +}) + +local tpl = { + drawtype = "allfaces_optional", + paramtype = "light", + description = S("Vault"), + _tt_help = S("Ejects loot when opened with the key"), + _doc_items_longdesc = S("A vault ejects loot when opened with the right key. It can only be opnend once by each player."), + _doc_items_usagehelp = S("A vault ejects loot when opened with the right key. It can only be opnend once by each player."), + groups = {pickaxey=1, material_stone=1, deco_block=1}, + is_ground_content = false, + drop = "", + _mcl_hardness = 30, + _mcl_blast_resitance = 50, +} + +function mcl_vaults.register_vault(name, def) + minetest.register_node("mcl_vaults:"..name, table.merge(tpl, { + + }, def.node_off)) + minetest.register_node("mcl_vaults:"..name.."_ejecting", table.merge(tpl, { + }, def.node_ejecting)) + + minetest.register_node("mcl_vaults:"..name.."_on", table.merge(tpl, { + on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) + if itemstack:get_name() == "mcl_vaults:trial_key" and can_open(pos, clicker) then + eject_items(pos, name, mcl_loot.get_multi_loot(def.loot, PseudoRandom(os.time()))) + node.name = "mcl_vaults:"..name.."_ejecting" + minetest.swap_node(pos, node) + if not minetest.is_creative_enabled(clicker:get_player_name()) then + itemstack:take_item() + end + return itemstack + end + end + }, def.node_on)) +end diff --git a/mods/ITEMS/mcl_vaults/init.lua b/mods/ITEMS/mcl_vaults/init.lua index e7d320fe1..be5bb370f 100644 --- a/mods/ITEMS/mcl_vaults/init.lua +++ b/mods/ITEMS/mcl_vaults/init.lua @@ -1,91 +1,27 @@ mcl_vaults = {} local modname = minetest.get_current_modname() -local S = minetest.get_translator(modname) +local modpath = minetest.get_modpath(modname) +--local S = minetest.get_translator(modname) -local function can_open(pos, player) - return true -end - -local function eject_items(pos, name, list) - if not list or #list == 0 then - local node = minetest.get_node(pos) - node.name = "mcl_vaults:"..name - minetest.swap_node(pos, node) - return - end - minetest.add_item(vector.offset(pos, 0, 0.5, 0), table.remove(list)) - minetest.after(0.5, eject_items, pos, name, list) -end - -minetest.register_craftitem("mcl_vaults:trial_key", { - inventory_image = "mcl_vaults_trial_key.png", -}) - -local tpl = { - drawtype = "allfaces_optional", - paramtype = "light", - description = S("Vault"), - _tt_help = S("Ejects loot when opened with the key"), - _doc_items_longdesc = S("A vault ejects loot when opened with the right key. It can only be opnend once by each player."), - _doc_items_usagehelp = S("A vault ejects loot when opened with the right key. It can only be opnend once by each player."), - groups = {pickaxey=1, material_stone=1, deco_block=1}, - is_ground_content = false, - drop = "", - _mcl_hardness = 30, - _mcl_blast_resitance = 50, -} - -function mcl_vaults.register_vault(name, def) - minetest.register_node("mcl_vaults:"..name, table.merge(tpl, { - - }, def.node_off)) - minetest.register_node("mcl_vaults:"..name.."_ejecting", table.merge(tpl, { - }, def.node_ejecting)) - - minetest.register_node("mcl_vaults:"..name.."_on", table.merge(tpl, { - on_rightclick = function(pos, node, clicker, itemstack, pointed_thing) - if itemstack:get_name() == "mcl_vaults:trial_key" and can_open(pos, clicker) then - eject_items(pos, name, mcl_loot.get_multi_loot(def.loot, PseudoRandom(os.time()))) - node.name = "mcl_vaults:"..name.."_ejecting" - minetest.swap_node(pos, node) - if not minetest.is_creative_enabled(clicker:get_player_name()) then - itemstack:take_item() - end - return itemstack - end - end - }, def.node_on)) -end +dofile(modpath.."/api.lua") mcl_vaults.register_vault("vault",{ node_off = { - tiles = { - "mcl_vaults_vault_top_off.png", - "mcl_vaults_vault_bottom.png", - "mcl_vaults_vault_side_off.png", - "mcl_vaults_vault_side_off.png", - "mcl_vaults_vault_side_off.png", - "mcl_vaults_vault_front_off.png", + tiles = { "mcl_vaults_vault_top_off.png", "mcl_vaults_vault_bottom.png", + "mcl_vaults_vault_side_off.png", "mcl_vaults_vault_side_off.png", + "mcl_vaults_vault_side_off.png", "mcl_vaults_vault_front_off.png", }, }, node_on = { - tiles = { - "mcl_vaults_vault_top_on.png", - "mcl_vaults_vault_bottom.png", - "mcl_vaults_vault_side_on.png", - "mcl_vaults_vault_side_on.png", - "mcl_vaults_vault_side_on.png", - "mcl_vaults_vault_front_on.png", + tiles = { "mcl_vaults_vault_top_on.png", "mcl_vaults_vault_bottom.png", + "mcl_vaults_vault_side_on.png", "mcl_vaults_vault_side_on.png", + "mcl_vaults_vault_side_on.png", "mcl_vaults_vault_front_on.png", }, }, node_ejecting = { - tiles = { - "mcl_vaults_vault_top_ejecting.png", - "mcl_vaults_vault_bottom.png", - "mcl_vaults_vault_side_ejecting.png", - "mcl_vaults_vault_side_ejecting.png", - "mcl_vaults_vault_side_ejecting.png", - "mcl_vaults_vault_front_ejecting.png", + tiles = { "mcl_vaults_vault_top_ejecting.png", "mcl_vaults_vault_bottom.png", + "mcl_vaults_vault_side_ejecting.png", "mcl_vaults_vault_side_ejecting.png", + "mcl_vaults_vault_side_ejecting.png", "mcl_vaults_vault_front_ejecting.png", }, }, loot ={