Skip to content

Commit

Permalink
get metatable proxying of attached players table working
Browse files Browse the repository at this point in the history
  • Loading branch information
wsor4035 committed Sep 7, 2024
1 parent f03aaee commit 9abd19c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
12 changes: 12 additions & 0 deletions doc/player.md
Original file line number Diff line number Diff line change
@@ -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
13 changes: 12 additions & 1 deletion src/player/mineclonia.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 7 additions & 17 deletions src/player/minetest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9abd19c

Please sign in to comment.