From ae123446adaf21c3076e2a56e560d08a6482e3d0 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 6 Apr 2025 22:07:38 +0200 Subject: [PATCH] Move some common tool functionality to separate files for further usage --- api/power.lua | 14 ------ api/tool.lua | 60 ----------------------- craftitems.lua | 15 ------ init.lua | 3 +- tools/electric_tool.lua | 45 +++++++++++++++++ tools/tool.lua | 104 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 151 insertions(+), 90 deletions(-) delete mode 100644 api/tool.lua create mode 100644 tools/electric_tool.lua create mode 100644 tools/tool.lua diff --git a/api/power.lua b/api/power.lua index a46be76..317159c 100644 --- a/api/power.lua +++ b/api/power.lua @@ -164,20 +164,6 @@ function industrialtest.api.updateItemPowerText(itemstack) itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65534) 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. -- \param itemstack ItemStack to which add power -- \param amount How much power to add diff --git a/api/tool.lua b/api/tool.lua deleted file mode 100644 index bcbf337..0000000 --- a/api/tool.lua +++ /dev/null @@ -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 . - --- \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/craftitems.lua b/craftitems.lua index 90379c4..72dd60e 100644 --- a/craftitems.lua +++ b/craftitems.lua @@ -891,18 +891,3 @@ minetest.register_craft({ {"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) diff --git a/init.lua b/init.lua index 14f6826..c0044f7 100644 --- a/init.lua +++ b/init.lua @@ -36,7 +36,6 @@ dofile(modpath.."/api/network.lua") dofile(modpath.."/api/power.lua") dofile(modpath.."/api/registration.lua") dofile(modpath.."/api/side.lua") -dofile(modpath.."/api/tool.lua") dofile(modpath.."/machines/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.."/tools/common.lua") +dofile(modpath.."/tools/tool.lua") +dofile(modpath.."/tools/electric_tool.lua") dofile(modpath.."/tools/batpack.lua") dofile(modpath.."/tools/electric_chainsaw.lua") dofile(modpath.."/tools/electric_drill.lua") diff --git a/tools/electric_tool.lua b/tools/electric_tool.lua new file mode 100644 index 0000000..69514c4 --- /dev/null +++ b/tools/electric_tool.lua @@ -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 . + +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"). + +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)