]> git.lizzy.rs Git - minetest.git/blob - games/devtest/mods/chest_of_everything/init.lua
11bc93d212b25f5f18e1dc01828e44e6169d6428
[minetest.git] / games / devtest / mods / chest_of_everything / init.lua
1 local F = minetest.formspec_escape
2
3 -- Create a detached inventory
4 local inv_everything = minetest.create_detached_inventory("everything", {
5         allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
6                 return 0
7         end,
8         allow_put = function(inv, listname, index, stack, player)
9                 return 0
10         end,
11         allow_take = function(inv, listname, index, stack, player)
12                 return -1
13         end,
14 })
15 local inv_trash = minetest.create_detached_inventory("trash", {
16         allow_take = function(inv, listname, index, stack, player)
17                 return 0
18         end,
19         allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
20                 return 0
21         end,
22         on_put = function(inv, listname, index, stack, player)
23                 inv:set_list("main", {})
24         end,
25 })
26 inv_trash:set_size("main", 1)
27
28 local max_page = 1
29
30 local function get_chest_formspec(page)
31         local start = 0 + (page-1)*32
32         return "size[8,9]"..
33         "list[detached:everything;main;0,0;8,4;"..start.."]"..
34         "list[current_player;main;0,5;8,4;]" ..
35         "label[6,4;Trash:]" ..
36         "list[detached:trash;main;7,4;1,1]" ..
37         "button[0,4;1,1;chest_of_everything_prev;"..F("<").."]"..
38         "button[1,4;1,1;chest_of_everything_next;"..F(">").."]"..
39         "label[2,4;"..F("Page: "..page).."]"..
40         "listring[detached:everything;main]"..
41         "listring[current_player;main]"..
42         "listring[detached:trash;main]"
43 end
44
45 minetest.register_node("chest_of_everything:chest", {
46         description = "Chest of Everything" .. "\n" ..
47                 "Grants access to all items",
48         tiles ={"chest_of_everything_chest.png^[sheet:2x2:0,0", "chest_of_everything_chest.png^[sheet:2x2:0,0",
49                 "chest_of_everything_chest.png^[sheet:2x2:1,0", "chest_of_everything_chest.png^[sheet:2x2:1,0",
50                 "chest_of_everything_chest.png^[sheet:2x2:1,0", "chest_of_everything_chest.png^[sheet:2x2:0,1"},
51         paramtype2 = "4dir",
52         groups = {dig_immediate=2,choppy=3},
53         is_ground_content = false,
54         on_construct = function(pos)
55                 local meta = minetest.get_meta(pos)
56                 meta:set_string("infotext", "Chest of Everything")
57                 meta:set_int("page", 1)
58                 meta:set_string("formspec", get_chest_formspec(1))
59         end,
60         on_receive_fields = function(pos, formname, fields, sender)
61                 if formname == "" then
62                         local meta = minetest.get_meta(pos)
63                         local page = meta:get_int("page")
64                         if fields.chest_of_everything_prev then
65                                 page = page - 1
66                         elseif fields.chest_of_everything_next then
67                                 page = page + 1
68                         end
69                         if page < 1 then
70                                 page = 1
71                         end
72                         if page > max_page then
73                                 page = max_page
74                         end
75                         meta:set_int("page", page)
76                         meta:set_string("formspec", get_chest_formspec(page))
77                 end
78         end,
79 })
80
81 minetest.register_on_mods_loaded(function()
82         local items = {}
83         for itemstring,_ in pairs(minetest.registered_items) do
84                 if itemstring ~= "" and itemstring ~= "unknown" and itemstring ~= "ignore" then
85                         table.insert(items, itemstring)
86                 end
87         end
88         --[[ Sort items in this order:
89         * Chest of Everything
90         * Test tools
91         * Other tools
92         * Craftitems
93         * Other items
94         * Items from the 'broken' mod
95         * Dummy items ]]
96         local function compare(item1, item2)
97                 local def1 = minetest.registered_items[item1]
98                 local def2 = minetest.registered_items[item2]
99                 local tool1 = def1.type == "tool"
100                 local tool2 = def2.type == "tool"
101                 local testtool1 = minetest.get_item_group(item1, "testtool") == 1
102                 local testtool2 = minetest.get_item_group(item2, "testtool") == 1
103                 local dummy1 = minetest.get_item_group(item1, "dummy") == 1
104                 local dummy2 = minetest.get_item_group(item2, "dummy") == 1
105                 local broken1 = def1.mod_origin == "broken"
106                 local broken2 = def2.mod_origin == "broken"
107                 local craftitem1 = def1.type == "craft"
108                 local craftitem2 = def2.type == "craft"
109                 if item1 == "chest_of_everything:chest" then
110                         return true
111                 elseif item2 == "chest_of_everything:chest" then
112                         return false
113                 elseif dummy1 and not dummy2 then
114                         return false
115                 elseif not dummy1 and dummy2 then
116                         return true
117                 elseif broken1 and not broken2 then
118                         return false
119                 elseif not broken1 and broken2 then
120                         return true
121                 elseif testtool1 and not testtool2 then
122                         return true
123                 elseif not testtool1 and testtool2 then
124                         return false
125                 elseif tool1 and not tool2 then
126                         return true
127                 elseif not tool1 and tool2 then
128                         return false
129                 elseif craftitem1 and not craftitem2 then
130                         return true
131                 elseif not craftitem1 and craftitem2 then
132                         return false
133                 else
134                         return item1 < item2
135                 end
136         end
137         table.sort(items, compare)
138         inv_everything:set_size("main", #items)
139         max_page = math.ceil(#items / 32)
140         for i=1, #items do
141                 inv_everything:add_item("main", items[i])
142         end
143 end)