diff --git a/SAMSiteAuth.cs b/SAMSiteAuth.cs index 6992cb5..6336209 100644 --- a/SAMSiteAuth.cs +++ b/SAMSiteAuth.cs @@ -3,7 +3,7 @@ namespace Oxide.Plugins { - [Info("SAMSiteAuth", "haggbart", "2.4.3")] + [Info("SAMSiteAuth", "haggbart", "2.4.4")] [Description("Makes SAM Sites act in a similar fashion to shotgun traps and flame turrets.")] internal class SAMSiteAuth : RustPlugin { @@ -11,37 +11,77 @@ internal class SAMSiteAuth : RustPlugin private object OnSamSiteTarget(SamSite samSite, BaseCombatEntity target) { - var mountPoints = (target as BaseVehicle)?.mountPoints; - if (!IsOccupied(target, mountPoints)) + if (target is Drone drone) + return HandleDroneTarget(samSite, drone); + + if (target is BaseVehicle vehicle) + return HandleVehicleTarget(samSite, vehicle); + + return null; + } + + // Mimic auto turrets, which check the auth of the drone owner (not the drone controller) + private object HandleDroneTarget(SamSite samSite, Drone drone) + { + // Don't interfere with static sam sites because players cannot be authorized to them + if (samSite.staticRespawn) + return null; + + // Don't interfere with drones that have no owner (we don't know whose auth to check) + if (drone.OwnerID == 0) + return null; + + // Don't interfere with sam sites that don't have a cupboard in range + var cupboard = samSite.GetBuildingPrivilege(samSite.WorldSpaceBounds()); + if (cupboard is null) + return null; + + // Block targeting if the drone owner is authed + if (cupboard.IsAuthed(drone.OwnerID)) return True; + // Don't interfere with targeting in this case + return null; + } + + private object HandleVehicleTarget(SamSite samSite, BaseVehicle vehicle) + { + // Don't target empty vehicles (intentionally applies even to static sam sites) + var mountPoints = vehicle.mountPoints; + if (!IsOccupied(vehicle, mountPoints)) + return True; + + // Don't interfere with static sam sites because players cannot be authorized to them if (samSite.staticRespawn) return null; + // Don't interfere with sam sites that don't have a cupboard in range var cupboard = samSite.GetBuildingPrivilege(samSite.WorldSpaceBounds()); - if ((object)cupboard == null) + if (cupboard is null) return null; + // Block targeting if any mounted player is authed if (mountPoints != null) { foreach (var mountPoint in mountPoints) { var player = mountPoint.mountable.GetMounted(); - if ((object)player != null && IsAuthed(cupboard, player.userID)) + if (player is not null && cupboard.IsAuthed(player.userID)) return True; } } - foreach (var child in target.children) + // Block targeting if any passenger is authed (e.g., in the back of a Scrap Heli) + foreach (var child in vehicle.children) { - var player = child as BasePlayer; - if ((object)player != null) + if (child is BasePlayer player) { - if (IsAuthed(cupboard, player.userID)) + if (cupboard.IsAuthed(player.userID)) return True; } } + // Don't interfere with targeting in this case return null; } @@ -52,7 +92,7 @@ private static bool IsOccupied(BaseCombatEntity entity, List mou foreach (var mountPoint in mountPoints) { var player = mountPoint.mountable.GetMounted(); - if ((object)player != null) + if (player is not null) return true; } } @@ -65,16 +105,5 @@ private static bool IsOccupied(BaseCombatEntity entity, List mou return false; } - - private static bool IsAuthed(BuildingPrivlidge cupboard, ulong userId) - { - foreach (var entry in cupboard.authorizedPlayers) - { - if (entry.userid == userId) - return true; - } - - return false; - } } } \ No newline at end of file