using Oxide.Game.Rust.Cui;
using System.Globalization;
using System.Linq;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Spectate", "YaMang -w-", "1.0.1")]
[Description("https://discord.gg/DTQuEE7neZ")]
internal class Spectate : RustPlugin
{
#region Hook
void OnPlayerRespawned(BasePlayer player)
{
CuiHelper.DestroyUi(player, "SpectateUI");
}
void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
CuiHelper.DestroyUi(player, "SpectateUI");
}
}
#endregion
#region Commands
[ChatCommand("spectate")]
private void SpectateCMD(BasePlayer player, string command, string[] args)
{
if(!permission.UserHasPermission(player.UserIDString, "spectate.use"))
{
PrintToChat(player, $"You don't have Permission");
return;
}
if(args.Length == 0)
{
PrintToChat(player, "Require Type Steamid Or Name\n/spectate yamang\n/spectate 765611*******");
return;
}
if (args[0] == "stop")
{
player.Respawn();
return;
}
var target = FindPlayers(args[0]);
if(target == null)
{
PrintToChat(player, $"플레이어를 찾을 수 없습니다.");
return;
}
if (!player.IsSpectating())
{
player.StartSpectating();
player.UpdateSpectateTarget(target.UserIDString);
}
else
{
player.UpdateSpectateTarget(target.UserIDString);
}
RustUI(player, target.displayName);
}
void OnServerInitialized(bool initial)
{
permission.RegisterPermission("spectate.use", this);
}
#endregion
#region UI
private void RustUI(BasePlayer player, string name)
{
var container = new CuiElementContainer();
container.Add(new CuiButton
{
Button = { Color = "0 0 0 0.3", Command = "respawn" },
Text = { Text = $"{name}", Font = "robotocondensed-regular.ttf", FontSize = 18, Align = TextAnchor.MiddleCenter, Color = "1 1 1 1" },
RectTransform = { AnchorMin = "0.5 0.5", AnchorMax = "0.5 0.5", OffsetMin = "-631.01 304.758", OffsetMax = "-420.81 351.242" }
}, "Overlay", "SpectateUI");
CuiHelper.DestroyUi(player, "SpectateUI");
CuiHelper.AddUi(player, container);
}
#endregion
#region Helper
private static BasePlayer FindPlayers(string nameOrIdOrIp)
{
foreach (var activePlayer in BasePlayer.activePlayerList)
{
if (activePlayer.userID.ToString() == nameOrIdOrIp)
return activePlayer;
if (activePlayer.displayName.Contains(nameOrIdOrIp, CompareOptions.OrdinalIgnoreCase))
return activePlayer;
if (activePlayer.net?.connection != null && activePlayer.net.connection.ipaddress == nameOrIdOrIp)
return activePlayer;
}
return null;
}
#endregion
}
}