diff --git a/machines/common.lua b/machines/common.lua index a8ec308..1b05317 100644 --- a/machines/common.lua +++ b/machines/common.lua @@ -14,6 +14,7 @@ -- You should have received a copy of the GNU General Public License -- along with this program. If not, see . +local machine={} local simpleElectricItemProcessor={} function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists) @@ -32,97 +33,119 @@ function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists) meta:from_table(meta2:to_table()) end -function industrialtest.internal.registerMachine(config) - local function getFormspec(pos) - local formspec - if industrialtest.mtgAvailable then - formspec={ - "formspec_version[4]", - "size[10.8,12]", - "label[0.5,0.5;"..config.displayName.."]", - (config.getFormspec and config.getFormspec(pos) or ""), - "list[current_player;main;0.5,6.25;8,1]", - "list[current_player;main;0.5,7.5;8,3;8]" - } - elseif industrialtest.mclAvailable then - formspec={ - "size[10.04,12]", - "label[0.25,0.25;"..config.displayName.."]", - (config.getFormspec and config.getFormspec(pos) or ""), - "list[current_player;main;0.5,7;9,3;9]", - mcl_formspec.get_itemslot_bg(0.5,7,9,3), - "list[current_player;main;0.5,10.24;9,1]", - mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) - } - end - return table.concat(formspec,"") +machine.getFormspec=function(pos,config) + local formspec + if industrialtest.mtgAvailable then + formspec={ + "formspec_version[4]", + "size[10.8,12]", + "label[0.5,0.5;"..config.displayName.."]", + (config.getFormspec and config.getFormspec(pos) or ""), + "list[current_player;main;0.5,6.25;8,1]", + "list[current_player;main;0.5,7.5;8,3;8]" + } + elseif industrialtest.mclAvailable then + formspec={ + "size[10.04,12]", + "label[0.25,0.25;"..config.displayName.."]", + (config.getFormspec and config.getFormspec(pos) or ""), + "list[current_player;main;0.5,7;9,3;9]", + mcl_formspec.get_itemslot_bg(0.5,7,9,3), + "list[current_player;main;0.5,10.24;9,1]", + mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) + } end + return table.concat(formspec,"") +end + +machine.onConstruct=function(pos,config) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + meta:set_string("formspec",machine.getFormspec(pos,config)) + if config.onConstruct then + config.onConstruct(pos,meta,inv) + end + industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig) + minetest.get_node_timer(pos):start(industrialtest.updateDelay) +end + +machine.onTimer=function(pos,elapsed,config) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local shouldRerunTimer=false + local shouldUpdateFormspec=false + + if config.onTimer then + shouldRerunTimer,shouldUpdateFormspec=config.onTimer(pos,elapsed,meta,inv) + end + + if shouldUpdateFormspec then + meta:set_string("formspec",machine.getFormspec(pos,config)) + end + + return shouldRerunTimer +end + +machine.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toIndex,count,config) + local meta=minetest.get_meta(pos) + local inv=meta:get_inventory() + local movedItemStack=inv:get_stack(fromList,1) + local found=false + if config.powerSlots then + for _,value in ipairs(config.powerSlots) do + if value==toList then + found=true + break + end + end + end + if found and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then + return 0 + end + if config.allowMetadataInventoryMove then + return config.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count) + end + return count +end + +machine.allowMetadataInventoryPut=function(pos,listname,index,stack,player,config) + local found=false + if config.powerSlots then + for _,value in ipairs(config.powerSlots) do + if value==listname then + found=true + break + end + end + end + if found and not industrialtest.api.hasPowerStorage(stack:get_meta()) then + return 0 + end + if config.allowMetadataInventoryPut then + return config.allowMetadataInventoryPut(pos,listname,index,stack,player) + end + return stack:get_count() +end + +machine.updateFormspec=function(pos,config) + local meta=minetest.get_meta(pos) + meta:set_string("formspec",machine.getFormspec(pos,config)) +end + +function industrialtest.internal.registerMachine(config) local definition={ description=config.displayName, on_construct=function(pos) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - meta:set_string("formspec",getFormspec(pos)) - if config.onConstruct then - config.onConstruct(pos,meta,inv) - end - industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig) - minetest.get_node_timer(pos):start(industrialtest.updateDelay) + machine.onConstruct(pos,config) end, on_timer=function(pos,elapsed) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local shouldRerunTimer=false - local shouldUpdateFormspec=false - - if config.onTimer then - shouldRerunTimer,shouldUpdateFormspec=config.onTimer(pos,elapsed,meta,inv) - end - - if shouldUpdateFormspec then - meta:set_string("formspec",getFormspec(pos)) - end - - return shouldRerunTimer + return machine.onTimer(pos,elapsed,config) end, allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) - local meta=minetest.get_meta(pos) - local inv=meta:get_inventory() - local movedItemStack=inv:get_stack(fromList,1) - local found=false - if config.powerSlots then - for _,value in ipairs(config.powerSlots) do - if value==toList then - found=true - break - end - end - end - if found and not industrialtest.api.hasPowerStorage(movedItemStack:get_meta()) then - return 0 - end - if config.allowMetadataInventoryMove then - return config.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count) - end - return count + return machine.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count,config) end, allow_metadata_inventory_put=function(pos,listname,index,stack,player) - local found=false - if config.powerSlots then - for _,value in ipairs(config.powerSlots) do - if value==listname then - found=true - break - end - end - end - if found and not industrialtest.api.hasPowerStorage(stack:get_meta()) then - return 0 - end - if config.allowMetadataInventoryPut then - return config.allowMetadataInventoryPut(pos,listname,index,stack,player) - end - return stack:get_count() + return machine.allowMetadataInventoryPut(pos,listname,index,stack,player,config) end, on_metadata_inventory_put=function(pos,listname,index,stack,player) if config.onMetadataInventoryPut then @@ -140,8 +163,7 @@ function industrialtest.internal.registerMachine(config) end end, _industrialtest_updateFormspec=function(pos) - local meta=minetest.get_meta(pos) - meta:set_string("formspec",getFormspec(pos)) + machine.updateFormspec(pos,config) end } if industrialtest.mtgAvailable then @@ -199,7 +221,7 @@ function industrialtest.internal.registerMachine(config) end if shouldUpdateFormspec then - meta:set_string("formspec",getFormspec(pos)) + meta:set_string("formspec",machine.getFormspec(pos,config)) end return shouldRerunTimer @@ -426,7 +448,7 @@ simpleElectricItemProcessor.onMetadataInventoryMove=function(pos,fromList,fromIn meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) if meta:get_int("industrialtest.powerAmount")>0 then - meta:set_string("formspec",getFormspec(pos)) + meta:set_string("formspec",simpleElectricItemProcessor.getFormspec(pos)) end elseif fromList=="dst" and dstSlot:get_free_space()==0 then minetest.get_node_timer(pos):start(industrialtest.updateDelay) @@ -442,7 +464,7 @@ simpleElectricItemProcessor.onMetadataInventoryTake=function(pos,listname,index, meta:set_float("srcTime",-1) meta:set_float("maxSrcTime",0) if meta:get_int("industrialtest.powerAmount")>0 then - meta:set_string("formspec",getFormspec(pos)) + meta:set_string("formspec",simpleElectricItemProcessor.getFormspec(pos)) end elseif listname=="dst" and dstSlot:get_free_space()==0 then minetest.get_node_timer(pos):start(industrialtest.updateDelay) @@ -512,7 +534,7 @@ function industrialtest.internal.registerSimpleElectricItemProcessor(config) name=config.name, displayName=config.displayName, capacity=config.capacity, - getFormspec=getFormspec, + getFormspec=simpleElectricItemProcessor.getFormspec, flow=config.flow, ioConfig="iiiiii", requiresWrench=config.requiresWrench,