Extract tool API functions to separate file

This commit is contained in:
mrkubax10 2024-09-08 22:41:37 +02:00
parent 0363a2ba26
commit e2d91cd8c5
3 changed files with 61 additions and 44 deletions

44
api.lua
View File

@ -16,50 +16,6 @@
local S=minetest.get_translator("industrialtest")
-- \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
industrialtest.api.prepareToolItem=function(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
industrialtest.api.afterToolUse=function(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
-- \brief Transfers power from source node to it's network, if sides is set then power will be only transfered to network connected to that sides
-- \param pos Vector with position of source node
-- \param (optional) sides table with Vectors

60
api/tool.lua Normal file
View File

@ -0,0 +1,60 @@
-- 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

@ -35,6 +35,7 @@ dofile(modpath.."/api/common.lua")
dofile(modpath.."/api/fluid.lua")
dofile(modpath.."/api/power.lua")
dofile(modpath.."/api/side.lua")
dofile(modpath.."/api/tool.lua")
dofile(modpath.."/api.lua")
dofile(modpath.."/machines/common.lua")