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 certain users.
- File: ProtectedDecayZones.cs
- Version: 1.0.1
- Notes: Added Clan Support.
- */
- using System.Collections.Generic;
- using UnityEngine;
- using Oxide.Core;
- using System;
- using Newtonsoft.Json.Linq;
- using Oxide.Core.Plugins;
- using System.IO;
- namespace Oxide.Plugins
- {
- class ProtectedDecayZones : RustPlugin
- {
- [PluginReference]
- Plugin Clans;
- #region Config
- private const float _damage = 0.0f; // Mutiply by zero (ie. No damage)
- private const float _radius = 60.0f;
- #endregion
- #region DataStructures
- struct BaseZoneInfo
- {
- public Vector3 Pos;
- public float Radius;
- public ulong Owner;
- };
- struct ClanReturnData
- {
- public bool isInClan;
- public List<BasePlayer> clanMates;
- };
- #endregion
- #region DataTypes
- private Dictionary<BasePlayer, BaseZoneInfo> VIPBuildData;
- private Dictionary<string, List<BasePlayer>> ClanCacheData;
- private bool clanPluginSupport;
- #endregion
- #region HelperFunctions
- private void WriteLog(string txt)
- {
- PrintWarning(txt);
- }
- private ClanReturnData getClanAffiliation(BasePlayer p)
- {
- ClanReturnData clan = new ClanReturnData();
- if (clanPluginSupport)
- {
- string tag = Clans.Call<string>("GetClanOf", p.userID);
- if (!string.IsNullOrEmpty(tag))
- {
- clan.clanMates = getClanMembersByTag(tag);
- }
- else
- {
- clan.isInClan = false;
- }
- }
- else
- {
- clan.isInClan = false;
- }
- return clan;
- }
- private bool hasPlayerSetTheirBase(BasePlayer p, ref BaseZoneInfo pInfo)
- {
- if (VIPBuildData.ContainsKey(p))
- {
- pInfo = VIPBuildData[p];
- return true;
- }
- return false;
- }
- private bool isPlayerAuthorized(BasePlayer p)
- {
- return true; // DEBUG DEBUG DEBUG DBEBUG
- if (p.net.connection.authLevel == 2)
- {
- return true;
- }
- return false;
- }
- private bool isBlockWithinRadius(BaseEntity bInfo, BaseZoneInfo pInfo)
- {
- float D = Vector3.Distance(bInfo.transform.position, pInfo.Pos);
- WriteLog(
- "Distance From Block: " + D.ToString() +
- " BLOCK XY: " + bInfo.transform.position.x.ToString() + ", " + bInfo.transform.position.x.ToString() +
- " Player XY: " + pInfo.Pos.x.ToString() + ", " + pInfo.Pos.y.ToString()
- );
- return Vector3.Distance(bInfo.transform.position, pInfo.Pos) <= pInfo.Radius;
- }
- private bool isBlockOwnedByVIP(BasePlayer p, ref BaseZoneInfo pInfo)
- {
- return (isPlayerAuthorized(p) && hasPlayerSetTheirBase(p, ref pInfo));
- }
- private bool PermissionCheck(BasePlayer p, BuildingBlock b)
- {
- BaseZoneInfo pInfo = new BaseZoneInfo();
- if (isBlockOwnedByVIP(p, ref pInfo))
- {
- if (isBlockWithinRadius(b, pInfo))
- {
- return true;
- }
- }
- return false;
- }
- private List<BasePlayer> getClanMembersByTag(string clanTag)
- {
- if (ClanCacheData.ContainsKey(clanTag))
- {
- return ClanCacheData[clanTag];
- }
- if (prefetchClan(clanTag))
- {
- return getClanMembersByTag(clanTag);
- }
- else
- {
- return new List<BasePlayer>();
- }
- }
- private bool prefetchClan(string clanTag)
- {
- JObject currClan = Clans.Call<JObject>("GetClan", clanTag);
- if (ClanCacheData.ContainsKey(clanTag))
- {
- ClanCacheData[clanTag].Clear();
- }
- if (currClan == null)
- {
- return false;
- }
- ulong ClanPlayrID;
- foreach (string memberid in currClan["members"])
- {
- ClanPlayrID = Convert.ToUInt64(memberid);
- if (!ClanCacheData.ContainsKey(clanTag))
- ClanCacheData.Add(clanTag, new List<BasePlayer>());
- ClanCacheData[clanTag].Add((BasePlayer.FindByID(ClanPlayrID)));
- }
- return true;
- }
- #endregion
- #region Chat
- [ChatCommand("SetBase")]
- private void setBase(BasePlayer p, string myCommand, string[] myArgs)
- {
- if (isPlayerAuthorized(p))
- addPlayer(p);
- }
- [ChatCommand("RemoveBase")]
- private void removeBase(BasePlayer p, string myCommand, string[] myArgs)
- {
- if (isPlayerAuthorized(p))
- removePlayer(p);
- }
- #endregion
- #region MaintenanceFunctions
- private bool addPlayer(BasePlayer p, float rad = _radius)
- {
- WriteLog("Adding Player to system: " + p.ToString());
- 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)
- {
- 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) && entity.OwnerID == 0) return;
- BuildingBlock block = entity as BuildingBlock;
- bool preventDecay = false;
- BasePlayer blockp = BasePlayer.FindByID(entity.OwnerID);
- if (blockp != null)
- {
- ClanReturnData ClanData = getClanAffiliation(blockp);
- if (ClanData.isInClan)
- {
- WriteLog("Player is in clan");
- foreach (BasePlayer ClanPlayer in ClanData.clanMates)
- {
- WriteLog("Checking Clan Player: " + ClanPlayer.ToString());
- preventDecay = PermissionCheck(ClanPlayer, block);
- if (preventDecay)
- {
- WriteLog("Found Valid Clan Player: " + ClanPlayer.ToString());
- break;
- }
- }
- }
- else
- {
- preventDecay = PermissionCheck(blockp, block);
- }
- if (preventDecay)
- {
- WriteLog("Preventing Damage decay to block for Player : " + blockp.ToString());
- hitInfo.damageTypes.Scale(Rust.DamageType.Decay, _damage);
- }
- }
- }
- void Loaded()
- {
- WriteLog("PDZ LOADED (PROBABLY CRASHED!)");
- VIPBuildData = new Dictionary<BasePlayer, BaseZoneInfo>();
- ClanCacheData = new Dictionary<string, List<BasePlayer>>();
- LoadAllData();
- clanPluginSupport = plugins.Exists("Clans");
- }
- void Unload()
- {
- SaveAllData();
- }
- void OnClanCreate(string tag)
- {
- prefetchClan(tag);
- }
- void OnClanUpdate(string tag)
- {
- prefetchClan(tag);
- }
- void OnClanDestroy(string tag)
- {
- if (ClanCacheData.ContainsKey(tag))
- {
- ClanCacheData.Remove(tag);
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement