diff --git a/api.lua b/api.lua index 6481690..74a9e3b 100644 --- a/api.lua +++ b/api.lua @@ -14,6 +14,8 @@ -- 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") + industrialtest.api={} industrialtest.api.addPowerStorage=function(meta,capacity,flow,ioConfig) @@ -31,6 +33,17 @@ industrialtest.api.hasPowerStorage=function(meta) end return true end +industrialtest.api.preparePowerStorageItem=function(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 + itemstack:set_wear(65535) + industrialtest.api.addPowerStorage(meta,def._industrialtest_powerCapacity,def._industrialtest_powerFlow,"n/a") + meta:set_string("description",S("@1\n@2 / @3 EU",def.description,meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity"))) + return true +end industrialtest.api.isFullyCharged=function(meta) return meta:get_int("industrialtest.powerAmount")>=meta:get_int("industrialtest.powerCapacity") end @@ -47,11 +60,13 @@ industrialtest.api.addPower=function(meta,amount) end industrialtest.api.addPowerToItem=function(itemstack,amount) local meta=itemstack:get_meta() + local def=minetest.registered_tools[itemstack:get_name()] if not industrialtest.api.hasPowerStorage(meta) then return 0 end local added=industrialtest.api.addPower(meta,amount) - itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65535) + itemstack:set_wear(65535-meta:get_int("industrialtest.powerAmount")/meta:get_int("industrialtest.powerCapacity")*65534) + meta:set_string("description",S("@1\n@2 / @3 EU",def.description,meta:get_int("industrialtest.powerAmount"),meta:get_int("industrialtest.powerCapacity"))) return added end industrialtest.api.transferPower=function(srcMeta,destMeta,amount) diff --git a/compatibility.lua b/compatibility.lua index 5c03f1f..d45ad6a 100644 --- a/compatibility.lua +++ b/compatibility.lua @@ -474,6 +474,7 @@ if industrialtest.mclAvailable then end -- assign element keys for elements that are required later industrialtest.elementKeys.stick="mcl_core:stick" + industrialtest.elementKeys.powerCarrier="mesecons:mesecon" -- register required minerals that are not available in MCL industrialtest.registerMetal("tin","Tin",3,3) diff --git a/craftitems.lua b/craftitems.lua index d356a76..c8eb45f 100644 --- a/craftitems.lua +++ b/craftitems.lua @@ -19,7 +19,10 @@ local S=minetest.get_translator("industrialtest") -- Power storage items minetest.register_tool("industrialtest:re_battery",{ description=S("RE-Battery"), - inventory_image="industrialtest_re_battery.png" + inventory_image="industrialtest_re_battery.png", + _industrialtest_powerStorage=true, + _industrialtest_powerCapacity=7000, + _industrialtest_powerFlow=200 }) minetest.register_craft({ type="shaped", @@ -33,10 +36,13 @@ minetest.register_craft({ }) -- Item callbacks -minetest.register_on_craft(function(itemstack,player,oldCraftGrid,craftInv) - if itemstack:get_name()=="industrialtest:re_battery" then - itemstack:set_wear(65535) - local meta=itemstack:get_meta() - industrialtest.api.addPowerStorage(meta,7000,100,"n/a") +minetest.register_on_player_inventory_action(function(player,action,inventory,info) + if action=="put" then + if industrialtest.api.preparePowerStorageItem(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) +end)