Skip to content

Commit 54539fb

Browse files
committed
fixes as per code review completed (2)
1 parent 63f6abe commit 54539fb

File tree

2 files changed

+86
-94
lines changed

2 files changed

+86
-94
lines changed

game_api.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ e.g. `table.insert(bones.player_inventory_lists, "backpack")`
8585

8686
Additionally, callbacks can be registered to transfer items into the bones on death:
8787

88-
`bones.register_dead_player_inv_management(function(player,callback){})`
88+
`bones.register_dead_player_inv_management(function(player){})`
8989

90-
In the above functions the provided callback should be used to add items to the bones inventory:
91-
92-
`callback(stack)`
90+
the registered function is supposed to return with a table of items to be transferred.
9391

9492
please note that the inventory these items were taken from still need to be disposed of.
9593

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 = {}
@@ -46,10 +46,10 @@ end
4646

4747
local function get_bones_formspec_wh(cols,rows)
4848
return
49-
"size[" .. cols .. "," .. ( rows + 5 ) .. "]" ..
49+
"size[" .. cols .. "," .. (rows + 5) .. "]" ..
5050
"list[current_name;main;0,0.3;" .. cols .. "," .. rows .. ";]" ..
51-
"list[current_player;main;" .. ( ( cols - 8 ) / 2 ) .. "," .. rows .. ".85;8,1;]" ..
52-
"list[current_player;main;".. ( ( cols - 8 ) / 2 ) .. "," .. ( rows + 2 ) .. ".08;8,3;8]" ..
51+
"list[current_player;main;" .. ((cols - 8) / 2) .. "," .. rows .. ".85;8,1;]" ..
52+
"list[current_player;main;".. ((cols - 8) / 2) .. "," .. (rows + 2) .. ".08;8,3;8]" ..
5353
"listring[current_name;main]" ..
5454
"listring[current_player;main]" ..
5555
default.get_hotbar_bg(0,4.85)
@@ -63,10 +63,10 @@ local function get_bones_formspec_for_size(numitems)
6363
end
6464
--if we're over 4*8, but below 4*15 we make it 4 rows and adjust the column count to make everything fit
6565
if numitems <= 4 * 15 then
66-
return get_bones_formspec_wh( math.floor ( ( numitems + 3 ) / 4 ), 4)
66+
return get_bones_formspec_wh(math.floor((numitems + 3) / 4), 4)
6767
end
6868
--if we're over 4*15 we'll make 15 columns and adjust the row count to make everything fit
69-
return get_bones_formspec_wh(15, math.floor ( ( numitems + 14 ) / 15 ) )
69+
return get_bones_formspec_wh(15, math.floor ((numitems + 14) / 15))
7070
end
7171

7272

@@ -224,71 +224,39 @@ end
224224
local player_inventory_lists = { "main", "craft" }
225225
bones.player_inventory_lists = player_inventory_lists
226226

227-
--functions registered this way won't becalled if bones_mode is keep
227+
--functions registered this way won't be called if bones_mode is keep
228228
function bones.register_dead_player_inv_management(func)
229229
table.insert(dead_player_callbacks, func)
230230
end
231231

232-
local function transfer_stack_to_bones(stk,current_dead_player)
233-
-- check if it's possible to place bones, if not find space near player
234-
if ( current_dead_player.bones_mode == "bones" ) and
235-
( current_dead_player.bones_pos == nil ) then
236-
current_dead_player.bones_pos = current_dead_player.player_pos
237-
local air
238-
if may_replace(current_dead_player.bones_pos, current_dead_player.player) then
239-
air = current_dead_player.bones_pos
240-
else
241-
air = minetest.find_node_near(current_dead_player.bones_pos, 1, {"air"})
242-
end
243-
244-
if air and not minetest.is_protected(air, current_dead_player.player_name) then
245-
current_dead_player.bones_pos = air
246-
local param2 = minetest.dir_to_facedir(current_dead_player.player:get_look_dir())
247-
minetest.set_node(current_dead_player.bones_pos, {name = "bones:bones", param2 = param2})
248-
local meta = minetest.get_meta(current_dead_player.bones_pos)
249-
current_dead_player.bones_inv = meta:get_inventory()
250-
--make it so big that anything reasonable will for sure fit inside
251-
current_dead_player.bones_inv:set_size("main", theoretical_max_slots)
252-
else
253-
current_dead_player.bones_mode = "drop"
254-
current_dead_player.bones_pos = nil
255-
end
256-
end
257-
258-
if ( current_dead_player.bones_mode == "bones" ) and
259-
( current_dead_player.bones_inv:room_for_item("main", stk) ) then
260-
current_dead_player.bones_inv:add_item("main", stk)
261-
else
262-
drop(current_dead_player.player_pos, stk)
263-
current_dead_player.dropped=true
264-
end
265-
end
266-
267-
local function player_dies_transfer_inventory(player,transfer_stack)
232+
local function player_dies_transfer_inventory(player)
233+
local result = {}
268234
local player_inv = player:get_inventory()
269235
for _, list_name in ipairs(player_inventory_lists) do
270236
for i = 1, player_inv:get_size(list_name) do
271-
local stack = player_inv:get_stack(list_name, i)
272-
transfer_stack(stack)
237+
table.insert(result, player_inv:get_stack(list_name, i))
273238
end
274239
player_inv:set_list(list_name, {})
275240
end
241+
return result
276242
end
277243

278244
bones.register_dead_player_inv_management(player_dies_transfer_inventory)
279245

280246
minetest.register_on_dieplayer(function(player)
281-
local pos = vector.round(player:get_pos())
247+
local player_pos = vector.round(player:get_pos())
282248
local bones_mode = minetest.settings:get("bones_mode") or "bones"
283249
if bones_mode ~= "bones" and bones_mode ~= "drop" and bones_mode ~= "keep" then
284250
bones_mode = "bones"
285251
end
286252
local player_name = player:get_player_name()
287-
local current_dead_player={player=player, player_name=player_name, bones_inv=nil, bones_pos=nil,
288-
bones_mode=bones_mode, player_pos=pos, dropped=false}
253+
local bones_inv = nil
254+
local bones_pos = nil
255+
local dropped = false
256+
local bones_meta = nil
289257

290258
local bones_position_message = minetest.settings:get_bool("bones_position_message") == true
291-
local pos_string = minetest.pos_to_string(pos)
259+
local pos_string = minetest.pos_to_string(player_pos)
292260

293261
-- return if keep inventory set or in creative mode
294262
if bones_mode == "keep" or minetest.is_creative_enabled(player_name) then
@@ -300,73 +268,99 @@ minetest.register_on_dieplayer(function(player)
300268
return
301269
end
302270

303-
local callback=function(stk)
304-
transfer_stack_to_bones(stk,current_dead_player)
305-
end
271+
for _, fun in ipairs(dead_player_callbacks) do
272+
local items = fun(player)
273+
for _, item in ipairs(items) do
274+
-- check if it's possible to place bones, if not find space near player
275+
if bones_mode == "bones" and bones_pos == nil then
276+
bones_pos = player_pos
277+
local air
278+
if may_replace(bones_pos, player) then
279+
air = bones_pos
280+
else
281+
air = minetest.find_node_near(bones_pos, 1, {"air"})
282+
end
283+
284+
if air and not minetest.is_protected(air, player_name) then
285+
bones_pos = air
286+
local param2 = minetest.dir_to_facedir(player:get_look_dir())
287+
minetest.set_node(bones_pos, {name = "bones:bones", param2 = param2})
288+
bones_meta = minetest.get_meta(bones_pos)
289+
bones_inv = bones_meta:get_inventory()
290+
--make it so big that anything reasonable will for sure fit inside
291+
bones_inv:set_size("main", theoretical_max_slots)
292+
else
293+
bones_mode = "drop"
294+
bones_pos = nil
295+
end
296+
end
306297

307-
for i=1,#dead_player_callbacks do
308-
local fun=dead_player_callbacks[i]
309-
fun(player,callback)
298+
if bones_mode == "bones" and bones_inv:room_for_item("main", item) then
299+
bones_inv:add_item("main", item)
300+
else
301+
drop(player_pos, item)
302+
dropped=true
303+
end
304+
end
310305
end
311306

312307
local bones_conclusion
308+
local public_conclusion
313309

314-
if not ( current_dead_player.bones_pos ) then
315-
drop(current_dead_player.player_pos, ItemStack("bones:bones"))
316-
if not ( current_dead_player.dropped ) then
317-
bones_conclusion="No bones placed"
318-
if bones_position_message then
319-
minetest.chat_send_player(player_name, S("@1 died at @2.", player_name, pos_string))
320-
end
310+
if not bones_pos then
311+
drop(player_pos, ItemStack("bones:bones"))
312+
if not dropped then
313+
bones_conclusion = "No bones placed"
314+
public_conclusion = S("@1 died at @2.", player_name, pos_string)
321315
else
322-
bones_conclusion="Inventory dropped"
323-
if bones_position_message then
324-
minetest.chat_send_player(player_name, S("@1 died at @2, and dropped their inventory.", player_name, pos_string, public_conclusion))
325-
end
316+
bones_conclusion = "Inventory dropped"
317+
public_conclusion = S("@1 died at @2, and dropped their inventory.", player_name, pos_string)
326318
end
327319
else
328-
if not ( current_dead_player.dropped ) then
329-
bones_conclusion="Bones placed"
330-
if bones_position_message then
331-
minetest.chat_send_player(player_name, S("@1 died at @2, and bones were placed.", player_name, pos_string, public_conclusion))
332-
end
320+
if not dropped then
321+
bones_conclusion = "Bones placed"
322+
public_conclusion = S("@1 died at @2, and bones were placed.", player_name, pos_string)
333323
else
334-
bones_conclusion="Inventory partially dropped"
335-
if bones_position_message then
336-
minetest.chat_send_player(player_name, S("@1 died at @2, and partially dropped their inventory.", player_name, pos_string, public_conclusion))
337-
end
324+
bones_conclusion = "Inventory partially dropped"
325+
public_conclusion = S("@1 died at @2, and partially dropped their inventory.", player_name, pos_string)
338326
end
339327
end
340328

341-
minetest.log("action", player_name .. " dies at " .. pos_string ..
342-
". " .. bones_conclusion)
329+
if bones_position_message then
330+
minetest.chat_send_player(player_name, public_conclusion)
331+
end
332+
333+
minetest.log("action", player_name .. " dies at " .. pos_string .. ". " .. bones_conclusion)
343334

344-
local inv = current_dead_player.bones_inv
345-
local inv_size = theoretical_max_slots
346-
if inv then
335+
if bones_inv then
336+
local inv_size = theoretical_max_slots
347337
for i = 1, theoretical_max_slots do
348-
local stack = inv:get_stack("main", i)
338+
local stack = bones_inv:get_stack("main", i)
349339
if stack:get_count() == 0 then
350340
inv_size = i - 1
351341
break
352342
end
353343
end
354-
local meta = minetest.get_meta(current_dead_player.bones_pos)
355-
meta:set_string("formspec", get_bones_formspec_for_size(inv_size))
356-
meta:set_string("owner", player_name)
344+
if inv_size <= 4 * 8 then
345+
bones_inv:set_size("main", 4 * 8)
346+
else
347+
bones_inv:set_size("main", inv_size)
348+
end
349+
bones_meta:set_string("formspec", get_bones_formspec_for_size(inv_size))
350+
bones_meta:set_string("owner", player_name)
357351

358352
if share_bones_time ~= 0 then
359-
meta:set_string("infotext", S("@1's fresh bones", player_name))
353+
bones_meta:set_string("infotext", S("@1's fresh bones", player_name))
360354

361-
if share_bones_time_early == 0 or not minetest.is_protected(pos, player_name) then
362-
meta:set_int("time", 0)
355+
if share_bones_time_early == 0 or not minetest.is_protected(bones_pos, player_name) then
356+
bones_meta:set_int("time", 0)
363357
else
364-
meta:set_int("time", (share_bones_time - share_bones_time_early))
358+
bones_meta:set_int("time", (share_bones_time - share_bones_time_early))
365359
end
366360

367-
minetest.get_node_timer(pos):start(10)
361+
minetest.get_node_timer(bones_pos):start(10)
368362
else
369-
meta:set_string("infotext", S("@1's bones", player_name))
363+
bones_meta:set_string("infotext", S("@1's bones", player_name))
370364
end
371365
end
372366
end)

0 commit comments

Comments
 (0)