Move common machine functions outside of register function

This commit is contained in:
mrkubax10 2023-11-12 15:00:28 +01:00
parent 7a6dacb614
commit edc70ad028

View File

@ -14,6 +14,7 @@
-- You should have received a copy of the GNU General Public License -- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>. -- along with this program. If not, see <http://www.gnu.org/licenses/>.
local machine={}
local simpleElectricItemProcessor={} local simpleElectricItemProcessor={}
function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists) function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists)
@ -32,8 +33,7 @@ function industrialtest.internal.mclAfterDigNode(pos,oldmeta,lists)
meta:from_table(meta2:to_table()) meta:from_table(meta2:to_table())
end end
function industrialtest.internal.registerMachine(config) machine.getFormspec=function(pos,config)
local function getFormspec(pos)
local formspec local formspec
if industrialtest.mtgAvailable then if industrialtest.mtgAvailable then
formspec={ formspec={
@ -56,20 +56,20 @@ function industrialtest.internal.registerMachine(config)
} }
end end
return table.concat(formspec,"") return table.concat(formspec,"")
end end
local definition={
description=config.displayName, machine.onConstruct=function(pos,config)
on_construct=function(pos)
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(pos)
local inv=meta:get_inventory() local inv=meta:get_inventory()
meta:set_string("formspec",getFormspec(pos)) meta:set_string("formspec",machine.getFormspec(pos,config))
if config.onConstruct then if config.onConstruct then
config.onConstruct(pos,meta,inv) config.onConstruct(pos,meta,inv)
end end
industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig) industrialtest.api.addPowerStorage(meta,config.capacity,config.flow,config.ioConfig)
minetest.get_node_timer(pos):start(industrialtest.updateDelay) minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end, end
on_timer=function(pos,elapsed)
machine.onTimer=function(pos,elapsed,config)
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(pos)
local inv=meta:get_inventory() local inv=meta:get_inventory()
local shouldRerunTimer=false local shouldRerunTimer=false
@ -80,12 +80,13 @@ function industrialtest.internal.registerMachine(config)
end end
if shouldUpdateFormspec then if shouldUpdateFormspec then
meta:set_string("formspec",getFormspec(pos)) meta:set_string("formspec",machine.getFormspec(pos,config))
end end
return shouldRerunTimer return shouldRerunTimer
end, end
allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count)
machine.allowMetadataInventoryMove=function(pos,fromList,fromIndex,toList,toIndex,count,config)
local meta=minetest.get_meta(pos) local meta=minetest.get_meta(pos)
local inv=meta:get_inventory() local inv=meta:get_inventory()
local movedItemStack=inv:get_stack(fromList,1) local movedItemStack=inv:get_stack(fromList,1)
@ -105,8 +106,9 @@ function industrialtest.internal.registerMachine(config)
return config.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count) return config.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count)
end end
return count return count
end, end
allow_metadata_inventory_put=function(pos,listname,index,stack,player)
machine.allowMetadataInventoryPut=function(pos,listname,index,stack,player,config)
local found=false local found=false
if config.powerSlots then if config.powerSlots then
for _,value in ipairs(config.powerSlots) do for _,value in ipairs(config.powerSlots) do
@ -123,6 +125,27 @@ function industrialtest.internal.registerMachine(config)
return config.allowMetadataInventoryPut(pos,listname,index,stack,player) return config.allowMetadataInventoryPut(pos,listname,index,stack,player)
end end
return stack:get_count() 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)
machine.onConstruct(pos,config)
end,
on_timer=function(pos,elapsed)
return machine.onTimer(pos,elapsed,config)
end,
allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count)
return machine.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count,config)
end,
allow_metadata_inventory_put=function(pos,listname,index,stack,player)
return machine.allowMetadataInventoryPut(pos,listname,index,stack,player,config)
end, end,
on_metadata_inventory_put=function(pos,listname,index,stack,player) on_metadata_inventory_put=function(pos,listname,index,stack,player)
if config.onMetadataInventoryPut then if config.onMetadataInventoryPut then
@ -140,8 +163,7 @@ function industrialtest.internal.registerMachine(config)
end end
end, end,
_industrialtest_updateFormspec=function(pos) _industrialtest_updateFormspec=function(pos)
local meta=minetest.get_meta(pos) machine.updateFormspec(pos,config)
meta:set_string("formspec",getFormspec(pos))
end end
} }
if industrialtest.mtgAvailable then if industrialtest.mtgAvailable then
@ -199,7 +221,7 @@ function industrialtest.internal.registerMachine(config)
end end
if shouldUpdateFormspec then if shouldUpdateFormspec then
meta:set_string("formspec",getFormspec(pos)) meta:set_string("formspec",machine.getFormspec(pos,config))
end end
return shouldRerunTimer return shouldRerunTimer
@ -426,7 +448,7 @@ simpleElectricItemProcessor.onMetadataInventoryMove=function(pos,fromList,fromIn
meta:set_float("srcTime",-1) meta:set_float("srcTime",-1)
meta:set_float("maxSrcTime",0) meta:set_float("maxSrcTime",0)
if meta:get_int("industrialtest.powerAmount")>0 then if meta:get_int("industrialtest.powerAmount")>0 then
meta:set_string("formspec",getFormspec(pos)) meta:set_string("formspec",simpleElectricItemProcessor.getFormspec(pos))
end end
elseif fromList=="dst" and dstSlot:get_free_space()==0 then elseif fromList=="dst" and dstSlot:get_free_space()==0 then
minetest.get_node_timer(pos):start(industrialtest.updateDelay) 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("srcTime",-1)
meta:set_float("maxSrcTime",0) meta:set_float("maxSrcTime",0)
if meta:get_int("industrialtest.powerAmount")>0 then if meta:get_int("industrialtest.powerAmount")>0 then
meta:set_string("formspec",getFormspec(pos)) meta:set_string("formspec",simpleElectricItemProcessor.getFormspec(pos))
end end
elseif listname=="dst" and dstSlot:get_free_space()==0 then elseif listname=="dst" and dstSlot:get_free_space()==0 then
minetest.get_node_timer(pos):start(industrialtest.updateDelay) minetest.get_node_timer(pos):start(industrialtest.updateDelay)
@ -512,7 +534,7 @@ function industrialtest.internal.registerSimpleElectricItemProcessor(config)
name=config.name, name=config.name,
displayName=config.displayName, displayName=config.displayName,
capacity=config.capacity, capacity=config.capacity,
getFormspec=getFormspec, getFormspec=simpleElectricItemProcessor.getFormspec,
flow=config.flow, flow=config.flow,
ioConfig="iiiiii", ioConfig="iiiiii",
requiresWrench=config.requiresWrench, requiresWrench=config.requiresWrench,