using HarmonyLib; using System; using Newtonsoft.Json; namespace Oxide.Plugins; [Info("No Interference", "nivex", "0.1.811")] [Description("Configure interference on auto turrets.")] class NoInterference : RustPlugin { private void OnServerInitialized(bool isStartup) => PatchInterference(); private void Unload() => UnpatchInterference(); #region Interference Patch private Harmony _harmony; private void PatchInterference() { _harmony = new Harmony(Name + "Patch"); _harmony.PatchAll(); foreach (var ent in BaseNetworkable.serverEntities) { if (ent is AutoTurret turret && turret.HasInterference() && !AllowInterference(turret)) { turret.SetFlag(BaseEntity.Flags.OnFire, false); } } } private void UnpatchInterference() { _harmony?.UnpatchAll(Name + "Patch"); config = null; } [HarmonyPatch(typeof(AutoTurret), "TryRegisterForInterferenceUpdate")] internal class AutoTurret_TryRegisterForInterferenceUpdate { [HarmonyPrefix] internal static bool Prefix(AutoTurret __instance) => AllowInterference(__instance); } private object OnInterferenceOthersUpdate(AutoTurret turret) { return OnInterferenceUpdate(turret); } private object OnInterferenceUpdate(AutoTurret turret) { return !AllowInterference(turret) ? true : (object)null; } private static bool AllowInterference(AutoTurret __instance) { if ((object)__instance == null || config == null) { return true; } return __instance.OwnerID.IsSteamId() ? !config.players : !config.other; } #endregion Interference Patch #region Configuration private static Configuration config; public class Configuration { [JsonProperty(PropertyName = "No interference on player turrets")] public bool players { get; set; } [JsonProperty(PropertyName = "No interference on other turrets")] public bool other { get; set; } = true; } protected override void LoadConfig() { base.LoadConfig(); config = Config.ReadObject(); config ??= new(); SaveConfig(); } protected override void SaveConfig() => Config.WriteObject(config); protected override void LoadDefaultConfig() => config = new(); #endregion Configuration }