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,97 +33,119 @@ 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={ "formspec_version[4]",
"formspec_version[4]", "size[10.8,12]",
"size[10.8,12]", "label[0.5,0.5;"..config.displayName.."]",
"label[0.5,0.5;"..config.displayName.."]", (config.getFormspec and config.getFormspec(pos) or ""),
(config.getFormspec and config.getFormspec(pos) or ""), "list[current_player;main;0.5,6.25;8,1]",
"list[current_player;main;0.5,6.25;8,1]", "list[current_player;main;0.5,7.5;8,3;8]"
"list[current_player;main;0.5,7.5;8,3;8]" }
} elseif industrialtest.mclAvailable then
elseif industrialtest.mclAvailable then formspec={
formspec={ "size[10.04,12]",
"size[10.04,12]", "label[0.25,0.25;"..config.displayName.."]",
"label[0.25,0.25;"..config.displayName.."]", (config.getFormspec and config.getFormspec(pos) or ""),
(config.getFormspec and config.getFormspec(pos) or ""), "list[current_player;main;0.5,7;9,3;9]",
"list[current_player;main;0.5,7;9,3;9]", mcl_formspec.get_itemslot_bg(0.5,7,9,3),
mcl_formspec.get_itemslot_bg(0.5,7,9,3), "list[current_player;main;0.5,10.24;9,1]",
"list[current_player;main;0.5,10.24;9,1]", mcl_formspec.get_itemslot_bg(0.5,10.24,9,1)
mcl_formspec.get_itemslot_bg(0.5,10.24,9,1) }
}
end
return table.concat(formspec,"")
end 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={ local definition={
description=config.displayName, description=config.displayName,
on_construct=function(pos) on_construct=function(pos)
local meta=minetest.get_meta(pos) machine.onConstruct(pos,config)
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)
end, end,
on_timer=function(pos,elapsed) on_timer=function(pos,elapsed)
local meta=minetest.get_meta(pos) return machine.onTimer(pos,elapsed,config)
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
end, end,
allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count) allow_metadata_inventory_move=function(pos,fromList,fromIndex,toList,toIndex,count)
local meta=minetest.get_meta(pos) return machine.allowMetadataInventoryMove(pos,fromList,fromIndex,toList,toIndex,count,config)
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, end,
allow_metadata_inventory_put=function(pos,listname,index,stack,player) allow_metadata_inventory_put=function(pos,listname,index,stack,player)
local found=false return machine.allowMetadataInventoryPut(pos,listname,index,stack,player,config)
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, 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,