diff --git a/tools/common.lua b/tools/common.lua index a2d7337..3f9ff64 100644 --- a/tools/common.lua +++ b/tools/common.lua @@ -19,45 +19,37 @@ industrialtest.internal.registeredElectricDrills={} industrialtest.internal.registeredElectricHoes={} industrialtest.internal.registeredElectricSabers={} -minetest.register_on_punchnode(function(pos,node,user,pointed) - if user then - local itemstack=user:get_wielded_item() - if industrialtest.internal.registeredElectricChainsaws[itemstack:get_name()] then - local meta=itemstack:get_meta() - if meta:get_int("industrialtest.powerAmount")>=20 then - local def=minetest.registered_nodes[node.name] - if (industrialtest.mtgAvailable and def.groups and def.groups.choppy) or (industrialtest.mclAvailable and def.groups and def.groups.axey) then - itemstack:set_name(itemstack:get_name().."_active") - user:set_wielded_item(itemstack) - end - end - elseif industrialtest.mclAvailable and industrialtest.internal.registeredElectricHoes[itemstack:get_name()] then - local meta=itemstack:get_meta() - if meta:get_int("industrialtest.powerAmount")>=20 then - local def=minetest.registered_nodes[node.name] - if def.groups and def.groups.hoey then - itemstack:set_name(itemstack:get_name().."_active") - user:set_wielded_item(itemstack) - end - end - elseif industrialtest.internal.registeredElectricDrills[itemstack:get_name()] then - local meta=itemstack:get_meta() - if meta:get_int("industrialtest.powerAmount")>=20 then - local def=minetest.registered_nodes[node.name] - if (industrialtest.mtgAvailable and def.groups and (def.groups.cracky or def.groups.crumbly)) or (industrialtest.mclAvailable and def.groups and (def.groups.pickaxey or def.groups.shovely)) then - itemstack:set_name(itemstack:get_name().."_active") - user:set_wielded_item(itemstack) - end - end - elseif industrialtest.internal.registeredElectricSabers[itemstack:get_name()] then - local meta=itemstack:get_meta() - if meta:get_int("industrialtest.powerAmount")>=20 then - local def=minetest.registered_nodes[node.name] - if (industrialtest.mtgAvailable and def.groups and def.groups.snappy) or (industrialtest.mclAvailable and def.groups and (def.groups.swordy or def.groups.swordy_cobweb)) then - itemstack:set_name(itemstack:get_name().."_active") - user:set_wielded_item(itemstack) - end - end +local function isActive(itemstack) + return string.sub(itemstack:get_name(),-string.len("_active"),-1)=="_active" +end + +local function beforeUse(user,itemstack,canDig) + local meta=itemstack:get_meta() + local def=itemstack:get_definition() + if meta:get_int("industrialtest.powerAmount")>=20 and canDig then + if not isActive(itemstack) then + itemstack:set_name(itemstack:get_name().."_active") end + else + itemstack:set_name(def._industrialtest_inactiveName) + end + user:set_wielded_item(itemstack) +end + +minetest.register_on_punchnode(function(pos,node,user,pointed) + if not user then + return + end + local itemstack=user:get_wielded_item() + local meta=itemstack:get_meta() + local def=minetest.registered_nodes[node.name] + if industrialtest.internal.registeredElectricChainsaws[itemstack:get_name()] then + beforeUse(user,itemstack,(industrialtest.mtgAvailable and def.groups and def.groups.choppy) or (industrialtest.mclAvailable and def.groups and def.groups.axey)) + elseif industrialtest.mclAvailable and industrialtest.internal.registeredElectricHoes[itemstack:get_name()] then + beforeUse(user,itemstack,def.groups and def.groups.hoey) + elseif industrialtest.internal.registeredElectricDrills[itemstack:get_name()] then + beforeUse(user,itemstack,(industrialtest.mtgAvailable and def.groups and (def.groups.cracky or def.groups.crumbly)) or (industrialtest.mclAvailable and def.groups and (def.groups.pickaxey or def.groups.shovely))) + elseif industrialtest.internal.registeredElectricSabers[itemstack:get_name()] then + beforeUse(user,itemstack,(industrialtest.mtgAvailable and def.groups and def.groups.snappy) or (industrialtest.mclAvailable and def.groups and (def.groups.swordy or def.groups.swordy_cobweb))) end end) diff --git a/tools/electric_chainsaw.lua b/tools/electric_chainsaw.lua index 26cc69f..f1cc747 100644 --- a/tools/electric_chainsaw.lua +++ b/tools/electric_chainsaw.lua @@ -19,8 +19,11 @@ local S=minetest.get_translator("industrialtest") local electricChainsaw={} electricChainsaw.afterUse=function(itemstack,config) + local meta=itemstack:get_meta() industrialtest.api.addPowerToItem(itemstack,-20) - itemstack:set_name("industrialtest:"..config.name) + if meta:get_int("industrialtest.powerAmount")<20 then + itemstack:set_name("industrialtest:"..config.name) + end return itemstack end @@ -30,7 +33,8 @@ local function registerElectricChainsaw(config) inventory_image="industrialtest_"..config.name..".png", _industrialtest_powerStorage=true, _industrialtest_powerCapacity=10000, - _industrialtest_powerFlow=industrialtest.api.lvPowerFlow + _industrialtest_powerFlow=industrialtest.api.lvPowerFlow, + _industrialtest_inactiveName="industrialtest:"..config.name } if industrialtest.mtgAvailable then definition.tool_capabilities={ @@ -93,6 +97,7 @@ local function registerElectricChainsaw(config) end minetest.register_tool("industrialtest:"..config.name.."_active",definition) industrialtest.internal.registeredElectricChainsaws["industrialtest:"..config.name]=true + industrialtest.internal.registeredElectricChainsaws["industrialtest:"..config.name.."_active"]=true end local definition={ name="electric_chainsaw", diff --git a/tools/electric_drill.lua b/tools/electric_drill.lua index 68c07f4..e8aa49c 100644 --- a/tools/electric_drill.lua +++ b/tools/electric_drill.lua @@ -19,8 +19,11 @@ local S=minetest.get_translator("industrialtest") local electricDrill={} electricDrill.afterUse=function(itemstack,config) + local meta=itemstack:get_meta() industrialtest.api.addPowerToItem(itemstack,-20) - itemstack:set_name("industrialtest:"..config.name) + if meta:get_int("industrialtest.powerAmount")<20 then + itemstack:set_name("industrialtest:"..config.name) + end return itemstack end @@ -30,7 +33,8 @@ local function registerElectricDrill(config) inventory_image="industrialtest_"..config.name..".png", _industrialtest_powerStorage=true, _industrialtest_powerCapacity=10000, - _industrialtest_powerFlow=industrialtest.api.lvPowerFlow + _industrialtest_powerFlow=industrialtest.api.lvPowerFlow, + _industrialtest_inactiveName="industrialtest:"..config.name } if industrialtest.mtgAvailable then definition.tool_capabilities={ @@ -93,6 +97,7 @@ local function registerElectricDrill(config) end minetest.register_tool("industrialtest:"..config.name.."_active",definition) industrialtest.internal.registeredElectricDrills["industrialtest:"..config.name]=true + industrialtest.internal.registeredElectricDrills["industrialtest:"..config.name.."_active"]=true end local definition={ name="electric_drill", diff --git a/tools/electric_hoe.lua b/tools/electric_hoe.lua index 31e67de..e162075 100644 --- a/tools/electric_hoe.lua +++ b/tools/electric_hoe.lua @@ -19,8 +19,11 @@ local S=minetest.get_translator("industrialtest") local electricHoe={} electricHoe.afterUse=function(itemstack,config) + local meta=itemstack:get_meta() industrialtest.api.addPowerToItem(itemstack,-20) - itemstack:set_name("industrialtest:"..config.name) + if meta:get_int("industrialtest.powerAmount")<20 then + itemstack:set_name("industrialtest:"..config.name) + end return itemstack end @@ -30,7 +33,8 @@ local function registerElectricHoe(config) inventory_image="industrialtest_"..config.name..".png", _industrialtest_powerStorage=true, _industrialtest_powerCapacity=10000, - _industrialtest_powerFlow=industrialtest.api.lvPowerFlow + _industrialtest_powerFlow=industrialtest.api.lvPowerFlow, + _industrialtest_inactiveName="industrialtest:"..config.name } if industrialtest.mtgAvailable then -- Taken and adapted from farming mod from Minetest Game @@ -175,6 +179,7 @@ local function registerElectricHoe(config) return electricHoe.afterUse(itemstack,config) end minetest.register_tool("industrialtest:"..config.name.."_active",definition) + industrialtest.internal.registeredElectricHoes["industrialtest:"..config.name.."_active"]=true end industrialtest.internal.registeredElectricHoes["industrialtest:"..config.name]=true end diff --git a/tools/electric_saber.lua b/tools/electric_saber.lua index 47d9ed0..452e011 100644 --- a/tools/electric_saber.lua +++ b/tools/electric_saber.lua @@ -16,13 +16,25 @@ local S=minetest.get_translator("industrialtest") +local electricSaber={} + +electricSaber.afterUse=function(itemstack,config) + local meta=itemstack:get_meta() + industrialtest.api.addPowerToItem(itemstack,-20) + if meta:get_int("industrialtest.powerAmount")<20 then + itemstack:set_name("industrialtest:"..config.name) + end + return itemstack +end + local function registerElectricSaber(config) local definition={ description=config.displayName, inventory_image="industrialtest_"..config.name..".png", _industrialtest_powerStorage=true, _industrialtest_powerCapacity=10000, - _industrialtest_powerFlow=industrialtest.api.lvPowerFlow + _industrialtest_powerFlow=industrialtest.api.lvPowerFlow, + _industrialtest_inactiveName="industrialtest:"..config.name } if industrialtest.mtgAvailable then definition.groups={ @@ -79,11 +91,12 @@ local function registerElectricSaber(config) definition._mcl_diggroups.swordy_cobweb.speed=config.activeDigSpeed end definition.groups.not_in_creative_inventory=1 - definition.after_use=function() - return nil + definition.after_use=function(itemstack) + return electricSaber.afterUse(itemstack,config) end minetest.register_tool("industrialtest:"..config.name.."_active",definition) industrialtest.internal.registeredElectricSabers["industrialtest:"..config.name]=true + industrialtest.internal.registeredElectricSabers["industrialtest:"..config.name.."_active"]=true end local definition={ name="electric_saber",