Compatibility with minetest_game’s sethome

If sethome is present, we use its API for
getting/setting homes, otherwise we use i3’s own.
This commit is contained in:
Jaidyn Ann 2024-01-09 23:57:05 -06:00
parent 58218fb30e
commit 4202980019
2 changed files with 31 additions and 13 deletions

View File

@ -132,18 +132,28 @@ local function inv_fields(player, data, fields)
elseif fields.sort then
sort_inventory(player, data)
elseif fields.home then
if not data.home then
return msg(name, "No home set")
elseif not check_privs(name, {home = true}) then
elseif (fields.home or fields.set_home) and not check_privs(name, {home = true}) then
return msg(name, "'home' privilege missing")
elseif fields.home then
if sethome then
if not sethome.go(name) then
return msg(name, "No home set")
end
elseif not data.home then
return msg(name, "No home set")
else
safe_teleport(player, str_to_pos(data.home))
end
safe_teleport(player, str_to_pos(data.home))
msg(name, S"Welcome back home!")
elseif fields.set_home then
if sethome then
sethome.set(name, player:get_pos())
else
data.home = pos_to_str(player:get_pos(), 1)
end
elseif fields.bag_rename then
data.bag_rename = true

View File

@ -569,7 +569,7 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa
end
end
local function show_settings(fs, data)
local function show_settings(fs, data, player)
if data.confirm_trash then
image(2.8, 10.65, 4.6, 0.7, PNG.bg_goto)
label(3.02, 11, "Confirm trash?")
@ -608,8 +608,16 @@ local function show_settings(fs, data)
if show_home then
local coords, c, str = {"X", "Y", "Z"}, 0, ES"No home set"
if data.home then
str = data.home:gsub(",", " "):sub(2,-2):gsub("%.%d", ""):gsub(
local home = data.home
if sethome then
-- i3 stores home coordinates with one decimal of precision, as the blow fmt() statement
-- assumes. So we need to trim sethomes coordinates to one decimal, likewise.
local home_pos = sethome.get(player:get_player_name())
home = string.format("(%.1f,%.1f,%.1f)", home_pos.x, home_pos.y, home_pos.z)
end
if home then
str = home:gsub(",", " "):sub(2,-2):gsub("%.%d", ""):gsub(
"(%-?%d+)", function(a)
c++
return fmt("<b>%s: <style color=%s font=mono>%s</style></b>",
@ -682,7 +690,7 @@ local function show_settings(fs, data)
end
end
local function get_footer(fs, data)
local function get_footer(fs, data, player)
local btn = {
{"trash", ES"Clear inventory"},
{"sort", ES"Sort inventory"},
@ -698,10 +706,10 @@ local function get_footer(fs, data)
fs("tooltip[%s;%s;#32333899;#fff]", btn_name, tooltip)
end
show_settings(fs, data)
show_settings(fs, data, player)
end
local function get_slots(fs, data)
local function get_slots(fs, data, player)
local legacy_inventory = data.legacy_inventory
local hotbar_len = data.hotbar_len
local inv_x = legacy_inventory and 0.23 or 0.22
@ -725,7 +733,7 @@ local function get_slots(fs, data)
fs"listring[current_player;craft]listring[current_player;main]"
get_footer(fs, data)
get_footer(fs, data, player)
end
local function get_inventory_fs(player, data, fs)
@ -1777,7 +1785,7 @@ local function make_fs(player, data)
end
if tab.slots then
get_slots(fs, data)
get_slots(fs, data, player)
end
end