add empty/full quiver

This commit is contained in:
Juraj Vajda 2022-10-28 09:55:18 -04:00
parent aaeaf2671c
commit 191928ffdc
13 changed files with 92 additions and 27 deletions

71
api.lua
View File

@ -53,7 +53,8 @@ XBows.__index = XBows
---@type XBowsQuiver ---@type XBowsQuiver
XBowsQuiver = { XBowsQuiver = {
hud_item_ids = {}, hud_item_ids = {},
after_job = {} after_job = {},
quiver_empty_state = {}
} }
XBowsQuiver.__index = XBowsQuiver XBowsQuiver.__index = XBowsQuiver
setmetatable(XBowsQuiver, XBows) setmetatable(XBowsQuiver, XBows)
@ -604,6 +605,12 @@ function XBows.shoot(self, itemstack, user, pointed_thing)
if is_arrow_from_quiver == 1 then if is_arrow_from_quiver == 1 then
XBowsQuiver:udate_or_create_hud(user, detached_inv:get_list('main'), found_arrow_stack_idx) XBowsQuiver:udate_or_create_hud(user, detached_inv:get_list('main'), found_arrow_stack_idx)
if detached_inv:is_empty('main') then
XBowsQuiver:show_3d_quiver(user, {is_empty = true})
else
XBowsQuiver:show_3d_quiver(user)
end
else else
local inv = user:get_inventory()--[[@as InvRef]] local inv = user:get_inventory()--[[@as InvRef]]
if not inv:is_empty('x_bows:arrow_inv') then if not inv:is_empty('x_bows:arrow_inv') then
@ -1813,6 +1820,16 @@ function XBowsQuiver.get_or_create_detached_inv(self, quiver_id, player_name, qu
---@param stack ItemStack stack of item what was put ---@param stack ItemStack stack of item what was put
---@param player ObjectRef ---@param player ObjectRef
on_put = function(inv, listname, index, stack, player) on_put = function(inv, listname, index, stack, player)
local quiver_inv_st = player:get_inventory():get_stack('x_bows:quiver_inv', 1)
if quiver_inv_st and quiver_inv_st:get_meta():get_string('quiver_id') == inv:get_location().name then
if inv:is_empty('main') then
self:show_3d_quiver(player, {is_empty = true})
else
self:show_3d_quiver(player)
end
end
self:save(inv, player) self:save(inv, player)
end, end,
---@param inv InvRef detached inventory ---@param inv InvRef detached inventory
@ -1821,6 +1838,16 @@ function XBowsQuiver.get_or_create_detached_inv(self, quiver_id, player_name, qu
---@param stack ItemStack ---@param stack ItemStack
---@param player ObjectRef ---@param player ObjectRef
on_take = function(inv, listname, index, stack, player) on_take = function(inv, listname, index, stack, player)
local quiver_inv_st = player:get_inventory():get_stack('x_bows:quiver_inv', 1)
if quiver_inv_st and quiver_inv_st:get_meta():get_string('quiver_id') == inv:get_location().name then
if inv:is_empty('main') then
self:show_3d_quiver(player, {is_empty = true})
else
self:show_3d_quiver(player)
end
end
self:save(inv, player) self:save(inv, player)
end, end,
}, player_name) }, player_name)
@ -2206,21 +2233,33 @@ function XBowsQuiver.ui_register_page(self)
}) })
end end
function XBows.show_3d_quiver(self, player) function XBowsQuiver.show_3d_quiver(self, player, props)
local _props = props or {}
local p_name = player:get_player_name() local p_name = player:get_player_name()
local quiver_texture = 'x_bows_quiver_mesh.png'
local player_textures local player_textures
if _props.is_empty then
quiver_texture = 'x_bows_quiver_empty_mesh.png'
end
if self._3d_armor then if self._3d_armor then
minetest.after(0.1, function() minetest.after(0.1, function()
player_textures = { player_textures = {
armor.textures[p_name].skin, armor.textures[p_name].skin,
armor.textures[p_name].armor, armor.textures[p_name].armor,
armor.textures[p_name].wielditem, armor.textures[p_name].wielditem,
'x_bows_quiver_mesh.png' quiver_texture
} }
if player_textures then if player_textures then
if _props.is_empty and not self.quiver_empty_state[player:get_player_name()] then
self.quiver_empty_state[player:get_player_name()] = true
player_api.set_textures(player, player_textures) player_api.set_textures(player, player_textures)
elseif not _props.is_empty and self.quiver_empty_state[player:get_player_name()] then
self.quiver_empty_state[player:get_player_name()] = false
player_api.set_textures(player, player_textures)
end
end end
end) end)
@ -2230,34 +2269,40 @@ function XBows.show_3d_quiver(self, player)
player_textures = { player_textures = {
u_skin_texture .. '.png', u_skin_texture .. '.png',
'x_bows_quiver_mesh.png' quiver_texture
} }
elseif self.wardrobe and wardrobe.playerSkins and wardrobe.playerSkins[p_name] then elseif self.wardrobe and wardrobe.playerSkins and wardrobe.playerSkins[p_name] then
player_textures = { player_textures = {
wardrobe.playerSkins[p_name], wardrobe.playerSkins[p_name],
'x_bows_quiver_mesh.png' quiver_texture
} }
else else
local textures = player_api.get_textures(player) local textures = player_api.get_textures(player)
---cleanup ---cleanup
for index, value in ipairs(textures) do for index, value in ipairs(textures) do
if value == 'x_bows_quiver_empty_mesh.png' or value == 'x_bows_quiver_mesh.png' then if value == 'x_bows_quiver_blank_mesh.png' or value == 'x_bows_quiver_mesh.png' or value == 'x_bows_quiver_empty_mesh.png' then
table.remove(textures, index) table.remove(textures, index)
end end
end end
table.insert(textures, 'x_bows_quiver_mesh.png') table.insert(textures, quiver_texture)
player_textures = textures player_textures = textures
end end
if player_textures then if player_textures then
if _props.is_empty and not self.quiver_empty_state[player:get_player_name()] then
self.quiver_empty_state[player:get_player_name()] = true
player_api.set_textures(player, player_textures)
elseif not _props.is_empty and self.quiver_empty_state[player:get_player_name()] then
self.quiver_empty_state[player:get_player_name()] = false
player_api.set_textures(player, player_textures) player_api.set_textures(player, player_textures)
end end
end end
end
function XBows.hide_3d_quiver(self, player) function XBowsQuiver.hide_3d_quiver(self, player)
local p_name = player:get_player_name() local p_name = player:get_player_name()
local player_textures local player_textures
@ -2267,7 +2312,7 @@ function XBows.hide_3d_quiver(self, player)
armor.textures[p_name].skin, armor.textures[p_name].skin,
armor.textures[p_name].armor, armor.textures[p_name].armor,
armor.textures[p_name].wielditem, armor.textures[p_name].wielditem,
'x_bows_quiver_empty_mesh.png' 'x_bows_quiver_blank_mesh.png'
} }
if player_textures then if player_textures then
@ -2282,24 +2327,24 @@ function XBows.hide_3d_quiver(self, player)
player_textures = { player_textures = {
u_skin_texture .. '.png', u_skin_texture .. '.png',
'x_bows_quiver_empty_mesh.png' 'x_bows_quiver_blank_mesh.png'
} }
elseif self.wardrobe and wardrobe.playerSkins and wardrobe.playerSkins[p_name] then elseif self.wardrobe and wardrobe.playerSkins and wardrobe.playerSkins[p_name] then
player_textures = { player_textures = {
wardrobe.playerSkins[p_name], wardrobe.playerSkins[p_name],
'x_bows_quiver_empty_mesh.png' 'x_bows_quiver_blank_mesh.png'
} }
else else
local textures = player_api.get_textures(player) local textures = player_api.get_textures(player)
---cleanup ---cleanup
for index, value in ipairs(textures) do for index, value in ipairs(textures) do
if value == 'x_bows_quiver_mesh.png' or value == 'x_bows_quiver_empty_mesh.png' then if value == 'x_bows_quiver_mesh.png' or value == 'x_bows_quiver_blank_mesh.png' or value == 'x_bows_quiver_empty_mesh.png' then
table.remove(textures, index) table.remove(textures, index)
end end
end end
table.insert(textures, 'x_bows_quiver_empty_mesh.png') table.insert(textures, 'x_bows_quiver_blank_mesh.png')
player_textures = textures player_textures = textures
end end

View File

@ -20,7 +20,6 @@ dofile(path .. '/particle_effects.lua')
dofile(path .. '/nodes.lua') dofile(path .. '/nodes.lua')
dofile(path .. '/arrow.lua') dofile(path .. '/arrow.lua')
dofile(path .. '/items.lua') dofile(path .. '/items.lua')
dofile(path .. '/quiver.lua')
if XBows.i3 then if XBows.i3 then
XBowsQuiver:i3_register_page() XBowsQuiver:i3_register_page()
@ -50,17 +49,23 @@ minetest.register_on_joinplayer(function(player)
local quiver_id = st_meta:get_string('quiver_id') local quiver_id = st_meta:get_string('quiver_id')
---create detached inventory ---create detached inventory
XBowsQuiver:get_or_create_detached_inv( local detached_inv = XBowsQuiver:get_or_create_detached_inv(
quiver_id, quiver_id,
player:get_player_name(), player:get_player_name(),
st_meta:get_string('quiver_items') st_meta:get_string('quiver_items')
) )
---set model textures ---set model textures
XBows:show_3d_quiver(player) if detached_inv:is_empty('main') then
XBowsQuiver.quiver_empty_state[player:get_player_name()] = false
XBowsQuiver:show_3d_quiver(player, {is_empty = true})
else
XBowsQuiver.quiver_empty_state[player:get_player_name()] = true
XBowsQuiver:show_3d_quiver(player)
end
else else
---set model textures ---set model textures
XBows:hide_3d_quiver(player) XBowsQuiver:hide_3d_quiver(player)
end end
XBows:reset_charged_bow(player, true) XBows:reset_charged_bow(player, true)
@ -207,7 +212,7 @@ minetest.register_on_player_inventory_action(function(player, action, inventory,
inventory:set_stack(inventory_info.to_list, inventory_info.to_index, stack) inventory:set_stack(inventory_info.to_list, inventory_info.to_index, stack)
end end
XBowsQuiver:get_or_create_detached_inv( local detached_inv = XBowsQuiver:get_or_create_detached_inv(
quiver_id, quiver_id,
player:get_player_name(), player:get_player_name(),
st_meta:get_string('quiver_items') st_meta:get_string('quiver_items')
@ -222,7 +227,13 @@ minetest.register_on_player_inventory_action(function(player, action, inventory,
end end
---set player visual ---set player visual
XBows:show_3d_quiver(player) if detached_inv:is_empty('main') then
XBowsQuiver.quiver_empty_state[player:get_player_name()] = false
XBowsQuiver:show_3d_quiver(player, {is_empty = true})
else
XBowsQuiver.quiver_empty_state[player:get_player_name()] = true
XBowsQuiver:show_3d_quiver(player)
end
elseif action == 'move' and inventory_info.from_list == 'x_bows:quiver_inv' then elseif action == 'move' and inventory_info.from_list == 'x_bows:quiver_inv' then
local stack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index) local stack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index)
@ -236,7 +247,7 @@ minetest.register_on_player_inventory_action(function(player, action, inventory,
---set player visual ---set player visual
if stack:is_empty() then if stack:is_empty() then
XBows:hide_3d_quiver(player) XBowsQuiver:hide_3d_quiver(player)
end end
elseif action == 'put' and inventory_info.listname == 'x_bows:quiver_inv' then elseif action == 'put' and inventory_info.listname == 'x_bows:quiver_inv' then
if XBows.i3 then if XBows.i3 then
@ -257,6 +268,12 @@ minetest.register_on_player_inventory_action(function(player, action, inventory,
end end
end) end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
if player and fields.quit then
XBowsQuiver:close_quiver(player, formname)
end
end)
---backwards compatibility ---backwards compatibility
minetest.register_alias('x_bows:arrow_diamond_tipped_poison', 'x_bows:arrow_diamond') minetest.register_alias('x_bows:arrow_diamond_tipped_poison', 'x_bows:arrow_diamond')

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +0,0 @@
minetest.register_on_player_receive_fields(function(player, formname, fields)
if player and fields.quit then
XBowsQuiver:close_quiver(player, formname)
end
end)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 236 B

After

Width:  |  Height:  |  Size: 232 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 128 B

After

Width:  |  Height:  |  Size: 124 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 70 B

After

Width:  |  Height:  |  Size: 303 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 333 B

After

Width:  |  Height:  |  Size: 338 B

View File

@ -5,6 +5,9 @@
---@field mesecons string|nil ---@field mesecons string|nil
---@field playerphysics string|nil ---@field playerphysics string|nil
---@field player_monoids string|nil ---@field player_monoids string|nil
---@field u_skins string|nil
---@field wardrobe string|nil
---@field _3d_armor string|nil
---@field registered_bows table<string, ItemDef|BowItemDefCustom> ---@field registered_bows table<string, ItemDef|BowItemDefCustom>
---@field registered_arrows table<string, ItemDef|ArrowItemDefCustom> ---@field registered_arrows table<string, ItemDef|ArrowItemDefCustom>
---@field registered_quivers table<string, ItemDef|QuiverItemDefCustom> ---@field registered_quivers table<string, ItemDef|QuiverItemDefCustom>
@ -47,6 +50,11 @@
---@field get_string_from_inv fun(self: XBowsQuiver, inv: InvRef): {['inv_string']: string, ['content_description']: string} Convert inventory of itemstacks to serialized string ---@field get_string_from_inv fun(self: XBowsQuiver, inv: InvRef): {['inv_string']: string, ['content_description']: string} Convert inventory of itemstacks to serialized string
---@field set_string_to_inv fun(self: XBowsQuiver, inv: InvRef, str: string): nil Set items from serialized string to inventory ---@field set_string_to_inv fun(self: XBowsQuiver, inv: InvRef, str: string): nil Set items from serialized string to inventory
---@field quiver_can_allow fun(self: XBowsQuiver, inv: InvRef, player: ObjectRef): boolean Check if we are allowing actions in the correct quiver inventory ---@field quiver_can_allow fun(self: XBowsQuiver, inv: InvRef, player: ObjectRef): boolean Check if we are allowing actions in the correct quiver inventory
---@field show_3d_quiver fun(self: XBowsQuiver, player: ObjectRef, props?: {["is_empty"]: boolean|nil}): nil Applies full/empty quiver textures
---@field hide_3d_quiver fun(self: XBowsQuiver, player: ObjectRef): nil Applies blank quiver textures - hiding the quiver
---@field sfinv_register_page fun(): nil register inventoy mod page
---@field i3_register_page fun(): nil register inventoy mod page
---@field ui_register_page fun(): nil register inventoy mod page
---Custom field in ParticlespawnerDef ---Custom field in ParticlespawnerDef