From 209b24a2fb1c3c1db5844b4f95b22f2d7cc3878a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikita=20Wi=C5=9Bniewski?= Date: Mon, 24 Jun 2024 20:19:43 +0700 Subject: [PATCH] Move LBMs out of init.lua and fix API.md --- mods/ITEMS/mcl_chests/API.md | 15 +++++++++++- mods/ITEMS/mcl_chests/api.lua | 9 ++++++++ mods/ITEMS/mcl_chests/chests.lua | 17 ++++++++++++++ mods/ITEMS/mcl_chests/ender.lua | 10 ++++++++ mods/ITEMS/mcl_chests/init.lua | 39 +------------------------------- 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/mods/ITEMS/mcl_chests/API.md b/mods/ITEMS/mcl_chests/API.md index 9d28b3de6..680d9374a 100644 --- a/mods/ITEMS/mcl_chests/API.md +++ b/mods/ITEMS/mcl_chests/API.md @@ -18,7 +18,7 @@ trapped chests. * `basename` is a string that will be concatenated to form full nodenames for chests, for example `"mcl_chests:basename_small"`. -* `definition` is a key-value table, with following fields: +* `definition` is a key-value table, with the following fields: ```lua { @@ -151,6 +151,18 @@ Returned value is either a luaentity, or `nil` if failed (in which case a warning message gets written into the console). +## `mcl_chests.select_and_spawn_entity(pos, node)` + +This function is a simple wrapper for `mcl_chests.find_or_create_entity`, +getting most of the fields from node definition. + +* `pos` is the position vector. +* `node` is a NodeRef. + +Returned value is either a luaentity, or `nil` if failed (in which case a +warning message gets written into the console). + + ## `mcl_chests.no_rotate` This function is equivalent to `screwdriver.disallow` and is used when a chest @@ -176,6 +188,7 @@ otherwise (where position is a vector value). This function is called in `allow_metadata_inventory_move` field of Node definition. + ## `mcl_chests.protection_check_put_take(pos, listname, index, stack, player)` This function is called in `allow_metadata_inventory_put` and diff --git a/mods/ITEMS/mcl_chests/api.lua b/mods/ITEMS/mcl_chests/api.lua index 0de631afe..0dd9b39bd 100644 --- a/mods/ITEMS/mcl_chests/api.lua +++ b/mods/ITEMS/mcl_chests/api.lua @@ -181,6 +181,15 @@ local function find_or_create_entity(pos, node_name, textures, param2, double, s end mcl_chests.find_or_create_entity = find_or_create_entity +local function select_and_spawn_entity(pos, node) + local node_name = node.name + local node_def = minetest.registered_nodes[node_name] + local double_chest = minetest.get_item_group(node_name, "double_chest") > 0 + find_or_create_entity(pos, node_name, node_def._chest_entity_textures, node.param2, double_chest, + node_def._chest_entity_sound, node_def._chest_entity_mesh, node_def._chest_entity_animation_type) +end +mcl_chests.select_and_spawn_entity = select_and_spawn_entity + local no_rotate, simple_rotate if screwdriver then no_rotate = screwdriver.disallow diff --git a/mods/ITEMS/mcl_chests/chests.lua b/mods/ITEMS/mcl_chests/chests.lua index 895b2994f..5771c3b87 100644 --- a/mods/ITEMS/mcl_chests/chests.lua +++ b/mods/ITEMS/mcl_chests/chests.lua @@ -155,3 +155,20 @@ minetest.register_craft({ recipe = "mcl_chests:trapped_chest", burntime = 15, }) + +-- Disable active/open trapped chests when loaded because nobody could have them open at loading time. +-- Fixes redstone weirdness. +minetest.register_lbm({ + label = "Disable active trapped chests", + name = "mcl_chests:reset_trapped_chests", + nodenames = { + "mcl_chests:trapped_chest_on_small", + "mcl_chests:trapped_chest_on_left", + "mcl_chests:trapped_chest_on_right" + }, + run_at_every_load = true, + action = function(pos, node) + minetest.log("action", "[mcl_chests] Disabled active trapped chest on load: " .. minetest.pos_to_string(pos)) + mcl_chests.chest_update_after_close(pos) + end, +}) diff --git a/mods/ITEMS/mcl_chests/ender.lua b/mods/ITEMS/mcl_chests/ender.lua index f41ec950a..e65c5e722 100644 --- a/mods/ITEMS/mcl_chests/ender.lua +++ b/mods/ITEMS/mcl_chests/ender.lua @@ -126,3 +126,13 @@ minetest.register_craft({ { "mcl_core:obsidian", "mcl_core:obsidian", "mcl_core:obsidian" }, }, }) + +minetest.register_lbm({ + label = "Upgrade old ender chest formspec", + name = "mcl_chests:replace_old_ender_form", + nodenames = { "mcl_chests:ender_chest_small" }, + run_at_every_load = false, + action = function(pos, node) + minetest.get_meta(pos):set_string("formspec", "") + end, +}) diff --git a/mods/ITEMS/mcl_chests/init.lua b/mods/ITEMS/mcl_chests/init.lua index bea6b9496..0a780a660 100644 --- a/mods/ITEMS/mcl_chests/init.lua +++ b/mods/ITEMS/mcl_chests/init.lua @@ -58,22 +58,12 @@ minetest.register_on_leaveplayer(function(player) mcl_chests.player_chest_close(player) end) - - -local function select_and_spawn_entity(pos, node) - local node_name = node.name - local node_def = minetest.registered_nodes[node_name] - local double_chest = minetest.get_item_group(node_name, "double_chest") > 0 - mcl_chests.find_or_create_entity(pos, node_name, node_def._chest_entity_textures, node.param2, double_chest, - node_def._chest_entity_sound, node_def._chest_entity_mesh, node_def._chest_entity_animation_type) -end - minetest.register_lbm({ label = "Spawn Chest entities", name = "mcl_chests:spawn_chest_entities", nodenames = { "group:chest_entity" }, run_at_every_load = true, - action = select_and_spawn_entity, + action = mcl_chests.select_and_spawn_entity, }) minetest.register_lbm({ @@ -101,30 +91,3 @@ minetest.register_lbm({ end end }) - --- Disable active/open trapped chests when loaded because nobody could have them open at loading time. --- Fixes redstone weirdness. -minetest.register_lbm({ - label = "Disable active trapped chests", - name = "mcl_chests:reset_trapped_chests", - nodenames = { - "mcl_chests:trapped_chest_on_small", - "mcl_chests:trapped_chest_on_left", - "mcl_chests:trapped_chest_on_right" - }, - run_at_every_load = true, - action = function(pos, node) - minetest.log("action", "[mcl_chests] Disabled active trapped chest on load: " .. minetest.pos_to_string(pos)) - mcl_chests.chest_update_after_close(pos) - end, -}) - -minetest.register_lbm({ - label = "Upgrade old ender chest formspec", - name = "mcl_chests:replace_old_ender_form", - nodenames = { "mcl_chests:ender_chest_small" }, - run_at_every_load = false, - action = function(pos, node) - minetest.get_meta(pos):set_string("formspec", "") - end, -})