using Newtonsoft.Json; using System.Collections.Generic; using UnityEngine; namespace Oxide.Plugins { [Info("MinPopEvents", "skavi", "2.1.0")] [Description("Only allow events when the population is above a threshold")] class MinPopEvents : RustPlugin { private ConfigFile config; private bool isConfiguredEvent(string prefabPath, ConfigFile.EventConfig config) { return prefabPath.Equals(config?.prefab); } private bool eventConfigCancelsEvent(string prefabPath, ConfigFile.EventConfig config, string configName, int numberOfActivePlayers) { if (!isConfiguredEvent(prefabPath, config)) { return false; } if (numberOfActivePlayers < config.minPop) { Puts("Cancelling " + configName + " event because active population ({0}) is less than configured minimum of {1}", numberOfActivePlayers, config.minPop); return true; } return false; } #region Hooks void OnEntitySpawned(BaseNetworkable networkable) { int numberOfActivePlayers = BasePlayer.activePlayerList.Count; if (networkable is BradleyAPC) { if (numberOfActivePlayers < config.bradley?.minPop) { Puts("Killing Bradley because pop ({0}) is less configured minimum of {1}", numberOfActivePlayers, config.bradley.minPop); networkable.Kill(BaseNetworkable.DestroyMode.None); } } } object OnEventTrigger(TriggeredEventPrefab prefab) { string prefabPath = prefab?.targetPrefab?.resourcePath; if (prefabPath == null) { return null; } int numberOfActivePlayers = BasePlayer.activePlayerList.Count; if (eventConfigCancelsEvent(prefabPath, config.cargoPlane, "cargo plane", numberOfActivePlayers)) { return false; } if (eventConfigCancelsEvent(prefabPath, config.santasSleigh, "santa's sleigh", numberOfActivePlayers)) { return false; } if (eventConfigCancelsEvent(prefabPath, config.patrolHeli, "patrol helicopter", numberOfActivePlayers)) { return false; } if (eventConfigCancelsEvent(prefabPath, config.chinook, "CH47 helicopter", numberOfActivePlayers)) { return false; } if (eventConfigCancelsEvent(prefabPath, config.cargoShip, "cargo ship", numberOfActivePlayers)) { return false; } if (eventConfigCancelsEvent(prefabPath, config.easterEggHunt, "easter egg hunt", numberOfActivePlayers)) { return false; } if (eventConfigCancelsEvent(prefabPath, config.halloweenHunt, "halloween hunt", numberOfActivePlayers)) { return false; } if (isConfiguredEvent(prefabPath, config.cargoShip) && (config.cargoShip.lootRoundsMinPop?.Count ?? 0) > 0) { int lootRounds = 0; while (lootRounds < config.cargoShip.lootRoundsMinPop.Count && numberOfActivePlayers >= config.cargoShip.lootRoundsMinPop[lootRounds]) { lootRounds++; } CargoShip.loot_rounds = lootRounds + 1; } return null; } #endregion #region Config void Init() { LoadConfig(); SaveConfig(); } protected override void LoadConfig() { base.LoadConfig(); config = Config.ReadObject(); } protected override void LoadDefaultConfig() => config = ConfigFile.getDefaultConfigFile(); protected override void SaveConfig() => Config.WriteObject(config); class ConfigFile { [JsonProperty(PropertyName = "Bradley APC")] public BradleyConfig bradley = new BradleyConfig(); [JsonProperty(PropertyName = "Cargo plane")] public EventConfig cargoPlane = new EventConfig("assets/prefabs/npc/cargo plane/cargo_plane.prefab"); [JsonProperty(PropertyName = "Santa's sleigh")] public EventConfig santasSleigh = new EventConfig("assets/prefabs/misc/xmas/sleigh/santasleigh.prefab"); [JsonProperty(PropertyName = "CH47 helicopter")] public EventConfig chinook = new EventConfig("assets/prefabs/npc/ch47/ch47scientists.entity.prefab"); [JsonProperty(PropertyName = "Patrol helicopter")] public EventConfig patrolHeli = new EventConfig("assets/prefabs/npc/patrol helicopter/patrolhelicopter.prefab"); [JsonProperty(PropertyName = "Easter egg hunt")] public EventConfig easterEggHunt = new EventConfig("assets/prefabs/misc/easter/egghunt.prefab"); [JsonProperty(PropertyName = "Halloween hunt")] public EventConfig halloweenHunt = new EventConfig("assets/prefabs/misc/halloween/halloweenhunt.prefab"); [JsonProperty(PropertyName = "Cargo ship")] public CargoShipConfig cargoShip = new CargoShipConfig(); public static ConfigFile getDefaultConfigFile() { return new ConfigFile(); } public class BradleyConfig { [JsonProperty(PropertyName = "Minimum players")] public int minPop = 0; } public class EventConfig { [JsonProperty(PropertyName = "Minimum players")] public int minPop = 0; [JsonProperty(PropertyName = "Prefab")] public string prefab; public EventConfig() { } public EventConfig(string prefab) { this.prefab = prefab; } } public class CargoShipConfig : EventConfig { [JsonProperty(PropertyName = "Noninitial loot round minimum players")] public List lootRoundsMinPop = new List(); public CargoShipConfig() : base("assets/content/vehicles/boats/cargoship/cargoshiptest.prefab") { } } } #endregion } }