Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Facepunch;
  4. using Oxide.Core.Libraries.Covalence;
  5. using Oxide.Core.Plugins;
  6. using Rust;
  7. using UnityEngine;
  8.  
  9. namespace Oxide.Plugins
  10. {
  11. [Info("RaidBuildBlock", "Lucky Luciano / Def", "1.2.3", ResourceId = 0)]
  12. public class RaidBuildBlock : RustPlugin
  13. {
  14.  
  15. #region Fields
  16.  
  17. [PluginReference]
  18. private Plugin Clans;
  19. private readonly Dictionary<Vector3, int> _hotSpots = new Dictionary<Vector3, int>();
  20. private readonly Dictionary<uint, int> _hotBuildings = new Dictionary<uint, int>();
  21. private static readonly DamageTypeList EmptyDmgList = new DamageTypeList();
  22.  
  23. #endregion
  24.  
  25. #region Config
  26.  
  27. private ulong _iconId = 76561197960839785; //chat icon ID
  28. private readonly bool _enable = true; // Global switch on/off
  29. private readonly bool _enableBuildings = true; // Enable blocking whole buildings or just points
  30. private readonly bool _enableCmdBlock = true; // Enable command blocking while player is in "raid zone". Requires prev. option to be on.
  31. private readonly List<string> _raidBlockedCmds = new List<string> { "trade", "tpa", "tpr", "home", "remove" }; // Which chat commands will be blocked during the raid.
  32. // Raid block time:
  33. private const int RaidBlockTime = 180;
  34.  
  35. #endregion
  36.  
  37. private void OnServerInitialized()
  38. {
  39. if (!_enable)
  40. {
  41. Unsubscribe(nameof(OnEntityDeath));
  42. Unsubscribe(nameof(CanBuild));
  43. Unsubscribe(nameof(CanChangeGrade));
  44. Unsubscribe(nameof(OnUserCommand));
  45. }
  46. else
  47. {
  48. if (!_enableCmdBlock)
  49. Unsubscribe(nameof(OnUserCommand));
  50. timer.Every(15f, MaintainHotSpots);
  51. }
  52.  
  53. }
  54.  
  55. private void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
  56. {
  57. var bb = entity as DecayEntity;
  58. if (!bb || bb.OwnerID == 0 || info == null || info.damageTypes.GetMajorityDamageType() != DamageType.Explosion ||
  59. info.InitiatorPlayer == null || bb.OwnerID == info.InitiatorPlayer.userID || !IsValidEnt(bb) || IsFriends(bb.OwnerID, info.InitiatorPlayer.userID))
  60. return;
  61. if (bb is Door)
  62. {
  63. var parent = GetParentBuildingBlock(bb);
  64. if (parent)
  65. _hotSpots[parent.transform.position] = Facepunch.Math.Epoch.Current + RaidBlockTime;
  66.  
  67. else
  68. _hotSpots[entity.transform.position] = Facepunch.Math.Epoch.Current + RaidBlockTime;
  69. }
  70. else
  71. _hotSpots[entity.transform.position] = Facepunch.Math.Epoch.Current + RaidBlockTime;
  72. _hotBuildings[bb.buildingID] = Facepunch.Math.Epoch.Current + RaidBlockTime;
  73. }
  74.  
  75. private static BuildingBlock GetParentBuildingBlock(BaseCombatEntity ent)
  76. {
  77. var links = ent.GetEntityLinks(false);
  78. if (links.Count == 0 || links[0].connections.Count == 0)
  79. return null;
  80. return links[0].connections[0].owner as BuildingBlock;
  81. }
  82.  
  83. private static bool IsValidEnt(DecayEntity ent) => (ent is BuildingBlock || ent is Door) && !IsTwigs(ent);
  84.  
  85. private static bool IsTwigs(DecayEntity ent) => ent is BuildingBlock && ((BuildingBlock)ent).grade == BuildingGrade.Enum.Twigs;
  86.  
  87. private void MaintainHotSpots()
  88. {
  89. if (_hotBuildings.Count == 0 && _hotSpots.Count == 0)
  90. return;
  91. var time = Facepunch.Math.Epoch.Current;
  92. foreach (var pos in _hotSpots.Where(kv => time >= kv.Value).Select(kv => kv.Key).ToArray())
  93. _hotSpots.Remove(pos);
  94. foreach (var id in _hotBuildings.Where(kv => time >= kv.Value).Select(kv => kv.Key).ToArray())
  95. _hotBuildings.Remove(id);
  96. }
  97.  
  98. private bool IsHotSpot<T, D>(T key, D dict, ref int time) where D : Dictionary<T, int>
  99. {
  100. if (!_enable)
  101. return false;
  102. if (!dict.TryGetValue(key, out time))
  103. return false;
  104. if (Facepunch.Math.Epoch.Current < time)
  105. return true;
  106. dict.Remove(key);
  107. return false;
  108. }
  109.  
  110. private object CanBuild(Planner planner, Construction prefab, Construction.Target target)
  111. {
  112. var player = target.player;
  113. if (player == null)
  114. return null;
  115. var shortName = Core.Utility.GetFileNameWithoutExtension(prefab.fullName);
  116. if (shortName == "ladder.wooden.wall" || ((shortName == "floor") && !player.IsBuildingAuthed()))
  117. return null;
  118. var time = 0;
  119. if (_enableBuildings && target.entity is DecayEntity && IsHotSpot(((DecayEntity)target.entity).buildingID, _hotBuildings, ref time))
  120. {
  121. Player.Message(player, $"This building is <color=#F7B267>Raid Blocked</color>!\nYou can\'t build up here for <color=#FF686B>{time - Facepunch.Math.Epoch.Current}</color> sec.", null, _iconId);
  122. return false;
  123. }
  124. else if (IsHotSpot(target.entity == null ? target.position : target.entity.transform.position, _hotSpots, ref time)
  125. || (target.socket != null && IsHotSpot(target.GetWorldPosition(), _hotSpots, ref time)))
  126. {
  127. Player.Message(player, $"This area is <color=#F7B267>Raid Blocked</color>!\nYou can\'t build up here for <color=#FF686B>{time - Facepunch.Math.Epoch.Current}</color> sec.", null, _iconId);
  128. return false;
  129. }
  130. var building = player.GetBuildingPrivilege()?.GetBuilding();
  131. if (building != null && building.HasBuildingBlocks() && IsHotSpot(building.ID, _hotBuildings, ref time))
  132. {
  133. Player.Message(player, $"This area is <color=#F7B267>Raid Blocked</color>!\nYou can\'t build up here for <color=#FF686B>{time - Facepunch.Math.Epoch.Current}</color> sec.", null, _iconId);
  134. return false;
  135. }
  136. return null;
  137.  
  138. }
  139.  
  140. private object CanChangeGrade(BasePlayer player, BuildingBlock block, BuildingGrade.Enum grade)
  141. {
  142. if (!_enable || !_enableBuildings)
  143. return null;
  144. var time = 0;
  145. if (!IsHotSpot(block.buildingID, _hotBuildings, ref time))
  146. return null;
  147. Player.Message(player, $"This building is <color=#F7B267>Raid Blocked</color>!\nYou can\'t upgrade structures here for <color=#FF686B>{time - Facepunch.Math.Epoch.Current}</color> sec.", null, _iconId);
  148. return false;
  149. }
  150.  
  151. private bool IsFriends(ulong uOwner, ulong uPlr)
  152. {
  153. return (bool)(Clans?.CallHook("HasFriend", uOwner, uPlr) ?? false);
  154. }
  155.  
  156. private static IEnumerable<uint> GetNearestBuildings(BasePlayer player)
  157. {
  158. var list = Pool.GetList<BuildingBlock>();
  159. var obb = player.WorldSpaceBounds();
  160. Vis.Entities(obb.position, 17f + obb.extents.magnitude, list, Layers.Server.Buildings);
  161. var result = list.Select(b => b.buildingID).Distinct().ToArray();
  162. //var result = list.Select(b => b.GetBuilding()).GroupBy(b => b.ID).Select(b => b.First());
  163. Pool.FreeList(ref list);
  164. return result;
  165. }
  166.  
  167. private object OnUserCommand(IPlayer player, string command, string[] args)
  168. {
  169. var plr = (BasePlayer)player.Object;
  170. if (!plr) return null;
  171.  
  172. if (!_raidBlockedCmds.Contains(command)) return null;
  173. var buildings = GetNearestBuildings(plr);
  174. var time = 0;
  175. if (!buildings.Any(building => IsHotSpot(building, _hotBuildings, ref time)))
  176. return null;
  177. Player.Message(plr, $"This command is <color=#F7B267>Blocked</color> while raiding!\nYou can\'t use it for <color=#FF686B>{time - Facepunch.Math.Epoch.Current}</color> sec.", null, _iconId);
  178. return true;
  179. }
  180.  
  181. private object API_IsRaidBlocked(BasePlayer player)
  182. {
  183. var buildings = GetNearestBuildings(player);
  184. var time = 0;
  185. if (!buildings.Any(building => IsHotSpot(building, _hotBuildings, ref time)))
  186. return null;
  187. return time;
  188. }
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement