Skip to content

Commit 3dc7b01

Browse files
committed
Added an argument to the dummy ExPlayer constructor which specifies if the dummy should be hidden from the player list (and improved the IsNpc property)
1 parent 46bf2ad commit 3dc7b01

1 file changed

Lines changed: 52 additions & 7 deletions

File tree

LabExtended/API/ExPlayer.cs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@
4949
using CommandSystem;
5050

5151
using Footprinting;
52-
using LabApi.Events.Arguments.PlayerEvents;
53-
using LabApi.Events.Handlers;
54-
using LabExtended.Attributes;
52+
5553
using NetworkManagerUtils.Dummies;
5654

5755
using NorthwoodLib.Pools;
58-
using PlayerRoles.FirstPersonControl.NetworkMessages;
56+
5957
using UserSettings.ServerSpecific;
6058

6159
#pragma warning disable CS8602 // Dereference of a possibly null reference.
@@ -474,7 +472,13 @@ internal Dictionary<int, SettingsEntry>?
474472
/// Spawns a new dummy player with the specified nickname.
475473
/// </summary>
476474
/// <param name="nickname">The nickname to add to the dummy (Dummy will be set if left null).</param>
477-
public ExPlayer(string? nickname = null) : this(DummyUtils.SpawnDummy(nickname ?? "Dummy"), SwitchContainer.GetNewNpcToggles(true))
475+
/// <param name="hideFromPlayerList">Whether or not to hide the dummy from the player list (this is done by setting the ID of the dummy to ID_Dedicated so it's
476+
/// considered as the server player which is hidden from the list)</param>
477+
public ExPlayer(string? nickname, bool hideFromPlayerList)
478+
: this(hideFromPlayerList
479+
? SpawnHiddenDummy(nickname ?? "Dummy")
480+
: DummyUtils.SpawnDummy(nickname ?? "Dummy"),
481+
SwitchContainer.GetNewNpcToggles(true))
478482
{
479483

480484
}
@@ -563,7 +567,8 @@ public ExPlayer(ReferenceHub referenceHub, SwitchContainer toggles) : base(refer
563567
}
564568
else
565569
{
566-
host = this;
570+
if (host is null || host?.ReferenceHub == null)
571+
host = this;
567572

568573
Toggles.ShouldSendPosition = false;
569574

@@ -949,7 +954,32 @@ public byte KickPower
949954
/// Whether the player is a NPC.
950955
/// </summary>
951956
[CommandPropertyAlias("isNpc")]
952-
public new bool IsNpc => InstanceMode is ClientInstanceMode.Dummy;
957+
public new bool IsNpc
958+
{
959+
get
960+
{
961+
if (InstanceMode is ClientInstanceMode.Dummy)
962+
return true;
963+
964+
if (Connection != null)
965+
{
966+
if (Connection is DummyNetworkConnection)
967+
return true;
968+
969+
if (Connection is LocalConnectionToClient)
970+
{
971+
if (host != null)
972+
return host.NetworkId != NetworkId;
973+
974+
// host player, not an NPC
975+
if (NetworkClient.connection != null && NetworkClient.connection == Connection)
976+
return false;
977+
}
978+
}
979+
980+
return false;
981+
}
982+
}
953983

954984
/// <summary>
955985
/// Whether the player is connected.
@@ -1448,6 +1478,21 @@ public void Dispose()
14481478
Rotation = null!;
14491479
}
14501480

1481+
private static ReferenceHub SpawnHiddenDummy(string nick)
1482+
{
1483+
var hubGo = UnityEngine.Object.Instantiate(NetworkManager.singleton.playerPrefab);
1484+
var hub = hubGo.GetComponent<ReferenceHub>();
1485+
1486+
NetworkServer.AddPlayerForConnection(new DummyNetworkConnection(), hubGo);
1487+
1488+
hub.nicknameSync.MyNick = nick;
1489+
1490+
hub.authManager.NetworkSyncedUserId = "ID_Dedicated";
1491+
hub.authManager.syncMode = (SyncMode)ClientInstanceMode.DedicatedServer;
1492+
1493+
return hub;
1494+
}
1495+
14511496
#region Operators
14521497

14531498
/// <inheritdoc/>

0 commit comments

Comments
 (0)