mirror of
https://github.com/minetest-mods/throwing.git
synced 2025-01-10 02:00:27 +01:00
Add a throw_itself field to throwing.register_bow definition table
This commit is contained in:
parent
d33a8b69ca
commit
01c10dff75
@ -41,6 +41,9 @@ Definition: definition table, containing:
|
|||||||
- itemstack: the itemstack of the bow
|
- itemstack: the itemstack of the bow
|
||||||
- should return true if the shot can be made, and false otherwise
|
- should return true if the shot can be made, and false otherwise
|
||||||
- default for this is function(player, itemstack) return throwing.is_arrow(itemstack) end
|
- default for this is function(player, itemstack) return throwing.is_arrow(itemstack) end
|
||||||
|
* 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.
|
||||||
]]
|
]]
|
||||||
|
|
||||||
-- Example:
|
-- Example:
|
||||||
|
22
init.lua
22
init.lua
@ -18,9 +18,14 @@ function throwing.is_arrow(itemstack)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
local function shoot_arrow(itemstack, player)
|
local function shoot_arrow(itemstack, player, throw_itself)
|
||||||
local inventory = player:get_inventory()
|
local inventory = player:get_inventory()
|
||||||
local arrow = inventory:get_stack("main", player:get_wield_index()+1):get_name()
|
local arrow
|
||||||
|
if throw_itself then
|
||||||
|
arrow = player:get_wielded_item():get_name()
|
||||||
|
else
|
||||||
|
arrow = inventory:get_stack("main", player:get_wield_index()+1):get_name()
|
||||||
|
end
|
||||||
|
|
||||||
local playerpos = player:getpos()
|
local playerpos = player:getpos()
|
||||||
local pos = {x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}
|
local pos = {x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}
|
||||||
@ -61,7 +66,7 @@ local function shoot_arrow(itemstack, player)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not minetest.setting_getbool("creative_mode") then
|
if not minetest.setting_getbool("creative_mode") then
|
||||||
player:get_inventory():remove_item("main", arrow)
|
inventory:remove_item("main", arrow)
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@ -305,14 +310,21 @@ function throwing.register_bow(name, def)
|
|||||||
description = def.description,
|
description = def.description,
|
||||||
inventory_image = def.texture,
|
inventory_image = def.texture,
|
||||||
on_use = function(itemstack, user, pointed_thing)
|
on_use = function(itemstack, user, pointed_thing)
|
||||||
if not def.allow_shot(user, user:get_inventory():get_stack("main", user:get_wield_index()+1)) then
|
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
|
return itemstack
|
||||||
end
|
end
|
||||||
if shoot_arrow(itemstack, user, pointed_thing) then
|
if shoot_arrow(itemstack, user, def.throw_itself) then
|
||||||
if not minetest.setting_getbool("creative_mode") then
|
if not minetest.setting_getbool("creative_mode") then
|
||||||
itemstack:add_wear(65535/30)
|
itemstack:add_wear(65535/30)
|
||||||
end
|
end
|
||||||
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)
|
||||||
|
end
|
||||||
return itemstack
|
return itemstack
|
||||||
end,
|
end,
|
||||||
groups = def.groups
|
groups = def.groups
|
||||||
|
Loading…
Reference in New Issue
Block a user