@@ -34,57 +34,54 @@ local function scaleToDefault(player, field)
34
34
return current / max_display * nominal
35
35
end
36
36
37
- local function initialize_builtin_statbars (player )
38
-
39
- if not player :is_player () then
40
- return
41
- end
42
-
37
+ local function update_builtin_statbars (player )
43
38
local name = player :get_player_name ()
44
39
45
40
if name == " " then
46
41
return
47
42
end
48
43
44
+ local flags = player :hud_get_flags ()
49
45
if not hud_ids [name ] then
50
46
hud_ids [name ] = {}
51
47
-- flags are not transmitted to client on connect, we need to make sure
52
48
-- our current flags are transmitted by sending them actively
53
- player :hud_set_flags (player : hud_get_flags () )
49
+ player :hud_set_flags (flags )
54
50
end
55
51
local hud = hud_ids [name ]
56
52
57
- if player :hud_get_flags ().healthbar and enable_damage then
53
+ if flags .healthbar and enable_damage then
54
+ local number = scaleToDefault (player , " hp" )
58
55
if hud .id_healthbar == nil then
59
56
local hud_def = table .copy (health_bar_definition )
60
- hud_def .number = scaleToDefault ( player , " hp " )
57
+ hud_def .number = number
61
58
hud .id_healthbar = player :hud_add (hud_def )
59
+ else
60
+ player :hud_change (hud .id_healthbar , " number" , number )
62
61
end
63
- elseif hud .id_healthbar ~= nil then
62
+ elseif hud .id_healthbar then
64
63
player :hud_remove (hud .id_healthbar )
65
64
hud .id_healthbar = nil
66
65
end
67
66
68
67
local breath_max = player :get_properties ().breath_max
69
- if player : hud_get_flags () .breathbar and enable_damage and
68
+ if flags .breathbar and enable_damage and
70
69
player :get_breath () < breath_max then
70
+ local number = 2 * scaleToDefault (player , " breath" )
71
71
if hud .id_breathbar == nil then
72
72
local hud_def = table .copy (breath_bar_definition )
73
- hud_def .number = 2 * scaleToDefault ( player , " breath " )
73
+ hud_def .number = number
74
74
hud .id_breathbar = player :hud_add (hud_def )
75
+ else
76
+ player :hud_change (hud .id_breathbar , " number" , number )
75
77
end
76
- elseif hud .id_breathbar ~= nil then
78
+ elseif hud .id_breathbar then
77
79
player :hud_remove (hud .id_breathbar )
78
80
hud .id_breathbar = nil
79
81
end
80
82
end
81
83
82
84
local function cleanup_builtin_statbars (player )
83
-
84
- if not player :is_player () then
85
- return
86
- end
87
-
88
85
local name = player :get_player_name ()
89
86
90
87
if name == " " then
@@ -99,32 +96,28 @@ local function player_event_handler(player,eventname)
99
96
100
97
local name = player :get_player_name ()
101
98
102
- if name == " " then
99
+ if name == " " or not hud_ids [ name ] then
103
100
return
104
101
end
105
102
106
103
if eventname == " health_changed" then
107
- initialize_builtin_statbars (player )
104
+ update_builtin_statbars (player )
108
105
109
- if hud_ids [name ].id_healthbar ~= nil then
110
- player :hud_change (hud_ids [name ].id_healthbar ,
111
- " number" , scaleToDefault (player , " hp" ))
106
+ if hud_ids [name ].id_healthbar then
112
107
return true
113
108
end
114
109
end
115
110
116
111
if eventname == " breath_changed" then
117
- initialize_builtin_statbars (player )
112
+ update_builtin_statbars (player )
118
113
119
- if hud_ids [name ].id_breathbar ~= nil then
120
- player :hud_change (hud_ids [name ].id_breathbar ,
121
- " number" , 2 * scaleToDefault (player , " breath" ))
114
+ if hud_ids [name ].id_breathbar then
122
115
return true
123
116
end
124
117
end
125
118
126
119
if eventname == " hud_changed" then
127
- initialize_builtin_statbars (player )
120
+ update_builtin_statbars (player )
128
121
return true
129
122
end
130
123
@@ -133,20 +126,20 @@ end
133
126
134
127
function core .hud_replace_builtin (name , definition )
135
128
136
- if definition == nil or
137
- type (definition ) ~= " table" or
138
- definition .hud_elem_type ~= " statbar" then
129
+ if type (definition ) ~= " table" or
130
+ definition .hud_elem_type ~= " statbar" then
139
131
return false
140
132
end
141
133
142
134
if name == " health" then
143
135
health_bar_definition = definition
144
136
145
- for name ,ids in pairs (hud_ids ) do
137
+ for name , ids in pairs (hud_ids ) do
146
138
local player = core .get_player_by_name (name )
147
- if player and hud_ids [name ].id_healthbar then
148
- player :hud_remove (hud_ids [name ].id_healthbar )
149
- initialize_builtin_statbars (player )
139
+ if player and ids .id_healthbar then
140
+ player :hud_remove (ids .id_healthbar )
141
+ ids .id_healthbar = nil
142
+ update_builtin_statbars (player )
150
143
end
151
144
end
152
145
return true
@@ -155,11 +148,12 @@ function core.hud_replace_builtin(name, definition)
155
148
if name == " breath" then
156
149
breath_bar_definition = definition
157
150
158
- for name ,ids in pairs (hud_ids ) do
151
+ for name , ids in pairs (hud_ids ) do
159
152
local player = core .get_player_by_name (name )
160
- if player and hud_ids [name ].id_breathbar then
161
- player :hud_remove (hud_ids [name ].id_breathbar )
162
- initialize_builtin_statbars (player )
153
+ if player and ids .id_breathbar then
154
+ player :hud_remove (ids .id_breathbar )
155
+ ids .id_breathbar = nil
156
+ update_builtin_statbars (player )
163
157
end
164
158
end
165
159
return true
@@ -168,6 +162,10 @@ function core.hud_replace_builtin(name, definition)
168
162
return false
169
163
end
170
164
171
- core .register_on_joinplayer (initialize_builtin_statbars )
165
+ -- Append "update_builtin_statbars" as late as possible
166
+ -- This ensures that the HUD is hidden when the flags are updated in this callback
167
+ core .register_on_mods_loaded (function ()
168
+ core .register_on_joinplayer (update_builtin_statbars )
169
+ end )
172
170
core .register_on_leaveplayer (cleanup_builtin_statbars )
173
171
core .register_playerevent (player_event_handler )
0 commit comments