zz
This commit is contained in:
60
GlobalTorch/Util/SEUtils.cs
Normal file
60
GlobalTorch/Util/SEUtils.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user