autocraft.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. minetest.register_node("castle:autocraft", {
  2. tiles = { -- Thanks to Eld's workbench texture
  3. "castle_autocrafter_top.png", "default_wood.png",
  4. "castle_autocrafter_side1.png", "castle_autocrafter_side1.png",
  5. "castle_autocrafter_side2.png", "castle_autocrafter_side2.png"
  6. },
  7. description = "Automatic Crafting Table",
  8. groups = {choppy = 1, oddly_breakable_by_hand = 2},
  9. paramtype = "light",
  10. paramtype2 = "facedir",
  11. sounds = default.node_sound_wood_defaults(),
  12. drop = "pipeworks:autocrafter",
  13. })--[[
  14. on_construct = function(pos)
  15. local meta = minetest.get_meta(pos)
  16. local inv = meta:get_inventory()
  17. inv:set_size("craft", 9)
  18. inv:set_size("in", 6 * 4) -- was 8 * 3
  19. meta:set_string("formspec",
  20. "size[10,9]"
  21. ..default.gui_bg..default.gui_bg_img..default.gui_slots
  22. .."list[context;craft;7,0.5;3,3]"
  23. .."list[context;in;0,0.5;6,4]"
  24. .."list[current_player;main;1,5;8,4]"
  25. .."listring[]"
  26. .."label[7,0;Recipe:]"
  27. .."label[0,0;Materials:")
  28. end,
  29. can_dig = function(pos,player)
  30. local meta = minetest.get_meta(pos) ; if not meta then return false end
  31. local inv = meta:get_inventory()
  32. return inv:is_empty("in") and inv:is_empty("craft")
  33. end,
  34. allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  35. if minetest.is_protected(pos, player:get_player_name()) then
  36. return 0
  37. end
  38. return count
  39. end,
  40. allow_metadata_inventory_take = function(pos, listname, index, stack, player)
  41. if minetest.is_protected(pos, player:get_player_name()) then
  42. return 0
  43. end
  44. return stack:get_count()
  45. end,
  46. allow_metadata_inventory_put = function(pos, listname, index, stack, player)
  47. if minetest.is_protected(pos, player:get_player_name()) then
  48. return 0
  49. end
  50. return stack:get_count()
  51. end,
  52. })
  53. minetest.register_craft({
  54. output = "castle:autocraft",
  55. recipe = {
  56. {"default:steel_ingot","default:steel_ingot","default:steel_ingot"},
  57. {"group:wood", "group:wood","default:steel_ingot"},
  58. {"group:tree", "group:tree","default:steel_ingot"},
  59. }
  60. })
  61. minetest.register_abm({
  62. nodenames = {"castle:autocraft"},
  63. interval = 5,
  64. chance = 1,
  65. catch_up = false,
  66. action = function(pos, node)
  67. local meta = minetest.get_meta(pos) ; if not meta then return end
  68. local inventory = meta:get_inventory()
  69. local recipe = inventory:get_list("craft")
  70. local result
  71. local new
  72. for i = 1, 9 do
  73. recipe[i] = ItemStack({
  74. name = recipe[i]:get_name(),
  75. count = 1
  76. })
  77. end
  78. result, new = minetest.get_craft_result({
  79. method = "normal",
  80. width = 3,
  81. items = recipe
  82. })
  83. if result.item:is_empty() then return end
  84. result = result.item
  85. local input = inventory:get_list("in")
  86. if not inventory:room_for_item("in", result) then return end
  87. local to_use = {}
  88. for _,item in pairs(recipe) do
  89. if item ~= nil and not item:is_empty() then
  90. if to_use[item:get_name()] == nil then
  91. to_use[item:get_name()] = 1
  92. else
  93. to_use[item:get_name()] = to_use[item:get_name()] + 1
  94. end
  95. end
  96. end
  97. local stack
  98. for itemname, number in pairs(to_use) do
  99. stack = ItemStack({
  100. name = itemname,
  101. count = number
  102. })
  103. if not inventory:contains_item("in", stack) then
  104. return
  105. end
  106. end
  107. for itemname, number in pairs(to_use) do
  108. stack = ItemStack({
  109. name = itemname,
  110. count = number
  111. })
  112. inventory:remove_item("in", stack)
  113. end
  114. inventory:add_item("in", result)
  115. for i = 1, 9 do
  116. inventory:add_item("in", new.items[i])
  117. end
  118. end,
  119. })
  120. ]]