Advertisement
Nahtryx

ZoneManager.cs

Nov 17th, 2015
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.78 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3. using System.Reflection;
  4. using System.Data;
  5. using UnityEngine;
  6. using Oxide.Core;
  7. using Oxide.Core.Plugins;
  8. using RustProto;
  9.  
  10. namespace Oxide.Plugins
  11. {
  12.     [Info("ZoneManager", "Reneb", "1.0.2")]
  13.     class ZoneManager : RustLegacyPlugin
  14.     {
  15.         ////////////////////////////////////////////
  16.         /// FIELDS
  17.         ////////////////////////////////////////////
  18.         static RustServerManagement management;
  19.          
  20.         StoredData storedData;
  21.  
  22.         static Hash<string, ZoneDefinition> zonedefinitions = new Hash<string, ZoneDefinition>();
  23.         public Hash<PlayerClient, string> LastZone = new Hash<PlayerClient, string>();
  24.         public static Hash<PlayerClient, List<Zone>> playerZones = new Hash<PlayerClient, List<Zone>>();
  25.  
  26.         public static int triggerLayer;
  27.         public static int playersMask;
  28.  
  29.         public FieldInfo[] allZoneFields;
  30.         public FieldInfo cachedField;
  31.         public static FieldInfo fieldInfo;
  32.          
  33.         /////////////////////////////////////////
  34.         /// Cached Fields, used to make the plugin faster
  35.         /////////////////////////////////////////
  36.         public static Vector3 cachedDirection;
  37.         public Collider[] cachedColliders;
  38.         //public DamageTypeList emptyDamageType;
  39.         //public List<DamageTypeEntry> emptyDamageList;
  40.         public PlayerClient cachedPlayer;
  41.  
  42.         /////////////////////////////////////////
  43.         // ZoneLocation
  44.         // Stored information for the zone location and radius
  45.         /////////////////////////////////////////
  46.         public class ZoneLocation
  47.         {
  48.             public string x;
  49.             public string y;
  50.             public string z;
  51.             public string r;
  52.             Vector3 position;
  53.             float radius;
  54.  
  55.             public ZoneLocation() { }
  56.  
  57.             public ZoneLocation(Vector3 position, string radius)
  58.             {
  59.                 x = position.x.ToString();
  60.                 y = position.y.ToString();
  61.                 z = position.z.ToString();
  62.  
  63.                 r = radius.ToString();
  64.  
  65.                 this.position = position;
  66.                 this.radius = float.Parse(radius);
  67.             }
  68.  
  69.             public Vector3 GetPosition()
  70.             {
  71.                 if (position == Vector3.zero)
  72.                     position = new Vector3(float.Parse(x), float.Parse(y), float.Parse(z));
  73.                 return position;
  74.             }
  75.             public float GetRadius()
  76.             {
  77.                 if (radius == 0f)
  78.                     radius = float.Parse(r);
  79.                 return radius;
  80.             }
  81.             public string String()
  82.             {
  83.                 return string.Format("Pos({0},{1},{2}) - Rad({3})", x, y, z, r);
  84.             }
  85.         }
  86.         /////////////////////////////////////////
  87.         // RadiationZone
  88.         // is a MonoBehaviour
  89.         // This is needed for zones that use radiations only
  90.         /////////////////////////////////////////
  91.         public class RadiateZone : MonoBehaviour
  92.         {
  93.             public RadiationZone radiation;
  94.             Zone zone;
  95.  
  96.             void Awake()
  97.             {
  98.                 radiation = gameObject.AddComponent<RadiationZone>();
  99.                 zone = GetComponent<Zone>();
  100.                 radiation.exposurePerMin = float.Parse(zone.info.radiation);
  101.                 radiation.radius = GetComponent<UnityEngine.SphereCollider>().radius;
  102.                 Interface.CallHook("anticheatAllowRadiationZone", radiation);
  103.             }
  104.             void OnDestroy()
  105.             {
  106.                 GameObject.Destroy(radiation);
  107.             }
  108.  
  109.         }
  110.         /////////////////////////////////////////
  111.         // Zone
  112.         // is a Monobehavior
  113.         // used to detect the colliders with players
  114.         // and created everything on it's own (radiations, locations, etc)
  115.         /////////////////////////////////////////
  116.         public class Zone : MonoBehaviour
  117.         {
  118.             public ZoneDefinition info;
  119.             public List<PlayerClient> inTrigger = new List<PlayerClient>();
  120.             public List<PlayerClient> whiteList = new List<PlayerClient>();
  121.             public List<PlayerClient> keepInList = new List<PlayerClient>();
  122.  
  123.             Rigidbody rigidbody;
  124.             RadiateZone radiationzone;
  125.             float radiationamount;
  126.  
  127.             void Awake()
  128.             {
  129.                 gameObject.layer = triggerLayer;
  130.                 gameObject.name = "Zone Manager";
  131.                // this.rigidbody = gameObject.AddComponent<UnityEngine.Rigidbody>();
  132.                // this.rigidbody.isKinematic = false;
  133.                 gameObject.AddComponent<UnityEngine.SphereCollider>();
  134.                 collider.isTrigger = true;
  135.                 gameObject.SetActive(true);
  136.                 enabled = false;
  137.             }
  138.             public void SetInfo(ZoneDefinition info)
  139.             {
  140.                 this.info = info;
  141.                 GetComponent<UnityEngine.Transform>().position = info.Location.GetPosition();
  142.                 GetComponent<UnityEngine.SphereCollider>().radius = info.Location.GetRadius();
  143.                 radiationamount = 0f;
  144.  
  145.               //  this.rigidbody.position = GetComponent<UnityEngine.Transform>().position;
  146.               //  this.rigidbody.constraints = UnityEngine.RigidbodyConstraints.FreezeAll;
  147.                 if (float.TryParse(info.radiation, out radiationamount))
  148.                   radiationzone = gameObject.AddComponent<RadiateZone>();
  149.             }
  150.  
  151.             void OnDestroy()
  152.             {
  153.                 GameObject.Destroy(this.rigidbody);
  154.                 if(this.radiationzone != null)
  155.                     GameObject.Destroy(this.radiationzone);
  156.             }
  157.             void OnTriggerEnter(Collider col)
  158.             {
  159.                 if (col.GetComponent<Character>())
  160.                 {
  161.                     inTrigger.Add(col.GetComponent<Character>().playerClient);
  162.                     OnEnterZone(this, col.GetComponent<Character>().playerClient);
  163.                 }
  164.             }
  165.             void OnTriggerExit(Collider col)
  166.             {
  167.                 if (col.GetComponent<Character>())
  168.                 {
  169.                     inTrigger.Remove(col.GetComponent<Character>().playerClient);
  170.                     OnExitZone(this, col.GetComponent<Character>().playerClient);
  171.                 }
  172.             }
  173.         }
  174.  
  175.         /////////////////////////////////////////
  176.         // ZoneDefinition
  177.         // Stored informations on the zones
  178.         /////////////////////////////////////////
  179.         public class ZoneDefinition
  180.         {
  181.  
  182.             public string name;
  183.             public string radius;
  184.             public ZoneLocation Location;
  185.             public string ID;
  186.             public string eject;
  187.             public string pvpgod;
  188.             public string pvegod;
  189.             public string sleepgod;
  190.             public string undestr;
  191.             public string nobuild;
  192.             public string notp;
  193.             public string nochat;
  194.             public string nodeploy;
  195.             public string nokits;
  196.             public string nosuicide;
  197.             public string killsleepers;
  198.             public string radiation;
  199.             public string enter_message;
  200.             public string leave_message;
  201.  
  202.             public ZoneDefinition()
  203.             {
  204.  
  205.             }
  206.  
  207.             public ZoneDefinition(Vector3 position)
  208.             {
  209.                 this.radius = "20";
  210.                 Location = new ZoneLocation(position, this.radius);
  211.             }
  212.  
  213.         }
  214.         /////////////////////////////////////////
  215.         // Data Management
  216.         /////////////////////////////////////////
  217.         class StoredData
  218.         {
  219.             public HashSet<ZoneDefinition> ZoneDefinitions = new HashSet<ZoneDefinition>();
  220.             public StoredData()
  221.             {
  222.             }
  223.         }
  224.         void SaveData()
  225.         {
  226.             Interface.GetMod().DataFileSystem.WriteObject("ZoneManager", storedData);
  227.         }
  228.         void LoadData()
  229.         {
  230.             zonedefinitions.Clear();
  231.             try
  232.             {
  233.                 storedData = Interface.GetMod().DataFileSystem.ReadObject<StoredData>("ZoneManager");
  234.             }
  235.             catch
  236.             {
  237.                 storedData = new StoredData();
  238.             }
  239.             foreach (var zonedef in storedData.ZoneDefinitions)
  240.                 zonedefinitions[zonedef.ID] = zonedef;
  241.         }
  242.         /////////////////////////////////////////
  243.         // OXIDE HOOKS
  244.         /////////////////////////////////////////
  245.  
  246.         /////////////////////////////////////////
  247.         // Loaded()
  248.         // Called when the plugin is loaded
  249.         /////////////////////////////////////////
  250.         void Loaded()
  251.         {
  252.             permission.RegisterPermission("zone", this);
  253.             permission.RegisterPermission("candeploy", this);
  254.             permission.RegisterPermission("canbuild", this);
  255.             triggerLayer = UnityEngine.LayerMask.NameToLayer("Character Collision");
  256.  
  257.             LoadData();
  258.         }  
  259.         /////////////////////////////////////////
  260.         // Unload()
  261.         // Called when the plugin is unloaded
  262.         /////////////////////////////////////////
  263.         void Unload()
  264.         {
  265.             var objects = GameObject.FindObjectsOfType(typeof(Zone));
  266.             if (objects != null)
  267.                 foreach (var gameObj in objects)
  268.                     GameObject.Destroy(gameObj);
  269.         }
  270.         void Unloaded()
  271.         {
  272.             SaveData();
  273.         }
  274.         /////////////////////////////////////////
  275.         // OnServerInitialized()
  276.         // Called when the server is initialized
  277.         /////////////////////////////////////////
  278.         void OnServerInitialized()
  279.         {
  280.             management = RustServerManagement.Get();
  281.             allZoneFields = typeof(ZoneDefinition).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
  282.             foreach (KeyValuePair<string, ZoneDefinition> pair in zonedefinitions)
  283.             {
  284.                 NewZone(pair.Value);
  285.             }
  286.         }
  287.  
  288.         /////////////////////////////////////////
  289.         // OnEntityBuilt(Planner planner, GameObject gameobject)
  290.         // Called when a buildingblock was created
  291.         /////////////////////////////////////////
  292.         //void OnStructurePlaced(StructureComponent component, IStructureComponentItem item)
  293.  
  294.         void OnStructureBuilt(StructureComponent component, IStructureComponentItem item)
  295.         {
  296.             if (item.controllable == null) return;
  297.             if (hasTag(item.controllable.playerClient, "nobuild"))
  298.             {
  299.                 if (!hasPermission(item.controllable.playerClient, "canbuild"))
  300.                 {
  301.                     SendMessage(item.controllable.playerClient, "You are not allowed to build here");
  302.                     timer.Once(0.2f, () => DestroyObject(component.gameObject));
  303.                    
  304.                 }
  305.             }
  306.         }
  307.         void DestroyObject(GameObject obj)
  308.         {
  309.             if (obj != null)
  310.                 NetCull.Destroy(obj);
  311.         }
  312.         /////////////////////////////////////////
  313.         // OnItemDeployed(Deployer deployer, BaseEntity deployedEntity)
  314.         // Called when an item was deployed
  315.         /////////////////////////////////////////
  316.         //void OnItemDeployedByPlayer(DeployableObject deployedEntity, IDeployableItem deployableItem)
  317.  
  318.         void OnItemDeployed(DeployableObject deployedEntity, IDeployableItem deployableItem)
  319.         {  
  320.             if (deployableItem.controllable == null) return;
  321.             if (hasTag(deployableItem.controllable.playerClient, "nodeploy"))
  322.             {
  323.                 if (!hasPermission(deployableItem.controllable.playerClient, "candeploy"))
  324.                 {
  325.                     timer.Once(0.01f, () => DestroyObject(deployedEntity.gameObject));
  326.                     SendMessage(deployableItem.controllable.playerClient, "You are not allowed to deploy here");
  327.                 }
  328.             }
  329.             else if(deployedEntity.GetComponent<TimedExplosive>())
  330.             {
  331.                 timer.Once(4f, () => CheckPositionExplosive(deployedEntity, deployableItem));
  332.             }
  333.         }
  334.         void CheckPositionExplosive(DeployableObject deployedEntity, IDeployableItem deployableItem)
  335.         {
  336.             var objects = GameObject.FindObjectsOfType(typeof(Zone));
  337.             if (objects != null)
  338.                 foreach (Zone zone in objects)
  339.                 {
  340.                     if (zone.info.undestr == null) continue;
  341.                     if(Vector3.Distance(deployedEntity.transform.position, zone.info.Location.GetPosition()) < (zone.info.Location.GetRadius() + 5f))
  342.                     {
  343.                         deployableItem.character.GetComponent<Inventory>().AddItemAmount(deployableItem.datablock, 1);
  344.                         NetCull.Destroy(deployedEntity.gameObject);
  345.                     }
  346.                 }
  347.         }
  348.  
  349.         /////////////////////////////////////////
  350.         // OnPlayerChat(ConsoleSystem.Arg arg)
  351.         // Called when a user writes something in the chat, doesn't take in count the commands
  352.         /////////////////////////////////////////
  353.         object OnPlayerChat(NetUser netuser, string args)
  354.         {
  355.             if(hasTag(netuser.playerClient, "nochat"))
  356.             {
  357.                 SendMessage(netuser.playerClient, "You are not allowed to chat here");
  358.                 return false;
  359.             }
  360.             return null;
  361.         }
  362.  
  363.         /////////////////////////////////////////
  364.         // OnRunCommand(ConsoleSystem.Arg arg)
  365.         // Called when a user executes a command
  366.         /////////////////////////////////////////
  367.         object OnRunCommand(ConsoleSystem.Arg arg, bool shouldAnswer)
  368.         {
  369.             if (arg == null) return null;
  370.             if (arg.argUser == null) return null;
  371.             if (arg.Function != "suicide") return null;
  372.             if (!hasTag(arg.argUser.playerClient, "nosuicide")) return null;
  373.             SendMessage(arg.argUser.playerClient, "You are not allowed to suicide here");
  374.             return false;
  375.         }
  376.  
  377.         /////////////////////////////////////////
  378.         // OnPlayerDisconnected(PlayerClient player)
  379.         // Called when a user disconnects
  380.         /////////////////////////////////////////
  381.         void OnPlayerDisconnected(uLink.NetworkPlayer netplayer)
  382.         {
  383.             PlayerClient player = ((NetUser)netplayer.GetLocalData()).playerClient;
  384.             if (hasTag(player, "killsleepers")) TakeDamage.KillSelf(player.controllable.GetComponent<Character>());
  385.         }
  386.  
  387.         /////////////////////////////////////////
  388.         // OnEntityAttacked(BaseCombatEntity entity, HitInfo hitinfo)
  389.         // Called when any entity is attacked
  390.         /////////////////////////////////////////
  391.         object ModifyDamage(TakeDamage takedamage, DamageEvent damage)
  392.         {
  393.             if (damage.victim.client != null)
  394.             {
  395.                 if (damage.attacker.client != null)
  396.                 {
  397.                     if (damage.attacker.client == damage.victim.client) return null;
  398.                     if (hasTag(damage.victim.client, "pvpgod"))
  399.                         return CancelDamage(damage);
  400.                 }
  401.                 else if (damage.attacker.networkView != null && damage.attacker.networkView.GetComponent<HostileWildlifeAI>())
  402.                 {
  403.                     if (hasTag(damage.victim.client, "pvegod"))
  404.                         return CancelDamage(damage);
  405.                 }
  406.             }
  407.             else if(takedamage.gameObject.name.Contains("MaleSleeper"))
  408.             {
  409.                 if(damage.attacker.client != null)
  410.                 {
  411.                     if (hasTag(damage.attacker.client, "sleepgod"))
  412.                         return CancelDamage(damage);
  413.                 }
  414.             }
  415.             return null;
  416.         }
  417.  
  418.         /////////////////////////////////////////
  419.         // OnEntityDeath(BaseNetworkable basenet)
  420.         // Called when any entity is spawned
  421.         /////////////////////////////////////////
  422.         void OnKilled(TakeDamage takedamage, DamageEvent damage)
  423.         {
  424.             if(damage.victim.client != null)
  425.             {
  426.                 if(playerZones[damage.victim.client] != null)
  427.                     playerZones[damage.victim.client].Clear();
  428.             }
  429.         }
  430.  
  431.  
  432.         /////////////////////////////////////////
  433.         // Outside Plugin Hooks
  434.         /////////////////////////////////////////
  435.  
  436.         /////////////////////////////////////////
  437.         // canRedeemKit(BasePlayer player)
  438.         // Called from the Kits plugin (Reneb) when trying to redeem a kit
  439.         /////////////////////////////////////////
  440.         object canRedeemKit(NetUser player)
  441.         {
  442.             if (hasTag(player.playerClient, "nokits")) { return "You may not redeem a kit inside this area"; }
  443.             return null;
  444.         }
  445.  
  446.         /////////////////////////////////////////
  447.         // canTeleport(BasePlayer player)
  448.         // Called from Teleportation System (Mughisi) when a player tries to teleport
  449.         /////////////////////////////////////////
  450.         object canTeleport(NetUser player)
  451.         {
  452.             if (hasTag(player.playerClient, "notp")) { return "You may not teleport in this area"; }
  453.             return null;
  454.         }
  455.  
  456.         /////////////////////////////////////////
  457.         // External calls to this plugin
  458.         /////////////////////////////////////////
  459.  
  460.         /////////////////////////////////////////
  461.         // CreateOrUpdateZone(string ZoneID, object[] args)
  462.         // Create or Update a zone from an external plugin
  463.         // ZoneID should be a name, like Arena (for an arena plugin) (even if it's called an ID :p)
  464.         // args are the same a the /zone command
  465.         // args[0] = "radius" args[1] = "50" args[2] = "eject" args[3] = "true", etc
  466.         // Third parameter is obviously need if you create a NEW zone (or want to update the position)
  467.         /////////////////////////////////////////
  468.         bool CreateOrUpdateZone(string ZoneID, string[] args, Vector3 position = default(Vector3))
  469.         {
  470.             ZoneDefinition zonedef;
  471.             if (zonedefinitions[ZoneID] == null) zonedef = new ZoneDefinition();
  472.             else zonedef = zonedefinitions[ZoneID];
  473.             zonedef.ID = ZoneID;
  474.  
  475.             string editvalue;
  476.             for (int i = 0; i < args.Length; i = i + 2)
  477.             {
  478.  
  479.                 cachedField = GetZoneField(args[i]);
  480.                 if (cachedField == null) continue;
  481.  
  482.                 switch (args[i + 1])
  483.                 {
  484.                     case "true":
  485.                     case "1":
  486.                         editvalue = "true";
  487.                         break;
  488.                     case "null":
  489.                     case "0":
  490.                     case "false":
  491.                     case "reset":
  492.                         editvalue = null;
  493.                         break;
  494.                     default:
  495.                         editvalue = (string)args[i + 1];
  496.                         break;
  497.                 }
  498.                 cachedField.SetValue(zonedef, editvalue);
  499.                 if (args[i].ToLower() == "radius") { if (zonedef.Location != null) zonedef.Location = new ZoneLocation(zonedef.Location.GetPosition(), editvalue); }
  500.             }
  501.  
  502.             if (position != default(Vector3)) { zonedef.Location = new ZoneLocation((Vector3)position, (zonedef.radius != null) ? zonedef.radius : "20"); }
  503.  
  504.             if (zonedefinitions[ZoneID] != null) storedData.ZoneDefinitions.Remove(zonedefinitions[ZoneID]);
  505.             zonedefinitions[ZoneID] = zonedef;
  506.             storedData.ZoneDefinitions.Add(zonedefinitions[ZoneID]);
  507.             SaveData();
  508.             if (zonedef.Location == null) return false;
  509.             RefreshZone(ZoneID);
  510.             return true;
  511.         }
  512.         bool EraseZone(string ZoneID)
  513.         {
  514.             if (zonedefinitions[ZoneID] == null) return false;
  515.             storedData.ZoneDefinitions.Remove(zonedefinitions[ZoneID]);
  516.             zonedefinitions[ZoneID] = null;
  517.            
  518.             SaveData();
  519.             RefreshZone(ZoneID);
  520.             return true;
  521.         }
  522.         List<PlayerClient> GetPlayersInZone(string ZoneID)
  523.         {
  524.             List<PlayerClient> baseplayers = new List<PlayerClient>();
  525.             foreach (KeyValuePair<PlayerClient, List<Zone>> pair in playerZones)
  526.             {
  527.                 foreach (Zone zone in pair.Value)
  528.                 {
  529.                     if (zone.info.ID == ZoneID)
  530.                     {
  531.                         baseplayers.Add(pair.Key);
  532.                     }
  533.                 }
  534.             }
  535.             return baseplayers;
  536.         }
  537.         bool isPlayerInZone(string ZoneID, PlayerClient player)
  538.         {
  539.             if (playerZones[player] == null) return false;
  540.             foreach (Zone zone in playerZones[player])
  541.             {
  542.                 if (zone.info.ID == ZoneID)
  543.                 {
  544.                     return true;
  545.                 }
  546.             }
  547.             return false;
  548.         }
  549.         bool AddPlayerToZoneWhitelist(string ZoneID, PlayerClient player)
  550.         {
  551.             Zone targetZone = GetZoneByID(ZoneID);
  552.             if (targetZone == null) return false;
  553.             AddToWhitelist(targetZone, player);
  554.             return true;
  555.         }
  556.         bool AddPlayerToZoneKeepinlist(string ZoneID, PlayerClient player)
  557.         {
  558.             Zone targetZone = GetZoneByID(ZoneID);
  559.             if (targetZone == null) return false;
  560.             AddToKeepinlist(targetZone, player);
  561.             return true;
  562.         }
  563.         bool RemovePlayerFromZoneWhitelist(string ZoneID, PlayerClient player)
  564.         {
  565.             Zone targetZone = GetZoneByID(ZoneID);
  566.             if (targetZone == null) return false;
  567.             RemoveFromWhitelist(targetZone, player);
  568.             return true;
  569.         }
  570.         bool RemovePlayerFromZoneKeepinlist(string ZoneID, PlayerClient player)
  571.         {
  572.             Zone targetZone = GetZoneByID(ZoneID);
  573.             if (targetZone == null) return false;
  574.             RemoveFromKeepinlist(targetZone, player);
  575.             return true;
  576.         }
  577.         /////////////////////////////////////////
  578.         // Random Commands
  579.         /////////////////////////////////////////
  580.         void AddToWhitelist(Zone zone, PlayerClient player) { if (!zone.whiteList.Contains(player)) zone.whiteList.Add(player); }
  581.         void RemoveFromWhitelist(Zone zone, PlayerClient player) { if (zone.whiteList.Contains(player)) zone.whiteList.Remove(player); }
  582.         void AddToKeepinlist(Zone zone, PlayerClient player) { if (!zone.keepInList.Contains(player)) zone.keepInList.Add(player); }
  583.         void RemoveFromKeepinlist(Zone zone, PlayerClient player) { if (zone.keepInList.Contains(player)) zone.keepInList.Remove(player); }
  584.  
  585.         Zone GetZoneByID(string ZoneID)
  586.         {
  587.             var objects = GameObject.FindObjectsOfType(typeof(Zone));
  588.             if (objects != null)
  589.                 foreach (Zone gameObj in objects)
  590.                 {
  591.                     if (gameObj.info.ID == ZoneID) return gameObj;
  592.                 }
  593.  
  594.             return null;
  595.         }
  596.  
  597.         void NewZone(ZoneDefinition zonedef)
  598.         {
  599.             if (zonedef == null) return;
  600.             var newgameObject = new UnityEngine.GameObject();
  601.             var newZone = newgameObject.AddComponent<Zone>();
  602.             newZone.SetInfo(zonedef);
  603.         }
  604.         void RefreshZone(string zoneID)
  605.         {
  606.             var objects = GameObject.FindObjectsOfType(typeof(Zone));
  607.             if (objects != null)
  608.                 foreach (Zone gameObj in objects)
  609.                 {
  610.                     if (gameObj.info.ID == zoneID)
  611.                     {
  612.                         foreach (KeyValuePair<PlayerClient, List<Zone>> pair in playerZones)
  613.                         {
  614.                             if (pair.Value.Contains(gameObj)) playerZones[pair.Key].Remove(gameObj);
  615.                         }
  616.                         GameObject.Destroy(gameObj);
  617.                        
  618.                         break;
  619.                     }
  620.                 }
  621.             if (zonedefinitions[zoneID] != null)
  622.             {
  623.                 NewZone(zonedefinitions[zoneID]);
  624.             }
  625.         }
  626.  
  627.         int GetRandom(int min, int max) { return UnityEngine.Random.Range(min, max); }
  628.  
  629.         FieldInfo GetZoneField(string name)
  630.         {
  631.             name = name.ToLower();
  632.             foreach (FieldInfo fieldinfo in allZoneFields) { if (fieldinfo.Name == name) return fieldinfo; }
  633.             return null;
  634.         }
  635.         static bool hasTag(PlayerClient player, string tagname)
  636.         {
  637.             if (playerZones[player] == null) { return false; }
  638.             if (playerZones[player].Count == 0) { return false; }
  639.             fieldInfo = typeof(ZoneDefinition).GetField(tagname, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
  640.             foreach (Zone zone in playerZones[player])
  641.             {
  642.                 if (fieldInfo.GetValue(zone.info) != null)
  643.                     return true;
  644.             }
  645.             return false;
  646.         }
  647.  
  648.  
  649.  
  650.         PlayerClient FindPlayerByRadius(Vector3 position, float rad)
  651.         {
  652.             cachedColliders = Physics.OverlapSphere(position, rad);
  653.             foreach (Collider collider in cachedColliders)
  654.             {
  655.                 if (collider.GetComponentInParent<PlayerClient>())
  656.                     return collider.GetComponentInParent<PlayerClient>();
  657.             }
  658.             return null;
  659.         }
  660.  
  661.         object CancelDamage(DamageEvent damage)
  662.         {
  663.             damage.amount = 0f;
  664.             damage.status = LifeStatus.IsAlive;
  665.             return damage;
  666.         }
  667.         static void OnEnterZone(Zone zone, PlayerClient player)
  668.         {
  669.             if (playerZones[player] == null) playerZones[player] = new List<Zone>();
  670.             if (!playerZones[player].Contains(zone)) playerZones[player].Add(zone);
  671.             if (zone.info.enter_message != null) SendMessage(player, zone.info.enter_message);
  672.             if (zone.info.eject != null && !isAdmin(player) && !zone.whiteList.Contains(player) && !zone.keepInList.Contains(player)) EjectPlayer(zone, player);
  673.             Interface.CallHook("OnEnterZone", zone.info.ID, player);
  674.         }
  675.         static void OnExitZone(Zone zone, PlayerClient player)
  676.         {
  677.             if (playerZones[player].Contains(zone)) playerZones[player].Remove(zone);
  678.             if (zone.info.leave_message != null) SendMessage(player, zone.info.leave_message);
  679.             if (zone.keepInList.Contains(player)) AttractPlayer(zone, player);
  680.             Interface.CallHook("OnExitZone", zone.info.ID, player);
  681.         }
  682.         static void IsCollidingEject(Zone zone, PlayerClient player)
  683.         {
  684.             if (playerZones[player] == null) return;
  685.             EjectPlayer(zone, player);
  686.         }
  687.         static void EjectPlayer(Zone zone, PlayerClient player)
  688.         {
  689.             cachedDirection = player.lastKnownPosition - zone.transform.position;
  690.             player.lastKnownPosition = zone.transform.position + (cachedDirection / cachedDirection.magnitude * (zone.GetComponent<UnityEngine.SphereCollider>().radius + 2f));
  691.             management.TeleportPlayerToWorld(player.netPlayer, player.lastKnownPosition);
  692.             Interface.CallHook("IsCollidingEject",zone, player);
  693.         }
  694.         static void AttractPlayer(Zone zone, PlayerClient player)
  695.         {
  696.             cachedDirection = player.lastKnownPosition - zone.transform.position;
  697.             player.lastKnownPosition = zone.transform.position + (cachedDirection / cachedDirection.magnitude * (zone.GetComponent<UnityEngine.SphereCollider>().radius - 2f));
  698.             management.TeleportPlayerToWorld(player.netPlayer, player.lastKnownPosition);
  699.         }
  700.         static bool isAdmin(PlayerClient player)
  701.         {
  702.             if (player.netUser.CanAdmin())
  703.                 return true;
  704.             return false;
  705.         }
  706.         bool hasPermission(PlayerClient player, string permname)
  707.         {
  708.             if (player.netUser.CanAdmin())
  709.                 return true;
  710.             return permission.UserHasPermission(player.userID.ToString(), permname);
  711.         }
  712.         //////////////////////////////////////////////////////////////////////////////
  713.         /// Chat Commands
  714.         //////////////////////////////////////////////////////////////////////////////
  715.         [ChatCommand("zone_add")]
  716.         void cmdChatZoneAdd(NetUser player, string command, string[] args)
  717.         {
  718.             if (!hasPermission(player.playerClient, "zone")) { SendMessage(player.playerClient, "You don't have access to this command"); return; }
  719.             var newzoneinfo = new ZoneDefinition(player.playerClient.lastKnownPosition);
  720.             newzoneinfo.ID = GetRandom(1, 99999999).ToString();
  721.             NewZone(newzoneinfo);
  722.             if (zonedefinitions[newzoneinfo.ID] != null) storedData.ZoneDefinitions.Remove(zonedefinitions[newzoneinfo.ID]);
  723.             zonedefinitions[newzoneinfo.ID] = newzoneinfo;
  724.             LastZone[player.playerClient] = newzoneinfo.ID;
  725.             storedData.ZoneDefinitions.Add(zonedefinitions[newzoneinfo.ID]);
  726.             SaveData();
  727.             SendMessage(player.playerClient, "New Zone created, you may now edit it: " + newzoneinfo.Location.String());
  728.         }
  729.         [ChatCommand("zone_reset")]
  730.         void cmdChatZoneReset(NetUser player, string command, string[] args)
  731.         {
  732.             if (!hasPermission(player.playerClient, "zone")) { SendMessage(player.playerClient, "You don't have access to this command"); return; }
  733.             zonedefinitions.Clear();
  734.             storedData.ZoneDefinitions.Clear();
  735.             SaveData();
  736.             Unload();
  737.             SendMessage(player.playerClient, "All Zones were removed");
  738.         }
  739.         [ChatCommand("zone_remove")]
  740.         void cmdChatZoneRemove(NetUser player, string command, string[] args)
  741.         {
  742.             if (!hasPermission(player.playerClient, "zone")) { SendMessage(player.playerClient, "You don't have access to this command"); return; }
  743.             if (args.Length == 0) { SendMessage(player.playerClient, "/zone_remove XXXXXID"); return; }
  744.             if (zonedefinitions[args[0]] == null) { SendMessage(player.playerClient, "This zone doesn't exist"); return; }
  745.             storedData.ZoneDefinitions.Remove(zonedefinitions[args[0]]);
  746.             zonedefinitions[args[0]] = null;
  747.             SaveData();
  748.             RefreshZone(args[0]);
  749.             SendMessage(player.playerClient, "Zone " + args[0] + " was removed");
  750.         }
  751.         [ChatCommand("zone_edit")]
  752.         void cmdChatZoneEdit(NetUser player, string command, string[] args)
  753.         {
  754.             if (!hasPermission(player.playerClient, "zone")) { SendMessage(player.playerClient, "You don't have access to this command"); return; }
  755.             if (args.Length == 0) { SendMessage(player.playerClient, "/zone_edit XXXXXID"); return; }
  756.             if (zonedefinitions[args[0]] == null) { SendMessage(player.playerClient, "This zone doesn't exist"); return; }
  757.             LastZone[player.playerClient] = args[0];
  758.             SendMessage(player.playerClient, "Editing zone ID: " + args[0]);
  759.         }
  760.         [ChatCommand("zone_list")]
  761.         void cmdChatZoneList(NetUser player, string command, string[] args)
  762.         {
  763.             if (!hasPermission(player.playerClient, "zone")) { SendMessage(player.playerClient, "You don't have access to this command"); return; }
  764.             SendMessage(player.playerClient, "========== Zone list ==========");
  765.             if (zonedefinitions.Count == 0) { SendMessage(player.playerClient, "empty"); return; }
  766.             foreach (KeyValuePair<string, ZoneDefinition> pair in zonedefinitions)
  767.             {
  768.                 SendMessage(player.playerClient, string.Format("{0} => {1} - {2}", pair.Key, pair.Value.name, pair.Value.Location.String()));
  769.             }
  770.         }
  771.         [ChatCommand("zone")]
  772.         void cmdChatZone(NetUser player, string command, string[] args)
  773.         {
  774.             if (!hasPermission(player.playerClient, "zone")) { SendMessage(player.playerClient, "You don't have access to this command"); return; }
  775.             if (LastZone[player.playerClient] == null) { SendMessage(player.playerClient, "You must first say: /zone_edit XXXXXID"); return; }
  776.             object value;
  777.  
  778.             if (args.Length < 2)
  779.             {
  780.                 SendMessage(player.playerClient, "/zone option value/reset");
  781.                 foreach (FieldInfo fieldinfo in allZoneFields)
  782.                 {
  783.                     value = fieldinfo.GetValue(zonedefinitions[LastZone[player.playerClient]]);
  784.                     switch (fieldinfo.Name)
  785.                     {
  786.                         case "Location":
  787.                             value = ((ZoneLocation)value).String();
  788.                             break;
  789.                         default:
  790.                             if (value == null) value = "false";
  791.                             break;
  792.                     }
  793.                     SendMessage(player.playerClient, string.Format("{0} => {1}", fieldinfo.Name, value.ToString()));
  794.                 }
  795.                 return;
  796.             }
  797.             string editvalue;
  798.             for (int i = 0; i < args.Length; i = i + 2)
  799.             {
  800.  
  801.                 cachedField = GetZoneField(args[i]);
  802.                 if (cachedField == null) continue;
  803.  
  804.                 switch (args[i + 1])
  805.                 {
  806.                     case "true":
  807.                     case "1":
  808.                         editvalue = "true";
  809.                         break;
  810.                     case "null":
  811.                     case "0":
  812.                     case "false":
  813.                     case "reset":
  814.                         editvalue = null;
  815.                         break;
  816.                     default:
  817.                         editvalue = args[i + 1];
  818.                         break;
  819.                 }
  820.                 cachedField.SetValue(zonedefinitions[LastZone[player.playerClient]], editvalue);
  821.                 if (args[i].ToLower() == "radius") { zonedefinitions[LastZone[player.playerClient]].Location = new ZoneLocation(zonedefinitions[LastZone[player.playerClient]].Location.GetPosition(), editvalue); }
  822.                 SendMessage(player.playerClient, string.Format("{0} set to {1}", cachedField.Name, editvalue));
  823.             }
  824.             RefreshZone(LastZone[player.playerClient]);
  825.             SaveData();
  826.         }
  827.         static void SendMessage(PlayerClient player, string message) { ConsoleNetworker.SendClientCommand(player.netPlayer, "chat.add Oxide " + Facepunch.Utility.String.QuoteSafe(message));  }
  828.     }
  829. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement