Aaron Suen ee99c24d52 More aggressive priv caching strategy
Instead of caching privs only for the current tick, and only for
NodeCore itself, change the base API to use caching universally,
including for naive mod code.  Privs are cached until invalidated
by a change; it's assumed here that the "auth_reload" command
exists in the first place because this kind of caching is allowed
and accounted for, even if it's not actually done by the engine
or builtin.
2022-10-07 06:59:04 -04:00

47 lines
1.5 KiB
Lua

-- LUALOCALS < ---------------------------------------------------------
local getmetatable, minetest, nodecore, string, type, vector
= getmetatable, minetest, nodecore, string, type, vector
local string_format
= string.format
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
local keepname = "keepinv"
minetest.register_privilege(keepname, {
description = "Allow player to keep inventory on teleport",
give_to_singleplayer = false,
give_to_admin = false
})
local function patchplayers()
local anyplayer = (minetest.get_connected_players())[1]
if not anyplayer then
return minetest.after(0, patchplayers)
end
local meta = getmetatable(anyplayer)
meta = meta and meta.__index or meta
if not meta.set_pos then return end
local setraw = meta.set_pos
function meta:set_pos(pos, ...)
if (not self) or (not self.is_player) or (not self:is_player())
or (not pos) or type(pos) ~= "table" or pos.keepinv
or minetest.get_player_privs(self)[keepname] then
return setraw(self, pos, ...)
end
local old = self:get_pos()
if old and vector.distance(pos, old) > 16 then
nodecore.log("action", string_format("%s teleports from %s to %s",
self:get_player_name(), minetest.pos_to_string(old, 0),
minetest.pos_to_string(pos, 0)))
nodecore.inventory_dump(self)
end
return setraw(self, pos, ...)
end
nodecore.log("info", modname .. " player:set_pos hooked")
end
minetest.after(0, patchplayers)