From e2d91cd8c544f17817e34b96891b2d123bddd274 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 8 Sep 2024 22:41:37 +0200 Subject: [PATCH] Extract tool API functions to separate file --- api.lua | 44 -------------------------------------- api/tool.lua | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 1 + 3 files changed, 61 insertions(+), 44 deletions(-) create mode 100644 api/tool.lua diff --git a/api.lua b/api.lua index 1c08810..d0b2fff 100644 --- a/api.lua +++ b/api.lua @@ -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 diff --git a/api/tool.lua b/api/tool.lua new file mode 100644 index 0000000..bcbf337 --- /dev/null +++ b/api/tool.lua @@ -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 . + +-- \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 diff --git a/init.lua b/init.lua index ac42666..75efbc7 100644 --- a/init.lua +++ b/init.lua @@ -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")