2015-12-08 07:28:41 -08:00
|
|
|
local original_pos = {}
|
|
|
|
|
2020-04-30 15:34:45 -07:00
|
|
|
minetest.register_privilege("watch", {
|
|
|
|
description = "Player can watch other players",
|
|
|
|
give_to_singleplayer = false,
|
|
|
|
give_to_admin = true,
|
|
|
|
})
|
2015-12-08 07:28:41 -08:00
|
|
|
|
2017-03-09 01:23:42 -08:00
|
|
|
local function toggle_hud_flags(player, bool)
|
|
|
|
local flags = player:hud_get_flags()
|
|
|
|
local new_hud_flags = {}
|
|
|
|
|
|
|
|
for flag in pairs(flags) do
|
|
|
|
new_hud_flags[flag] = bool
|
|
|
|
end
|
|
|
|
|
|
|
|
player:hud_set_flags(new_hud_flags)
|
|
|
|
end
|
|
|
|
|
2015-12-22 07:49:07 -08:00
|
|
|
local function unwatching(name)
|
2015-12-25 05:35:06 -08:00
|
|
|
local watcher = minetest.get_player_by_name(name)
|
2015-12-22 07:49:07 -08:00
|
|
|
local privs = minetest.get_player_privs(name)
|
|
|
|
|
|
|
|
if watcher and default.player_attached[name] == true then
|
|
|
|
watcher:set_detach()
|
2020-04-30 15:28:00 -07:00
|
|
|
player_api.player_attached[name] = false
|
2017-03-09 01:23:42 -08:00
|
|
|
watcher:set_eye_offset(vector.new(), vector.new())
|
|
|
|
watcher:set_nametag_attributes({color = {a = 255, r = 255, g = 255, b = 255}})
|
|
|
|
|
|
|
|
toggle_hud_flags(watcher, true)
|
2015-12-22 07:49:07 -08:00
|
|
|
|
|
|
|
watcher:set_properties({
|
2017-03-09 01:23:42 -08:00
|
|
|
visual_size = {x = 1, y = 1},
|
2015-12-22 07:49:07 -08:00
|
|
|
makes_footstep_sound = true,
|
2020-07-15 11:34:52 -07:00
|
|
|
collisionbox = {-0.3, 0.0, -0.3, 0.3, 1.7, 0.3}
|
2015-12-22 07:49:07 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
if not privs.interact and privs.watch == true then
|
|
|
|
privs.interact = true
|
|
|
|
minetest.set_player_privs(name, privs)
|
|
|
|
end
|
|
|
|
|
2016-08-05 10:51:05 -07:00
|
|
|
local pos = original_pos[watcher]
|
|
|
|
if pos then
|
2020-04-30 15:28:00 -07:00
|
|
|
-- set_pos seems to be very unreliable
|
2016-08-05 10:51:05 -07:00
|
|
|
-- this workaround helps though
|
2015-12-22 07:49:07 -08:00
|
|
|
minetest.after(0.1, function()
|
2020-04-30 15:28:00 -07:00
|
|
|
watcher:set_pos(pos)
|
2015-12-22 07:49:07 -08:00
|
|
|
end)
|
2016-08-05 10:51:05 -07:00
|
|
|
original_pos[watcher] = nil
|
2015-12-22 07:49:07 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-08 07:28:41 -08:00
|
|
|
minetest.register_chatcommand("watch", {
|
|
|
|
params = "<to_name>",
|
2020-04-30 15:28:00 -07:00
|
|
|
description = "Watch a given player",
|
2017-03-09 01:23:42 -08:00
|
|
|
privs = {watch = true},
|
2015-12-08 07:28:41 -08:00
|
|
|
func = function(name, param)
|
2015-12-25 05:35:06 -08:00
|
|
|
local watcher = minetest.get_player_by_name(name)
|
2016-01-02 08:20:50 -08:00
|
|
|
local target = minetest.get_player_by_name(param)
|
2015-12-08 09:39:06 -08:00
|
|
|
local privs = minetest.get_player_privs(name)
|
2015-12-08 07:28:41 -08:00
|
|
|
|
2015-12-22 07:49:07 -08:00
|
|
|
if target and watcher ~= target then
|
2020-04-30 15:28:00 -07:00
|
|
|
if player_api.player_attached[name] == true then
|
2015-12-22 07:49:07 -08:00
|
|
|
unwatching(param)
|
|
|
|
else
|
2020-04-30 15:28:00 -07:00
|
|
|
original_pos[watcher] = watcher:get_pos()
|
2015-12-22 07:49:07 -08:00
|
|
|
end
|
2017-03-08 21:20:27 -08:00
|
|
|
|
2020-04-30 15:28:00 -07:00
|
|
|
player_api.player_attached[name] = true
|
2017-03-09 01:23:42 -08:00
|
|
|
watcher:set_attach(target, "", vector.new(0, -5, -20), vector.new())
|
2017-03-30 08:00:18 -07:00
|
|
|
watcher:set_eye_offset(vector.new(0, -5, -20), vector.new())
|
2017-03-09 01:23:42 -08:00
|
|
|
watcher:set_nametag_attributes({color = {a = 0}})
|
|
|
|
|
|
|
|
toggle_hud_flags(watcher, true)
|
2015-12-08 07:28:41 -08:00
|
|
|
|
|
|
|
watcher:set_properties({
|
2017-03-09 01:23:42 -08:00
|
|
|
visual_size = {x = 0, y = 0},
|
2015-12-08 07:28:41 -08:00
|
|
|
makes_footstep_sound = false,
|
2015-12-25 05:35:06 -08:00
|
|
|
collisionbox = {0}
|
2015-12-08 07:28:41 -08:00
|
|
|
})
|
|
|
|
|
2015-12-08 09:39:06 -08:00
|
|
|
privs.interact = nil
|
|
|
|
minetest.set_player_privs(name, privs)
|
|
|
|
|
2017-03-09 01:23:42 -08:00
|
|
|
return true, "Watching '" .. param .. "' at "..
|
2020-04-30 15:28:00 -07:00
|
|
|
minetest.pos_to_string(vector.round(target:get_pos()))
|
2015-12-08 07:28:41 -08:00
|
|
|
end
|
|
|
|
|
2017-03-09 01:23:42 -08:00
|
|
|
return false, "Invalid parameter ('" .. param .. "')."
|
2015-12-08 07:28:41 -08:00
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2015-12-08 13:02:19 -08:00
|
|
|
minetest.register_chatcommand("unwatch", {
|
2020-04-30 15:28:00 -07:00
|
|
|
description = "Unwatch a player",
|
2015-12-08 13:02:19 -08:00
|
|
|
privs = {watch=true},
|
2017-03-08 21:20:27 -08:00
|
|
|
func = function(name, param)
|
2015-12-08 13:02:19 -08:00
|
|
|
unwatching(name)
|
2015-12-08 07:28:41 -08:00
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2015-12-08 09:39:06 -08:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
2015-12-08 13:02:19 -08:00
|
|
|
unwatching(name)
|
2015-12-08 09:39:06 -08:00
|
|
|
end)
|