mirror of
https://repo.or.cz/minetest_hudbars.git
synced 2024-11-28 01:53:46 +01:00
Fix possible crash after player left
This commit is contained in:
parent
893ba4e577
commit
6c0c617b39
17
API.md
17
API.md
@ -95,8 +95,7 @@ the HUD bar will be initially be shown to the player.
|
||||
* `start_hidden`: Whether the HUD bar is initially hidden. This is optional, `default_start_hidden` of the registration function will be used as default
|
||||
|
||||
#### Return value
|
||||
Always `nil`.
|
||||
|
||||
`true` on success, `false` otherwise.
|
||||
|
||||
|
||||
## Modifying a HUD bar
|
||||
@ -135,7 +134,7 @@ such network optimization for the “styling” parameters, so keep this in mind
|
||||
* `new_text_color`: A 3-octet number defining the new color of the text.
|
||||
|
||||
#### Return value
|
||||
Always `nil`.
|
||||
`true` on success, `false` otherwise.
|
||||
|
||||
|
||||
## Hiding and unhiding a HUD bar
|
||||
@ -156,7 +155,7 @@ Hides the specified HUD bar from the screen of the specified player.
|
||||
* `identifier`: The identifier of the HUD bar type to hide, as specified in `hb.register_hudbar`.
|
||||
|
||||
#### Return value
|
||||
Always `nil`.
|
||||
`true` on success, `false` otherwise.
|
||||
|
||||
|
||||
### `hb.unhide_hudbar(player, identifier)`
|
||||
@ -167,7 +166,7 @@ Makes a previously hidden HUD bar visible again to a player.
|
||||
* `identifier`: The identifier of the HUD bar type to unhide, as specified in `hb.register_hudbar`.
|
||||
|
||||
#### Return value
|
||||
Always `nil`.
|
||||
`true` on success, `false` otherwise.
|
||||
|
||||
|
||||
## Reading HUD bar information
|
||||
@ -181,12 +180,14 @@ Returns the current state of the active player's HUD bar.
|
||||
* `identifier`: The identifier of the HUD bar type to hide, as specified in `hb.register_hudbar`.
|
||||
|
||||
#### Return value
|
||||
A table which holds information on the current state of the HUD bar. Note the table is a deep
|
||||
copy of the internal HUD bar state, it is *not* a reference; the information hold by the table is
|
||||
only true for the moment you called this function. The fields of this table are:
|
||||
On success, returns a table which holds information on the current state of the HUD bar. Note
|
||||
the table is a deep copy of the internal HUD bar state, it is *not* a reference; the information
|
||||
hold by the table is only true for the moment you called this function. The fields of this table are:
|
||||
|
||||
* `value`: Current value of HUD bar.
|
||||
* `max`: Current maximum value of HUD bar.
|
||||
* `hidden`: Boolean denoting whether the HUD bar is hidden.
|
||||
* `barlength`: The length of the HUD bar in pixels. This field is meaningless if the HUD bar is currently hidden.
|
||||
* `text`: The text shown on the HUD bar. This fiels is meaningless if the HUD bar is currently hidden.
|
||||
|
||||
If the player does not exist, returns `nil` instead.
|
||||
|
23
init.lua
23
init.lua
@ -90,6 +90,10 @@ else
|
||||
hb.settings.sorting_reverse = { [0] = "health", [1] = "breath" }
|
||||
end
|
||||
|
||||
local function player_exists(player)
|
||||
return player ~= nil and player:is_player()
|
||||
end
|
||||
|
||||
-- Table which contains all players with active default HUD bars (only for internal use)
|
||||
hb.players = {}
|
||||
|
||||
@ -285,13 +289,18 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
||||
end
|
||||
|
||||
function hb.init_hudbar(player, identifier, start_value, start_max, start_hidden)
|
||||
if not player_exists(player) then return false end
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
hb.hudtables[identifier].add_all(player, hudtable, start_value, start_max, start_hidden)
|
||||
return true
|
||||
end
|
||||
|
||||
function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon, new_bgicon, new_bar, new_label, new_text_color)
|
||||
if new_value == nil and new_max_value == nil and new_icon == nil and new_bgicon == nil and new_bar == nil and new_label == nil and new_text_color == nil then
|
||||
return
|
||||
return true
|
||||
end
|
||||
if not player_exists(player) then
|
||||
return false
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
@ -379,11 +388,14 @@ function hb.change_hudbar(player, identifier, new_value, new_max_value, new_icon
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function hb.hide_hudbar(player, identifier)
|
||||
if not player_exists(player) then return false end
|
||||
local name = player:get_player_name()
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
if hudtable == nil then return false end
|
||||
if(hudtable.hudstate[name].hidden == false) then
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
if hudtable.hudids[name].icon ~= nil then
|
||||
@ -397,11 +409,14 @@ function hb.hide_hudbar(player, identifier)
|
||||
player:hud_change(hudtable.hudids[name].bar, "number", 0)
|
||||
hudtable.hudstate[name].hidden = true
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function hb.unhide_hudbar(player, identifier)
|
||||
if not player_exists(player) then return false end
|
||||
local name = player:get_player_name()
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
if hudtable == nil then return false end
|
||||
if(hudtable.hudstate[name].hidden) then
|
||||
local value = hudtable.hudstate[name].value
|
||||
local max = hudtable.hudstate[name].max
|
||||
@ -419,9 +434,11 @@ function hb.unhide_hudbar(player, identifier)
|
||||
player:hud_change(hudtable.hudids[name].bar, "number", hb.value_to_barlength(value, max))
|
||||
hudtable.hudstate[name].hidden = false
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function hb.get_hudbar_state(player, identifier)
|
||||
if not player_exists(player) then return nil end
|
||||
local ref = hb.get_hudtable(identifier).hudstate[player:get_player_name()]
|
||||
-- Do not forget to update this chunk of code in case the state changes
|
||||
local copy = {
|
||||
@ -467,6 +484,7 @@ end
|
||||
|
||||
-- update built-in HUD bars
|
||||
local function update_hud(player)
|
||||
if not player_exists() then return end
|
||||
if minetest.setting_getbool("enable_damage") then
|
||||
if hb.settings.forceload_default_hudbars then
|
||||
hb.unhide_hudbar(player, "health")
|
||||
@ -510,7 +528,8 @@ minetest.register_globalstep(function(dtime)
|
||||
if minetest.setting_getbool("enable_damage") or hb.settings.forceload_default_hudbars then
|
||||
for _, player in pairs(hb.players) do
|
||||
-- update all hud elements
|
||||
update_hud(player)
|
||||
minetest.after(1,function(player) update_hud(player) end, player)
|
||||
-- update_hud(player)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user