From 91b4a654c97d2a7103c955f3976037310c78f6d7 Mon Sep 17 00:00:00 2001 From: k Date: Sun, 3 May 2020 16:04:36 +0200 Subject: [PATCH] Added method to generate a chest definition --- utils/base.lua | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 utils/base.lua diff --git a/utils/base.lua b/utils/base.lua new file mode 100644 index 0000000..2405fee --- /dev/null +++ b/utils/base.lua @@ -0,0 +1,97 @@ +-- NOTE: `require` is not allowed with mod security on +-- `dofile` with a return on the module is used instead +generate_formspec_string = dofile(minetest.get_modpath("more_chests").."/utils/formspec.lua") +local actions = dofile(minetest.get_modpath("more_chests").."/utils/actions.lua") +local S = minetest.get_translator("more_chests") + +local function parse_action(value, default_getter) + if value == false then -- model disabled this attribute + return nil + elseif value == nil then -- model wants the default attribute + return default_getter() + else -- model provided its own attribute + return value + end +end + +function generate_chest_def(def) + -- TODO assert def.size in ("big", "small") + local out = { + description = def.description, + tiles = { + def.tiles.top or def.tiles.side, + def.tiles.top or def.tiles.side, + def.tiles.side, + def.tiles.side, + def.tiles.side, + def.tiles.front + }, + paramtype2 = "facedir", + legacy_facedir_simple = true, + groups = { + snappy=2, + choppy=2, + oddly_breakable_by_hand=2 + }, + sounds = def.sounds or default.node_sound_wood_defaults(), + recipe = def.recipe, -- TODO this is buggy, reason why all models except from toolbox have their own extra call to minetest.register_craft - TODO possibily other similar bugs? + after_place_node = function(pos, placer) + local meta = minetest.get_meta(pos) + meta:set_string("owner", placer:get_player_name() or "") + meta:set_string("infotext", S("@1 (owned by @2)", def.description, meta:get_string("owner"))) + end, + on_construct = function(pos) + local meta = minetest.get_meta(pos) + local formspec_str = def.formspec or generate_formspec_string(def.size) + meta:set_string("formspec", formspec_str) + meta:set_string("infotext", def.description) + meta:set_string("owner", "") + local inv = meta:get_inventory() + local chest_size = def.size == "big" and 14*5 or 8*4 + inv:set_size("main", chest_size) + end, + can_dig = function(pos,player) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + return inv:is_empty("main") + end, + } + -- register log actions, NOTE passing an anonymous function to avoid getting the default if not necessary + out.allow_metadata_inventory_move = parse_action(def.allow_metadata_inventory_move, function() actions.get_allow_metadata_inventory_move{def.type} end) + out.allow_metadata_inventory_put = parse_action(def.allow_metadata_inventory_put, function() actions.get_allow_metadata_inventory_put{def.type} end) + out.allow_metadata_inventory_take = parse_action(def.allow_metadata_inventory_take, function() actions.get_allow_metadata_inventory_take{def.type} end) + out.on_metadata_inventory_move = parse_action(def.on_metadata_inventory_move, function() actions.get_on_metadata_inventory_move(def.type) end) + out.on_metadata_inventory_put = parse_action(def.on_metadata_inventory_put, function() actions.get_on_metadata_inventory_put(def.type) end) + out.on_metadata_inventory_take = parse_action(def.on_metadata_inventory_take, function() actions.get_on_metadata_inventory_take(def.type) end) + -- if model is not a simple block handle node_box attribute + if def.node_box then + out.drawtype = "nodebox" + out.node_box = { + type = "fixed", + fixed = def.node_box, + } + end + -- add pipeworks compatibility, TODO needs proper testing + if def.pipeworks_enabled == true then + out.groups.tubedevice = 1 + out.groups.tubedevice_receiver = 1 + out.tube = { + insert_object = function(pos, node, stack, direction) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + return inv:add_item("main", stack) + end, + can_insert = function(pos, node, stack, direction) + local meta = minetest.get_meta(pos) + local inv = meta:get_inventory() + return inv:room_for_item("main", stack) + end, + input_inventory = "main", + connect_sides = {left = 1, right = 1, back = 1, front = 1, bottom = 1, top = 1} + } + end + return out +end + + +return generate_chest_def