Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ID_Dedicated/null for Npc.UserId Parameter #75

Merged
merged 9 commits into from
Sep 23, 2024
Merged
Changes from all 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
66 changes: 41 additions & 25 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 @@ -131,55 +133,69 @@ public Npc(GameObject gameObject)
/// <param name="userId">The userID of the NPC.</param>
/// <param name="position">The position to spawn the NPC.</param>
/// <returns>The <see cref="Npc"/> spawned.</returns>
public static Npc Spawn(string name, RoleTypeId role, int id = 0, string userId = "", Vector3? position = null)
public static Npc Spawn(string name, RoleTypeId role, int id = 0, string userId = PlayerAuthenticationManager.DedicatedId, Vector3? position = null)
{
GameObject newObject = Object.Instantiate(NetworkManager.singleton.playerPrefab);
GameObject newObject = UnityEngine.Object.Instantiate(Mirror.NetworkManager.singleton.playerPrefab);

Npc npc = new(newObject)
{
IsVerified = true,
IsNPC = true,
};
try
{
npc.ReferenceHub.roleManager.InitializeNewRole(RoleTypeId.None, RoleChangeReason.None);
}
catch (Exception e)

if (!RecyclablePlayerId.FreeIds.Contains(id) && RecyclablePlayerId._autoIncrement >= id)
{
Log.Debug($"Ignore: {e}");
Log.Warn($"{Assembly.GetCallingAssembly().GetName().Name} tried to spawn an NPC with a duplicate PlayerID. Using auto-incremented ID instead to avoid an ID clash.");
id = new RecyclablePlayerId(true).Value;
}

if (RecyclablePlayerId.FreeIds.Contains(id))
try
{
RecyclablePlayerId.FreeIds.RemoveFromQueue(id);
if (userId == PlayerAuthenticationManager.DedicatedId)
{
npc.ReferenceHub.authManager.SyncedUserId = userId;
try
{
npc.ReferenceHub.authManager.InstanceMode = ClientInstanceMode.DedicatedServer;
}
catch (Exception e)
{
Log.Debug($"Ignore: {e.Message}");
}
}
else
{
npc.ReferenceHub.authManager.InstanceMode = ClientInstanceMode.Unverified;
npc.ReferenceHub.authManager._privUserId = userId == string.Empty ? $"Dummy@localhost" : userId;
}
}
else if (RecyclablePlayerId._autoIncrement >= id)
catch (Exception e)
{
RecyclablePlayerId._autoIncrement = id = RecyclablePlayerId._autoIncrement + 1;
Log.Debug($"Ignore: {e.Message}");
}

FakeConnection fakeConnection = new(id);
NetworkServer.AddPlayerForConnection(fakeConnection, newObject);
try
{
npc.ReferenceHub.authManager.UserId = string.IsNullOrEmpty(userId) ? $"Dummy@localhost" : userId;
npc.ReferenceHub.roleManager.InitializeNewRole(RoleTypeId.None, RoleChangeReason.None);
}
catch (Exception e)
{
Log.Debug($"Ignore: {e}");
Log.Debug($"Ignore: {e.Message}");
}

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

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)
npc.Position = position.Value;
});

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

Expand Down
Loading