From 3be6cc586cd65f4e4a4e3d32d357596f2cc7cf08 Mon Sep 17 00:00:00 2001 From: jp Date: Wed, 30 Dec 2015 20:08:39 +0100 Subject: [PATCH] Avoid some formspec concatenations --- chess.lua | 17 +++++------ enchanting.lua | 5 ++-- handlers/registration.lua | 56 +++++++++++++++++------------------ hive.lua | 17 ++++++----- mailbox.lua | 26 ++++++++-------- worktable.lua | 62 +++++++++++++++++++-------------------- 6 files changed, 92 insertions(+), 91 deletions(-) diff --git a/chess.lua b/chess.lua index 153dced..95a62b1 100644 --- a/chess.lua +++ b/chess.lua @@ -17,18 +17,17 @@ end function realchess.init(pos) local meta = minetest.get_meta(pos) local inv = meta:get_inventory() - local slots = "listcolors[#00000000;#00000000;#00000000;#30434C;#FFF]" - local formspec inv:set_size("board", 64) + + local formspec = [[ size[8,8.6;] + bgcolor[#080808BB;true] + background[0,0;8,8;chess_bg.png] + button[3.1,7.8;2,2;new;New game] + list[context;board;0,0;8,8;] + listcolors[#00000000;#00000000;#00000000;#30434C;#FFF] ]] - meta:set_string("formspec", - "size[8,8.6;]".. - "bgcolor[#080808BB;true]".. - "background[0,0;8,8;chess_bg.png]".. - "button[3.1,7.8;2,2;new;New game]".. - "list[context;board;0,0;8,8;]".. - slots) + meta:set_string("formspec", formspec) meta:set_string("infotext", "Chess Board") meta:set_string("playerBlack", "") diff --git a/enchanting.lua b/enchanting.lua index 4288b20..c70738d 100644 --- a/enchanting.lua +++ b/enchanting.lua @@ -15,6 +15,7 @@ function enchanting.formspec(pos, tooltype) tooltip[fast;Your tool digs faster] tooltip[strong;Your armor is more resistant] tooltip[speed;Your speed is increased] ]] + ..default.gui_slots..default.get_hotbar_bg(0.5,4.5) if tooltype == "sword" then formspec = formspec.."image_button[3.9,2.9;4,0.92;bg_btn.png;sharp;Sharpness]" @@ -30,7 +31,7 @@ function enchanting.formspec(pos, tooltype) image_button[3.9,1.77;4,1.12;bg_btn.png;speed;Speed] ]] end - meta:set_string("formspec", formspec..default.gui_slots..default.get_hotbar_bg(0.5,4.5)) + meta:set_string("formspec", formspec) return formspec end @@ -125,7 +126,7 @@ xdecor.register("enchantment_table", { on_receive_fields = enchanting.fields, on_metadata_inventory_put = enchanting.on_put, allow_metadata_inventory_put = enchanting.put, - allow_metadata_inventory_move = function(...) return 0 end, + allow_metadata_inventory_move = function() return 0 end, on_metadata_inventory_take = function(pos, listname, _, _, _) if listname == "tool" then enchanting.formspec(pos, nil) diff --git a/handlers/registration.lua b/handlers/registration.lua index efb6608..b148d35 100644 --- a/handlers/registration.lua +++ b/handlers/registration.lua @@ -9,34 +9,34 @@ local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots local default_inventory_size = 32 local default_inventory_formspecs = { - ["8"] = "size[8,6]"..xbg.. - "list[context;main;0,0;8,1;]".. - "list[current_player;main;0,2;8,4;]".. - "listring[current_player;main]".. - "listring[context;main]".. - default.get_hotbar_bg(0, 2), + ["8"] = [[ size[8,6] + list[context;main;0,0;8,1;] + list[current_player;main;0,2;8,4;] + listring[current_player;main] + listring[context;main] ]] .. + xbg..default.get_hotbar_bg(0,2), - ["16"] = "size[8,7]"..xbg.. - "list[context;main;0,0;8,2;]".. - "list[current_player;main;0,3;8,4;]".. - "listring[current_player;main]".. - "listring[context;main]".. - default.get_hotbar_bg(0, 3), + ["16"] = [[ size[8,7] + list[context;main;0,0;8,2;] + list[current_player;main;0,3;8,4;] + listring[current_player;main] + listring[context;main] ]] .. + xbg..default.get_hotbar_bg(0,3), - ["24"] = "size[8,8]"..xbg.. - "list[context;main;0,0;8,3;]".. - "list[current_player;main;0,4;8,4;]".. - "listring[current_player;main]".. - "listring[context;main]".. - default.get_hotbar_bg(0, 4), + ["24"] = [[ size[8,8] + list[context;main;0,0;8,3;] + list[current_player;main;0,4;8,4;] + listring[current_player;main] + listring[context;main]" ]] .. + xbg..default.get_hotbar_bg(0,4), - ["32"] = "size[8,9]"..xbg.. - "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]".. - "listring[current_player;main]".. - "listring[context;main]".. - default.get_hotbar_bg(0, 4.85) + ["32"] = [[ size[8,9] + 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] + listring[current_player;main] + listring[context;main] ]] .. + xbg..default.get_hotbar_bg(0,4.85) } local function get_formspec_by_size(size) @@ -45,7 +45,6 @@ local function get_formspec_by_size(size) end local function drop_stuff() -- thanks to LNJplus for this function - local random = math.random return function(pos, oldnode, oldmetadata, digger) local meta = minetest.get_meta(pos) meta:from_table(oldmetadata) @@ -55,9 +54,10 @@ local function drop_stuff() -- thanks to LNJplus for this function local stack = inv:get_stack("main", i) if not stack:is_empty() then local p = { - x = pos.x + random(0,5) / 5 - 0.5, + x = pos.x + math.random(0,5) / 5 - 0.5, y = pos.y, - z = pos.z + random(0,5) / 5 - 0.5} + z = pos.z + math.random(0,5) / 5 - 0.5 + } minetest.add_item(p, stack) end end diff --git a/hive.lua b/hive.lua index 2bb12ed..b7b3745 100644 --- a/hive.lua +++ b/hive.lua @@ -5,14 +5,15 @@ function hive.construct(pos) local inv = meta:get_inventory() local xbg = default.gui_bg..default.gui_bg_img..default.gui_slots..default.get_hotbar_bg(0,1.35) - local formspec = "size[8,5;]"..xbg.. - "label[1.35,0;Bees are making honey\nwith pollen around...]".. - "image[6,0;1,1;hive_bee.png]".. - "image[5,0;1,1;hive_layout.png]".. - "list[context;honey;5,0;1,1;]".. - "list[current_player;main;0,1.35;8,4;]" + local formspec = [[ size[8,5;] + label[1.35,0;Bees are making honey] + label[1.35,0.5;with pollen around...] + image[6,0;1,1;hive_bee.png] + image[5,0;1,1;hive_layout.png] + list[context;honey;5,0;1,1;] + list[current_player;main;0,1.35;8,4;] ]] - meta:set_string("formspec", formspec) + meta:set_string("formspec", formspec..xbg) meta:set_string("infotext", "Artificial Hive") inv:set_size("honey", 1) end @@ -40,7 +41,7 @@ xdecor.register("hive", { local health = clicker:get_hp() clicker:set_hp(health - 1) end, - allow_metadata_inventory_put = function(...) return 0 end + allow_metadata_inventory_put = function() return 0 end }) minetest.register_abm({ diff --git a/mailbox.lua b/mailbox.lua index 2897bcc..fdc63c7 100644 --- a/mailbox.lua +++ b/mailbox.lua @@ -100,26 +100,26 @@ function mailbox.formspec(pos, owner, num) end end - return "size[9.5,9]"..xbg.. - default.get_hotbar_bg(0.75,5.25).. - "label[0,0;Mailbox]".. - "label[6,0;Last donators]".. - "box[6,0.72;3.3,3.5;#555555]".. + return [[ size[9.5,9] + label[0,0;Mailbox] + label[6,0;Last donators] + box[6,0.72;3.3,3.5;#555555] + listring[current_player;main] + list[current_player;main;0.75,5.25;8,4;] + tableoptions[background=#00000000;highlight=#00000000;border=false] ]].. "tablecolumns[color;text;image,"..img.."0;color;text]".. - "tableoptions[background=#00000000;highlight=#00000000;border=false]".. "table[6,0.75;3.3,4;givers;"..giver.."]".. "list[nodemeta:"..spos..";mailbox;0,0.75;6,4;]".. - "list[current_player;main;0.75,5.25;8,4;]".. "listring[nodemeta:"..spos..";mailbox]".. - "listring[current_player;main]" + xbg..default.get_hotbar_bg(0.75,5.25) else - return "size[8,5]"..xbg.. - default.get_hotbar_bg(0,1.25).. - "tablecolumns[color;text;color;text]".. - "tableoptions[background=#00000000;highlight=#00000000;border=false]".. + return [[ size[8,5] + list[current_player;main;0,1.25;8,4;] + tablecolumns[color;text;color;text] + tableoptions[background=#00000000;highlight=#00000000;border=false] ]].. "table[0,0;3,1;sendform;#FFFFFF,Send your goods to,,,#FFFF00,"..owner.."]".. "list[nodemeta:"..spos..";drop;3.5,0;1,1;]".. - "list[current_player;main;0,1.25;8,4;]" + xbg..default.get_hotbar_bg(0,1.25) end end diff --git a/worktable.lua b/worktable.lua index 61901b6..0d11919 100644 --- a/worktable.lua +++ b/worktable.lua @@ -40,22 +40,22 @@ function worktable.craft_output_recipe(pos, start_i, pagenum, stackname, recipe_ local inventory_size = #meta:to_table().inventory.inv_items_list local pagemax = math.floor((inventory_size - 1) / (8*4) + 1) - local formspec = "size[8,8;]"..xbg.. + local formspec = [[ size[8,8;] + list[context;item_craft_input;3,6.3;1,1;] + tablecolumns[color;text;color;text] + tableoptions[background=#00000000;highlight=#00000000;border=false] + button[5.5,0;0.7,1;prev;<] + button[7.3,0;0.7,1;next;>] + button[4,0.2;0.7,0.5;search;?] + button[4.6,0.2;0.7,0.5;clearfilter;X] + button[0,0;1.5,1;backcraft;< Back] + tooltip[search;Search] + tooltip[clearfilter;Reset] + label[3,5.8;Input] ]].. "list[context;inv_items_list;0,1;8,4;"..tostring(start_i).."]".. - "list[context;item_craft_input;3,6.3;1,1;]".. - "tablecolumns[color;text;color;text]".. - "tableoptions[background=#00000000;highlight=#00000000;border=false]".. "table[6.1,0.2;1.1,0.5;pagenum;#FFFF00,"..tostring(math.floor(pagenum)).. ",#FFFFFF,/ "..tostring(pagemax).."]".. - "button[5.5,0;0.7,1;prev;<]".. - "button[7.3,0;0.7,1;next;>]".. - "button[4,0.2;0.7,0.5;search;?]".. - "button[4.6,0.2;0.7,0.5;clearfilter;X]".. - "button[0,0;1.5,1;backcraft;< Back]".. - "tooltip[search;Search]".. - "tooltip[clearfilter;Reset]".. - "label[3,5.8;Input]".. - "field[1.8,0.32;2.6,1;filter;;"..filter.."]" + "field[1.8,0.32;2.6,1;filter;;"..filter.."]"..xbg if stackname then local items_num = #minetest.get_all_craft_recipes(stackname) @@ -70,8 +70,7 @@ function worktable.craft_output_recipe(pos, start_i, pagenum, stackname, recipe_ local stack_count = stack_output:match("%s(%d+)") if items_num > 1 then - formspec = formspec.. - "button[0,5.7;1.6,1;alternate;Alternate]".. + formspec = formspec.."button[0,5.7;1.6,1;alternate;Alternate]".. "label[0,5.2;Recipe "..recipe_num.." of "..items_num.."]" end @@ -112,17 +111,16 @@ function worktable.craft_output_recipe(pos, start_i, pagenum, stackname, recipe_ end elseif stack_width == 3 then if stack_type == "cooking" then - formspec = formspec.."list[context;craft_output_recipe;5,6.3;1,1;]".. - "image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png]" + formspec = formspec..[[ list[context;craft_output_recipe;5,6.3;1,1;] + image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png] ]] inv:set_size("craft_output_recipe", 1) else formspec = formspec.."list[context;craft_output_recipe;5,5.3;3,3;]" inv:set_size("craft_output_recipe", 3*3) end elseif stack_type == "cooking" and stack_width > 3 then - formspec = formspec.. - "list[context;craft_output_recipe;5,6.3;1,1;]".. - "image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png]" + formspec = formspec..[[ list[context;craft_output_recipe;5,6.3;1,1;] + image[4.25,5.9;0.5,0.5;default_furnace_fire_fg.png] ]] inv:set_size("craft_output_recipe", 1) end @@ -160,12 +158,12 @@ function worktable.craft_output_recipe(pos, start_i, pagenum, stackname, recipe_ def = def:gsub("group", "default") else for node, definition in pairs(minetest.registered_items) do - for group in pairs(definition.groups) do - if group == def:match("^group:([%w_]+)$") then - inv:set_stack("craft_output_recipe", k, node) - end + for group in pairs(definition.groups) do + if def:match("^group:"..group.."$") then + inv:set_stack("craft_output_recipe", k, node) end end + end end end end @@ -175,10 +173,9 @@ function worktable.craft_output_recipe(pos, start_i, pagenum, stackname, recipe_ end end - formspec = formspec.. - "image[4,6.3;1,1;gui_furnace_arrow_bg.png^[transformR90]".. - "button[0,6.5;1.6,1;trash;Clear]".. - "label[0,7.5;"..stackname:sub(1,30).."]" + formspec = formspec..[[ image[4,6.3;1,1;gui_furnace_arrow_bg.png^[transformR90] + button[0,6.5;1.6,1;trash;Clear] ]].. + "label[0,7.5;"..stackname:sub(1,30).."]" end meta:set_string("formspec", formspec) @@ -214,8 +211,9 @@ function worktable.crafting(pos) list[current_player;craftpreview;6,1;1,1;] listring[current_player;main] listring[current_player;craft] ]] + ..xbg..default.get_hotbar_bg(0,3.3) - meta:set_string("formspec", formspec..xbg..default.get_hotbar_bg(0,3.3)) + meta:set_string("formspec", formspec) end function worktable.storage(pos) @@ -226,8 +224,9 @@ function worktable.storage(pos) listring[context;storage] listring[current_player;main] button[0,0;1.5,1;back;< Back] ]] + ..xbg..default.get_hotbar_bg(0,3.25) - meta:set_string("formspec", formspec..xbg..default.get_hotbar_bg(0,3.25)) + meta:set_string("formspec", formspec) end function worktable.main(pos) @@ -248,8 +247,9 @@ function worktable.main(pos) list[current_player;main;0,3.25;8,4;] button[0,0;2,1;craft;Crafting] button[2,0;2,1;storage;Storage] ]] + ..xbg..default.get_hotbar_bg(0,3.25) - meta:set_string("formspec", formspec..xbg..default.get_hotbar_bg(0,3.25)) + meta:set_string("formspec", formspec) end function worktable.construct(pos)