Advertisement
Brandan

Rust Plugin - Protect Decay Zones

Dec 8th, 2016
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. /*
  2.     Author:     Brandan Lasley
  3.     Date:       12/8/2016
  4.     Purpose:    Manages custom zones in rust which can prevent decay for certian users.
  5.     File:       ProtectedDecayZones.cs
  6.     Version:    1.0
  7. */
  8.  
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using Oxide.Core;
  12. using System;
  13.  
  14. namespace Oxide.Plugins
  15. {
  16.     class ProtectedBuildZones : RustPlugin
  17.     {
  18.         #region Config
  19.         private const float _damage = 0.0f; // Mutiply by zero (ie. No damage)
  20.         private const float _radius = 40.0f;
  21.         #endregion
  22.  
  23.         #region DataStructures
  24.         struct BaseZoneInfo
  25.         {
  26.             public Vector3 Pos;
  27.             public float Radius;
  28.             public ulong Owner;
  29.         };
  30.         #endregion
  31.  
  32.         #region DataTypes
  33.         private Dictionary<BasePlayer, BaseZoneInfo> VIPBuildData;
  34.         #endregion  
  35.  
  36.         #region HelperFunctions
  37.         private bool hasPlayerSetTheirBase(BasePlayer p, ref BaseZoneInfo pInfo)
  38.         {
  39.             if (VIPBuildData.ContainsKey(p))
  40.             {
  41.                 pInfo = VIPBuildData[p];
  42.                 return true;
  43.             }
  44.             return false;
  45.         }
  46.  
  47.         private bool isPlayerAuthorized(BasePlayer p)
  48.         {
  49.             if (p.net.connection.authLevel == 2)
  50.             {
  51.                 return true;
  52.             }
  53.             return false;
  54.         }
  55.  
  56.         private bool isBlockWithinRadius(BaseEntity bInfo, BaseZoneInfo pInfo)
  57.         {
  58.             return bInfo.Distance2D(pInfo.Pos) <= pInfo.Radius;
  59.         }
  60.  
  61.         private bool isBlockOwnedByVIP(BaseEntity bInfo, ref BaseZoneInfo pInfo)
  62.         {
  63.             BasePlayer p = BasePlayer.FindByID(bInfo.OwnerID);
  64.             return (isPlayerAuthorized(p) && hasPlayerSetTheirBase(p, ref pInfo));
  65.         }
  66.         #endregion
  67.  
  68.         #region Chat
  69.         [ChatCommand("SetBase")]
  70.         private void setBase(BasePlayer p, string myCommand, string[] myArgs)
  71.         {
  72.             if (myArgs.Length == 1)
  73.             {
  74.                 // ================================ ADMIN/TEST =========================================
  75.                 try
  76.                 {
  77.                     if (p.net.connection.authLevel == 2)
  78.                     {
  79.                         // Admin custom radius command.
  80.                         float radius = float.Parse(myArgs[0]);
  81.                         addPlayer(p, radius);
  82.                     }
  83.                 }
  84.                 catch (FormatException)
  85.                 {
  86.                     Puts("Error not a valid float!");
  87.                 }
  88.                 catch (OverflowException)
  89.                 {
  90.                     Puts("Value is too large or too small!");
  91.                 }
  92.                 // =====================================================================================
  93.             }
  94.             else if (myArgs.Length == 0)
  95.             {
  96.                 if (isPlayerAuthorized(p))
  97.                     addPlayer(p);
  98.             }
  99.             else
  100.             {
  101.                 Puts("Too many parameters!");
  102.             }
  103.         }
  104.  
  105.         [ChatCommand("RemoveBase")]
  106.         private void removeBase(BasePlayer p, string myCommand, string[] myArgs)
  107.         {
  108.             if (isPlayerAuthorized(p))
  109.                 removePlayer(p, p.transform.position);
  110.         }
  111.         #endregion
  112.  
  113.         #region MaintenanceFunctions
  114.         private bool addPlayer(BasePlayer p, float rad = _radius)
  115.         {
  116.             BaseZoneInfo PlayerData;
  117.             PlayerData.Pos = p.transform.position;
  118.             PlayerData.Radius = rad;
  119.             PlayerData.Owner = p.userID;
  120.             VIPBuildData[p] = PlayerData;
  121.             SaveAllData(); // In case server crashes.
  122.             return true;
  123.         }
  124.  
  125.         private bool removePlayer(BasePlayer p, Vector3 pos)
  126.         {
  127.             if (VIPBuildData.ContainsKey(p))
  128.             {
  129.                 VIPBuildData.Remove(p);
  130.                 SaveAllData(); // In case server crashes.
  131.                 return true;
  132.             }
  133.             return false;
  134.         }
  135.  
  136.         private void SaveAllData()
  137.         {
  138.             Interface.Oxide.DataFileSystem.WriteObject("ProtectedDecayZones", VIPBuildData);
  139.         }
  140.  
  141.         private void LoadAllData()
  142.         {
  143.             VIPBuildData = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<BasePlayer, BaseZoneInfo>>("ProtectedDecayZones");
  144.         }
  145.         #endregion
  146.  
  147.         #region Hooks
  148.         void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo hitInfo)
  149.         {
  150.             if (!hitInfo.damageTypes.Has(Rust.DamageType.Decay)) return;
  151.             var block = entity as BuildingBlock;
  152.             BaseZoneInfo pInfo = new BaseZoneInfo();
  153.  
  154.             if (isBlockOwnedByVIP(entity, ref pInfo))
  155.             {
  156.                 if (isBlockWithinRadius(entity, pInfo))
  157.                 {
  158.                     hitInfo.damageTypes.Scale(Rust.DamageType.Decay, _damage);
  159.                 }
  160.             }
  161.         }
  162.  
  163.         void Loaded()
  164.         {
  165.             VIPBuildData = new Dictionary<BasePlayer, BaseZoneInfo>();
  166.  
  167.             LoadAllData();
  168.         }
  169.  
  170.         void Unload()
  171.         {
  172.             SaveAllData();
  173.         }
  174.         #endregion
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement