-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added player-local context and /luaclear command
- Loading branch information
1 parent
d1ac137
commit 63d0d60
Showing
3 changed files
with
118 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
local PrintablePos = | ||
{ | ||
__tostring = function(pos) | ||
return "(" .. pos.x .. ", " .. pos.y .. ", " .. pos.z .. ")"; | ||
end | ||
}; | ||
function PrintablePos:new(pos) | ||
return setmetatable(pos, self); | ||
end | ||
|
||
local PlayerEnv = | ||
{ | ||
playerContexts = setmetatable({}, { __mode = "k" }), -- weak keys | ||
|
||
playerFuncs = | ||
{ | ||
me = function(player) | ||
return player; | ||
end, | ||
|
||
myname = function(player) | ||
return player:get_player_name(); | ||
end, | ||
|
||
here = function(player) | ||
return PrintablePos:new(player:getpos()); | ||
end, | ||
|
||
print = function(player) | ||
return function(...) | ||
local str = ""; | ||
for _, arg in ipairs({...}) do | ||
str = str .. tostring(arg); | ||
end | ||
minetest.chat_send_player(player:get_player_name(), str, false); | ||
end | ||
end | ||
}; | ||
}; | ||
|
||
function PlayerEnv:new(player) | ||
local env = {}; | ||
self.playerContexts[env] = player; | ||
setmetatable(env, self); | ||
return env; | ||
end | ||
setmetatable(PlayerEnv, { __call = PlayerEnv.new }); | ||
|
||
function PlayerEnv.__index(env, key) | ||
local playerFunc = PlayerEnv.playerFuncs[key]; | ||
if playerFunc then | ||
local player = PlayerEnv.playerContexts[env]; | ||
return playerFunc(player); | ||
else | ||
return _G[key]; | ||
end | ||
end | ||
|
||
function PlayerEnv.__newindex(env, key, value) | ||
if PlayerEnv.playerFuncs[key] then | ||
error("cannot set special '"..key.."' player variable"); | ||
end | ||
rawset(env, key, value); | ||
end | ||
|
||
return PlayerEnv; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters