Skip to content

Commit

Permalink
Npc
Browse files Browse the repository at this point in the history
  • Loading branch information
NotZer0Two committed Aug 21, 2024
1 parent 92af4ce commit db1d884
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions EXILED/Exiled.API/Features/Npc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace Exiled.API.Features
using System;
using System.Collections.Generic;
using System.Linq;

using System.Reflection;
using CentralAuth;
using CommandSystem;

using Exiled.API.Enums;
Expand Down Expand Up @@ -134,11 +135,13 @@ public Npc(GameObject gameObject)
public static Npc Spawn(string name, RoleTypeId role, int id = 0, string userId = "", Vector3? position = null)
{
GameObject newObject = Object.Instantiate(NetworkManager.singleton.playerPrefab);

Npc npc = new(newObject)
{
IsVerified = true,
IsVerified = userId != PlayerAuthenticationManager.DedicatedId && userId != null,
IsNPC = true,
};

try
{
npc.ReferenceHub.roleManager.InitializeNewRole(RoleTypeId.None, RoleChangeReason.None);
Expand All @@ -148,20 +151,33 @@ public static Npc Spawn(string name, RoleTypeId role, int id = 0, string userId
Log.Debug($"Ignore: {e}");
}

if (RecyclablePlayerId.FreeIds.Contains(id))
if (!RecyclablePlayerId.FreeIds.Contains(id) && RecyclablePlayerId._autoIncrement >= id)
{
RecyclablePlayerId.FreeIds.RemoveFromQueue(id);
}
else if (RecyclablePlayerId._autoIncrement >= id)
{
RecyclablePlayerId._autoIncrement = id = RecyclablePlayerId._autoIncrement + 1;
Log.Warn($"{Assembly.GetCallingAssembly().GetName().Name} tried to spawn an NPC with a duplicate PlayerID. Using auto-incremented ID instead to avoid issues.");
id = new RecyclablePlayerId(false).Value;
}

FakeConnection fakeConnection = new(id);
NetworkServer.AddPlayerForConnection(fakeConnection, newObject);

try
{
npc.ReferenceHub.authManager.UserId = string.IsNullOrEmpty(userId) ? $"Dummy@localhost" : userId;
if (userId == PlayerAuthenticationManager.DedicatedId)
{
npc.ReferenceHub.authManager.SyncedUserId = userId;
try
{
npc.ReferenceHub.authManager.InstanceMode = ClientInstanceMode.DedicatedServer;
}
catch (Exception e)
{
Log.Debug($"Ignore: {e}");
}
}
else
{
npc.ReferenceHub.authManager.UserId = userId == string.Empty ? $"Dummy@localhost" : userId;
}
}
catch (Exception e)
{
Expand All @@ -171,15 +187,14 @@ public static Npc Spawn(string name, RoleTypeId role, int id = 0, string userId
npc.ReferenceHub.nicknameSync.Network_myNickSync = name;
Dictionary.Add(newObject, npc);

Timing.CallDelayed(
0.3f,
() =>
{
npc.Role.Set(role, SpawnReason.RoundStart, position is null ? RoleSpawnFlags.All : RoleSpawnFlags.AssignInventory);
});
Timing.CallDelayed(0.5f, () =>
{
npc.Role.Set(role, SpawnReason.RoundStart, position is null ? RoleSpawnFlags.All : RoleSpawnFlags.AssignInventory);
});

if (position is not null)
Timing.CallDelayed(0.5f, () => npc.Position = position.Value);

return npc;
}

Expand Down

0 comments on commit db1d884

Please sign in to comment.