init.lua 4.2 KB

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