add basic target selector

This commit is contained in:
AFCMS 2021-03-31 17:16:59 +02:00
parent 0c96ff672d
commit f9a03a3c98
1 changed files with 46 additions and 0 deletions

View File

@ -99,6 +99,15 @@ mcl_commands.types = {
return S("Player doesn't exist.")
end
end},
target = {"([A-Za-z0-9-_]+)",
function(res, pointer)
--if minetest.player_exists(res[pointer]) then
if res[pointer] then
return nil, res[pointer], pointer+1
else
return S("Player doesn't exist.")
end
end},
json = {"(.+)", --FIXME
function(res, pointer)
local parsed = parse_json(res[pointer])
@ -361,3 +370,40 @@ function mcl_commands.rename_chatcommand(newname, cmd, bypass)
minetest.log("action", "[mcl_commands] ["..cmd.."] command not renamed to ["..newname.."]")
end
end
function mcl_commands.get_target_selector(target, pos, name)
if minetest.player_exists(target) then
return minetest.get_player_by_name(target)
elseif target == "@a" then
return nil, minetest.get_connected_players()
elseif target == "@r" then
local connected = minetest.get_connected_players()
return nil, connected[math.random(1, #connected)]
elseif target == "@s" then
if name then
local player = minetest.get_player_by_name(name)
end
if player then
return nil, player
else
return S("Not a valid player")
end
elseif target == "@p" then
local smallest = math.huge
local nearest
for _,player in pairs(minetest.get_connected_players()) do
local distance = vector.distance(pos, player:get_pos())
if distance < min_distance then
min_distance = distance
nearest = player
end
end
if nearest then
return nil, nearest
else
return S("No player online")
end
elseif target == "@e" then
return minetest.luaentities --TODO: add filtering of not valid entities.
end
end