forked from decaprime/CommunityCommands
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathHelper.cs
More file actions
318 lines (279 loc) · 10 KB
/
Copy pathHelper.cs
File metadata and controls
318 lines (279 loc) · 10 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System.Collections.Generic;
using Il2CppInterop.Runtime;
using Il2CppSystem;
using KindredCommands.Data;
using ProjectM;
using ProjectM.CastleBuilding;
using ProjectM.Gameplay.Clan;
using ProjectM.Network;
using ProjectM.Scripting;
using ProjectM.Shared;
using Stunlock.Core;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using VampireCommandFramework;
namespace KindredCommands;
// This is an anti-pattern, move stuff away from Helper not into it
internal static partial class Helper
{
public static AdminAuthSystem adminAuthSystem = Core.Server.GetExistingSystemManaged<AdminAuthSystem>();
public static ClanSystem_Server clanSystem = Core.Server.GetExistingSystemManaged<ClanSystem_Server>();
public static EntityCommandBufferSystem entityCommandBufferSystem = Core.Server.GetExistingSystemManaged<EntityCommandBufferSystem>();
public static PrefabGUID GetPrefabGUID(Entity entity)
{
var entityManager = Core.EntityManager;
PrefabGUID guid;
try
{
guid = entityManager.GetComponentData<PrefabGUID>(entity);
}
catch
{
guid = new PrefabGUID(0);
}
return guid;
}
public static bool TryGetClanEntityFromPlayer(Entity User, out Entity ClanEntity)
{
if (User.Read<TeamReference>().Value._Value.ReadBuffer<TeamAllies>().Length > 0)
{
ClanEntity = User.Read<TeamReference>().Value._Value.ReadBuffer<TeamAllies>()[0].Value;
return true;
}
ClanEntity = new Entity();
return false;
}
public static Entity AddItemToInventory(Entity recipient, PrefabGUID guid, int amount)
{
try
{
ServerGameManager serverGameManager = Core.Server.GetExistingSystemManaged<ServerScriptMapper>()._ServerGameManager;
var inventoryResponse = serverGameManager.TryAddInventoryItem(recipient, guid, amount);
return inventoryResponse.NewEntity;
}
catch (System.Exception e)
{
Core.LogException(e);
}
return new Entity();
}
public static NativeArray<Entity> GetEntitiesByComponentType<T1>(bool includeAll = false, bool includeDisabled = false, bool includeSpawn = false, bool includePrefab = false, bool includeDestroyed = false)
{
EntityQueryOptions options = EntityQueryOptions.Default;
if (includeAll) options |= EntityQueryOptions.IncludeAll;
if (includeDisabled) options |= EntityQueryOptions.IncludeDisabled;
if (includeSpawn) options |= EntityQueryOptions.IncludeSpawnTag;
if (includePrefab) options |= EntityQueryOptions.IncludePrefab;
if (includeDestroyed) options |= EntityQueryOptions.IncludeDestroyTag;
var entityQueryBuilder = new EntityQueryBuilder(Allocator.Temp)
.AddAll(new(Il2CppType.Of<T1>(), ComponentType.AccessMode.ReadWrite))
.WithOptions(options);
var query = Core.EntityManager.CreateEntityQuery(ref entityQueryBuilder);
var entities = query.ToEntityArray(Allocator.Temp);
return entities;
}
public static NativeArray<Entity> GetEntitiesByComponentTypes<T1, T2>(bool includeAll = false, bool includeDisabled = false, bool includeSpawn = false, bool includePrefab = false, bool includeDestroyed = false)
{
EntityQueryOptions options = EntityQueryOptions.Default;
if (includeAll) options |= EntityQueryOptions.IncludeAll;
if (includeDisabled) options |= EntityQueryOptions.IncludeDisabled;
if (includeSpawn) options |= EntityQueryOptions.IncludeSpawnTag;
if (includePrefab) options |= EntityQueryOptions.IncludePrefab;
if (includeDestroyed) options |= EntityQueryOptions.IncludeDestroyTag;
var entityQueryBuilder = new EntityQueryBuilder(Allocator.Temp)
.AddAll(new(Il2CppType.Of<T1>(), ComponentType.AccessMode.ReadWrite))
.AddAll(new(Il2CppType.Of<T2>(), ComponentType.AccessMode.ReadWrite))
.WithOptions(options);
var query = Core.EntityManager.CreateEntityQuery(ref entityQueryBuilder);
var entities = query.ToEntityArray(Allocator.Temp);
return entities;
}
public static IEnumerable<Entity> GetAllEntitiesInRadius<T>(float2 center, float radius)
{
var spatialData = Core.GenerateCastle._TileModelLookupSystemData;
var tileModelSpatialLookupRO = spatialData.GetSpatialLookupReadOnlyAndComplete(Core.GenerateCastle);
var gridPos = ConvertPosToTileGrid(center);
var gridPosMin = ConvertPosToTileGrid(center - radius);
var gridPosMax = ConvertPosToTileGrid(center + radius);
var bounds = new BoundsMinMax(Mathf.FloorToInt(gridPosMin.x), Mathf.FloorToInt(gridPosMin.y),
Mathf.CeilToInt(gridPosMax.x), Mathf.CeilToInt(gridPosMax.y));
var entities = tileModelSpatialLookupRO.GetEntities(ref bounds, TileType.All);
foreach (var entity in entities)
{
if (!entity.Has<T>()) continue;
if (!entity.Has<Translation>()) continue;
var pos = entity.Read<Translation>().Value;
if (math.distance(center, pos.xz) <= radius)
{
yield return entity;
}
}
entities.Dispose();
}
public static Entity FindClosestTilePosition(Vector3 pos, bool ignoreFloors = false)
{
var spatialData = Core.GenerateCastle._TileModelLookupSystemData;
var tileModelSpatialLookupRO = spatialData.GetSpatialLookupReadOnlyAndComplete(Core.GenerateCastle);
var gridPos = ConvertPosToTileGrid(pos);
var bounds = new BoundsMinMax((int)(gridPos.x - 2.5), (int)(gridPos.z - 2.5),
(int)(gridPos.x + 2.5), (int)(gridPos.z + 2.5));
var closestEntity = Entity.Null;
var closestDistance = float.MaxValue;
var entities = tileModelSpatialLookupRO.GetEntities(ref bounds, TileType.All);
for (var i = 0; i < entities.Length; ++i)
{
var entity = entities[i];
if (!entity.Has<TilePosition>()) continue;
if (!entity.Has<Translation>()) continue;
if (ignoreFloors && entity.Has<CastleFloor>()) continue;
var entityPos = entity.Read<Translation>().Value;
var distance = math.distancesq(pos, entityPos);
if (distance < closestDistance)
{
var prefabName = GetPrefabGUID(entity).LookupName();
if (!prefabName.StartsWith("TM_")) continue;
closestDistance = distance;
closestEntity = entity;
}
}
entities.Dispose();
return closestEntity;
}
public static float2 ConvertPosToTileGrid(float2 pos)
{
return new float2(Mathf.FloorToInt(pos.x * 2) + 6400, Mathf.FloorToInt(pos.y * 2) + 6400);
}
public static float3 ConvertPosToTileGrid(float3 pos)
{
return new float3(Mathf.FloorToInt(pos.x * 2) + 6400, pos.y, Mathf.FloorToInt(pos.z * 2) + 6400);
}
public static void RepairGear(Entity Character, bool repair = true)
{
Equipment equipment = Character.Read<Equipment>();
NativeList<Entity> equippedItems = new(Allocator.Temp);
equipment.GetAllEquipmentEntities(equippedItems);
foreach (var equippedItem in equippedItems)
{
if (equippedItem.Has<Durability>())
{
var durability = equippedItem.Read<Durability>();
if (repair)
{
durability.Value = durability.MaxDurability;
}
else
{
durability.Value = 0;
}
equippedItem.Write(durability);
}
}
equippedItems.Dispose();
for (int i = 0; i < 36; i++)
{
if (InventoryUtilities.TryGetItemAtSlot(Core.EntityManager, Character, i, out InventoryBuffer item))
{
var itemEntity = item.ItemEntity._Entity;
if (itemEntity.Has<Durability>())
{
var durability = itemEntity.Read<Durability>();
if (repair)
{
durability.Value = durability.MaxDurability;
}
else
{
durability.Value = 0;
}
itemEntity.Write(durability);
}
}
}
}
public static void ReviveCharacter(Entity Character, Entity User, ChatCommandContext ctx = null)
{
var health = Character.Read<Health>();
ctx?.Reply("TryGetbuff");
if (BuffUtility.TryGetBuff(Core.EntityManager, Character, Prefabs.Buff_General_Vampire_Wounded_Buff, out var buffData))
{
ctx?.Reply("Destroy");
DestroyUtility.Destroy(Core.EntityManager, buffData, DestroyDebugReason.TryRemoveBuff);
ctx?.Reply("Health");
health.Value = health.MaxHealth;
health.MaxRecoveryHealth = health.MaxHealth;
Character.Write(health);
}
if (health.IsDead)
{
ctx?.Reply("Respawn");
var pos = Character.Read<LocalToWorld>().Position;
Nullable_Unboxed<float3> spawnLoc = new() { value = pos };
ctx?.Reply("Respawn2");
var sbs = Core.Server.GetExistingSystemManaged<ServerBootstrapSystem>();
var bufferSystem = Core.Server.GetExistingSystemManaged<EntityCommandBufferSystem>();
var buffer = bufferSystem.CreateCommandBuffer();
ctx?.Reply("Respawn3");
sbs.RespawnCharacter(buffer, User,
customSpawnLocation: spawnLoc,
previousCharacter: Character);
}
}
public static void KickPlayer(Entity userEntity)
{
EntityManager entityManager = Core.Server.EntityManager;
User user = userEntity.Read<User>();
if (!user.IsConnected || user.PlatformId==0) return;
Entity entity = entityManager.CreateEntity(new ComponentType[3]
{
ComponentType.ReadOnly<NetworkEventType>(),
ComponentType.ReadOnly<SendEventToUser>(),
ComponentType.ReadOnly<KickEvent>()
});
entity.Write(new KickEvent()
{
PlatformId = user.PlatformId
});
entity.Write(new SendEventToUser()
{
UserIndex = user.Index
});
entity.Write(new NetworkEventType()
{
EventId = NetworkEvents.EventId_KickEvent,
IsAdminEvent = false,
IsDebugEvent = false
});
}
public static void UnlockWaypoints(Entity userEntity)
{
DynamicBuffer<UnlockedWaypointElement> dynamicBuffer = Core.EntityManager.AddBuffer<UnlockedWaypointElement>(userEntity);
dynamicBuffer.Clear();
foreach (Entity waypoint in Helper.GetEntitiesByComponentType<ChunkWaypoint>())
dynamicBuffer.Add(new UnlockedWaypointElement()
{
Waypoint = waypoint.Read<NetworkId>()
});
}
public static void RevealMapForPlayer(Entity userEntity)
{
var mapZoneElements = Core.EntityManager.GetBuffer<UserMapZoneElement>(userEntity);
foreach (var mapZone in mapZoneElements)
{
var userZoneEntity = mapZone.UserZoneEntity.GetEntityOnServer();
var revealElements = Core.EntityManager.GetBuffer<UserMapZonePackedRevealElement>(userZoneEntity);
revealElements.Clear();
var revealElement = new UserMapZonePackedRevealElement
{
PackedPixel = 255
};
for (var i = 0; i < 8192; i++)
{
revealElements.Add(revealElement);
}
}
}
// add the component debugunlock
}