init.lua 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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",
  43. tiles ={"chest_of_everything_chest.png^[sheet:2x2:0,0", "chest_of_everything_chest.png^[sheet:2x2:0,0",
  44. "chest_of_everything_chest.png^[sheet:2x2:1,0", "chest_of_everything_chest.png^[sheet:2x2:1,0",
  45. "chest_of_everything_chest.png^[sheet:2x2:1,0", "chest_of_everything_chest.png^[sheet:2x2:0,1"},
  46. paramtype2 = "facedir",
  47. groups = {dig_immediate=2,choppy=3},
  48. is_ground_content = false,
  49. on_construct = function(pos)
  50. local meta = minetest.get_meta(pos)
  51. meta:set_string("infotext", "Chest of Everything")
  52. meta:set_int("page", 1)
  53. meta:set_string("formspec", get_chest_formspec(1))
  54. end,
  55. on_receive_fields = function(pos, formname, fields, sender)
  56. if formname == "" then
  57. local meta = minetest.get_meta(pos)
  58. local page = meta:get_int("page")
  59. if fields.chest_of_everything_prev then
  60. page = page - 1
  61. elseif fields.chest_of_everything_next then
  62. page = page + 1
  63. end
  64. if page < 1 then
  65. page = 1
  66. end
  67. if page > max_page then
  68. page = max_page
  69. end
  70. meta:set_int("page", page)
  71. meta:set_string("formspec", get_chest_formspec(page))
  72. end
  73. end,
  74. })
  75. minetest.register_on_mods_loaded(function()
  76. local items = {}
  77. for itemstring,_ in pairs(minetest.registered_items) do
  78. if itemstring ~= "" and itemstring ~= "unknown" and itemstring ~= "ignore" then
  79. table.insert(items, itemstring)
  80. end
  81. end
  82. --[[ Sort items in this order:
  83. * Chest of Everything
  84. * Test tools
  85. * Other tools
  86. * Craftitems
  87. * Other items
  88. * Dummy items ]]
  89. local function compare(item1, item2)
  90. local def1 = minetest.registered_items[item1]
  91. local def2 = minetest.registered_items[item2]
  92. local tool1 = def1.type == "tool"
  93. local tool2 = def2.type == "tool"
  94. local testtool1 = minetest.get_item_group(item1, "testtool") == 1
  95. local testtool2 = minetest.get_item_group(item2, "testtool") == 1
  96. local dummy1 = minetest.get_item_group(item1, "dummy") == 1
  97. local dummy2 = minetest.get_item_group(item2, "dummy") == 1
  98. local craftitem1 = def1.type == "craft"
  99. local craftitem2 = def2.type == "craft"
  100. if item1 == "chest_of_everything:chest" then
  101. return true
  102. elseif item2 == "chest_of_everything:chest" then
  103. return false
  104. elseif dummy1 and not dummy2 then
  105. return false
  106. elseif not dummy1 and dummy2 then
  107. return true
  108. elseif testtool1 and not testtool2 then
  109. return true
  110. elseif not testtool1 and testtool2 then
  111. return false
  112. elseif tool1 and not tool2 then
  113. return true
  114. elseif not tool1 and tool2 then
  115. return false
  116. elseif craftitem1 and not craftitem2 then
  117. return true
  118. elseif not craftitem1 and craftitem2 then
  119. return false
  120. else
  121. return item1 < item2
  122. end
  123. end
  124. table.sort(items, compare)
  125. inv_everything:set_size("main", #items)
  126. max_page = math.ceil(#items / 32)
  127. for i=1, #items do
  128. inv_everything:add_item("main", items[i])
  129. end
  130. end)