Skip to content

Commit 77168cb

Browse files
committed
fixes as per code review completed (2)
1 parent 1424a17 commit 77168cb

File tree

1 file changed

+84
-90
lines changed

1 file changed

+84
-90
lines changed

mods/bones/init.lua

Lines changed: 84 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
-- Load support for MT game translation.
77
local S = minetest.get_translator("bones")
88

9-
local theoretical_max_slots = minetest.settings:get("bones_max_slots") or ( 15 * 10 )
9+
local theoretical_max_slots = minetest.settings:get("bones_max_slots") or 15 * 10
1010
local dead_player_callbacks={}
1111

1212
bones = {}
@@ -21,10 +21,10 @@ end
2121

2222
local function get_bones_formspec_wh(cols,rows)
2323
return
24-
"size[" .. cols .. "," .. ( rows + 5 ) .. "]" ..
24+
"size[" .. cols .. "," .. (rows + 5) .. "]" ..
2525
"list[current_name;main;0,0.3;" .. cols .. "," .. rows .. ";]" ..
26-
"list[current_player;main;" .. ( ( cols - 8 ) / 2 ) .. "," .. rows .. ".85;8,1;]" ..
27-
"list[current_player;main;".. ( ( cols - 8 ) / 2 ) .. "," .. ( rows + 2 ) .. ".08;8,3;8]" ..
26+
"list[current_player;main;" .. ((cols - 8) / 2) .. "," .. rows .. ".85;8,1;]" ..
27+
"list[current_player;main;".. ((cols - 8) / 2) .. "," .. (rows + 2) .. ".08;8,3;8]" ..
2828
"listring[current_name;main]" ..
2929
"listring[current_player;main]" ..
3030
default.get_hotbar_bg(0,4.85)
@@ -37,10 +37,10 @@ local function get_bones_formspec_for_size(numitems)
3737
end
3838
--if we're over 4*8, but below 4*15 we make it 4 rows and adjust the column count to make everything fit
3939
if numitems <= 4 * 15 then
40-
return get_bones_formspec_wh( math.floor ( ( numitems + 3 ) / 4 ), 4)
40+
return get_bones_formspec_wh(math.floor((numitems + 3) / 4), 4)
4141
end
4242
--if we're over 4*15 we'll make 15 columns and adjust the row count to make everything fit
43-
return get_bones_formspec_wh(15, math.floor ( ( numitems + 14 ) / 15 ) )
43+
return get_bones_formspec_wh(15, math.floor ((numitems + 14) / 15))
4444
end
4545

4646
local share_bones_time = tonumber(minetest.settings:get("share_bones_time")) or 1200
@@ -199,71 +199,39 @@ end
199199
local player_inventory_lists = { "main", "craft" }
200200
bones.player_inventory_lists = player_inventory_lists
201201

202-
--functions registered this way won't becalled if bones_mode is keep
202+
--functions registered this way won't be called if bones_mode is keep
203203
function bones.register_dead_player_inv_management(func)
204204
table.insert(dead_player_callbacks, func)
205205
end
206206

207-
local function transfer_stack_to_bones(stk,current_dead_player)
208-
-- check if it's possible to place bones, if not find space near player
209-
if ( current_dead_player.bones_mode == "bones" ) and
210-
( current_dead_player.bones_pos == nil ) then
211-
current_dead_player.bones_pos = current_dead_player.player_pos
212-
local air
213-
if may_replace(current_dead_player.bones_pos, current_dead_player.player) then
214-
air = current_dead_player.bones_pos
215-
else
216-
air = minetest.find_node_near(current_dead_player.bones_pos, 1, {"air"})
217-
end
218-
219-
if air and not minetest.is_protected(air, current_dead_player.player_name) then
220-
current_dead_player.bones_pos = air
221-
local param2 = minetest.dir_to_facedir(current_dead_player.player:get_look_dir())
222-
minetest.set_node(current_dead_player.bones_pos, {name = "bones:bones", param2 = param2})
223-
local meta = minetest.get_meta(current_dead_player.bones_pos)
224-
current_dead_player.bones_inv = meta:get_inventory()
225-
--make it so big that anything reasonable will for sure fit inside
226-
current_dead_player.bones_inv:set_size("main", theoretical_max_slots)
227-
else
228-
current_dead_player.bones_mode = "drop"
229-
current_dead_player.bones_pos = nil
230-
end
231-
end
232-
233-
if ( current_dead_player.bones_mode == "bones" ) and
234-
( current_dead_player.bones_inv:room_for_item("main", stk) ) then
235-
current_dead_player.bones_inv:add_item("main", stk)
236-
else
237-
drop(current_dead_player.player_pos, stk)
238-
current_dead_player.dropped=true
239-
end
240-
end
241-
242-
local function player_dies_transfer_inventory(player,transfer_stack)
207+
local function player_dies_transfer_inventory(player)
208+
local result = {}
243209
local player_inv = player:get_inventory()
244210
for _, list_name in ipairs(player_inventory_lists) do
245211
for i = 1, player_inv:get_size(list_name) do
246-
local stack = player_inv:get_stack(list_name, i)
247-
transfer_stack(stack)
212+
table.insert(result, player_inv:get_stack(list_name, i))
248213
end
249214
player_inv:set_list(list_name, {})
250215
end
216+
return result
251217
end
252218

253219
bones.register_dead_player_inv_management(player_dies_transfer_inventory)
254220

255221
minetest.register_on_dieplayer(function(player)
256-
local pos = vector.round(player:get_pos())
222+
local player_pos = vector.round(player:get_pos())
257223
local bones_mode = minetest.settings:get("bones_mode") or "bones"
258224
if bones_mode ~= "bones" and bones_mode ~= "drop" and bones_mode ~= "keep" then
259225
bones_mode = "bones"
260226
end
261227
local player_name = player:get_player_name()
262-
local current_dead_player={player=player, player_name=player_name, bones_inv=nil, bones_pos=nil,
263-
bones_mode=bones_mode, player_pos=pos, dropped=false}
228+
local bones_inv = nil
229+
local bones_pos = nil
230+
local dropped = false
231+
local bones_meta = nil
264232

265233
local bones_position_message = minetest.settings:get_bool("bones_position_message") == true
266-
local pos_string = minetest.pos_to_string(pos)
234+
local pos_string = minetest.pos_to_string(player_pos)
267235

268236
-- return if keep inventory set or in creative mode
269237
if bones_mode == "keep" or minetest.is_creative_enabled(player_name) then
@@ -275,73 +243,99 @@ minetest.register_on_dieplayer(function(player)
275243
return
276244
end
277245

278-
local callback=function(stk)
279-
transfer_stack_to_bones(stk,current_dead_player)
280-
end
246+
for _, fun in ipairs(dead_player_callbacks) do
247+
local items = fun(player)
248+
for _, item in ipairs(items) do
249+
-- check if it's possible to place bones, if not find space near player
250+
if bones_mode == "bones" and bones_pos == nil then
251+
bones_pos = player_pos
252+
local air
253+
if may_replace(bones_pos, player) then
254+
air = bones_pos
255+
else
256+
air = minetest.find_node_near(bones_pos, 1, {"air"})
257+
end
258+
259+
if air and not minetest.is_protected(air, player_name) then
260+
bones_pos = air
261+
local param2 = minetest.dir_to_facedir(player:get_look_dir())
262+
minetest.set_node(bones_pos, {name = "bones:bones", param2 = param2})
263+
bones_meta = minetest.get_meta(bones_pos)
264+
bones_inv = bones_meta:get_inventory()
265+
--make it so big that anything reasonable will for sure fit inside
266+
bones_inv:set_size("main", theoretical_max_slots)
267+
else
268+
bones_mode = "drop"
269+
bones_pos = nil
270+
end
271+
end
281272

282-
for i=1,#dead_player_callbacks do
283-
local fun=dead_player_callbacks[i]
284-
fun(player,callback)
273+
if bones_mode == "bones" and bones_inv:room_for_item("main", item) then
274+
bones_inv:add_item("main", item)
275+
else
276+
drop(player_pos, item)
277+
dropped=true
278+
end
279+
end
285280
end
286281

287282
local bones_conclusion
283+
local public_conclusion
288284

289-
if not ( current_dead_player.bones_pos ) then
290-
drop(current_dead_player.player_pos, ItemStack("bones:bones"))
291-
if not ( current_dead_player.dropped ) then
292-
bones_conclusion="No bones placed"
293-
if bones_position_message then
294-
minetest.chat_send_player(player_name, S("@1 died at @2.", player_name, pos_string))
295-
end
285+
if not bones_pos then
286+
drop(player_pos, ItemStack("bones:bones"))
287+
if not dropped then
288+
bones_conclusion = "No bones placed"
289+
public_conclusion = S("@1 died at @2.", player_name, pos_string)
296290
else
297-
bones_conclusion="Inventory dropped"
298-
if bones_position_message then
299-
minetest.chat_send_player(player_name, S("@1 died at @2, and dropped their inventory.", player_name, pos_string, public_conclusion))
300-
end
291+
bones_conclusion = "Inventory dropped"
292+
public_conclusion = S("@1 died at @2, and dropped their inventory.", player_name, pos_string)
301293
end
302294
else
303-
if not ( current_dead_player.dropped ) then
304-
bones_conclusion="Bones placed"
305-
if bones_position_message then
306-
minetest.chat_send_player(player_name, S("@1 died at @2, and bones were placed.", player_name, pos_string, public_conclusion))
307-
end
295+
if not dropped then
296+
bones_conclusion = "Bones placed"
297+
public_conclusion = S("@1 died at @2, and bones were placed.", player_name, pos_string)
308298
else
309-
bones_conclusion="Inventory partially dropped"
310-
if bones_position_message then
311-
minetest.chat_send_player(player_name, S("@1 died at @2, and partially dropped their inventory.", player_name, pos_string, public_conclusion))
312-
end
299+
bones_conclusion = "Inventory partially dropped"
300+
public_conclusion = S("@1 died at @2, and partially dropped their inventory.", player_name, pos_string)
313301
end
314302
end
315303

316-
minetest.log("action", player_name .. " dies at " .. pos_string ..
317-
". " .. bones_conclusion)
304+
if bones_position_message then
305+
minetest.chat_send_player(player_name, public_conclusion)
306+
end
307+
308+
minetest.log("action", player_name .. " dies at " .. pos_string .. ". " .. bones_conclusion)
318309

319-
local inv = current_dead_player.bones_inv
320-
local inv_size = theoretical_max_slots
321-
if inv then
310+
if bones_inv then
311+
local inv_size = theoretical_max_slots
322312
for i = 1, theoretical_max_slots do
323-
local stack = inv:get_stack("main", i)
313+
local stack = bones_inv:get_stack("main", i)
324314
if stack:get_count() == 0 then
325315
inv_size = i - 1
326316
break
327317
end
328318
end
329-
local meta = minetest.get_meta(current_dead_player.bones_pos)
330-
meta:set_string("formspec", get_bones_formspec_for_size(inv_size))
331-
meta:set_string("owner", player_name)
319+
if inv_size <= 4 * 8 then
320+
bones_inv:set_size("main", 4 * 8)
321+
else
322+
bones_inv:set_size("main", inv_size)
323+
end
324+
bones_meta:set_string("formspec", get_bones_formspec_for_size(inv_size))
325+
bones_meta:set_string("owner", player_name)
332326

333327
if share_bones_time ~= 0 then
334-
meta:set_string("infotext", S("@1's fresh bones", player_name))
328+
bones_meta:set_string("infotext", S("@1's fresh bones", player_name))
335329

336-
if share_bones_time_early == 0 or not minetest.is_protected(pos, player_name) then
337-
meta:set_int("time", 0)
330+
if share_bones_time_early == 0 or not minetest.is_protected(bones_pos, player_name) then
331+
bones_meta:set_int("time", 0)
338332
else
339-
meta:set_int("time", (share_bones_time - share_bones_time_early))
333+
bones_meta:set_int("time", (share_bones_time - share_bones_time_early))
340334
end
341335

342-
minetest.get_node_timer(pos):start(10)
336+
minetest.get_node_timer(bones_pos):start(10)
343337
else
344-
meta:set_string("infotext", S("@1's bones", player_name))
338+
bones_meta:set_string("infotext", S("@1's bones", player_name))
345339
end
346340
end
347341
end)

0 commit comments

Comments
 (0)