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

75
api.lua
View File

@ -53,7 +53,8 @@ XBows.__index = XBows
---@type XBowsQuiver
XBowsQuiver = {
hud_item_ids = {},
after_job = {}
after_job = {},
quiver_empty_state = {}
}
XBowsQuiver.__index = XBowsQuiver
setmetatable(XBowsQuiver, XBows)
@ -604,6 +605,12 @@ function XBows.shoot(self, itemstack, user, pointed_thing)
if is_arrow_from_quiver == 1 then
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
local inv = user:get_inventory()--[[@as InvRef]]
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 player ObjectRef
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)
end,
---@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 player ObjectRef
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)
end,
}, player_name)
@ -2206,21 +2233,33 @@ function XBowsQuiver.ui_register_page(self)
})
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 quiver_texture = 'x_bows_quiver_mesh.png'
local player_textures
if _props.is_empty then
quiver_texture = 'x_bows_quiver_empty_mesh.png'
end
if self._3d_armor then
minetest.after(0.1, function()
player_textures = {
armor.textures[p_name].skin,
armor.textures[p_name].armor,
armor.textures[p_name].wielditem,
'x_bows_quiver_mesh.png'
quiver_texture
}
if player_textures then
player_api.set_textures(player, player_textures)
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)
end
end
end)
@ -2230,34 +2269,40 @@ function XBows.show_3d_quiver(self, player)
player_textures = {
u_skin_texture .. '.png',
'x_bows_quiver_mesh.png'
quiver_texture
}
elseif self.wardrobe and wardrobe.playerSkins and wardrobe.playerSkins[p_name] then
player_textures = {
wardrobe.playerSkins[p_name],
'x_bows_quiver_mesh.png'
quiver_texture
}
else
local textures = player_api.get_textures(player)
---cleanup
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)
end
end
table.insert(textures, 'x_bows_quiver_mesh.png')
table.insert(textures, quiver_texture)
player_textures = textures
end
if player_textures then
player_api.set_textures(player, player_textures)
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)
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 player_textures
@ -2267,7 +2312,7 @@ function XBows.hide_3d_quiver(self, player)
armor.textures[p_name].skin,
armor.textures[p_name].armor,
armor.textures[p_name].wielditem,
'x_bows_quiver_empty_mesh.png'
'x_bows_quiver_blank_mesh.png'
}
if player_textures then
@ -2282,24 +2327,24 @@ function XBows.hide_3d_quiver(self, player)
player_textures = {
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
player_textures = {
wardrobe.playerSkins[p_name],
'x_bows_quiver_empty_mesh.png'
'x_bows_quiver_blank_mesh.png'
}
else
local textures = player_api.get_textures(player)
---cleanup
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)
end
end
table.insert(textures, 'x_bows_quiver_empty_mesh.png')
table.insert(textures, 'x_bows_quiver_blank_mesh.png')
player_textures = textures
end

View File

@ -20,7 +20,6 @@ dofile(path .. '/particle_effects.lua')
dofile(path .. '/nodes.lua')
dofile(path .. '/arrow.lua')
dofile(path .. '/items.lua')
dofile(path .. '/quiver.lua')
if XBows.i3 then
XBowsQuiver:i3_register_page()
@ -50,17 +49,23 @@ minetest.register_on_joinplayer(function(player)
local quiver_id = st_meta:get_string('quiver_id')
---create detached inventory
XBowsQuiver:get_or_create_detached_inv(
local detached_inv = XBowsQuiver:get_or_create_detached_inv(
quiver_id,
player:get_player_name(),
st_meta:get_string('quiver_items')
)
---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
---set model textures
XBows:hide_3d_quiver(player)
XBowsQuiver:hide_3d_quiver(player)
end
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)
end
XBowsQuiver:get_or_create_detached_inv(
local detached_inv = XBowsQuiver:get_or_create_detached_inv(
quiver_id,
player:get_player_name(),
st_meta:get_string('quiver_items')
@ -222,7 +227,13 @@ minetest.register_on_player_inventory_action(function(player, action, inventory,
end
---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
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
if stack:is_empty() then
XBows:hide_3d_quiver(player)
XBowsQuiver:hide_3d_quiver(player)
end
elseif action == 'put' and inventory_info.listname == 'x_bows:quiver_inv' then
if XBows.i3 then
@ -257,6 +268,12 @@ minetest.register_on_player_inventory_action(function(player, action, inventory,
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
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 playerphysics 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_arrows table<string, ItemDef|ArrowItemDefCustom>
---@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 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 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