/*▄▄▄ ███▄ ▄███▓ ▄████ ▄▄▄██▀▀▀▓█████▄▄▄█████▓ ▓█████▄ ▓██▒▀█▀ ██▒ ██▒ ▀█▒ ▒██ ▓█ ▀▓ ██▒ ▓▒ ▒██▒ ▄██▓██ ▓██░▒██░▄▄▄░ ░██ ▒███ ▒ ▓██░ ▒░ ▒██░█▀ ▒██ ▒██ ░▓█ ██▓▓██▄██▓ ▒▓█ ▄░ ▓██▓ ░ ░▓█ ▀█▓▒██▒ ░██▒░▒▓███▀▒ ▓███▒ ░▒████▒ ▒██▒ ░ ░▒▓███▀▒░ ▒░ ░ ░ ░▒ ▒ ▒▓▒▒░ ░░ ▒░ ░ ▒ ░░ ▒░▒ ░ ░ ░ ░ ░ ░ ▒ ░▒░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░*/ using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace Oxide.Plugins { [Info("SteamGenerator", "bmgjet", "1.0.0")] [Description("Produce power with steam")] class SteamGenerator : RustPlugin { //Reference List private Dictionary _WaterPurifier = new Dictionary(); private int PowerPerSteamTick = 50; //How much power to add to battery per tick #region ChatCommand [ChatCommand("killsg")] private void AdminKillAllSteamGenerators(BasePlayer player, string command, string[] args) { if (player.IsAdmin) //limit to admin { //Remove All Batterys Parented To WaterPurifiers foreach (var generator in _WaterPurifier) { if (generator.Value != null && !generator.Value.IsDestroyed) { generator.Value.Kill(); } } _WaterPurifier.Clear(); Puts("Removed All Steam Generators!"); } } #endregion #region Oxide Hooks //UnSub Spawn Hook Since Will Scan Server On Startup private void Init() { Unsubscribe("OnEntitySpawned"); } //Scan Server For All WaterPurifiers private void OnServerInitialized(bool initial) { ServerMgr.Instance.StartCoroutine(Startup(initial)); } //Water Purifier Cook Tick private void OnWaterPurify(WaterPurifier purifier) { SteamPower(purifier); } //Catch Water Purifiers spawning private void OnEntitySpawned(WaterPurifier waterPurifier) { if (waterPurifier != null) { ModP(waterPurifier); } } //Admin Kill private void OnEntityKill(WaterPurifier entity) { DoRemove(entity); } //Killed normally private void OnEntityDeath(WaterPurifier entity) { DoRemove(entity); } //Remove Tool Killed private void OnNormalRemovedEntity(BasePlayer player, BaseEntity entity) { DoRemove(entity); } //Block Hooking Into Battery Input private object OnWireConnect(BasePlayer player, IOEntity dest) { if (dest == null || !(dest is ElectricBattery)) { return null; } //Run normal code if (_WaterPurifier.ContainsValue(dest as ElectricBattery)) { return true; } return null; } #endregion #region Methods private IEnumerator Startup(bool initial) { yield return CoroutineEx.waitForSeconds(initial ? 15 : 3); //Add delay on startup more for first start int checks = 0; int last = 0; int done = 0; int count = 0; int loops = BaseNetworkable.serverEntities.entityList.Get().Count(); foreach (var ent in BaseNetworkable.serverEntities.entityList.Get().Values.ToList()) //List since might be changed during wait times. { done++; if (++checks >= 10000) //Check conditions so many loops { //Limit rate based on FPS if (Performance.report.frameRate < 15 && ConVar.FPS.limit > 15) { yield return CoroutineEx.waitForSeconds(0.01f); } else { yield return CoroutineEx.waitForSeconds(0.0035f); } checks = 0; //Output Percentage Debug if (done > last) { last += (loops / 5); Puts("Scanning WaterPurifiers " + (int)Math.Round((double)(100 * done) / loops) + "%"); } } //Found Purifier Not Already In List if (ent != null && ent is WaterPurifier && !_WaterPurifier.Keys.Contains(ent.net.ID.Value)) { ModP(ent as WaterPurifier); //Apply Mod count++; } } Puts("Found " + count + " WaterPurifiers."); Subscribe("OnEntitySpawned"); //Allow spawn hook to catch from now on } //Clean Up List Something Has Been Removed private void DoRemove(BaseNetworkable bn) { if (bn == null || bn?.net?.ID == null || !(bn is WaterPurifier)) { return; } if (_WaterPurifier.ContainsKey(bn.net.ID.Value)) { _WaterPurifier.Remove(bn.net.ID.Value); } } //Find Battery Already Attached private ElectricBattery AlreadyModded(WaterPurifier waterPurifier) { foreach (var child in waterPurifier.children) { if (child is ElectricBattery) { return child as ElectricBattery; } } return null; } //Add Power To Battery private void SteamPower(WaterPurifier waterPurifier) { if (_WaterPurifier.ContainsKey(waterPurifier.net.ID.Value)) { ElectricBattery battery = _WaterPurifier[waterPurifier.net.ID.Value]; if (battery != null) { float oldCharge = battery.rustWattSeconds; battery.rustWattSeconds += PowerPerSteamTick; battery.rustWattSeconds = Mathf.Clamp(battery.rustWattSeconds, 0f, battery.maxCapactiySeconds); //Limit Max Value battery.ChargeChanged(oldCharge); //Send Updates } else { _WaterPurifier.Remove(waterPurifier.net.ID.Value); } } } //Get Attached Battery Or Create One private void ModP(WaterPurifier waterPurifier) { ElectricBattery power = AlreadyModded(waterPurifier); if (power != null) { _WaterPurifier.Add(waterPurifier.net.ID.Value, power); return; } power = GameManager.server.CreateEntity("assets/prefabs/deployable/playerioents/batteries/smallrechargablebattery.deployed.prefab", waterPurifier.transform.position + waterPurifier.transform.rotation * (new Vector3(.01f, 0.73f, -0.23f)), waterPurifier.transform.rotation * Quaternion.Euler(55, 0, 0)) as ElectricBattery; power.Spawn(); power.rustWattSeconds = 0; power.decay = null; power.pickup.enabled = false; power.SetParent(waterPurifier, true, true); _WaterPurifier.Add(waterPurifier.net.ID.Value, power); } #endregion } }