detached.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. local ALLOW_PUT_MAX = 1
  2. local ALLOW_TAKE_MAX = 4
  3. local function print_to_everything(msg)
  4. core.log("action", "[chest] " .. msg)
  5. core.chat_send_all(msg)
  6. end
  7. -- Create a detached inventory
  8. local inv = core.create_detached_inventory("detached_inventory", {
  9. allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  10. print_to_everything("Detached inventory: "..player:get_player_name().." triggered allow_move")
  11. return count -- Allow all
  12. end,
  13. allow_put = function(inv, listname, index, stack, player)
  14. print_to_everything("Detached inventory: "..player:get_player_name().." triggered allow_put for "..stack:to_string().." (max. allowed: "..ALLOW_PUT_MAX..")")
  15. return ALLOW_PUT_MAX -- Allow to put a limited amount of items
  16. end,
  17. allow_take = function(inv, listname, index, stack, player)
  18. print_to_everything("Detached inventory: "..player:get_player_name().." triggered allow_take for "..stack:to_string().." (max. allowed: "..ALLOW_TAKE_MAX..")")
  19. return ALLOW_TAKE_MAX -- Allow to take a limited amount of items
  20. end,
  21. on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
  22. print_to_everything("Detached inventory: " .. player:get_player_name().." moved item(s)")
  23. end,
  24. on_put = function(inv, listname, index, stack, player)
  25. print_to_everything("Detached inventory: " .. player:get_player_name().." put "..stack:to_string())
  26. end,
  27. on_take = function(inv, listname, index, stack, player)
  28. print_to_everything("Detached inventory: " .. player:get_player_name().." took "..stack:to_string())
  29. end,
  30. })
  31. inv:set_size("main", 8*3)
  32. -- Add a special chest to grant access to this inventory
  33. core.register_node("chest:detached_chest", {
  34. description = "Detached Chest" .. "\n" ..
  35. "Grants access to a shared detached inventory" .."\n" ..
  36. "Max. item put count: "..ALLOW_PUT_MAX .."\n"..
  37. "Max. item take count: "..ALLOW_TAKE_MAX,
  38. tiles = {"chest_detached_chest.png^[sheet:2x2:0,0", "chest_detached_chest.png^[sheet:2x2:0,0",
  39. "chest_detached_chest.png^[sheet:2x2:1,0", "chest_detached_chest.png^[sheet:2x2:1,0",
  40. "chest_detached_chest.png^[sheet:2x2:1,0", "chest_detached_chest.png^[sheet:2x2:0,1"},
  41. paramtype2 = "4dir",
  42. groups = {dig_immediate=2,choppy=3,meta_is_privatizable=1},
  43. is_ground_content = false,
  44. on_construct = function(pos)
  45. local meta = core.get_meta(pos)
  46. meta:set_string("formspec",
  47. "size[8,9]"..
  48. "list[detached:detached_inventory;main;0,0;8,3;0]"..
  49. "list[current_player;main;0,5;8,4;]"..
  50. "listring[]")
  51. meta:set_string("infotext", "Detached Chest")
  52. end,
  53. })