From 08d4d548648a1b40ede638ba4fb0d5685f185d23 Mon Sep 17 00:00:00 2001 From: Juraj Vajda Date: Thu, 27 Oct 2022 15:59:47 -0400 Subject: [PATCH] cleanup and fallbacks --- api.lua | 113 +++++++++++++++++++++++-------------------- mod.conf | 2 +- types/xbows.type.lua | 1 + 3 files changed, 62 insertions(+), 54 deletions(-) diff --git a/api.lua b/api.lua index a5a28be..9bcf121 100644 --- a/api.lua +++ b/api.lua @@ -44,7 +44,8 @@ XBows = { settings = { 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 @@ -468,6 +469,7 @@ function XBows.load(self, itemstack, user, pointed_thing) local itemstack_arrow = quiver_result.found_arrow_stack if itemstack_arrow then + ---we got arrow from quiver local itemstack_arrow_meta = itemstack_arrow:get_meta() 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}) end - -- for i, st in ipairs(inv_list) do - -- local st_name = st:get_name() + ---if everything else fails + if self.fallback_quiver then + local inv_list = inv:get_list('main') - -- if not st:is_empty() and self.registered_arrows[st_name] then - -- local is_allowed_ammunition = self:is_allowed_ammunition(bow_name, st_name) + for i, st in ipairs(inv_list) do + local st_name = st:get_name() - -- if self.registered_arrows[st_name] and is_allowed_ammunition then - -- table.insert(itemstack_arrows, {stack = st, idx = i}) - -- end - -- end - -- end + if not st:is_empty() and self.registered_arrows[st_name] then + local _is_allowed_ammunition = self:is_allowed_ammunition(bow_name, st_name) + + if self.registered_arrows[st_name] and _is_allowed_ammunition then + table.insert(itemstack_arrows, {stack = st, idx = i}) + end + end + end + end -- take 1st found arrow in the list 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 - ---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 - -- if player_inv and player_inv:contains_item('main', 'x_bows:quiver') then - -- local inv_list = player_inv:get_list('main') + for i, st in ipairs(inv_list) do + if not st:is_empty() and st:get_name() == 'x_bows:quiver' then + 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 - -- if not st:is_empty() and st:get_name() == 'x_bows:quiver' then - -- local st_meta = st:get_meta() - -- local player_name = player:get_player_name() - -- quiver_id = st_meta:get_string('quiver_id') + local detached_inv = self:get_or_create_detached_inv( + quiver_id, + player_name, + st_meta:get_string('quiver_items') + ) - -- local detached_inv = self:get_or_create_detached_inv( - -- quiver_id, - -- player_name, - -- st_meta:get_string('quiver_items') - -- ) + if not detached_inv:is_empty('main') then + local detached_inv_list = detached_inv:get_list('main') - -- if not detached_inv:is_empty('main') then - -- local detached_inv_list = detached_inv:get_list('main') + ---find arrows inside quiver inventory + 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 - -- 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)) + 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()) - -- 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()) + if is_allowed_ammunition then + quiver_name = st:get_name() + found_arrow_stack = qst:take_item() + found_arrow_stack_idx = j - -- if is_allowed_ammunition then - -- quiver_name = st:get_name() - -- found_arrow_stack = qst:take_item() - -- found_arrow_stack_idx = j + if not self:is_creative(player_name) then + detached_inv:set_list('main', detached_inv_list) + self:save(detached_inv, player, true) + end + end + end + end + end + end - -- if not self:is_creative(player_name) then - -- detached_inv:set_list('main', detached_inv_list) - -- self:save(detached_inv, player, true) - -- end - -- end - -- end - -- end - -- end - -- end + if found_arrow_stack then + ---show HUD - quiver inventory + self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx) - -- if found_arrow_stack then - -- ---show HUD - quiver inventory - -- self:udate_or_create_hud(player, prev_detached_inv_list, found_arrow_stack_idx) - - -- break - -- end - -- end - -- end + break + end + end + end + end return { found_arrow_stack = found_arrow_stack, diff --git a/mod.conf b/mod.conf index fde9cfa..337a062 100644 --- a/mod.conf +++ b/mod.conf @@ -1,6 +1,6 @@ name = x_bows description = Adds bow and arrows to Minetest. 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 min_minetest_version = 5.4 diff --git a/types/xbows.type.lua b/types/xbows.type.lua index a231e04..0b58105 100644 --- a/types/xbows.type.lua +++ b/types/xbows.type.lua @@ -28,6 +28,7 @@ ---@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 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