From 63bdb0e0e6a6384c5b9557285c3a5340726b7d17 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Wed, 15 Mar 2023 18:10:54 +0100 Subject: [PATCH] Add rubber tree related items --- api.lua | 31 ++++++++++++++++++++++++ craftitems.lua | 23 ++++++++++++++++-- init.lua | 1 + tools.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 tools.lua diff --git a/api.lua b/api.lua index f25cebc..043bfb2 100644 --- a/api.lua +++ b/api.lua @@ -49,6 +49,37 @@ industrialtest.api.preparePowerStorageItem=function(itemstack) industrialtest.api.updateItemPowerText(itemstack) return true end +industrialtest.api.prepareToolItem=function(itemstack) + 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 false + end + local meta=itemstack:get_meta() + meta:set_int("industrialtest.uses",def.tool_capabilities.uses) + return true +end +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 industrialtest.api.isFullyCharged=function(meta) return meta:get_int("industrialtest.powerAmount")>=meta:get_int("industrialtest.powerCapacity") end diff --git a/craftitems.lua b/craftitems.lua index 1f6f806..0ddf9b4 100644 --- a/craftitems.lua +++ b/craftitems.lua @@ -52,6 +52,22 @@ minetest.register_craft({ } }) +minetest.register_craftitem("industrialtest:sticky_resin",{ + description=S("Sticky Resin"), + inventory_image="industrialtest_sticky_resin.png" +}) + +minetest.register_craftitem("industrialtest:rubber",{ + description=S("Rubber"), + inventory_image="industrialtest_rubber.png" +}) +minetest.register_craft({ + type="cooking", + output="industrialtest:rubber", + recipe="industrialtest:sticky_resin" +}) +-- TODO: add rubber crafts using extractor + -- Other items minetest.register_craftitem("industrialtest:electronic_circuit",{ description=S("Electronic Circuit"), @@ -61,11 +77,14 @@ minetest.register_craftitem("industrialtest:electronic_circuit",{ -- Item callbacks minetest.register_on_player_inventory_action(function(player,action,inventory,info) if action=="put" then - if industrialtest.api.preparePowerStorageItem(info.stack) then + if industrialtest.api.preparePowerStorageItem(info.stack) or industrialtest.api.prepareToolItem(info.stack) then inventory:set_stack(info.listname,info.index,info.stack) end end end) minetest.register_on_craft(function(itemstack) - industrialtest.api.preparePowerStorageItem(itemstack) + if industrialtest.api.preparePowerStorageItem(itemstack) then + return + end + industrialtest.api.prepareItemTool(itemstack) end) diff --git a/init.lua b/init.lua index b7e824a..4043132 100644 --- a/init.lua +++ b/init.lua @@ -39,3 +39,4 @@ if industrialtest.developerMode then end dofile(modpath.."/cables.lua") dofile(modpath.."/mapgen.lua") +dofile(modpath.."/tools.lua") diff --git a/tools.lua b/tools.lua new file mode 100644 index 0000000..415ba2e --- /dev/null +++ b/tools.lua @@ -0,0 +1,66 @@ +-- IndustrialTest +-- Copyright (C) 2023 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 . + +local S=minetest.get_translator("industrialtest") + +local definition={ + description=S("Treetap"), + inventory_image="industrialtest_tree_tap.png", + tool_capabilities={ + full_punch_interval=1, + uses=50 + }, + on_place=function(itemstack,user,pointed) + if pointed.type=="node" and user and user:is_player() then + local node=minetest.get_node_or_nil(pointed.under) + if not node then + return nil + end + -- Note: if more nodes from which treetap can extract will be added then they shouldn't be added here + -- Instead they should have additional entry in definition which will denote that treetap can be used on them + if node.name=="industrialtest:rubber_wood_with_rubber" then + local inv=user:get_inventory() + inv:add_item("main",ItemStack("industrialtest:sticky_resin")) + minetest.set_node(pointed.under,{name="industrialtest:rubber_wood"}) + industrialtest.api.afterToolUse(itemstack) + return itemstack + end + end + return nil + end, + _industrialtest_tool=true +} +if industrialtest.mtgAvailable then + definition.groups={ + flammable=2 + } +elseif industrialtest.mclAvailable then + definition.groups={ + tool=1 + } + definition._repair_material="group:wood" + definition._mcl_toollike_wield=true +end +minetest.register_tool("industrialtest:treetap",definition) +minetest.register_craft({ + type="shaped", + output="industrialtest:treetap", + recipe={ + {"","group:wood",""}, + {"group:wood","group:wood","group:wood"}, + {"group:wood","",""} + } +})