Judess69er

WaterWorks Water Pistol/Gun addition

Sep 16th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 10.99 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using Oxide.Core;
  3. using System;
  4. using System.Linq;
  5.  
  6. /*
  7. Added `Reset To Vanilla Defaults On Unload` (false)
  8. Server restarts no longer reset to vanilla defaults
  9. */
  10.  
  11. namespace Oxide.Plugins
  12. {
  13.     [Info("Water Works", "nivex", "1.0.7")]
  14.     [Description("Control the monopoly on your water supplies.")]
  15.     class WaterWorks : RustPlugin
  16.     {
  17.         private ItemModContainer bucketWaterMod;
  18.         private ItemModContainer waterJugMod;
  19.         private ItemModContainer pistolWaterMod;
  20.         private ItemModContainer gunWaterMod;
  21.         private ItemDefinition waterDef;
  22.  
  23.         private void Init()
  24.         {
  25.             Unsubscribe(nameof(OnEntitySpawned));
  26.         }
  27.  
  28.         private void OnServerInitialized()
  29.         {
  30.             bucketWaterMod = ItemManager.FindItemDefinition("bucket.water")?.GetComponent<ItemModContainer>();
  31.             waterJugMod = ItemManager.FindItemDefinition("waterjug")?.GetComponent<ItemModContainer>();
  32.             pistolWaterMod = ItemManager.FindItemDefinition("pistol.water")?.GetComponent<ItemModContainer>();
  33.             gunWaterMod = ItemManager.FindItemDefinition("gun.water")?.GetComponent<ItemModContainer>();
  34.             waterDef = ItemManager.FindItemDefinition("water");
  35.  
  36.             SetLiquidContainerStackSize(true);
  37.             ConfigureWaterDefinitions(true);
  38.             Subscribe(nameof(OnEntitySpawned));
  39.         }
  40.  
  41.         private void Unload()
  42.         {
  43.             if (Interface.Oxide.IsShuttingDown)
  44.             {
  45.                 return;
  46.             }
  47.  
  48.             if (_config.Reset)
  49.             {
  50.                 SetLiquidContainerStackSize(false);
  51.                 ConfigureWaterDefinitions(false);
  52.             }
  53.         }
  54.  
  55.         private void OnEntitySpawned(LiquidContainer lc)
  56.         {
  57.             SetLiquidContainerStackSize(lc, true);
  58.         }
  59.  
  60.         private void SetSlotAmounts(LiquidContainer lc, int num)
  61.         {
  62.             var slot = lc.inventory.GetSlot(0);
  63.  
  64.             if (slot != null && slot.amount > num)
  65.             {
  66.                 slot.amount = num;                
  67.             }
  68.         }
  69.  
  70.         private void SetLiquidContainerStackSize(bool state)
  71.         {
  72.             foreach (var lc in BaseNetworkable.serverEntities.OfType<LiquidContainer>())
  73.             {
  74.                 SetLiquidContainerStackSize(lc, state);
  75.             }
  76.         }
  77.  
  78.         private void SetLiquidContainerStackSize(LiquidContainer lc, bool state)
  79.         {
  80.             if (lc == null || lc.IsDestroyed)
  81.             {
  82.                 return;
  83.             }
  84.  
  85.             if (lc.prefabID == 3746060889 && _config.WaterBarrel > 0)
  86.             {
  87.                 int num = state ? _config.WaterBarrel : 20000;
  88.  
  89.                 SetSlotAmounts(lc, num);
  90.  
  91.                 lc.maxStackSize = num;
  92.                 lc.inventory.maxStackSize = num;
  93.                 lc.MarkDirty();
  94.                 lc.inventory.MarkDirty();                
  95.                 lc.SendNetworkUpdateImmediate();
  96.             }
  97.             else if (lc is WaterCatcher)
  98.             {
  99.                 UpdateWaterCatcher(lc as WaterCatcher, state);
  100.             }
  101.             else if (lc.prefabID == 1259335874 && _config.PoweredWaterPurifier > 0)
  102.             {
  103.                 int num = state ? _config.PoweredWaterPurifier : 5000;
  104.  
  105.                 SetSlotAmounts(lc, num);
  106.  
  107.                 lc.maxStackSize = num;
  108.                 lc.inventory.maxStackSize = num;
  109.                 lc.MarkDirty();
  110.                 lc.inventory.MarkDirty();
  111.                 lc.SendNetworkUpdateImmediate();
  112.             }
  113.             else if (lc.prefabID == 2905007296 && _config.WaterPurifier > 0)
  114.             {
  115.                 var purifier = lc as WaterPurifier;
  116.                 int num = state ? _config.WaterPurifier : 5000;
  117.                
  118.                 SetSlotAmounts(lc, num);
  119.  
  120.                 purifier.stopWhenOutputFull = true;
  121.                 purifier.waterStorage.maxStackSize = num;
  122.                 purifier.maxStackSize = num;
  123.                 purifier.inventory.maxStackSize = num;
  124.                 purifier.MarkDirty();
  125.                 purifier.inventory.MarkDirty();
  126.                 purifier.SendNetworkUpdateImmediate();
  127.             }
  128.             else if (lc is WaterPump && _config.WaterPump.StackSize > 0)
  129.             {
  130.                 var pump = lc as WaterPump;
  131.                 int num = state ? _config.WaterPump.StackSize : 2000;
  132.  
  133.                 SetSlotAmounts(lc, num);
  134.  
  135.                 pump.maxStackSize = num;
  136.                 pump.inventory.maxStackSize = num;
  137.                 pump.MarkDirty();
  138.                 pump.inventory.MarkDirty();
  139.                 pump.SendNetworkUpdateImmediate();
  140.                 UpdateWaterPump(pump, state);
  141.             }
  142.         }
  143.  
  144.         private void ConfigureWaterDefinitions(bool state)
  145.         {
  146.             if (bucketWaterMod != null) bucketWaterMod.maxStackSize = state ? _config.WaterBucket : 2000;
  147.             if (waterJugMod != null) waterJugMod.maxStackSize = state ? _config.WaterJug : 5000;
  148.             if (pistolWaterMod != null) pistolWaterMod.maxStackSize = state ? _config.WaterPistol : 250;
  149.             if (gunWaterMod != null) gunWaterMod.maxStackSize = state ? _config.WaterGun : 1000;
  150.             if (waterDef != null) waterDef.stackable = state ? _config.WaterJug * 62 : 249999;
  151.  
  152.         }
  153.  
  154.         private void UpdateWaterCatcher(WaterCatcher wc, bool state)
  155.         {
  156.             if (state)
  157.             {
  158.                 wc.maxStackSize = wc.inventory.maxStackSize = wc.prefabID == 3418194637 ? _config.LargeWaterCatcher.StackSize : _config.SmallWaterCatcher.StackSize;
  159.                 wc.maxItemToCreate = wc.prefabID == 3418194637 ? _config.LargeWaterCatcher.Amount : _config.SmallWaterCatcher.Amount;
  160.             }
  161.             else
  162.             {
  163.                 wc.maxStackSize = wc.inventory.maxStackSize = wc.prefabID == 3418194637 ? 50000 : 10000;
  164.                 wc.maxItemToCreate = wc.prefabID == 3418194637 ? 30 : 10;
  165.             }
  166.  
  167.             SetSlotAmounts(wc, wc.maxStackSize);
  168.  
  169.             wc.Invoke(() =>
  170.             {
  171.                 if (wc.IsDestroyed) return;
  172.                 wc.CancelInvoke(wc.CollectWater);
  173.  
  174.                 if (state)
  175.                 {
  176.                     float interval = wc.prefabID == 3418194637 ? _config.LargeWaterCatcher.Interval : _config.SmallWaterCatcher.Interval;
  177.                     wc.InvokeRepeating(wc.CollectWater, interval, interval);
  178.                 }
  179.                 else wc.InvokeRandomized(wc.CollectWater, WaterCatcher.collectInterval, WaterCatcher.collectInterval, 6f);
  180.             }, 0.1f);
  181.         }
  182.  
  183.         private void UpdateWaterPump(WaterPump pump, bool state)
  184.         {
  185.             pump.PumpInterval = state ? _config.WaterPump.Interval : 10f;
  186.             pump.AmountPerPump = state ? _config.WaterPump.Amount : 85;
  187.             pump.CancelInvoke(pump.CreateWater);
  188.             pump.InvokeRandomized(pump.CreateWater, pump.PumpInterval, pump.PumpInterval, pump.PumpInterval * 0.1f);
  189.         }
  190.  
  191.         #region Configuration
  192.  
  193.         private Configuration _config;
  194.  
  195.         private class SmallWaterCatcherSettings
  196.         {
  197.             [JsonProperty(PropertyName = "Stack Size (vanilla: 10000)")]
  198.             public int StackSize { get; set; } = 50000;
  199.  
  200.             [JsonProperty(PropertyName = "Add Amount Of Water (vanilla: 10)")]
  201.             public float Amount { get; set; } = 20f;
  202.  
  203.             [JsonProperty(PropertyName = "Add Water Every X Seconds (vanilla: 60)")]
  204.             public float Interval { get; set; } = 30f;
  205.         }
  206.  
  207.         private class LargeWaterCatcherSettings
  208.         {
  209.             [JsonProperty(PropertyName = "Stack Size (vanilla: 50000)")]
  210.             public int StackSize { get; set; } = 250000;
  211.  
  212.             [JsonProperty(PropertyName = "Add Amount Of Water (vanilla: 30)")]
  213.             public float Amount { get; set; } = 60f;
  214.  
  215.             [JsonProperty(PropertyName = "Add Water Every X Seconds (vanilla: 60)")]
  216.             public float Interval { get; set; } = 30f;
  217.         }
  218.  
  219.         private class WaterPumpSettings
  220.         {
  221.             //[JsonProperty(PropertyName = "Max Output (vanilla: 12)")]
  222.             //public int Output { get; set; } = 20;
  223.  
  224.             [JsonProperty(PropertyName = "ML Capacity (vanilla: 2000)")]
  225.             public int StackSize { get; set; } = 10000;
  226.  
  227.             [JsonProperty(PropertyName = "Add Amount Of Water To Water Pumps")]
  228.             public int Amount { get; set; } = 130;
  229.  
  230.             [JsonProperty(PropertyName = "Add Water To Water Pumps Every X Seconds")]
  231.             public float Interval { get; set; } = 10f;
  232.         }
  233.  
  234.         private class Configuration
  235.         {
  236.             [JsonProperty(PropertyName = "Small Water Catcher")]
  237.             public SmallWaterCatcherSettings SmallWaterCatcher { get; set; } = new SmallWaterCatcherSettings();
  238.  
  239.             [JsonProperty(PropertyName = "Large Water Catcher")]
  240.             public LargeWaterCatcherSettings LargeWaterCatcher { get; set; } = new LargeWaterCatcherSettings();
  241.  
  242.             [JsonProperty(PropertyName = "Water Pump")]
  243.             public WaterPumpSettings WaterPump { get; set; } = new WaterPumpSettings();
  244.  
  245.             [JsonProperty(PropertyName = "Water Jug ML Capacity (vanilla: 5000)")]
  246.             public int WaterJug { get; set; } = 25000;
  247.  
  248.             [JsonProperty(PropertyName = "Water Barrel ML Capacity (vanilla: 20000)")]
  249.             public int WaterBarrel { get; set; } = 100000;
  250.  
  251.             [JsonProperty(PropertyName = "Water Bucket ML Capacity (vanilla: 2000)")]
  252.             public int WaterBucket { get; set; } = 10000;
  253.  
  254.             [JsonProperty(PropertyName = "Water Purifier ML Capacity (vanilla: 5000)")]
  255.             public int WaterPurifier { get; set; } = 25000;
  256.  
  257.             [JsonProperty(PropertyName = "Water Purifier (Powered) ML Capacity (vanilla: 5000)")]
  258.             public int PoweredWaterPurifier { get; set; } = 25000;
  259.  
  260.             [JsonProperty(PropertyName = "Water Pistol ML Capacity (vanilla: 250)")]
  261.             public int WaterPistol { get; set; } = 500;
  262.  
  263.             [JsonProperty(PropertyName = "Water Gun ML Capacity (vanilla: 1000)")]
  264.             public int WaterGun { get; set; } = 2000;
  265.  
  266.             [JsonProperty(PropertyName = "Reset To Vanilla Defaults On Unload")]
  267.             public bool Reset { get; set; }
  268.         }
  269.  
  270.         protected override void LoadConfig()
  271.         {
  272.             base.LoadConfig();
  273.  
  274.             try
  275.             {
  276.                 _config = Config.ReadObject<Configuration>();
  277.                 if (_config == null) throw new Exception();
  278.             }
  279.             catch
  280.             {
  281.                 PrintError("Your configuration file contains an error. Using default configuration values.");
  282.                 LoadDefaultConfig();
  283.             }
  284.  
  285.             SaveConfig();
  286.         }
  287.  
  288.         protected override void SaveConfig() => Config.WriteObject(_config);
  289.  
  290.         protected override void LoadDefaultConfig() => _config = new Configuration();
  291.  
  292.         #endregion
  293.     }
  294. }
  295.  
Advertisement
Add Comment
Please, Sign In to add comment