forked from markvaaz/ScarletCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerDataExtensions.cs
More file actions
128 lines (113 loc) · 4.99 KB
/
Copy pathPlayerDataExtensions.cs
File metadata and controls
128 lines (113 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using ProjectM;
using ProjectM.Network;
using ScarletCore.Services;
using Unity.Entities;
namespace ScarletCore;
/// <summary>
/// Provides extension methods for retrieving PlayerData from various types.
/// </summary>
public static class PlayerDataExtensions {
/// <summary>
/// Gets the PlayerData for the specified player name.
/// </summary>
/// <param name="playerName">The player name.</param>
/// <returns>The PlayerData if found; otherwise, null.</returns>
public static PlayerData GetPlayerData(this string playerName) {
return PlayerService.TryGetByName(playerName, out PlayerData playerData) ? playerData : null;
}
/// <summary>
/// Gets the PlayerData for the specified player ID.
/// </summary>
/// <param name="playerId">The player ID.</param>
/// <returns>The PlayerData if found; otherwise, null.</returns>
public static PlayerData GetPlayerData(this ulong playerId) {
return PlayerService.TryGetById(playerId, out PlayerData playerData) ? playerData : null;
}
/// <summary>
/// Gets the PlayerData for the specified entity, if it represents a user or player character.
/// </summary>
/// <param name="entity">The entity to query.</param>
/// <returns>The PlayerData if found; otherwise, null.</returns>
public static PlayerData GetPlayerData(this Entity entity) {
if (entity.Has<User>()) {
return PlayerService.TryGetById(entity.Read<User>().PlatformId, out PlayerData playerData) ? playerData : null;
}
if (entity.Has<PlayerCharacter>()) {
var playerCharacter = entity.Read<PlayerCharacter>();
var user = playerCharacter.UserEntity.Read<User>();
return PlayerService.TryGetById(user.PlatformId, out PlayerData playerData) ? playerData : null;
}
return null;
}
/// <summary>
/// Gets the PlayerData for the specified network ID.
/// </summary>
/// <param name="networkId">The network ID.</param>
/// <returns>The PlayerData if found; otherwise, null.</returns>
public static PlayerData GetPlayerData(this NetworkId networkId) {
return PlayerService.TryGetByNetworkId(networkId, out PlayerData playerData) ? playerData : null;
}
/// <summary>
/// Gets the PlayerData for the specified User.
/// </summary>
/// <param name="user">The User instance.</param>
/// <returns>The PlayerData if found; otherwise, null.</returns>
public static PlayerData GetPlayerData(this User user) {
return PlayerService.TryGetById(user.PlatformId, out PlayerData playerData) ? playerData : null;
}
/// <summary>
/// Tries to get the PlayerData for the specified player name.
/// </summary>
/// <param name="playerName">The player name.</param>
/// <param name="playerData">Output PlayerData if found.</param>
/// <returns>True if found; otherwise, false.</returns>
public static bool TryGetPlayerData(this string playerName, out PlayerData playerData) {
return PlayerService.TryGetByName(playerName, out playerData);
}
/// <summary>
/// Tries to get the PlayerData for the specified player ID.
/// </summary>
/// <param name="playerId">The player ID.</param>
/// <param name="playerData">Output PlayerData if found.</param>
/// <returns>True if found; otherwise, false.</returns>
public static bool TryGetPlayerData(this ulong playerId, out PlayerData playerData) {
return PlayerService.TryGetById(playerId, out playerData);
}
/// <summary>
/// Tries to get the PlayerData for the specified entity, if it represents a user or player character.
/// </summary>
/// <param name="entity">The entity to query.</param>
/// <param name="playerData">Output PlayerData if found.</param>
/// <returns>True if found; otherwise, false.</returns>
public static bool TryGetPlayerData(this Entity entity, out PlayerData playerData) {
playerData = null;
if (entity.Has<User>()) {
var user = entity.Read<User>();
return PlayerService.TryGetById(user.PlatformId, out playerData);
}
if (entity.Has<PlayerCharacter>()) {
var playerCharacter = entity.Read<PlayerCharacter>();
var user = playerCharacter.UserEntity.Read<User>();
return PlayerService.TryGetById(user.PlatformId, out playerData);
}
return false;
}
/// <summary>
/// Tries to get the PlayerData for the specified network ID.
/// </summary>
/// <param name="networkId">The network ID.</param>
/// <param name="playerData">Output PlayerData if found.</param>
/// <returns>True if found; otherwise, false.</returns>
public static bool TryGetPlayerData(this NetworkId networkId, out PlayerData playerData) {
return PlayerService.TryGetByNetworkId(networkId, out playerData);
}
/// <summary>
/// Tries to get the PlayerData for the specified User.
/// </summary>
/// <param name="user">The User instance.</param>
/// <param name="playerData">Output PlayerData if found.</param>
/// <returns>True if found; otherwise, false.</returns>
public static bool TryGetPlayerData(this User user, out PlayerData playerData) {
return PlayerService.TryGetById(user.PlatformId, out playerData);
}
}