From 9abd19c43019d42caf74eee6339c0312b66f1fe5 Mon Sep 17 00:00:00 2001 From: wsor4035 <24964441+wsor4035@users.noreply.github.com> Date: Sat, 7 Sep 2024 18:13:44 -0400 Subject: [PATCH] get metatable proxying of attached players table working --- doc/player.md | 12 ++++++++++++ src/player/mineclonia.lua | 13 ++++++++++++- src/player/minetest.lua | 24 +++++++----------------- 3 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 doc/player.md diff --git a/doc/player.md b/doc/player.md new file mode 100644 index 0000000..bb81b6b --- /dev/null +++ b/doc/player.md @@ -0,0 +1,12 @@ +# Player API + +mimic mtg player_api + +## NOTE + +`xcompat.player.player_attached` + +read/write from it is fine, looping over it is not as it is a proxy table. s +would need lua5.2 __pairs/__ipairs metamethods support which i could polyfill +for using https://stackoverflow.com/a/77354254 but didnt feel like doing at +this time \ No newline at end of file diff --git a/src/player/mineclonia.lua b/src/player/mineclonia.lua index e8a8e66..baa73e3 100644 --- a/src/player/mineclonia.lua +++ b/src/player/mineclonia.lua @@ -24,6 +24,17 @@ function papi.set_animation(player, anim_name, speed) return mcl_player.player_set_animation(player, anim_name, speed) end ---todo: handle ignoring animations (maybe metatables?) +local metatable = { + __index = function (_, key) + return mcl_player.player_attached[key] + end, + __newindex = function (_, key, value) + rawset(mcl_player.player_attached, key, value) + end +} + +papi.player_attached = {} + +setmetatable(papi.player_attached, metatable) return papi \ No newline at end of file diff --git a/src/player/minetest.lua b/src/player/minetest.lua index 1182f8f..265598a 100644 --- a/src/player/minetest.lua +++ b/src/player/minetest.lua @@ -25,27 +25,17 @@ function papi.set_animation(player, anim_name, speed, loop) end -local papi_metatable = { - __newindex = function (table, key, value) - player_api.player_attached[key] = value - - rawset(table, key, value) - end -} - -local player_api_metatable = { - __newindex = function (table, key, value) - rawset(papi.player_attached, key, value) - rawset(table, key, value) +local metatable = { + __index = function (_, key) + return player_api.player_attached[key] + end, + __newindex = function (_, key, value) + rawset(player_api.player_attached, key, value) end } papi.player_attached = {} ---when a value is set in xcompat api, this will also set it in the player api -setmetatable(papi.player_attached, papi_metatable) - ---when a value is set in player api, this will also set it in the xcompat api -setmetatable(player_api.player_attached, player_api_metatable) +setmetatable(papi.player_attached, metatable) return papi \ No newline at end of file