Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Brandan Lasley
- Date: 12/8/2016
- Purpose: Manages custom zones in rust which can prevent decay for certian users.
- File: ProtectedDecayZones.cs
- Version: 1.0
- */
- using System.Collections.Generic;
- using UnityEngine;
- using Oxide.Core;
- using System;
- namespace Oxide.Plugins
- {
- class ProtectedBuildZones : RustPlugin
- {
- #region Config
- private const float _damage = 0.0f; // Mutiply by zero (ie. No damage)
- private const float _radius = 40.0f;
- #endregion
- #region DataStructures
- struct BaseZoneInfo
- {
- public Vector3 Pos;
- public float Radius;
- public ulong Owner;
- };
- #endregion
- #region DataTypes
- private Dictionary<BasePlayer, BaseZoneInfo> VIPBuildData;
- #endregion
- #region HelperFunctions
- private bool hasPlayerSetTheirBase(BasePlayer p, ref BaseZoneInfo pInfo)
- {
- if (VIPBuildData.ContainsKey(p))
- {
- pInfo = VIPBuildData[p];
- return true;
- }
- return false;
- }
- private bool isPlayerAuthorized(BasePlayer p)
- {
- if (p.net.connection.authLevel == 2)
- {
- return true;
- }
- return false;
- }
- private bool isBlockWithinRadius(BaseEntity bInfo, BaseZoneInfo pInfo)
- {
- return bInfo.Distance2D(pInfo.Pos) <= pInfo.Radius;
- }
- private bool isBlockOwnedByVIP(BaseEntity bInfo, ref BaseZoneInfo pInfo)
- {
- BasePlayer p = BasePlayer.FindByID(bInfo.OwnerID);
- return (isPlayerAuthorized(p) && hasPlayerSetTheirBase(p, ref pInfo));
- }
- #endregion
- #region Chat
- [ChatCommand("SetBase")]
- private void setBase(BasePlayer p, string myCommand, string[] myArgs)
- {
- if (myArgs.Length == 1)
- {
- // ================================ ADMIN/TEST =========================================
- try
- {
- if (p.net.connection.authLevel == 2)
- {
- // Admin custom radius command.
- float radius = float.Parse(myArgs[0]);
- addPlayer(p, radius);
- }
- }
- catch (FormatException)
- {
- Puts("Error not a valid float!");
- }
- catch (OverflowException)
- {
- Puts("Value is too large or too small!");
- }
- // =====================================================================================
- }
- else if (myArgs.Length == 0)
- {
- if (isPlayerAuthorized(p))
- addPlayer(p);
- }
- else
- {
- Puts("Too many parameters!");
- }
- }
- [ChatCommand("RemoveBase")]
- private void removeBase(BasePlayer p, string myCommand, string[] myArgs)
- {
- if (isPlayerAuthorized(p))
- removePlayer(p, p.transform.position);
- }
- #endregion
- #region MaintenanceFunctions
- private bool addPlayer(BasePlayer p, float rad = _radius)
- {
- BaseZoneInfo PlayerData;
- PlayerData.Pos = p.transform.position;
- PlayerData.Radius = rad;
- PlayerData.Owner = p.userID;
- VIPBuildData[p] = PlayerData;
- SaveAllData(); // In case server crashes.
- return true;
- }
- private bool removePlayer(BasePlayer p, Vector3 pos)
- {
- if (VIPBuildData.ContainsKey(p))
- {
- VIPBuildData.Remove(p);
- SaveAllData(); // In case server crashes.
- return true;
- }
- return false;
- }
- private void SaveAllData()
- {
- Interface.Oxide.DataFileSystem.WriteObject("ProtectedDecayZones", VIPBuildData);
- }
- private void LoadAllData()
- {
- VIPBuildData = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<BasePlayer, BaseZoneInfo>>("ProtectedDecayZones");
- }
- #endregion
- #region Hooks
- void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo hitInfo)
- {
- if (!hitInfo.damageTypes.Has(Rust.DamageType.Decay)) return;
- var block = entity as BuildingBlock;
- BaseZoneInfo pInfo = new BaseZoneInfo();
- if (isBlockOwnedByVIP(entity, ref pInfo))
- {
- if (isBlockWithinRadius(entity, pInfo))
- {
- hitInfo.damageTypes.Scale(Rust.DamageType.Decay, _damage);
- }
- }
- }
- void Loaded()
- {
- VIPBuildData = new Dictionary<BasePlayer, BaseZoneInfo>();
- LoadAllData();
- }
- void Unload()
- {
- SaveAllData();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement