FuelStream

CupboardProtection 1.5.2

Dec 2nd, 2020 (edited)
459
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System.Linq;
  4. using Oxide.Core;
  5. using Newtonsoft.Json;
  6.  
  7. namespace Oxide.Plugins
  8. {
  9.     [Info("CupboardProtection", "uMod Community", "1.5.2")]
  10.     [Description("Makes cupboards and their foundations invulnerable, unable to be destroyed.")]
  11.  
  12.     public class CupboardProtection : RustPlugin
  13.     {
  14.         private readonly int Mask = LayerMask.GetMask("Construction");
  15.  
  16.         #region Hooks
  17.  
  18.         private object OnEntityTakeDamage(DecayEntity entity, HitInfo info)
  19.         {
  20.             var Initiator = info.Initiator;
  21.             if (!Initiator) return null;
  22.  
  23.             if (entity.name.Contains("cupboard"))
  24.             {
  25.                 if (info.InitiatorPlayer != null && info.InitiatorPlayer.userID == entity.OwnerID)
  26.                     return null;
  27.                 Puts((CHook("CanDamageTc", Initiator, entity)).ToString());
  28.                 return CHook("CanDamageTc", Initiator, entity);
  29.             }
  30.  
  31.             if (!Configuration.foundation || !entity.name.Contains("foundation")) return null;
  32.             if (info.InitiatorPlayer != null && info.InitiatorPlayer.userID == entity.OwnerID)
  33.                 return null;
  34.             return IDData.IDs.Values.ToList().Exists(id => id == entity.net.ID) ? CHook("CanDamageTcFloor", Initiator, entity) : null;
  35.         }
  36.  
  37.         private static object CHook(string Name, BaseEntity Player, DecayEntity Entity)
  38.         {
  39.             var Hook = Interface.CallHook(Name, Player, Entity);
  40.             return Hook is bool ? Hook : false;
  41.         }
  42.  
  43.         private void Init()
  44.         {
  45.             LoadConfig();
  46.             if (!Configuration.foundation)
  47.             {
  48.                 Unsubscribe("OnEntityBuilt");
  49.                 Unsubscribe("OnEntityKill");
  50.             }
  51.             IDData = Interface.Oxide.DataFileSystem.ReadObject<Data>(Name);
  52.         }
  53.  
  54.         private void Unload() => SaveData();
  55.  
  56.         private void OnEntityBuilt(Planner plan, GameObject go)
  57.         {
  58.             var Priv = go.GetComponent<BaseEntity>() as BuildingPrivlidge;
  59.             if (!Priv) return;
  60.             var Foundation = GetFoundation(Priv);
  61.             if (!Foundation)
  62.                 return;
  63.             IDData.IDs.Add(Priv.net.ID, Foundation.net.ID);
  64.         }
  65.  
  66.         private BuildingBlock GetFoundation(BuildingPrivlidge Priv) => Physics.RaycastAll(Priv.transform.position + new Vector3(0,0.1f,0), Vector3.down, 2f, Mask, QueryTriggerInteraction.Ignore).Select(Hit => Hit.GetEntity() as BuildingBlock).FirstOrDefault(E => E);
  67.  
  68.         private void OnEntityKill(BuildingPrivlidge entity)
  69.         {
  70.             if (IDData.IDs.ContainsKey(entity.net.ID))
  71.                 IDData.IDs.Remove(entity.net.ID);
  72.         }
  73.         #endregion
  74.  
  75.         #region Data
  76.         private Data IDData;
  77.  
  78.         private class Data
  79.         {
  80.             public readonly Dictionary<uint, uint> IDs = new Dictionary<uint, uint>();
  81.         }
  82.  
  83.         private void SaveData()
  84.         {
  85.             Interface.Oxide.DataFileSystem.WriteObject(Name, IDData);
  86.         }
  87.         #endregion
  88.  
  89.         #region Configuration
  90.  
  91.         private ConfigFile Configuration;
  92.  
  93.         public class ConfigFile
  94.         {
  95.  
  96.             [JsonProperty("Foundation Invincible?")]
  97.             public bool foundation;
  98.  
  99.             public static ConfigFile DefaultConfig()
  100.             {
  101.                 return new ConfigFile()
  102.                 {
  103.                     foundation = true
  104.                 };
  105.             }
  106.         }
  107.  
  108.         protected override void LoadConfig()
  109.         {
  110.             base.LoadConfig();
  111.             Configuration = Config.ReadObject<ConfigFile>();
  112.             if (Config == null)
  113.             {
  114.                 LoadDefaultConfig();
  115.             }
  116.         }
  117.  
  118.         protected override void LoadDefaultConfig()
  119.         {
  120.             Configuration = ConfigFile.DefaultConfig();
  121.             PrintWarning("Default configuration has been loaded.");
  122.         }
  123.  
  124.         protected override void SaveConfig()
  125.         {
  126.             Config.WriteObject(Configuration);
  127.         }
  128.         #endregion
  129.     }
  130. }
RAW Paste Data