cleanup and fallbacks

This commit is contained in:
Juraj Vajda 2022-10-27 15:59:47 -04:00
parent cf759edfec
commit 08d4d54864
3 changed files with 62 additions and 54 deletions

111
api.lua
View File

@ -44,7 +44,8 @@ XBows = {
settings = { settings = {
x_bows_attach_arrows_to_entities = minetest.settings:get_bool('x_bows_attach_arrows_to_entities', false) x_bows_attach_arrows_to_entities = minetest.settings:get_bool('x_bows_attach_arrows_to_entities', false)
}, },
charge_sound_after_job = {} charge_sound_after_job = {},
fallback_quiver = not minetest.global_exists('sfinv') and not minetest.global_exists('unified_inventory') and not minetest.global_exists('i3')
} }
XBows.__index = XBows XBows.__index = XBows
@ -468,6 +469,7 @@ function XBows.load(self, itemstack, user, pointed_thing)
local itemstack_arrow = quiver_result.found_arrow_stack local itemstack_arrow = quiver_result.found_arrow_stack
if itemstack_arrow then if itemstack_arrow then
---we got arrow from quiver
local itemstack_arrow_meta = itemstack_arrow:get_meta() local itemstack_arrow_meta = itemstack_arrow:get_meta()
itemstack_arrow_meta:set_int('is_arrow_from_quiver', 1) itemstack_arrow_meta:set_int('is_arrow_from_quiver', 1)
@ -485,17 +487,22 @@ function XBows.load(self, itemstack, user, pointed_thing)
table.insert(itemstack_arrows, {stack = arrow_stack, idx = 1}) table.insert(itemstack_arrows, {stack = arrow_stack, idx = 1})
end end
-- for i, st in ipairs(inv_list) do ---if everything else fails
-- local st_name = st:get_name() if self.fallback_quiver then
local inv_list = inv:get_list('main')
-- if not st:is_empty() and self.registered_arrows[st_name] then for i, st in ipairs(inv_list) do
-- local is_allowed_ammunition = self:is_allowed_ammunition(bow_name, st_name) local st_name = st:get_name()
-- if self.registered_arrows[st_name] and is_allowed_ammunition then if not st:is_empty() and self.registered_arrows[st_name] then
-- table.insert(itemstack_arrows, {stack = st, idx = i}) local _is_allowed_ammunition = self:is_allowed_ammunition(bow_name, st_name)
-- end
-- end if self.registered_arrows[st_name] and _is_allowed_ammunition then
-- end table.insert(itemstack_arrows, {stack = st, idx = i})
end
end
end
end
-- take 1st found arrow in the list -- take 1st found arrow in the list
itemstack_arrow = #itemstack_arrows > 0 and itemstack_arrows[1].stack or nil itemstack_arrow = #itemstack_arrows > 0 and itemstack_arrows[1].stack or nil
@ -1503,58 +1510,58 @@ function XBowsQuiver.get_itemstack_arrow_from_quiver(self, player)
end end
end end
---check arrows inventory slot if self.fallback_quiver then
---find matching quiver item in players inventory with the open formspec name ---find matching quiver item in players inventory with the open formspec name
-- if player_inv and player_inv:contains_item('main', 'x_bows:quiver') then if player_inv and player_inv:contains_item('main', 'x_bows:quiver') then
-- local inv_list = player_inv:get_list('main') local inv_list = player_inv:get_list('main')
-- for i, st in ipairs(inv_list) do for i, st in ipairs(inv_list) do
-- if not st:is_empty() and st:get_name() == 'x_bows:quiver' then if not st:is_empty() and st:get_name() == 'x_bows:quiver' then
-- local st_meta = st:get_meta() local st_meta = st:get_meta()
-- local player_name = player:get_player_name() local player_name = player:get_player_name()
-- quiver_id = st_meta:get_string('quiver_id') quiver_id = st_meta:get_string('quiver_id')
-- local detached_inv = self:get_or_create_detached_inv( local detached_inv = self:get_or_create_detached_inv(
-- quiver_id, quiver_id,
-- player_name, player_name,
-- st_meta:get_string('quiver_items') st_meta:get_string('quiver_items')
-- ) )
-- if not detached_inv:is_empty('main') then if not detached_inv:is_empty('main') then
-- local detached_inv_list = detached_inv:get_list('main') local detached_inv_list = detached_inv:get_list('main')
-- ---find arrows inside quiver inventory ---find arrows inside quiver inventory
-- for j, qst in ipairs(detached_inv_list) do for j, qst in ipairs(detached_inv_list) do
-- ---save copy of inv list before we take the item ---save copy of inv list before we take the item
-- table.insert(prev_detached_inv_list, detached_inv:get_stack('main', j)) table.insert(prev_detached_inv_list, detached_inv:get_stack('main', j))
-- if not qst:is_empty() and not found_arrow_stack then if not qst:is_empty() and not found_arrow_stack then
-- local is_allowed_ammunition = self:is_allowed_ammunition(wielded_stack:get_name(), qst:get_name()) local is_allowed_ammunition = self:is_allowed_ammunition(wielded_stack:get_name(), qst:get_name())
-- if is_allowed_ammunition then if is_allowed_ammunition then
-- quiver_name = st:get_name() quiver_name = st:get_name()
-- found_arrow_stack = qst:take_item() found_arrow_stack = qst:take_item()
-- found_arrow_stack_idx = j found_arrow_stack_idx = j
-- if not self:is_creative(player_name) then if not self:is_creative(player_name) then
-- detached_inv:set_list('main', detached_inv_list) detached_inv:set_list('main', detached_inv_list)
-- self:save(detached_inv, player, true) self:save(detached_inv, player, true)
-- end end
-- end end
-- end end
-- end end
-- end end
-- end end
-- if found_arrow_stack then if found_arrow_stack then
-- ---show HUD - quiver inventory ---show HUD - quiver inventory
-- self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx) self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx)
-- break break
-- end end
-- end end
-- end end
end
return { return {
found_arrow_stack = found_arrow_stack, found_arrow_stack = found_arrow_stack,

View File

@ -1,6 +1,6 @@
name = x_bows name = x_bows
description = Adds bow and arrows to Minetest. description = Adds bow and arrows to Minetest.
depends = depends =
optional_depends = default, farming, 3d_armor, mesecons, playerphysics, player_monoids, wool, i3, unified_inventory, simple_skins, u_skins, wardrobe optional_depends = default, farming, 3d_armor, mesecons, playerphysics, player_monoids, wool, i3, unified_inventory, simple_skins, u_skins, wardrobe, sfinv
supported_games = minetest_game supported_games = minetest_game
min_minetest_version = 5.4 min_minetest_version = 5.4

View File

@ -28,6 +28,7 @@
---@field register_particle_effect fun(self: XBows, name: string, def: ParticlespawnerDef|ParticlespawnerDefCustom): nil Add new particle to XBow registration ---@field register_particle_effect fun(self: XBows, name: string, def: ParticlespawnerDef|ParticlespawnerDefCustom): nil Add new particle to XBow registration
---@field open_quiver fun(self: XBowsQuiver, itemstack: ItemStack, user: ObjectRef): ItemStack Open quiver ---@field open_quiver fun(self: XBowsQuiver, itemstack: ItemStack, user: ObjectRef): ItemStack Open quiver
---@field uuid fun(): string Creates UUID ---@field uuid fun(): string Creates UUID
---@field fallback_quiver boolean If no invenotory mod is detected then fallback solution will be used
---XBowsQuiver class extended from XBows ---XBowsQuiver class extended from XBows