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

113
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
if player_inv and player_inv:contains_item('main', 'x_bows:quiver') then
local inv_list = player_inv:get_list('main')
---find matching quiver item in players inventory with the open formspec name for i, st in ipairs(inv_list) do
-- if player_inv and player_inv:contains_item('main', 'x_bows:quiver') then if not st:is_empty() and st:get_name() == 'x_bows:quiver' then
-- local inv_list = player_inv:get_list('main') local st_meta = st:get_meta()
local player_name = player:get_player_name()
quiver_id = st_meta:get_string('quiver_id')
-- for i, st in ipairs(inv_list) do local detached_inv = self:get_or_create_detached_inv(
-- if not st:is_empty() and st:get_name() == 'x_bows:quiver' then quiver_id,
-- local st_meta = st:get_meta() player_name,
-- local player_name = player:get_player_name() st_meta:get_string('quiver_items')
-- quiver_id = st_meta:get_string('quiver_id') )
-- local detached_inv = self:get_or_create_detached_inv( if not detached_inv:is_empty('main') then
-- quiver_id, local detached_inv_list = detached_inv:get_list('main')
-- player_name,
-- st_meta:get_string('quiver_items')
-- )
-- if not detached_inv:is_empty('main') then ---find arrows inside quiver inventory
-- local detached_inv_list = detached_inv:get_list('main') for j, qst in ipairs(detached_inv_list) do
---save copy of inv list before we take the item
table.insert(prev_detached_inv_list, detached_inv:get_stack('main', j))
-- ---find arrows inside quiver inventory if not qst:is_empty() and not found_arrow_stack then
-- for j, qst in ipairs(detached_inv_list) do local is_allowed_ammunition = self:is_allowed_ammunition(wielded_stack:get_name(), qst:get_name())
-- ---save copy of inv list before we take the item
-- table.insert(prev_detached_inv_list, detached_inv:get_stack('main', j))
-- if not qst:is_empty() and not found_arrow_stack then if is_allowed_ammunition then
-- local is_allowed_ammunition = self:is_allowed_ammunition(wielded_stack:get_name(), qst:get_name()) quiver_name = st:get_name()
found_arrow_stack = qst:take_item()
found_arrow_stack_idx = j
-- if is_allowed_ammunition then if not self:is_creative(player_name) then
-- quiver_name = st:get_name() detached_inv:set_list('main', detached_inv_list)
-- found_arrow_stack = qst:take_item() self:save(detached_inv, player, true)
-- found_arrow_stack_idx = j end
end
end
end
end
end
-- if not self:is_creative(player_name) then if found_arrow_stack then
-- detached_inv:set_list('main', detached_inv_list) ---show HUD - quiver inventory
-- self:save(detached_inv, player, true) self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx)
-- end
-- end
-- end
-- end
-- end
-- end
-- if found_arrow_stack then break
-- ---show HUD - quiver inventory end
-- self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx) end
end
-- break 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