2020-03-09 06:53:23 +01:00
local S = minetest.get_translator ( " crafting_bench " )
2023-11-22 20:26:27 +01:00
local F = minetest.formspec_escape
local C = minetest.colorize
2017-02-26 03:55:21 +01:00
minetest.register_alias ( " castle:workbench " , " crafting_bench:workbench " )
2023-11-22 20:26:27 +01:00
local has_mcl = minetest.get_modpath ( " mcl_formspec " )
local has_default = minetest.get_modpath ( " default " )
2017-02-26 05:17:23 +01:00
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. " )
2023-11-22 20:26:27 +01:00
if ( minetest.get_modpath ( " hopper " ) and hopper ~= nil and hopper.add_container ~= nil ) or has_mcl then
2017-02-26 05:17:23 +01:00
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. " )
end
2017-03-01 05:12:46 +01:00
2020-05-27 20:57:45 +02:00
local crafting_rate = tonumber ( minetest.settings : get ( " crafting_bench_crafting_rate " ) ) or 5
2017-03-01 05:12:46 +01:00
2023-11-22 20:26:27 +01:00
if not has_default and not has_mcl then
error ( " The crafting bench mod needs either the default mod (minetest game) or mineclonia/mineclone2 to work " )
end
local invsize_src = 2 * 4
local invsize_dst = 1 * 4
if has_mcl then
invsize_src = 2 * 3
invsize_dst = 1 * 3
end
local groups
local mcl_hardness
local mcl_blast_res
local sounds
local formspec
if has_default then
formspec = ' size[10,10;] ' ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
' label[1,0; ' .. S ( ' Source Material ' ) .. ' ] ' ..
' list[context;src;1,1;2,4;] ' ..
' label[4,0; ' .. S ( ' Recipe to Use ' ) .. ' ] ' ..
' list[context;rec;4,1;3,3;] ' ..
' label[7.5,0; ' .. S ( ' Craft Output ' ) .. ' ] ' ..
' list[context;dst;8,1;1,4;] ' ..
' list[current_player;main;1,6;8,4;] ' ..
' listring[current_name;dst] ' ..
' listring[current_player;main] ' ..
' listring[current_name;src] ' ..
' listring[current_player;main] '
groups = { choppy = 2 , oddly_breakable_by_hand = 2 , flammable = 2 }
sounds = default.node_sound_wood_defaults ( )
elseif has_mcl then
formspec = ' formspec_version[4] ' ..
' size[11.75,10.425] ' ..
mcl_formspec.get_itemslot_bg_v4 ( 1 , 0.75 , 2 , 3 ) ..
mcl_formspec.get_itemslot_bg_v4 ( 5 , 0.75 , 3 , 3 ) ..
mcl_formspec.get_itemslot_bg_v4 ( 10 , 0.75 , 1 , 3 ) ..
' label[1,0.375; ' .. S ( ' Source Material ' ) .. ' ] ' ..
' list[context;src;1,0.75;2,4;] ' ..
' label[5,0.375; ' .. S ( ' Recipe to Use ' ) .. ' ] ' ..
' list[context;rec;5,0.75;3,3;] ' ..
' label[9.5,0.375; ' .. S ( ' Craft Output ' ) .. ' ] ' ..
' list[context;dst;10,0.75;1,4;] ' ..
' label[0.375,4.7; ' .. F ( C ( mcl_formspec.label_color , S ( ' Inventory ' ) ) ) .. ' ] ' ..
mcl_formspec.get_itemslot_bg_v4 ( 0.375 , 5.1 , 9 , 3 ) ..
' list[current_player;main;0.375,5.1;9,3;9] ' ..
mcl_formspec.get_itemslot_bg_v4 ( 0.375 , 9.05 , 9 , 1 ) ..
' list[current_player;main;0.375,9.05;9,1;] ' ..
' listring[current_name;dst] ' ..
' listring[current_player;main] ' ..
' listring[current_name;src] ' ..
' listring[current_player;main] '
groups = { axey = 2 , handy = 2 , flammable =- 1 , container = 4 }
sounds = mcl_sounds.node_sound_wood_defaults ( )
mcl_hardness = 2
mcl_blast_res = 3
end
2017-03-01 05:12:46 +01:00
2017-02-26 03:55:21 +01:00
minetest.register_node ( " crafting_bench:workbench " , {
description = S ( " Workbench " ) ,
2020-03-09 06:53:23 +01:00
_doc_items_longdesc = S ( " A workbench that does work for you. Set a crafting recipe and provide raw materials and items will magically craft themselves once every @1 seconds. " , crafting_rate ) ,
2017-02-26 05:17:23 +01:00
_doc_items_usagehelp = usage_help ,
2017-02-26 03:55:21 +01:00
tiles = {
" crafting_bench_workbench_top.png " ,
" crafting_bench_workbench_bottom.png " ,
" crafting_bench_workbench_side.png " ,
" crafting_bench_workbench_side.png " ,
" crafting_bench_workbench_back.png " ,
" crafting_bench_workbench_front.png "
} ,
paramtype2 = " facedir " ,
paramtype = " light " ,
2023-11-22 20:26:27 +01:00
groups = groups ,
sounds = sounds ,
2017-02-26 03:55:21 +01:00
drawtype = " normal " ,
2023-11-22 20:26:27 +01:00
_mcl_hardness = mcl_hardness ,
_mcl_blast_resistance = mcl_blast_res ,
2017-02-26 03:55:21 +01:00
on_construct = function ( pos )
local meta = minetest.get_meta ( pos )
2023-11-22 20:26:27 +01:00
meta : set_string ( ' formspec ' , formspec )
2017-02-26 03:55:21 +01:00
meta : set_string ( ' infotext ' , S ( ' Workbench ' ) )
local inv = meta : get_inventory ( )
2023-11-22 20:26:27 +01:00
inv : set_size ( ' src ' , invsize_src )
2017-02-26 03:55:21 +01:00
inv : set_size ( ' rec ' , 3 * 3 )
2023-11-22 20:26:27 +01:00
inv : set_size ( ' dst ' , invsize_dst )
2017-02-26 03:55:21 +01:00
end ,
can_dig = function ( pos , player )
local meta = minetest.get_meta ( pos ) ;
local inv = meta : get_inventory ( )
2023-05-14 14:52:47 +02:00
return inv : is_empty ( " src " ) and inv : is_empty ( " rec " ) and inv : is_empty ( " dst " )
2017-02-26 03:55:21 +01:00
end ,
2023-05-14 14:52:47 +02:00
on_blast = function ( pos ) end ,
2017-02-26 03:55:21 +01:00
on_metadata_inventory_move = function ( pos , from_list , from_index , to_list , to_index , count , player )
2020-03-09 06:53:23 +01:00
minetest.log ( " action " , player : get_player_name ( ) .. " moves stuff in workbench at " .. minetest.pos_to_string ( pos ) )
2017-02-26 03:55:21 +01:00
end ,
on_metadata_inventory_put = function ( pos , listname , index , stack , player )
2020-03-09 06:53:23 +01:00
minetest.log ( " action " , player : get_player_name ( ) .. " moves stuff to workbench at " .. minetest.pos_to_string ( pos ) )
2017-02-26 03:55:21 +01:00
end ,
on_metadata_inventory_take = function ( pos , listname , index , stack , player )
2020-03-09 06:53:23 +01:00
minetest.log ( " action " , player : get_player_name ( ) .. " takes stuff from workbench at " .. minetest.pos_to_string ( pos ) )
2017-02-26 03:55:21 +01:00
end ,
} )
local get_recipe = function ( inv )
2021-08-21 09:42:22 +02:00
local output , needed , decremented_input
2017-02-26 03:55:21 +01:00
needed = inv : get_list ( ' rec ' )
2021-08-21 09:42:22 +02:00
output , decremented_input = minetest.get_craft_result ( {
2017-02-26 03:55:21 +01:00
method = ' normal ' ,
width = 3 ,
items = needed
} )
local totalneed = { }
2021-08-21 09:42:22 +02:00
if output.item : is_empty ( ) then
output = nil
2017-02-26 03:55:21 +01:00
else
2021-08-21 09:42:22 +02:00
output = output.item
2017-02-26 03:55:21 +01:00
for _ , item in ipairs ( needed ) do
if item ~= nil and not item : is_empty ( ) and not inv : contains_item ( ' src ' , item ) then
2021-08-21 09:42:22 +02:00
output = nil
2017-02-26 03:55:21 +01:00
break
end
if item ~= nil and not item : is_empty ( ) then
if totalneed [ item : get_name ( ) ] == nil then
totalneed [ item : get_name ( ) ] = 1
else
totalneed [ item : get_name ( ) ] = totalneed [ item : get_name ( ) ] + 1
end
end
end
for name , number in pairs ( totalneed ) do
local totallist = inv : get_list ( ' src ' )
for i , srcitem in pairs ( totallist ) do
if srcitem : get_name ( ) == name then
local taken = srcitem : take_item ( number )
number = number - taken : get_count ( )
totallist [ i ] = srcitem
end
if number <= 0 then
break
end
end
if number > 0 then
2021-08-21 09:42:22 +02:00
output = nil
2017-02-26 03:55:21 +01:00
break
end
end
end
2021-08-21 09:42:22 +02:00
return needed , decremented_input , output
2017-02-26 03:55:21 +01:00
end
minetest.register_abm ( {
nodenames = { ' crafting_bench:workbench ' } ,
2017-03-01 05:12:46 +01:00
interval = crafting_rate ,
2017-02-26 03:55:21 +01:00
chance = 1 ,
action = function ( pos , node )
local meta = minetest.get_meta ( pos )
local inv = meta : get_inventory ( )
2021-08-21 09:42:22 +02:00
local output , decremented_input , needed
2017-02-26 03:55:21 +01:00
if not inv : is_empty ( ' src ' ) then
-- Check for a valid recipe and sufficient resources to craft it
2021-08-21 09:42:22 +02:00
needed , decremented_input , output = get_recipe ( inv )
if output ~= nil and inv : room_for_item ( ' dst ' , output ) then
inv : add_item ( ' dst ' , output )
for _ , item in pairs ( needed ) do
2017-02-26 03:55:21 +01:00
if item ~= nil and item ~= ' ' then
inv : remove_item ( ' src ' , ItemStack ( item ) )
end
2021-08-21 09:42:22 +02:00
end
for i = 1 , 9 do
inv : add_item ( ' dst ' , decremented_input.items [ i ] )
2017-02-26 03:55:21 +01:00
end
end
end
end
} )
minetest.register_craft ( {
output = " crafting_bench:workbench " ,
recipe = {
{ " default:steel_ingot " , " default:steel_ingot " , " default:steel_ingot " } ,
{ " default:wood " , " default:wood " , " default:steel_ingot " } ,
{ " default:tree " , " default:tree " , " default:steel_ingot " } ,
}
} )
-- Hopper compatibility
if minetest.get_modpath ( " hopper " ) and hopper ~= nil and hopper.add_container ~= nil then
hopper : add_container ( {
{ " top " , " crafting_bench:workbench " , " dst " } ,
{ " side " , " crafting_bench:workbench " , " src " } ,
{ " bottom " , " crafting_bench:workbench " , " src " } ,
} )
end