using HarmonyLib; using Newtonsoft.Json; using System.Collections; using System.Reflection; namespace Oxide.Plugins { [Info("No Interference", "nivex", "0.1.9")] [Description("Configure interference on auto turrets.")] class NoInterference : RustPlugin { private Harmony _harmony; private MethodInfo TryRegisterForInterferenceUpdateMethod = AccessTools.Method(typeof(AutoTurret), "TryRegisterForInterferenceUpdate"); private void OnServerInitialized() { if (TryRegisterForInterferenceUpdateMethod != null) { _harmony = new Harmony(Name + "Patch"); MethodInfo patchMethod = AccessTools.Method(typeof(AutoTurret_TryRegisterForInterferenceUpdate_Patch), "Prefix"); _harmony.Patch(TryRegisterForInterferenceUpdateMethod, prefix: patchMethod); Puts($"Patched {TryRegisterForInterferenceUpdateMethod.DeclaringType} {TryRegisterForInterferenceUpdateMethod} {patchMethod}"); } ServerMgr.Instance.StartCoroutine(Loop()); } private void Unload() { if (_harmony != null) { _harmony.Unpatch(TryRegisterForInterferenceUpdateMethod, HarmonyPatchType.Prefix, _harmony.Id); } config = null; } private IEnumerator Loop() { int checks = 0; foreach (var ent in BaseNetworkable.serverEntities) { if (++checks % 300 == 0) { yield return null; } if (ent is AutoTurret turret && turret != null && turret.HasInterference() && !AllowInterference(turret)) { turret.SetFlag(BaseEntity.Flags.OnFire, false); } } } private class AutoTurret_TryRegisterForInterferenceUpdate_Patch { 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 (__instance == null || config == null) { return false; } return __instance.OwnerID.IsSteamId() ? !config.players : !config.other; } #region Configuration private static Configuration config; public class Configuration { [JsonProperty(PropertyName = "No interference on player turrets")] public bool players { get; set; } = true; [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 } }