From cc9a1a9c3cf044453aa0faef7a8a7194160ce188 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 23 Jan 2015 02:23:39 +0100 Subject: [PATCH] select a formspec automaticly from a table if none is supplied --- homedecor/furniture.lua | 3 -- homedecor/kitchen_cabinet.lua | 9 ------ homedecor/misc-bathroom.lua | 3 -- homedecor/misc-nodes.lua | 6 ---- homedecor/nightstands.lua | 12 -------- homedecor/refrigerator.lua | 7 +---- homedecor/registration_handler.lua | 46 ++++++++++++++++++++++-------- 7 files changed, 35 insertions(+), 51 deletions(-) diff --git a/homedecor/furniture.lua b/homedecor/furniture.lua index 4346a01..61667ed 100644 --- a/homedecor/furniture.lua +++ b/homedecor/furniture.lua @@ -348,9 +348,6 @@ homedecor.register("wardrobe_bottom", { infotext = S("Wardrobe cabinet"), inventory = { size=24, - formspec="size[8,8]".. - "list[context;main;0,0;8,3;]".. - "list[current_player;main;0,4;8,4;]", }, }) diff --git a/homedecor/kitchen_cabinet.lua b/homedecor/kitchen_cabinet.lua index 8aa9088..b2afaee 100644 --- a/homedecor/kitchen_cabinet.lua +++ b/homedecor/kitchen_cabinet.lua @@ -29,9 +29,6 @@ for _, mat in ipairs(counter_materials) do infotext=S("Kitchen Cabinet"), inventory = { size=24, - formspec="size[8,8]".. - "list[context;main;0,0;8,3;]".. - "list[current_player;main;0,4;8,4;]", }, }) end @@ -59,9 +56,6 @@ homedecor.register("kitchen_cabinet_half", { infotext=S("Kitchen Cabinet"), inventory = { size=12, - formspec="size[8,7]".. - "list[context;main;1,0;6,2;]".. - "list[current_player;main;0,3;8,4;]", }, }) @@ -77,8 +71,5 @@ homedecor.register("kitchen_cabinet_with_sink", { infotext=S("Under-sink cabinet"), inventory = { size=16, - formspec="size[8,7]".. - "list[context;main;0,0;8,2;]".. - "list[current_player;main;0,3;8,4;]", }, }) diff --git a/homedecor/misc-bathroom.lua b/homedecor/misc-bathroom.lua index 71a4bd4..5daf87d 100644 --- a/homedecor/misc-bathroom.lua +++ b/homedecor/misc-bathroom.lua @@ -90,9 +90,6 @@ homedecor.register("medicine_cabinet", { infotext=S("Medicine cabinet"), inventory = { size=16, - formspec="size[8,7]".. - "list[context;main;1,0;6,1;]".. - "list[current_player;main;0,3;8,4;]", }, }) diff --git a/homedecor/misc-nodes.lua b/homedecor/misc-nodes.lua index 631214d..e3e4ea4 100644 --- a/homedecor/misc-nodes.lua +++ b/homedecor/misc-nodes.lua @@ -328,9 +328,6 @@ homedecor.register("cardboard_box", { infotext=S("Cardboard box"), inventory = { size=8, - formspec="size[8,6]".. - "list[current_name;main;2,0;4,1;]".. - "list[current_player;main;0,2;8,4;]", }, }) @@ -396,9 +393,6 @@ homedecor.register("filing_cabinet", { infotext=S("Filing cabinet"), inventory = { size=16, - formspec="size[8,7]".. - "list[context;main;0,0;8,2;]".. - "list[current_player;main;0,3;8,4;]", }, }) diff --git a/homedecor/nightstands.lua b/homedecor/nightstands.lua index 853886c..863056d 100644 --- a/homedecor/nightstands.lua +++ b/homedecor/nightstands.lua @@ -32,9 +32,6 @@ homedecor.register("nightstand_oak_one_drawer", { infotext=S("One-drawer Nightstand"), inventory = { size=8, - formspec="size[8,6]".. - "list[context;main;0,0;8,1;]".. - "list[current_player;main;0,2;8,4;]", }, }) @@ -69,9 +66,6 @@ homedecor.register("nightstand_oak_two_drawers", { infotext=S("Two-drawer Nightstand"), inventory = { size=16, - formspec="size[8,7]".. - "list[context;main;0,0;8,2;]".. - "list[current_player;main;0,3;8,4;]", }, }) @@ -105,9 +99,6 @@ homedecor.register("nightstand_mahogany_one_drawer", { infotext=S("One-drawer Nightstand"), inventory = { size=8, - formspec="size[8,6]".. - "list[context;main;0,0;8,1;]".. - "list[current_player;main;0,2;8,4;]", }, }) @@ -139,8 +130,5 @@ homedecor.register("nightstand_mahogany_two_drawers", { infotext=S("Two-drawer Nightstand"), inventory = { size=16, - formspec="size[8,7]".. - "list[context;main;0,0;8,2;]".. - "list[current_player;main;0,3;8,4;]", }, }) diff --git a/homedecor/refrigerator.lua b/homedecor/refrigerator.lua index d7543b1..c0e8789 100644 --- a/homedecor/refrigerator.lua +++ b/homedecor/refrigerator.lua @@ -60,9 +60,6 @@ homedecor.register("refrigerator_steel_bottom", { inventory = { size=50, lockable=true, - formspec="size[10,10]".. - "list[context;main;0,0;10,5;]".. - "list[current_player;main;1,6;8,4;]", }, }) @@ -108,9 +105,7 @@ homedecor.register("refrigerator_white_bottom", { infotext=S("Refrigerator"), inventory = { size=50, - formspec="size[10,10]".. - "list[context;main;0,0;10,5;]".. - "list[current_player;main;1,6;8,4;]", + lockable=true }, }) diff --git a/homedecor/registration_handler.lua b/homedecor/registration_handler.lua index 366a2a9..a66561c 100644 --- a/homedecor/registration_handler.lua +++ b/homedecor/registration_handler.lua @@ -7,14 +7,39 @@ local default_can_dig = function(pos,player) end local default_inventory_size = 32 -local default_inventory_formspec = "size[8,9]".. - default.gui_bg.. - default.gui_bg_img.. - default.gui_slots.. - "list[current_name;main;0,0.3;8,4;]".. +local default_inventory_formspecs = { + ["8"]="size[8,6]".. + "list[context;main;0,0;8,1;]".. + "list[current_player;main;0,2;8,4;]", + + ["12"]="size[8,7]".. + "list[context;main;0,0;8,2;]".. + "list[current_player;main;0,3;8,4;]", + + ["16"]="size[8,7]".. + "list[context;main;0,0;8,2;]".. + "list[current_player;main;0,3;8,4;]", + + ["24"]="size[8,8]".. + "list[context;main;0,0;8,3;]".. + "list[current_player;main;0,4;8,4;]", + + ["32"]="size[8,9]".. default.gui_bg .. default.gui_bg_img .. default.gui_slots .. + "list[context;main;0,0.3;8,4;]".. "list[current_player;main;0,4.85;8,1;]".. "list[current_player;main;0,6.08;8,3;8]".. - default.get_hotbar_bg(0,4.85) + default.get_hotbar_bg(0,4.85), + + ["50"]="size[10,10]".. + "list[context;main;0,0;10,5;]".. + "list[current_player;main;1,6;8,4;]", +} + +local function get_formspec_by_size(size) + --TODO heuristic to use the "next best size" + local formspec = default_inventory_formspecs[tostring(size)] + return formspec or default_inventory_formspecs +end --wrapper around minetest.register_node that sets sane defaults and interprets some specialized settings function homedecor.register(name, def) @@ -37,17 +62,14 @@ function homedecor.register(name, def) def.inventory = nil if inventory then - assert((inventory.formspec == nil) == (inventory.size == nil), - "inventory.formspec and inventory.size either have both to be set or both be left nil" ) - def.on_construct = def.on_construct or function(pos) local meta = minetest.get_meta(pos) if infotext then meta:set_string("infotext", infotext) end - - meta:set_string("formspec", inventory.formspec or default_inventory_formspec) - meta:get_inventory():set_size("main", inventory.size or default_inventory_size) + local size = inventory.size or default_inventory_size + meta:get_inventory():set_size("main", size) + meta:set_string("formspec", inventory.formspec or get_formspec_by_size(size)) end def.can_dig = def.can_dig or default_can_dig