60 lines
2.3 KiB
C#
60 lines
2.3 KiB
C#
using Sandbox.Game.World;
|
|
|
|
namespace Global.Util
|
|
{
|
|
public static class SEUtils
|
|
{
|
|
public static MyIdentity TryGetIdentity(string playerNameOrSteamId)
|
|
{
|
|
var player = MySession.Static.Players.GetPlayerByName(playerNameOrSteamId);
|
|
if (player != null) return player.Identity;
|
|
player = MySession.Static.Players.GetPlayerById(new MyPlayer.PlayerId(ulong.Parse(playerNameOrSteamId)));
|
|
if (player != null) return player.Identity;
|
|
if (MySession.Static.Players.TryGetPlayerBySteamId(ulong.Parse(playerNameOrSteamId), out player))
|
|
return player.Identity;
|
|
|
|
foreach (var identity in MySession.Static.Players.GetAllIdentities())
|
|
{
|
|
if (identity.DisplayName == playerNameOrSteamId) return identity;
|
|
|
|
if (!ulong.TryParse(playerNameOrSteamId, out var steamId)) continue;
|
|
|
|
var id = MySession.Static.Players.TryGetSteamId(identity.IdentityId);
|
|
if (id == steamId) return identity;
|
|
if (identity.IdentityId == (long)steamId) return identity;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static MyIdentity GetIdentityByNameOrId(string playerNameOrSteamId)
|
|
{
|
|
var player = MySession.Static.Players.GetPlayerByName(playerNameOrSteamId);
|
|
if (player != null) return player.Identity;
|
|
player = MySession.Static.Players.GetPlayerById(new MyPlayer.PlayerId(ulong.Parse(playerNameOrSteamId)));
|
|
if (player != null) return player.Identity;
|
|
|
|
foreach (var identity in MySession.Static.Players.GetAllIdentities())
|
|
{
|
|
if (identity.DisplayName == playerNameOrSteamId) return identity;
|
|
|
|
if (!ulong.TryParse(playerNameOrSteamId, out var steamId)) continue;
|
|
|
|
var id = MySession.Static.Players.TryGetSteamId(identity.IdentityId);
|
|
|
|
if (id == steamId) return identity;
|
|
if (identity.IdentityId == (long)steamId) return identity;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string GetPlayerName(ulong steamId)
|
|
{
|
|
var id = GetIdentityByNameOrId(steamId.ToString());
|
|
if (id != null && id.DisplayName != null) return id.DisplayName;
|
|
|
|
return steamId.ToString();
|
|
}
|
|
}
|
|
} |