Move some common tool functionality to separate files for further usage

This commit is contained in:
mrkubax10 2025-04-06 22:07:38 +02:00
parent 25680ace72
commit ae123446ad
6 changed files with 151 additions and 90 deletions

View File

@ -164,20 +164,6 @@ function industrialtest.api.updateItemPowerText(itemstack)
itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65534) itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65534)
end end
-- \brief Adds power storage to item depending on it's definition
-- \param itemstack ItemStack to which item storage should be added
-- \returns true if power storage was successfully added, false otherwise
function industrialtest.api.preparePowerStorageItem(itemstack)
local meta=itemstack:get_meta()
local def=minetest.registered_tools[itemstack:get_name()]
if industrialtest.api.hasPowerStorage(meta) or not def or not def._industrialtest_powerStorage or not def._industrialtest_powerCapacity or not def._industrialtest_powerFlow then
return false
end
industrialtest.api.addPowerStorage(meta,def._industrialtest_powerCapacity,def._industrialtest_powerFlow,"n/a")
industrialtest.api.updateItemPowerText(itemstack)
return true
end
-- \brief Adds power to itemstack. Function checks if itemstack has power storage. -- \brief Adds power to itemstack. Function checks if itemstack has power storage.
-- \param itemstack ItemStack to which add power -- \param itemstack ItemStack to which add power
-- \param amount How much power to add -- \param amount How much power to add

View File

@ -1,60 +0,0 @@
-- IndustrialTest
-- Copyright (C) 2024 mrkubax10
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- \brief Sets uses metadata value depending on item's definition
-- \param itemstack ItemStack which should be altered
-- \returns true if value was successfully added, false otherwise
function industrialtest.api.prepareToolItem(itemstack)
local def=minetest.registered_tools[itemstack:get_name()]
if not def then
return false
end
if def._industrialtest_tool and def.tool_capabilities and def.tool_capabilities.uses then
local meta=itemstack:get_meta()
meta:set_int("industrialtest.uses",def.tool_capabilities.uses)
return true
elseif def.groups and def.groups._industrialtest_emptyOnConstruct and itemstack:get_wear()==0 then
itemstack:set_wear(65534)
return true
end
return false
end
-- \brief Adds wear to item after it's use
-- \param itemstack ItemStack to which wear should be added
-- \returns nil
function industrialtest.api.afterToolUse(itemstack)
local meta=itemstack:get_meta()
local def=minetest.registered_tools[itemstack:get_name()]
if not def or not def._industrialtest_tool or not def.tool_capabilities or not def.tool_capabilities.uses then
return
end
if not meta:contains("industrialtest.uses") then
industrialtest.prepareToolItem(itemstack)
end
local uses=meta:get_int("industrialtest.uses")-1
if uses==0 then
itemstack:set_count(0)
minetest.sound_play({name="default_tool_breaks"},{
gain=1,
fade=0,
pitch=1
},true)
return
end
meta:set_int("industrialtest.uses",uses)
itemstack:set_wear(65535-uses/def.tool_capabilities.uses*65535)
end

View File

@ -891,18 +891,3 @@ minetest.register_craft({
{"industrialtest:tin_plate","industrialtest:tin_plate","industrialtest:tin_plate"} {"industrialtest:tin_plate","industrialtest:tin_plate","industrialtest:tin_plate"}
} }
}) })
-- Item callbacks
minetest.register_on_player_inventory_action(function(player,action,inventory,info)
if action=="put" then
if industrialtest.api.preparePowerStorageItem(info.stack) or industrialtest.api.prepareToolItem(info.stack) or industrialtest.api.prepareFluidStorageItem(info.stack) then
inventory:set_stack(info.listname,info.index,info.stack)
end
end
end)
minetest.register_on_craft(function(itemstack)
if industrialtest.api.preparePowerStorageItem(itemstack) or industrialtest.api.prepareToolItem(itemstack) then
return
end
industrialtest.api.prepareFluidStorageItem(itemstack)
end)

View File

@ -36,7 +36,6 @@ dofile(modpath.."/api/network.lua")
dofile(modpath.."/api/power.lua") dofile(modpath.."/api/power.lua")
dofile(modpath.."/api/registration.lua") dofile(modpath.."/api/registration.lua")
dofile(modpath.."/api/side.lua") dofile(modpath.."/api/side.lua")
dofile(modpath.."/api/tool.lua")
dofile(modpath.."/machines/machine.lua") dofile(modpath.."/machines/machine.lua")
dofile(modpath.."/machines/activated_machine.lua") dofile(modpath.."/machines/activated_machine.lua")
@ -65,6 +64,8 @@ dofile(modpath.."/machines/solar_panel_generator.lua")
dofile(modpath.."/machines/wind_mill.lua") dofile(modpath.."/machines/wind_mill.lua")
dofile(modpath.."/tools/common.lua") dofile(modpath.."/tools/common.lua")
dofile(modpath.."/tools/tool.lua")
dofile(modpath.."/tools/electric_tool.lua")
dofile(modpath.."/tools/batpack.lua") dofile(modpath.."/tools/batpack.lua")
dofile(modpath.."/tools/electric_chainsaw.lua") dofile(modpath.."/tools/electric_chainsaw.lua")
dofile(modpath.."/tools/electric_drill.lua") dofile(modpath.."/tools/electric_drill.lua")

45
tools/electric_tool.lua Normal file
View File

@ -0,0 +1,45 @@
-- IndustrialTest
-- Copyright (C) 2025 mrkubax10
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
industrialtest.ElectricTool=table.copy(industrialtest.Tool)
function industrialtest.ElectricTool.onPlace(self,itemstack,user,pointed)
self:prepareTool(itemstack)
local meta=itemstack:get_meta()
local opPower=self:getOpPower()
if meta:get_int("industrialtest.powerAmount")<opPower then
return false
end
self:use(itemstack,user,pointed)
industrialtest.api.addPowerToItem(itemstack,-opPower)
return true
end
function industrialtest.ElectricTool.prepareTool(self,itemstack)
local meta=itemstack:get_meta()
if industrialtest.api.hasPowerStorage(meta) then
return
end
industrialtest.api.addPowerStorage(meta,self.capacity,self.flow,"n/a")
industrialtest.api.updateItemPowerText(itemstack)
end
function industrialtest.ElectricTool.getOpPower(self,itemstack)
-- dummy function
return 0
end

104
tools/tool.lua Normal file
View File

@ -0,0 +1,104 @@
-- IndustrialTest
-- Copyright (C) 2025 mrkubax10
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
industrialtest.Tool={}
function industrialtest.Tool.use(self,itemstack,user,pointed)
-- dummy function
return false
end
function industrialtest.Tool.onPlace(self,itemstack,user,pointed)
if self:use(itemstack,user,pointed) then
local meta=itemstack:get_meta()
if not meta:contains("industrialtest.uses") then
self:prepareTool(itemstack)
end
local uses=meta:get_int("industrialtest.uses")-1
if uses==0 then
itemstack:set_count(0)
minetest.sound_play({name="default_tool_breaks"},{
gain=1,
fade=0,
pitch=1
},true)
return true
end
meta:set_int("industrialtest.uses",uses)
itemstack:set_wear(65535-uses/self.uses*65535)
return true
end
return false
end
function industrialtest.Tool.prepareTool(self,itemstack)
local meta=itemstack:get_meta()
meta:set_int("industrialtest.uses",self.uses)
end
function industrialtest.Tool.createDefinitionTable(self)
local def={
description=self.description,
inventory_image=self.inventoryImage,
tool_capabilities={
full_punch_interval=1,
uses=self.uses
},
on_place=function(itemstack,user,pointed)
if self:onPlace(itemstack,user,pointed) then
return itemstack
end
return nil
end,
_industrialtest_self=self
}
if industrialtest.mtgAvailable then
def.groups={
flammable=self.flammable
}
elseif industrialtest.mclAvailable then
def.groups={
tool=1
}
def._repair_material=self.repairMaterial
def._mcl_toollike_wield=true
end
return def
end
function industrialtest.Tool.register(self)
local def=self:createDefinitionTable()
minetest.register_tool(self.name,def)
end
-- Item callbacks
minetest.register_on_player_inventory_action(function(player,action,inventory,info)
if action=="put" then
local def=info.stack:get_definition()
if def and def._industrialtest_self then
def._industrialtest_self:prepareTool(info.stack)
inventory:set_stack(info.listname,info.index,info.stack)
end
end
end)
minetest.register_on_craft(function(itemstack)
local def=itemstack:get_definition()
if def and def._industrialtest_self then
def._industrialtest_self:prepareTool(itemstack)
end
end)