Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions EXILED/Exiled.API/Features/Npc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ 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 +136,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,
Comment thread
NotZer0Two marked this conversation as resolved.
Outdated
IsNPC = true,
};

try
{
npc.ReferenceHub.roleManager.InitializeNewRole(RoleTypeId.None, RoleChangeReason.None);
Expand All @@ -148,20 +152,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.");
Comment thread
NotZer0Two marked this conversation as resolved.
Outdated
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 +188,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);

Comment thread
NotZer0Two marked this conversation as resolved.
return npc;
}

Expand Down