From 1be6638b564abec96fd0f098a9730893e7224bad Mon Sep 17 00:00:00 2001 From: upsilon Date: Tue, 20 Jun 2017 20:19:27 +0200 Subject: [PATCH] Add a cooldown --- README.md | 3 +++ init.lua | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index deb1554..5797e28 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ throwing.vertical_acceleration = -10 throwing.allow_arrow_placing = false throwing.arrow_teleport_in_protected = true + +throwing.bow_cooldown = 0.2 ``` ## API @@ -45,6 +47,7 @@ Definition: definition table, containing: * throw_itself (optional): whether the bow should throw itself instead of the arrow next to it in the inventory. If present, allow_shot is ignored. Default is false. + * cooldown: bow cooldown. Default is setting throwing.bow_cooldown ]] -- Example: diff --git a/init.lua b/init.lua index c667f9f..d6aff21 100644 --- a/init.lua +++ b/init.lua @@ -292,6 +292,8 @@ end ---------- Bows ----------- +local bow_cooldown = {} + function throwing.register_bow(name, def) if not string.find(name, ":") then name = throwing.modname..":"..name @@ -302,23 +304,39 @@ function throwing.register_bow(name, def) return throwing.is_arrow(itemstack) end end - def.inventory_image = def.texture - def.textre = nil + if not def.inventory_image then + def.inventory_image = def.texture + end def.on_use = function(itemstack, user, pointed_thing) + -- Cooldown + local meta = itemstack:get_meta() + local cooldown = def.cooldown or tonumber(minetest.settings:get("throwing.bow_cooldown")) or 0.2 + + if cooldown > 0 and meta:get_int("cooldown") > os.time() then + return + end + + -- Throw itself? if not def.throw_itself and not def.allow_shot(user, user:get_inventory():get_stack("main", user:get_wield_index()+1)) then return itemstack end + + -- Shoot arrow if shoot_arrow(itemstack, user, def.throw_itself) then if not minetest.setting_getbool("creative_mode") then itemstack:add_wear(65535 / (def.uses or 50)) end end + + if def.throw_itself then -- This is a bug. If we return ItemStack(nil), the player punches the entity, -- and if the entity if a __builtin:item, it gets back to his inventory. minetest.after(0.1, function() user:get_inventory():remove_item("main", itemstack) end) + elseif cooldown > 0 then + meta:set_int("cooldown", os.time() + cooldown) end return itemstack end