fixes as per code review completed (3)

This commit is contained in:
Imre Péntek 2023-05-12 09:37:26 +02:00
parent eeec7d6be0
commit 5141c96c01
1 changed files with 28 additions and 25 deletions

View File

@ -6,8 +6,10 @@
-- Load support for MT game translation. -- Load support for MT game translation.
local S = minetest.get_translator("bones") local S = minetest.get_translator("bones")
local theoretical_max_slots = minetest.settings:get("bones_max_slots") or 15 * 10 local bones_max_slots = minetest.settings:get("bones_max_slots") or 15 * 10
local dead_player_callbacks={} local dead_player_callbacks={}
-- we're going to display no less than 4*8 slots, we'll also provide at least 4*8 slots in bones
local min_inv_size = 4 * 8
bones = {} bones = {}
@ -32,15 +34,15 @@ end
local function get_bones_formspec_for_size(numitems) local function get_bones_formspec_for_size(numitems)
--the absolute minimum is 4*8 --the absolute minimum is 4*8
if numitems <= 4 * 8 then if numitems <= min_inv_size then
return get_bones_formspec_wh(8, 4) return get_bones_formspec_wh(8, 4)
end end
--if we're over 4*8, but below 4*15 we make it 4 rows and adjust the column count to make everything fit --if we're over 4*8, but below 4*15 we make it 4 rows and adjust the column count to make everything fit
if numitems <= 4 * 15 then if numitems <= 4 * 15 then
return get_bones_formspec_wh(math.floor((numitems + 3) / 4), 4) return get_bones_formspec_wh(math.ceil(numitems / 4), 4)
end end
--if we're over 4*15 we'll make 15 columns and adjust the row count to make everything fit --if we're over 4*15 we'll make 15 columns and adjust the row count to make everything fit
return get_bones_formspec_wh(15, math.floor ((numitems + 14) / 15)) return get_bones_formspec_wh(15, math.ceil(numitems / 15))
end end
local share_bones_time = tonumber(minetest.settings:get("share_bones_time")) or 1200 local share_bones_time = tonumber(minetest.settings:get("share_bones_time")) or 1200
@ -267,7 +269,7 @@ minetest.register_on_dieplayer(function(player)
bones_meta = minetest.get_meta(bones_pos) bones_meta = minetest.get_meta(bones_pos)
bones_inv = bones_meta:get_inventory() bones_inv = bones_meta:get_inventory()
--make it so big that anything reasonable will for sure fit inside --make it so big that anything reasonable will for sure fit inside
bones_inv:set_size("main", theoretical_max_slots) bones_inv:set_size("main", bones_max_slots)
else else
bones_mode = "drop" bones_mode = "drop"
bones_pos = nil bones_pos = nil
@ -283,45 +285,46 @@ minetest.register_on_dieplayer(function(player)
end end
end end
local bones_conclusion local log_message
local public_conclusion local chat_message
if not bones_pos then if bones_pos then
drop(player_pos, ItemStack("bones:bones")) if dropped then
if not dropped then log_message = "Inventory partially dropped"
bones_conclusion = "No bones placed" chat_message = "@1 died at @2, and partially dropped their inventory."
public_conclusion = S("@1 died at @2.", player_name, pos_string)
else else
bones_conclusion = "Inventory dropped" log_message = "Bones placed"
public_conclusion = S("@1 died at @2, and dropped their inventory.", player_name, pos_string) chat_message = "@1 died at @2, and bones were placed."
end end
else else
if not dropped then drop(player_pos, ItemStack("bones:bones"))
bones_conclusion = "Bones placed" if dropped then
public_conclusion = S("@1 died at @2, and bones were placed.", player_name, pos_string) log_message = "Inventory dropped"
chat_message = "@1 died at @2, and dropped their inventory."
else else
bones_conclusion = "Inventory partially dropped" log_message = "No bones placed"
public_conclusion = S("@1 died at @2, and partially dropped their inventory.", player_name, pos_string) chat_message = "@1 died at @2."
end end
end end
if bones_position_message then if bones_position_message then
minetest.chat_send_player(player_name, public_conclusion) chat_message = S(chat_message, player_name, pos_string)
minetest.chat_send_player(player_name, chat_message)
end end
minetest.log("action", player_name .. " dies at " .. pos_string .. ". " .. bones_conclusion) minetest.log("action", player_name .. " dies at " .. pos_string .. ". " .. log_message)
if bones_inv then if bones_inv then
local inv_size = theoretical_max_slots local inv_size = bones_max_slots
for i = 1, theoretical_max_slots do for i = 1, bones_max_slots do
local stack = bones_inv:get_stack("main", i) local stack = bones_inv:get_stack("main", i)
if stack:get_count() == 0 then if stack:get_count() == 0 then
inv_size = i - 1 inv_size = i - 1
break break
end end
end end
if inv_size <= 4 * 8 then if inv_size <= min_inv_size then
bones_inv:set_size("main", 4 * 8) bones_inv:set_size("main", min_inv_size)
else else
bones_inv:set_size("main", inv_size) bones_inv:set_size("main", inv_size)
end end