diff --git a/init.lua b/init.lua index b213236..4a3ba05 100644 --- a/init.lua +++ b/init.lua @@ -2,46 +2,6 @@ local original_pos = {} minetest.register_privilege("watch", "Player can watch other players") -minetest.register_chatcommand("watch", { - params = "", - description = "watch a given player", - privs = {watch=true}, - func = function(name, param) - local watcher, target = nil, nil - watcher = minetest.get_player_by_name(name) - target = minetest.get_player_by_name(param:match("^([^ ]+)$")) - original_pos[watcher] = watcher:getpos() - local privs = minetest.get_player_privs(name) - - if target and watcher ~= target and default.player_attached[name] == false then - default.player_attached[name] = true - watcher:set_attach(target, "", {x=0, y=5, z=-20}, {x=0, y=0, z=0}) - watcher:set_eye_offset({x=0, y=5, z=-20},{x=0, y=0, z=0}) - watcher:set_nametag_attributes({color = {a=0}}) - - watcher:hud_set_flags({ - hotbar = false, - healthbar = false, - crosshair = false, - wielditem = false - }) - - watcher:set_properties({ - visual_size = {x=0, y=0}, - makes_footstep_sound = false, - collisionbox = {0, 0, 0, 0, 0, 0} - }) - - privs.interact = nil - minetest.set_player_privs(name, privs) - - return true, "Watching '"..param.."' at "..minetest.pos_to_string(vector.round(target:getpos())) - end - - return false, "Invalid parameters ('"..param.."') or you're already watching a player." - end -}) - local function unwatching(name) local watcher = nil watcher = minetest.get_player_by_name(name) @@ -72,13 +32,62 @@ local function unwatching(name) end if original_pos[watcher] then - watcher:setpos(original_pos[watcher]) + minetest.after(0.1, function() + watcher:setpos(original_pos[watcher]) + end) end - original_pos[watcher] = {} + minetest.after(0.2, function() + original_pos[watcher] = {} + end) end end +minetest.register_chatcommand("watch", { + params = "", + description = "watch a given player", + privs = {watch=true}, + func = function(name, param) + local watcher, target = nil, nil + watcher = minetest.get_player_by_name(name) + target = minetest.get_player_by_name(param:match("^([^ ]+)$")) + local privs = minetest.get_player_privs(name) + + if target and watcher ~= target then + if default.player_attached[name] == true then + unwatching(param) + else + original_pos[watcher] = watcher:getpos() + end + + default.player_attached[name] = true + watcher:set_attach(target, "", {x=0, y=5, z=-20}, {x=0, y=0, z=0}) + watcher:set_eye_offset({x=0, y=5, z=-20},{x=0, y=0, z=0}) + watcher:set_nametag_attributes({color = {a=0}}) + + watcher:hud_set_flags({ + hotbar = false, + healthbar = false, + crosshair = false, + wielditem = false + }) + + watcher:set_properties({ + visual_size = {x=0, y=0}, + makes_footstep_sound = false, + collisionbox = {0, 0, 0, 0, 0, 0} + }) + + privs.interact = nil + minetest.set_player_privs(name, privs) + + return true, "Watching '"..param.."' at "..minetest.pos_to_string(vector.round(target:getpos())) + end + + return false, "Invalid parameter ('"..param.."')." + end +}) + minetest.register_chatcommand("unwatch", { description = "unwatch a player", privs = {watch=true},