init.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. -- internationalization boilerplate
  2. local MP = minetest.get_modpath(minetest.get_current_modname())
  3. local S, NS = dofile(MP.."/intllib.lua")
  4. minetest.register_alias("castle:workbench", "crafting_bench:workbench")
  5. local usage_help = S("The inventory on the left is for raw materials, the inventory on the right holds finished products. The crafting grid in the center defines what recipe this workbench will make use of; place raw materials into it in the crafting pattern corresponding to what you want to build.")
  6. if minetest.get_modpath("hopper") and hopper ~= nil and hopper.add_container ~= nil then
  7. usage_help = usage_help .. "\n\n" .. S("This workbench is compatible with hoppers. Hoppers will insert into the raw material inventory and remove items from the finished goods inventory.")
  8. end
  9. local crafting_rate = minetest.settings:get("crafting_bench_crafting_rate")
  10. if crafting_rate == nil then crafting_rate = 5 end
  11. minetest.register_node("crafting_bench:workbench",{
  12. description = S("Autocrafting Workbench"),
  13. _doc_items_longdesc = string.format(S("A workbench that does work for you. Set a crafting recipe and provide raw materials and items will magically craft themselves once every %i seconds."), crafting_rate),
  14. _doc_items_usagehelp = usage_help,
  15. tiles = {
  16. "crafting_bench_workbench_top.png",
  17. "crafting_bench_workbench_bottom.png",
  18. "crafting_bench_workbench_side.png",
  19. "crafting_bench_workbench_side.png",
  20. "crafting_bench_workbench_back.png",
  21. "crafting_bench_workbench_front.png"
  22. },
  23. paramtype2 = "facedir",
  24. paramtype = "light",
  25. groups = {choppy=2,oddly_breakable_by_hand=2,flammable=2},
  26. sounds = default.node_sound_wood_defaults(),
  27. drawtype = "normal",
  28. on_construct = function ( pos )
  29. local meta = minetest.get_meta( pos )
  30. meta:set_string( 'formspec',
  31. 'size[10,10;]' ..
  32. default.gui_bg ..
  33. default.gui_bg_img ..
  34. default.gui_slots ..
  35. 'label[1,0;'..S('Source Material')..']' ..
  36. 'list[context;src;1,1;2,4;]' ..
  37. 'label[4,0;'..S('Recipe to Use')..']' ..
  38. 'list[context;rec;4,1;3,3;]' ..
  39. 'label[7.5,0;'..S('Craft Output')..']' ..
  40. 'list[context;dst;8,1;1,4;]' ..
  41. 'list[current_player;main;1,6;8,4;]' )
  42. meta:set_string( 'infotext', S('Workbench'))
  43. local inv = meta:get_inventory()
  44. inv:set_size( 'src', 2 * 4 )
  45. inv:set_size( 'rec', 3 * 3 )
  46. inv:set_size( 'dst', 1 * 4 )
  47. end,
  48. can_dig = function(pos,player)
  49. local meta = minetest.get_meta(pos);
  50. local inv = meta:get_inventory()
  51. return inv:is_empty("main")
  52. end,
  53. on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
  54. minetest.log("action", S("@1 moves stuff in workbench at @2", player:get_player_name(), minetest.pos_to_string(pos)))
  55. end,
  56. on_metadata_inventory_put = function(pos, listname, index, stack, player)
  57. minetest.log("action", S("@1 moves stuff to workbench at @2", player:get_player_name(), minetest.pos_to_string(pos)))
  58. end,
  59. on_metadata_inventory_take = function(pos, listname, index, stack, player)
  60. minetest.log("action", S("@1 takes stuff from workbench at @2", player:get_player_name(), minetest.pos_to_string(pos)))
  61. end,
  62. })
  63. local get_recipe = function ( inv )
  64. local result, needed, input
  65. needed = inv:get_list( 'rec' )
  66. result, input = minetest.get_craft_result( {
  67. method = 'normal',
  68. width = 3,
  69. items = needed
  70. })
  71. local totalneed = {}
  72. if result.item:is_empty() then
  73. result = nil
  74. else
  75. result = result.item
  76. for _, item in ipairs( needed ) do
  77. if item ~= nil and not item:is_empty() and not inv:contains_item( 'src', item ) then
  78. result = nil
  79. break
  80. end
  81. if item ~= nil and not item:is_empty() then
  82. if totalneed[item:get_name()] == nil then
  83. totalneed[item:get_name()] = 1
  84. else
  85. totalneed[item:get_name()] = totalneed[item:get_name()] + 1
  86. end
  87. end
  88. end
  89. for name, number in pairs( totalneed ) do
  90. local totallist = inv:get_list( 'src' )
  91. for i, srcitem in pairs( totallist ) do
  92. if srcitem:get_name() == name then
  93. local taken = srcitem:take_item( number )
  94. number = number - taken:get_count()
  95. totallist[i] = srcitem
  96. end
  97. if number <= 0 then
  98. break
  99. end
  100. end
  101. if number > 0 then
  102. result = nil
  103. break
  104. end
  105. end
  106. end
  107. return needed, input, result
  108. end
  109. minetest.register_abm( {
  110. nodenames = { 'crafting_bench:workbench' },
  111. interval = crafting_rate,
  112. chance = 1,
  113. action = function ( pos, node )
  114. local meta = minetest.get_meta( pos )
  115. local inv = meta:get_inventory()
  116. local result, newinput, needed
  117. if not inv:is_empty( 'src' ) then
  118. -- Check for a valid recipe and sufficient resources to craft it
  119. needed, newinput, result = get_recipe( inv )
  120. if result ~= nil and inv:room_for_item( 'dst', result ) then
  121. inv:add_item( 'dst', result )
  122. for i, item in pairs( needed ) do
  123. if item ~= nil and item ~= '' then
  124. inv:remove_item( 'src', ItemStack( item ) )
  125. end
  126. if newinput[i] ~= nil and not newinput[i]:is_empty() then
  127. inv:add_item( 'src', newinput[i] )
  128. end
  129. end
  130. end
  131. end
  132. end
  133. } )
  134. local function has_locked_chest_privilege(meta, player)
  135. if player:get_player_name() ~= meta:get_string("owner") then
  136. return false
  137. end
  138. return true
  139. end
  140. minetest.register_craft({
  141. output = "crafting_bench:workbench",
  142. recipe = {
  143. {"default:steel_ingot","default:steel_ingot","default:steel_ingot"},
  144. {"default:wood", "default:wood","default:steel_ingot"},
  145. {"default:tree", "default:tree","default:steel_ingot"},
  146. }
  147. })
  148. -- Hopper compatibility
  149. if minetest.get_modpath("hopper") and hopper ~= nil and hopper.add_container ~= nil then
  150. hopper:add_container({
  151. {"top", "crafting_bench:workbench", "dst"},
  152. {"side", "crafting_bench:workbench", "src"},
  153. {"bottom", "crafting_bench:workbench", "src"},
  154. })
  155. end