chest_cart.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. --[[
  2. Tubelib Addons 3
  3. ================
  4. Copyright (C) 2017-2020 Joachim Stolberg
  5. AGPL v3
  6. See LICENSE.txt for more information
  7. chest.lua
  8. A high performance chest
  9. ]]--
  10. -- Load support for I18n
  11. local S = tubelib_addons3.S
  12. local P2S = function(pos) if pos then return minetest.pos_to_string(pos) end end
  13. local S2P = minetest.string_to_pos
  14. local M = minetest.get_meta
  15. local function on_rightclick(pos, node, clicker)
  16. if clicker and clicker:is_player() then
  17. if M(pos):get_int("userID") == 0 then
  18. minecart.show_formspec(pos, clicker)
  19. end
  20. end
  21. end
  22. local function formspec()
  23. return "size[8,6]"..
  24. default.gui_bg..
  25. default.gui_bg_img..
  26. default.gui_slots..
  27. "list[context;main;3,0;2,2;]"..
  28. "list[current_player;main;0,2.3;8,4;]"..
  29. "listring[context;main]"..
  30. "listring[current_player;main]"
  31. end
  32. local function allow_metadata_inventory_put(pos, listname, index, stack, player)
  33. local owner = M(pos):get_string("owner")
  34. if owner ~= "" and owner ~= player:get_player_name() then
  35. return 0
  36. end
  37. return stack:get_count()
  38. end
  39. local function allow_metadata_inventory_take(pos, listname, index, stack, player)
  40. local owner = M(pos):get_string("owner")
  41. if owner ~= "" and owner ~= player:get_player_name() then
  42. return 0
  43. end
  44. return stack:get_count()
  45. end
  46. minetest.register_node("tubelib_addons3:chest_cart", {
  47. description = S("TA Chest Cart"),
  48. tiles = {
  49. -- up, down, right, left, back, front
  50. "tubelib_addons3_chest_cart_top.png",
  51. "tubelib_addons3_chest_cart_bottom.png",
  52. "tubelib_addons3_chest_cart_side.png",
  53. "tubelib_addons3_chest_cart_side.png",
  54. "tubelib_addons3_chest_cart_front.png",
  55. "tubelib_addons3_chest_cart_front.png",
  56. },
  57. drawtype = "nodebox",
  58. node_box = {
  59. type = "fixed",
  60. fixed = {
  61. {-7/16, 3/16, -7/16, 7/16, 8/16, 7/16},
  62. {-8/16, -8/16, -8/16, 8/16, 3/16, 8/16},
  63. },
  64. },
  65. paramtype2 = "facedir",
  66. paramtype = "light",
  67. use_texture_alpha = true,
  68. sunlight_propagates = true,
  69. is_ground_content = false,
  70. groups = {cracky = 2, crumbly = 2, choppy = 2},
  71. node_placement_prediction = "",
  72. diggable = false,
  73. on_place = minecart.on_nodecart_place,
  74. on_punch = minecart.on_nodecart_punch,
  75. allow_metadata_inventory_put = allow_metadata_inventory_put,
  76. allow_metadata_inventory_take = allow_metadata_inventory_take,
  77. on_rightclick = on_rightclick,
  78. after_place_node = function(pos, placer)
  79. local inv = M(pos):get_inventory()
  80. inv:set_size('main', 4)
  81. if placer and placer:is_player() then
  82. minecart.show_formspec(pos, placer)
  83. else
  84. M(pos):set_string("formspec", formspec())
  85. end
  86. end,
  87. set_cargo = function(pos, data)
  88. local inv = M(pos):get_inventory()
  89. for idx, stack in ipairs(data) do
  90. inv:set_stack("main", idx, stack)
  91. end
  92. end,
  93. get_cargo = function(pos)
  94. local inv = M(pos):get_inventory()
  95. local data = {}
  96. for idx = 1, 4 do
  97. local stack = inv:get_stack("main", idx)
  98. data[idx] = {name = stack:get_name(), count = stack:get_count()}
  99. end
  100. return data
  101. end,
  102. has_cargo = function(pos)
  103. local inv = minetest.get_meta(pos):get_inventory()
  104. return not inv:is_empty("main")
  105. end
  106. })
  107. minecart.register_cart_entity("tubelib_addons3:chest_cart_entity", "tubelib_addons3:chest_cart", "chest", {
  108. initial_properties = {
  109. physical = false,
  110. collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
  111. visual = "wielditem",
  112. textures = {"tubelib_addons3:chest_cart"},
  113. visual_size = {x=0.66, y=0.66, z=0.66},
  114. static_save = false,
  115. },
  116. })
  117. tubelib.register_node("tubelib_addons3:chest_cart", {}, {
  118. on_pull_stack = function(pos, side)
  119. local meta = minetest.get_meta(pos)
  120. return tubelib.get_stack(meta, "main")
  121. end,
  122. on_pull_item = function(pos, side)
  123. local meta = minetest.get_meta(pos)
  124. return tubelib.get_item(meta, "main")
  125. end,
  126. on_push_item = function(pos, side, item)
  127. local meta = minetest.get_meta(pos)
  128. return tubelib.put_item(meta, "main", item)
  129. end,
  130. on_unpull_item = function(pos, side, item)
  131. local meta = minetest.get_meta(pos)
  132. return tubelib.put_item(meta, "main", item)
  133. end,
  134. on_recv_message = function(pos, topic, payload)
  135. if topic == "state" then
  136. local meta = minetest.get_meta(pos)
  137. return tubelib.get_inv_state(meta, "main")
  138. else
  139. return "unsupported"
  140. end
  141. end,
  142. })
  143. minetest.register_craft({
  144. output = "tubelib_addons3:chest_cart",
  145. recipe = {
  146. {"default:junglewood", "default:chest_locked", "default:junglewood"},
  147. {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
  148. },
  149. })