From 248e17e4d6e916b633e82c19636020cf38315def Mon Sep 17 00:00:00 2001 From: Jdance-Media <67397422+Jdance-Media@users.noreply.github.com> Date: Mon, 19 Jan 2026 14:25:20 -0500 Subject: [PATCH 1/2] Utilized NetIds to get drops for structures and barricades. This cuts down on number of ticks required by 48%. --- SellDoor/Helpers/BarricadeHelper.cs | 25 ++++++++++++++++++++----- SellDoor/Helpers/RaycastHelper.cs | 29 +++++++++++++++++++++++++---- SellDoor/Helpers/StructureHelper.cs | 26 ++++++++++++++++++++------ SellDoor/Models/Door.cs | 6 +++++- SellDoor/Models/DoorItem.cs | 7 ++++++- SellDoor/Models/TransformBase.cs | 18 +++++++++++++++--- SellDoor/SellDoor.csproj | 5 +++++ 7 files changed, 96 insertions(+), 20 deletions(-) diff --git a/SellDoor/Helpers/BarricadeHelper.cs b/SellDoor/Helpers/BarricadeHelper.cs index d849a47..922d1db 100644 --- a/SellDoor/Helpers/BarricadeHelper.cs +++ b/SellDoor/Helpers/BarricadeHelper.cs @@ -1,4 +1,5 @@ using SDG.Unturned; +using Steamworks; using System; using System.Collections.Generic; using System.Linq; @@ -20,12 +21,19 @@ public static BarricadeDrop FindBarricadeDrop(Guid assetGuid, Vector3 point) foreach (Transform result in results) { - BarricadeDrop barricadeDrop = BarricadeManager.FindBarricadeByRootTransform(result); - BarricadeData barricadeData = barricadeDrop.GetServersideData(); - if (barricadeData.point == point && barricadeDrop.asset.GUID == assetGuid) + NetId netId = NetIdRegistry.GetTransformNetId(result); + if (netId == NetId.INVALID) + return null; + netId.id--; + BarricadeDrop drop = NetIdRegistry.Get(netId); + + + BarricadeData barricadeData = drop.GetServersideData(); + + if (barricadeData.point == point && drop.asset.GUID == assetGuid) { - return barricadeDrop; + return drop; } } @@ -41,7 +49,14 @@ public static BarricadeDrop ForceDropBarricade(ItemBarricadeAsset asset, Vector3 Transform transform = BarricadeManager.dropNonPlantedBarricade(barricade, point, rotation, owner, group); - return BarricadeManager.FindBarricadeByRootTransform(transform); + + NetId netId = NetIdRegistry.GetTransformNetId(transform); + if (netId == NetId.INVALID) + return null; + netId.id--; + BarricadeDrop drop = NetIdRegistry.Get(netId); + + return drop; } } } diff --git a/SellDoor/Helpers/RaycastHelper.cs b/SellDoor/Helpers/RaycastHelper.cs index b6d0896..b230ced 100644 --- a/SellDoor/Helpers/RaycastHelper.cs +++ b/SellDoor/Helpers/RaycastHelper.cs @@ -1,4 +1,5 @@ using SDG.Unturned; +using Steamworks; using System.Collections.Generic; using UnityEngine; @@ -22,7 +23,12 @@ public static Transform GetBarricadeTransform(Player player, out BarricadeData b transform = door.transform; } - drop = BarricadeManager.FindBarricadeByRootTransform(transform); + NetId netId = NetIdRegistry.GetTransformNetId(transform); + if (netId == NetId.INVALID) + return null; + netId.id--; + drop = NetIdRegistry.Get(netId); + if (drop != null) { barricadeData = drop.GetServersideData(); @@ -40,7 +46,11 @@ public static Transform GetStructureTransform(Player player, out StructureData s Ray ray = new Ray(player.look.aim.position, player.look.aim.forward); if (Physics.Raycast(ray, out hit, 3, RayMasks.STRUCTURE_INTERACT)) { - StructureDrop drop = StructureManager.FindStructureByRootTransform(hit.transform); + NetId netId = NetIdRegistry.GetTransformNetId(hit.transform); + if (netId == NetId.INVALID) + return null; + netId.id--; + StructureDrop drop = NetIdRegistry.Get(netId); if (drop != null) { structureData = drop.GetServersideData(); @@ -62,7 +72,13 @@ public static Transform GetBarricadeTransform(Vector3 position) { if (transform.position == position) { - return BarricadeManager.FindBarricadeByRootTransform(transform)?.model ?? null; + + NetId netId = NetIdRegistry.GetTransformNetId(transform); + if (netId == NetId.INVALID) + return null; + netId.id--; + BarricadeDrop drop = NetIdRegistry.Get(netId); + return drop?.model ?? null; } } return null; @@ -79,7 +95,12 @@ public static Transform GetStructureTransform(Vector3 position) { if (transform.position == position) { - return StructureManager.FindStructureByRootTransform(transform)?.model ?? null; + NetId netId = NetIdRegistry.GetTransformNetId(transform); + if (netId == NetId.INVALID) + return null; + netId.id--; + StructureDrop drop = NetIdRegistry.Get(netId); + return drop?.model ?? null; } } diff --git a/SellDoor/Helpers/StructureHelper.cs b/SellDoor/Helpers/StructureHelper.cs index e7fe7d1..39fec35 100644 --- a/SellDoor/Helpers/StructureHelper.cs +++ b/SellDoor/Helpers/StructureHelper.cs @@ -7,8 +7,10 @@ namespace RestoreMonarchy.SellDoor.Helpers { public class StructureHelper { + /* public static StructureDrop FindStructureDropByPosition(Guid assetGuid, Vector3 point) { + List regions = new(); Regions.getRegionsInRadius(point, 0.1f, regions); @@ -17,17 +19,25 @@ public static StructureDrop FindStructureDropByPosition(Guid assetGuid, Vector3 foreach (Transform result in results) { - StructureDrop structureDrop = StructureManager.FindStructureByRootTransform(result); - StructureData structureData = structureDrop.GetServersideData(); - - if (structureData.point == point && structureDrop.asset.GUID == assetGuid) + NetId netId = NetIdRegistry.GetTransformNetId(result); + if (netId == NetId.INVALID) + return null; + netId.id--; + StructureDrop drop = NetIdRegistry.Get(netId); + StructureData structureData = drop.GetServersideData(); + + if (structureData.point == point && drop.asset.GUID == assetGuid) { - return structureDrop; + return drop; } } + + + return null; } + */ public static StructureDrop ForceDropStructure(ItemStructureAsset asset, Vector3 point, Vector3 angle, ulong owner, ulong group) { @@ -36,7 +46,11 @@ public static StructureDrop ForceDropStructure(ItemStructureAsset asset, Vector3 StructureManager.dropReplicatedStructure(structure, point, rotation, owner, group); - return FindStructureDropByPosition(asset.GUID, point); + NetId dropNetId = new NetId(NetIdRegistry.counter - 2); + + StructureDrop drop = NetIdRegistry.Get(dropNetId); + + return drop; } } } diff --git a/SellDoor/Models/Door.cs b/SellDoor/Models/Door.cs index 642fefe..beff061 100644 --- a/SellDoor/Models/Door.cs +++ b/SellDoor/Models/Door.cs @@ -39,7 +39,11 @@ public bool TryGetDoorOwners(out CSteamID steamID, out CSteamID groupID) { steamID = CSteamID.Nil; groupID = CSteamID.Nil; - BarricadeDrop drop = BarricadeManager.FindBarricadeByRootTransform(Transform); + NetId netId = NetIdRegistry.GetTransformNetId(Transform); + if (netId == NetId.INVALID) + return false; + netId.id--; + BarricadeDrop drop = NetIdRegistry.Get(netId); if (drop == null) { return false; diff --git a/SellDoor/Models/DoorItem.cs b/SellDoor/Models/DoorItem.cs index 2ec7b32..ed05014 100644 --- a/SellDoor/Models/DoorItem.cs +++ b/SellDoor/Models/DoorItem.cs @@ -1,5 +1,6 @@ using SDG.Unturned; using Steamworks; +using UnityEngine; namespace RestoreMonarchy.SellDoor.Models { @@ -29,7 +30,11 @@ public void UpdateSign(string text) return; } - BarricadeDrop drop = BarricadeManager.FindBarricadeByRootTransform(Transform); + NetId netId = NetIdRegistry.GetTransformNetId(Transform); + if (netId == NetId.INVALID) + return; + netId.id--; + BarricadeDrop drop = NetIdRegistry.Get(netId); if (drop == null) { return; diff --git a/SellDoor/Models/TransformBase.cs b/SellDoor/Models/TransformBase.cs index 3708323..4f9d673 100644 --- a/SellDoor/Models/TransformBase.cs +++ b/SellDoor/Models/TransformBase.cs @@ -25,7 +25,13 @@ public void UpdatePosition() if (AssetId == null) { - BarricadeDrop drop = BarricadeManager.FindBarricadeByRootTransform(Transform); + BarricadeDrop drop = null; + NetId netId = NetIdRegistry.GetTransformNetId(Transform); + if (netId != NetId.INVALID) + { + netId.id--; + drop = NetIdRegistry.Get(netId); + } if (drop != null) { AssetId = drop.asset.GUID; @@ -33,8 +39,14 @@ public void UpdatePosition() } else { - StructureDrop structureDrop = StructureManager.FindStructureByRootTransform(Transform); - if (drop != null) + StructureDrop structureDrop = null; + NetId structureNetId = NetIdRegistry.GetTransformNetId(Transform); + if (structureNetId != NetId.INVALID) + { + structureNetId.id--; + structureDrop = NetIdRegistry.Get(structureNetId); + } + if (structureDrop != null) { AssetId = structureDrop.asset.GUID; AssetName = structureDrop.asset.itemName; diff --git a/SellDoor/SellDoor.csproj b/SellDoor/SellDoor.csproj index 1a6339e..1fa365c 100644 --- a/SellDoor/SellDoor.csproj +++ b/SellDoor/SellDoor.csproj @@ -8,8 +8,13 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + From 6b61e295dccddd1deb78fbe26a33f53ff829f8ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Wrona?= Date: Sat, 2 May 2026 19:58:26 +0200 Subject: [PATCH 2/2] Fix off-by-one in ForceDropStructure and foreach early-return bugs - StructureHelper.ForceDropStructure: dropReplicatedStructure goes through ClaimBlock(2u) (StructureManager.cs:445), so drop is at counter-1 (transform at counter), not counter-2. The previous value pointed to either an unassigned slot or the previous block, returning null or an unrelated object. - BarricadeHelper.FindBarricadeDrop: 'return null' inside the foreach exited the whole loop on the first non-registered transform. Changed to 'continue' so other results in the radius still get checked. - RaycastHelper.GetBarricadeTransform(Vector3) / RaycastHelper.GetStructureTransform(Vector3): same foreach early-exit bug, also changed to 'continue'. Added drop != null check before returning so an invalid type cast falls through to the next match. - TransformBase.UpdatePosition: was calling GetTransformNetId(Transform) twice on the same Transform. Single lookup, then try BarricadeDrop cast; if null, try StructureDrop cast on the same netId. - StructureHelper: deleted the commented-out FindStructureDropByPosition block (no callers in repo, dead code). --- SellDoor/Helpers/BarricadeHelper.cs | 12 +++----- SellDoor/Helpers/RaycastHelper.cs | 11 ++++---- SellDoor/Helpers/StructureHelper.cs | 44 +++-------------------------- SellDoor/Models/TransformBase.cs | 29 ++++++++----------- 4 files changed, 25 insertions(+), 71 deletions(-) diff --git a/SellDoor/Helpers/BarricadeHelper.cs b/SellDoor/Helpers/BarricadeHelper.cs index 922d1db..a72e12f 100644 --- a/SellDoor/Helpers/BarricadeHelper.cs +++ b/SellDoor/Helpers/BarricadeHelper.cs @@ -21,16 +21,15 @@ public static BarricadeDrop FindBarricadeDrop(Guid assetGuid, Vector3 point) foreach (Transform result in results) { - NetId netId = NetIdRegistry.GetTransformNetId(result); if (netId == NetId.INVALID) - return null; + continue; netId.id--; BarricadeDrop drop = NetIdRegistry.Get(netId); - + if (drop == null) + continue; BarricadeData barricadeData = drop.GetServersideData(); - if (barricadeData.point == point && drop.asset.GUID == assetGuid) { return drop; @@ -49,14 +48,11 @@ public static BarricadeDrop ForceDropBarricade(ItemBarricadeAsset asset, Vector3 Transform transform = BarricadeManager.dropNonPlantedBarricade(barricade, point, rotation, owner, group); - NetId netId = NetIdRegistry.GetTransformNetId(transform); if (netId == NetId.INVALID) return null; netId.id--; - BarricadeDrop drop = NetIdRegistry.Get(netId); - - return drop; + return NetIdRegistry.Get(netId); } } } diff --git a/SellDoor/Helpers/RaycastHelper.cs b/SellDoor/Helpers/RaycastHelper.cs index b230ced..495c5e6 100644 --- a/SellDoor/Helpers/RaycastHelper.cs +++ b/SellDoor/Helpers/RaycastHelper.cs @@ -72,13 +72,13 @@ public static Transform GetBarricadeTransform(Vector3 position) { if (transform.position == position) { - NetId netId = NetIdRegistry.GetTransformNetId(transform); if (netId == NetId.INVALID) - return null; + continue; netId.id--; BarricadeDrop drop = NetIdRegistry.Get(netId); - return drop?.model ?? null; + if (drop != null) + return drop.model; } } return null; @@ -97,10 +97,11 @@ public static Transform GetStructureTransform(Vector3 position) { NetId netId = NetIdRegistry.GetTransformNetId(transform); if (netId == NetId.INVALID) - return null; + continue; netId.id--; StructureDrop drop = NetIdRegistry.Get(netId); - return drop?.model ?? null; + if (drop != null) + return drop.model; } } diff --git a/SellDoor/Helpers/StructureHelper.cs b/SellDoor/Helpers/StructureHelper.cs index 39fec35..1f263f8 100644 --- a/SellDoor/Helpers/StructureHelper.cs +++ b/SellDoor/Helpers/StructureHelper.cs @@ -1,44 +1,10 @@ -using SDG.Unturned; -using System; -using System.Collections.Generic; +using SDG.Unturned; using UnityEngine; namespace RestoreMonarchy.SellDoor.Helpers { public class StructureHelper { - /* - public static StructureDrop FindStructureDropByPosition(Guid assetGuid, Vector3 point) - { - - List regions = new(); - Regions.getRegionsInRadius(point, 0.1f, regions); - - List results = new(); - StructureManager.getStructuresInRadius(point, 0.1f, regions, results); - - foreach (Transform result in results) - { - NetId netId = NetIdRegistry.GetTransformNetId(result); - if (netId == NetId.INVALID) - return null; - netId.id--; - StructureDrop drop = NetIdRegistry.Get(netId); - StructureData structureData = drop.GetServersideData(); - - if (structureData.point == point && drop.asset.GUID == assetGuid) - { - return drop; - } - } - - - - - return null; - } - */ - public static StructureDrop ForceDropStructure(ItemStructureAsset asset, Vector3 point, Vector3 angle, ulong owner, ulong group) { Structure structure = new(asset, asset.health); @@ -46,11 +12,9 @@ public static StructureDrop ForceDropStructure(ItemStructureAsset asset, Vector3 StructureManager.dropReplicatedStructure(structure, point, rotation, owner, group); - NetId dropNetId = new NetId(NetIdRegistry.counter - 2); - - StructureDrop drop = NetIdRegistry.Get(dropNetId); - - return drop; + // dropReplicatedStructure goes through ClaimBlock(2u): drop is at counter-1, transform at counter. + NetId dropNetId = new NetId(NetIdRegistry.counter - 1); + return NetIdRegistry.Get(dropNetId); } } } diff --git a/SellDoor/Models/TransformBase.cs b/SellDoor/Models/TransformBase.cs index 4f9d673..0897de5 100644 --- a/SellDoor/Models/TransformBase.cs +++ b/SellDoor/Models/TransformBase.cs @@ -25,31 +25,24 @@ public void UpdatePosition() if (AssetId == null) { - BarricadeDrop drop = null; NetId netId = NetIdRegistry.GetTransformNetId(Transform); if (netId != NetId.INVALID) { netId.id--; - drop = NetIdRegistry.Get(netId); - } - if (drop != null) - { - AssetId = drop.asset.GUID; - AssetName = drop.asset.itemName; - } - else - { - StructureDrop structureDrop = null; - NetId structureNetId = NetIdRegistry.GetTransformNetId(Transform); - if (structureNetId != NetId.INVALID) + BarricadeDrop drop = NetIdRegistry.Get(netId); + if (drop != null) { - structureNetId.id--; - structureDrop = NetIdRegistry.Get(structureNetId); + AssetId = drop.asset.GUID; + AssetName = drop.asset.itemName; } - if (structureDrop != null) + else { - AssetId = structureDrop.asset.GUID; - AssetName = structureDrop.asset.itemName; + StructureDrop structureDrop = NetIdRegistry.Get(netId); + if (structureDrop != null) + { + AssetId = structureDrop.asset.GUID; + AssetName = structureDrop.asset.itemName; + } } } }