#if CARBON using HarmonyLib; #else // Reference: 0Harmony using Harmony; #endif using System; using Newtonsoft.Json; namespace Oxide.Plugins { [Info("No Interference", "nivex", "0.1.3")] [Description("Configure interference on auto turrets.")] class NoInterference : RustPlugin { private void OnServerInitialized(bool isStartup) => PatchInterference(); private void Unload() => UnpatchInterference(); #region Interference Patch #if CARBON private Harmony _harmony; #else private HarmonyInstance _harmony; #endif private void PatchInterference() { #if CARBON _harmony = new Harmony(Name + "Patch"); _harmony.PatchAll(); #else _harmony = HarmonyInstance.Create(Name + "Patch"); Type[] patchType = { AccessTools.Inner(typeof(NoInterference), "AutoTurret_UpdateInterference"), AccessTools.Inner(typeof(NoInterference), "AutoTurret_UpdateInterferenceOnOthers"), }; foreach (var t in patchType) { new PatchProcessor(_harmony, t, HarmonyMethod.Merge(t.GetHarmonyMethods())).Patch(); } #endif foreach (var ent in BaseNetworkable.serverEntities) { if (ent is AutoTurret turret && turret.HasInterference()) { turret.SetFlag(BaseEntity.Flags.OnFire, HandleInterference(turret)); } } } private void UnpatchInterference() { _harmony?.UnpatchAll(Name + "Patch"); config = null; } [HarmonyPatch(typeof(AutoTurret), "UpdateInterference")] internal class AutoTurret_UpdateInterference { [HarmonyPrefix] internal static bool Prefix(AutoTurret __instance) => HandleInterference(__instance); } [HarmonyPatch(typeof(AutoTurret), "UpdateInterferenceOnOthers")] internal class AutoTurret_UpdateInterferenceOnOthers { [HarmonyPrefix] internal static bool Prefix(AutoTurret __instance) => HandleInterference(__instance); } private static bool HandleInterference(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 } }