]> git.lizzy.rs Git - xdecor.git/blobdiff - worktable.lua
Craft guide : remember tab id on clearing filter
[xdecor.git] / worktable.lua
index bd59cd7681203e00d5d6a06dfd904124cdbbda5c..4a44b592ec3ef5770ce9255517fdb4cbc25bf9ae 100644 (file)
@@ -34,12 +34,12 @@ local def = { -- Nodebox name, yield, definition.
 function worktable.get_recipe(item)
        if item:find("^group:") then
                if item:find("wool$") or item:find("dye$") then
-                       item = item:match("[^,:]+$")..":white"
-               elseif minetest.registered_items["default:"..item:match("[^,:]+$")] then
+                       item = item:sub(7)..":white"
+               elseif minetest.registered_items["default:"..item:sub(7)] then
                        item = item:gsub("group:", "default:")
                else
-                       for node, definition in pairs(minetest.registered_items) do
-                               if definition.groups[item:match("[^,:]+$")] then
+                       for node, def in pairs(minetest.registered_items) do
+                               if def.groups[item:match("[^,:]+$")] then
                                        item = node
                                end
                        end
@@ -128,7 +128,8 @@ local function tab_category(tab_id)
                minetest.registered_items,
                minetest.registered_nodes,
                minetest.registered_tools,
-               minetest.registered_craftitems }
+               minetest.registered_craftitems
+       }
 
        return id_category[tab_id] or id_category[1]
 end
@@ -152,10 +153,12 @@ end
 worktable.formspecs = {
        crafting = function(meta)
                meta:set_string("formspec", [[ size[8,7;]
-                       list[current_player;main;0,3.3;8,4;]
                        image[5,1;1,1;gui_furnace_arrow_bg.png^[transformR270]
+                       image[0.06,2.12;0.8,0.8;trash_icon.png]
                        button[0,0;1.5,1;back;< Back]
-                       button[0,1;1.5,1;craftguide;Guide]
+                       button[0,0.85;1.5,1;craftguide;Guide]
+                       list[context;trash;0,2;1,1;]
+                       list[current_player;main;0,3.3;8,4;]
                        list[current_player;craft;2,0;3,3;]
                        list[current_player;craftpreview;6,1;1,1;]
                        listring[current_player;main]
@@ -164,6 +167,8 @@ worktable.formspecs = {
        end,
        storage = function(meta)
                meta:set_string("formspec", [[ size[8,7]
+                       image[7.06,0.12;0.8,0.8;trash_icon.png]
+                       list[context;trash;7,0;1,1;]
                        list[context;storage;0,1;8,2;]
                        list[current_player;main;0,3.25;8,4;]
                        listring[context;storage]
@@ -197,6 +202,7 @@ function worktable.construct(pos)
        local inv = meta:get_inventory()
 
        inv:set_size("tool", 1)
+       inv:set_size("trash", 1)
        inv:set_size("input", 1)
        inv:set_size("hammer", 1)
        inv:set_size("forms", 4*3)
@@ -212,7 +218,7 @@ function worktable.fields(pos, _, fields)
        local formspec = meta:to_table().fields.formspec
        local filter = formspec:match("filter;;([%w_:]+)") or ""
        local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
-       local tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*]")) or 1
+       local tab_id = tonumber(formspec:match("tabheader%[.*;(%d+)%;.*%]")) or 1
 
        if fields.back then
                worktable.formspecs.main(meta)
@@ -221,8 +227,8 @@ function worktable.fields(pos, _, fields)
        elseif fields.storage then
                worktable.formspecs.storage(meta)
        elseif fields.craftguide or fields.clearfilter then
-               worktable.craftguide_main_list(meta, nil, 1)
-               worktable.craftguide_formspec(meta, 1, nil, 1, "", 1)
+               worktable.craftguide_main_list(meta, nil, tab_id)
+               worktable.craftguide_formspec(meta, 1, nil, 1, "", tab_id)
        elseif fields.alternate then
                local item = formspec:match("item_image%[.*;([%w_:]+)%]") or ""
                local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
@@ -243,8 +249,7 @@ function worktable.fields(pos, _, fields)
                worktable.craftguide_formspec(meta, pagenum, nil, 1, filter, tab_id)
        else
                for item in pairs(fields) do
-                       if item:match("[%w_]+:[%w_]+") and
-                                       minetest.get_craft_recipe(item).items then
+                       if item:match(".-:") and minetest.get_craft_recipe(item).items then
                                worktable.craftguide_formspec(meta, pagenum, item, 1, filter, tab_id)
                        end
                end
@@ -257,34 +262,40 @@ function worktable.dig(pos)
                inv:is_empty("tool") and inv:is_empty("storage")
 end
 
-function worktable.contains(table, element)
-       if table then
-               for _, value in pairs(table) do
-                       if value == element then
-                               return true
-                       end
-               end
+function worktable.allowed(mod, node)
+       if not mod then return end
+       for _, it in pairs(mod) do
+               if it == node then return true end
        end
        return false
 end
 
-function worktable.put(_, listname, _, stack)
+local function trash_delete(pos)
+       local inv = minetest.get_meta(pos):get_inventory()
+       minetest.after(0, function()
+               inv:set_stack("trash", 1, "")
+       end)
+end
+
+function worktable.put(pos, listname, _, stack)
        local stackname = stack:get_name()
-       local mod, node = stackname:match("([%w_]+):([%w_]+)")
+       local mod, node = stackname:match("(.*):(.*)")
 
        if (listname == "tool" and stack:get_wear() > 0 and stackname ~= "xdecor:hammer") or
-                       (listname == "input" and worktable.contains(nodes[mod], node)) or
+                       (listname == "input" and worktable.allowed(nodes[mod], node)) or
                        (listname == "hammer" and stackname == "xdecor:hammer") or
-                       listname == "storage" then
+                       listname == "storage" or listname == "trash" then
+               if listname == "trash" then trash_delete(pos) end
                return stack:get_count()
        end
+
        return 0
 end
 
-function worktable.take(pos, listname, _, stack, player)
+function worktable.take(_, listname, _, stack, player)
        if listname == "forms" then
-               local user_inv = player:get_inventory()
-               if user_inv:room_for_item("main", stack:get_name()) then
+               local inv = player:get_inventory()
+               if inv:room_for_item("main", stack:get_name()) then
                        return -1
                end
                return 0
@@ -292,8 +303,11 @@ function worktable.take(pos, listname, _, stack, player)
        return stack:get_count()
 end
 
-function worktable.move(_, from_list, _, to_list, _, count)
-       if from_list == "storage" and to_list == "storage" then
+function worktable.move(pos, _, _, to_list, _, count)
+       if to_list == "storage" then
+               return count
+       elseif to_list == "trash" then
+               trash_delete(pos)
                return count
        end
        return 0