Guest User

/bgrade fix

a guest
Oct 6th, 2024
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.84 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. using Rust;
  6. using UnityEngine;
  7.  
  8. using Oxide.Core;
  9. using Oxide.Core.Libraries;
  10. using Oxide.Plugins.BGradeExt;
  11.  
  12. namespace Oxide.Plugins
  13. {
  14. [Info("BGrade", "Ryan / Rustoria.co", "1.1.5")]
  15. [Description("Auto update building blocks when placed")]
  16. public class BGrade : RustPlugin
  17. {
  18. #region Declaration
  19.  
  20. public static BGrade Instance;
  21. private ListHashSet<string> _registeredPermissions = new ListHashSet<string>();
  22. private Dictionary<Vector3, int> _lastAttacked = new Dictionary<Vector3, int>();
  23.  
  24. #endregion
  25.  
  26. #region Config
  27.  
  28. private bool ConfigChanged;
  29.  
  30. // Timer settings
  31. private bool AllowTimer;
  32. private int MaxTimer;
  33. private int DefaultTimer;
  34.  
  35. // Last attack settings
  36. private bool CheckLastAttack;
  37. private int UpgradeCooldown;
  38.  
  39. // Command settings
  40. private List<string> ChatCommands;
  41. private List<string> ConsoleCommands;
  42.  
  43. // Refund settings
  44. private bool RefundOnBlock;
  45.  
  46. // Player Component settings
  47. private bool DestroyOnDisconnect;
  48.  
  49. protected override void LoadDefaultConfig() => PrintWarning("Generating default configuration file...");
  50.  
  51. private void InitConfig()
  52. {
  53. AllowTimer = GetConfig(true, "Timer Settings", "Enabled");
  54. DefaultTimer = GetConfig(30, "Timer Settings", "Default Timer");
  55. MaxTimer = GetConfig(180, "Timer Settings", "Max Timer");
  56. ChatCommands = GetConfig(new List<string>
  57. {
  58. "bgrade",
  59. "grade"
  60. }, "Command Settings", "Chat Commands");
  61. ConsoleCommands = GetConfig(new List<string>
  62. {
  63. "bgrade.up"
  64. }, "Command Settings", "Console Commands");
  65. CheckLastAttack = GetConfig(true, "Building Attack Settings", "Enabled");
  66. UpgradeCooldown = GetConfig(30, "Building Attack Settings", "Cooldown Time");
  67. RefundOnBlock = GetConfig(true, "Refund Settings", "Refund on Block");
  68. DestroyOnDisconnect = GetConfig(false, "Destroy Data on Player Disconnect (for high pop servers)");
  69.  
  70. if (ConfigChanged)
  71. {
  72. PrintWarning("Updated configuration file with new/changed values.");
  73. SaveConfig();
  74. }
  75. }
  76.  
  77. private T GetConfig<T>(T defaultVal, params string[] path)
  78. {
  79. var data = Config.Get(path);
  80. if (data != null)
  81. {
  82. return Config.ConvertValue<T>(data);
  83. }
  84.  
  85. Config.Set(path.Concat(new object[] { defaultVal }).ToArray());
  86. ConfigChanged = true;
  87. return defaultVal;
  88. }
  89.  
  90. #endregion
  91.  
  92. #region Lang
  93.  
  94. protected override void LoadDefaultMessages()
  95. {
  96. lang.RegisterMessages(new Dictionary<string, string>
  97. {
  98. ["Permission"] = "You don't have permission to use that command",
  99.  
  100. ["Error.InvalidArgs"] = "Invalid arguments, please use /{0} help",
  101. ["Error.Resources"] = "You don't have enough resources to upgrade.",
  102. ["Error.InvalidTime"] = "Please enter a valid time. '<color=orange>{0}</color>' is not recognised as a number.",
  103. ["Error.TimerTooLong"] = "Please enter a time that is below the value of <color=orange>{0}</color>.",
  104.  
  105. ["Notice.SetGrade"] = "Automatic upgrading is now set to grade <color=orange>{0}</color>.",
  106. ["Notice.SetTime"] = "The disable timer is now set to <color=orange>{0}</color>.",
  107. ["Notice.Disabled"] = "Automatic upgrading is now disabled.",
  108. ["Notice.Disabled.Auto"] = "Automatic upgrading has been automatically disabled.",
  109. ["Notice.Time"] = "It'll automatically disable in <color=orange>{0}</color> seconds.",
  110.  
  111. ["Command.Help"] = "<color=orange><size=16>BGrade Command Usages</size></color>",
  112. ["Command.Help.0"] = "/{0} 0 - Disables BGrade",
  113. ["Command.Help.1"] = "/{0} 1 - Upgrades to Wood upon placement",
  114. ["Command.Help.2"] = "/{0} 2 - Upgrades to Stone upon placement",
  115. ["Command.Help.3"] = "/{0} 3 - Upgrades to Metal upon placement",
  116. ["Command.Help.4"] = "/{0} 4 - Upgrades to Armoured upon placement",
  117. ["Command.Help.T"] = "/{0} t <seconds> - Time until BGrade is disabled",
  118.  
  119. ["Command.Settings"] = "<color=orange><size=16>Your current settings</size></color>",
  120. ["Command.Settings.Timer"] = "Timer: <color=orange>{0}</color> seconds",
  121. ["Command.Settings.Grade"] = "Grade: <color=orange>{0}</color>",
  122.  
  123. ["Words.Disabled"] = "disabled"
  124. }, this);
  125. }
  126.  
  127. #endregion
  128.  
  129. #region Methods
  130.  
  131. private void RegisterPermissions()
  132. {
  133. _registeredPermissions = new ListHashSet<string>( );
  134.  
  135. for (var i = 1; i < 5; i++)
  136. {
  137. RegisterPermission( Name.ToLower() + "." + i );
  138. }
  139.  
  140. RegisterPermission( Name.ToLower() + "." + "nores" );
  141. RegisterPermission( Name.ToLower() + "." + "all" );
  142. }
  143.  
  144. private void RegisterPermission( string permissionName )
  145. {
  146. if ( !_registeredPermissions.Contains( permissionName ) )
  147. {
  148. _registeredPermissions.Add( permissionName );
  149. }
  150.  
  151. permission.RegisterPermission( permissionName, this );
  152. }
  153.  
  154. private void RegisterCommands()
  155. {
  156. foreach (var command in ChatCommands)
  157. {
  158. cmd.AddChatCommand(command, this, BGradeCommand);
  159. }
  160.  
  161. foreach (var command in ConsoleCommands)
  162. {
  163. cmd.AddConsoleCommand(command, this, nameof(BGradeUpCommand));
  164. }
  165. }
  166.  
  167. private void DestroyAll<T>() where T : MonoBehaviour
  168. {
  169. foreach (var type in UnityEngine.Object.FindObjectsOfType<T>())
  170. {
  171. UnityEngine.Object.Destroy(type);
  172. }
  173. }
  174.  
  175. private void DealWithHookResult(BasePlayer player, BuildingBlock buildingBlock, int hookResult, GameObject gameObject)
  176. {
  177. if (hookResult <= 0)
  178. {
  179. return;
  180. }
  181.  
  182. if (RefundOnBlock)
  183. {
  184. foreach (var itemToGive in buildingBlock.BuildCost())
  185. {
  186. player.GiveItem(ItemManager.CreateByItemID(itemToGive.itemid, (int)itemToGive.amount));
  187. }
  188. }
  189.  
  190. gameObject.GetComponent<BaseEntity>().Kill();
  191. }
  192.  
  193. private string TakeResources(BasePlayer player, int playerGrade, BuildingBlock buildingBlock, out Dictionary<int, int> items)
  194. {
  195. var itemsToTake = new Dictionary<int, int>();
  196.  
  197. List<ItemAmount> costToBuild = null;
  198. foreach (var grade in buildingBlock.blockDefinition.grades)
  199. {
  200. if (grade.gradeBase.type == (BuildingGrade.Enum) playerGrade)
  201. {
  202. costToBuild = grade.CostToBuild();
  203. break;
  204. }
  205. }
  206.  
  207. if (costToBuild == null)
  208. {
  209. PrintError($"COULDN'T FIND COST TO BUILD WITH GRADE: {playerGrade} FOR {buildingBlock.PrefabName}");
  210. items = itemsToTake;
  211. return "Error.Resources".Lang(player.UserIDString);
  212. }
  213.  
  214. foreach (var itemAmount in costToBuild)
  215. {
  216. if (!itemsToTake.ContainsKey(itemAmount.itemid))
  217. {
  218. itemsToTake.Add(itemAmount.itemid, 0);
  219. }
  220.  
  221. itemsToTake[itemAmount.itemid] += (int)itemAmount.amount;
  222. }
  223.  
  224. var canAfford = true;
  225. foreach (var itemToTake in itemsToTake)
  226. {
  227. if (!player.HasItemAmount(itemToTake.Key, itemToTake.Value))
  228. {
  229. canAfford = false;
  230. }
  231. }
  232.  
  233. items = itemsToTake;
  234. return canAfford ? null : "Error.Resources".Lang(player.UserIDString);
  235. }
  236.  
  237. private void CheckLastAttacked()
  238. {
  239. foreach (var lastAttackEntry in _lastAttacked.ToList())
  240. {
  241. if (!WasAttackedRecently(lastAttackEntry.Key))
  242. {
  243. _lastAttacked.Remove(lastAttackEntry.Key);
  244. }
  245. }
  246. }
  247.  
  248. private bool WasAttackedRecently(Vector3 position)
  249. {
  250. int time;
  251. if (!_lastAttacked.TryGetValue(position, out time))
  252. {
  253. return false;
  254. }
  255.  
  256. if (time < Facepunch.Math.Epoch.Current)
  257. {
  258. return true;
  259. }
  260.  
  261. return false;
  262. }
  263.  
  264. #endregion
  265.  
  266. #region BGrade Player
  267.  
  268. private class BGradePlayer : FacepunchBehaviour
  269. {
  270. public static Dictionary<BasePlayer, BGradePlayer> Players = new Dictionary<BasePlayer, BGradePlayer>();
  271.  
  272. private BasePlayer _player;
  273. private Timer _timer;
  274. private int _grade;
  275. private int _time;
  276.  
  277. public void Awake()
  278. {
  279. var attachedPlayer = GetComponent<BasePlayer>();
  280. if ( attachedPlayer == null || !attachedPlayer.IsConnected )
  281. {
  282. return;
  283. }
  284.  
  285. _player = attachedPlayer;
  286. Players[_player] = this;
  287.  
  288. _time = GetTime(false);
  289. }
  290.  
  291. public int GetTime(bool updateTime = true)
  292. {
  293. if (!Instance.AllowTimer)
  294. {
  295. return 0;
  296. }
  297.  
  298. if (updateTime)
  299. {
  300. UpdateTime();
  301. }
  302.  
  303. return _time != 0 ? _time : Instance.DefaultTimer;
  304. }
  305.  
  306. public void UpdateTime()
  307. {
  308. if (_time <= 0)
  309. {
  310. return;
  311. }
  312.  
  313. DestroyTimer();
  314.  
  315. SetTimer(Instance.timer.Once(_time, () =>
  316. {
  317. _grade = 0;
  318. DestroyTimer();
  319. _player.ChatMessage("Notice.Disabled.Auto".Lang(_player.UserIDString));
  320. }));
  321. }
  322.  
  323. public int GetGrade() => _grade;
  324.  
  325. public bool IsTimerValid
  326. {
  327. get
  328. {
  329. return _timer != null && !_timer.Destroyed;
  330. }
  331. }
  332.  
  333. private void SetTimer(Timer timer)
  334. {
  335. _timer = timer;
  336. }
  337.  
  338. public void SetGrade(int newGrade)
  339. {
  340. _grade = newGrade;
  341. }
  342.  
  343. public void SetTime(int newTime)
  344. {
  345. _time = newTime;
  346. }
  347.  
  348. public void DestroyTimer()
  349. {
  350. _timer?.Destroy();
  351. _timer = null;
  352. }
  353.  
  354. public void Destroy()
  355. {
  356. Destroy(this);
  357. }
  358.  
  359. public void OnDestroy()
  360. {
  361. if ( Players.ContainsKey( _player ) )
  362. {
  363. Players.Remove( _player );
  364. }
  365. }
  366. }
  367.  
  368. #endregion
  369.  
  370. #region Hooks
  371.  
  372. private void Init()
  373. {
  374. Instance = this;
  375.  
  376. InitConfig();
  377. RegisterCommands();
  378. RegisterPermissions();
  379.  
  380. if (!CheckLastAttack)
  381. {
  382. Unsubscribe(nameof(OnEntityDeath));
  383. Unsubscribe(nameof(OnServerSave));
  384. }
  385.  
  386. if (!DestroyOnDisconnect)
  387. {
  388. Unsubscribe(nameof(OnPlayerDisconnected));
  389. }
  390. }
  391.  
  392. private void OnServerSave()
  393. {
  394. CheckLastAttacked();
  395. }
  396.  
  397. private void Unload()
  398. {
  399. Instance = null;
  400. DestroyAll<BGradePlayer>();
  401. BGradePlayer.Players.Clear();
  402. }
  403.  
  404. private void OnEntityBuilt(Planner plan, GameObject gameObject)
  405. {
  406. var player = plan?.GetOwnerPlayer();
  407. if (player == null)
  408. {
  409. return;
  410. }
  411.  
  412. if ( plan.isTypeDeployable )
  413. {
  414. return;
  415. }
  416.  
  417. var buildingBlock = gameObject.GetComponent<BuildingBlock>();
  418. if ( buildingBlock == null )
  419. {
  420. return;
  421. }
  422.  
  423. if (!player.CanBuild())
  424. {
  425. return;
  426. }
  427.  
  428. if ( !player.HasAnyPermission( _registeredPermissions ) )
  429. {
  430. return;
  431. }
  432.  
  433. BGradePlayer bgradePlayer;
  434. if ( !BGradePlayer.Players.TryGetValue( player, out bgradePlayer ) )
  435. {
  436. return;
  437. }
  438.  
  439. var playerGrade = bgradePlayer.GetGrade();
  440. if (playerGrade == 0)
  441. {
  442. return;
  443. }
  444.  
  445. if (!player.HasPluginPerm("all") && !player.HasPluginPerm(playerGrade.ToString()))
  446. {
  447. return;
  448. }
  449.  
  450. var hookCall = Interface.Call("CanBGrade", player, playerGrade, buildingBlock, plan);
  451.  
  452. if (hookCall is int)
  453. {
  454. DealWithHookResult(player, buildingBlock, (int) hookCall, gameObject);
  455. return;
  456. }
  457.  
  458. if (playerGrade < (int) buildingBlock.grade || buildingBlock.blockDefinition.grades[playerGrade] == null)
  459. {
  460. return;
  461. }
  462.  
  463. if (CheckLastAttack && WasAttackedRecently(buildingBlock.transform.position))
  464. {
  465. return;
  466. }
  467.  
  468. if (Interface.Call("OnStructureUpgrade", buildingBlock, player, (BuildingGrade.Enum) playerGrade) != null)
  469. {
  470. return;
  471. }
  472.  
  473. if (!player.HasPluginPerm("nores"))
  474. {
  475. Dictionary<int, int> itemsToTake;
  476. var resourceResponse = TakeResources(player, playerGrade, buildingBlock, out itemsToTake);
  477. if (!string.IsNullOrEmpty(resourceResponse))
  478. {
  479. player.ChatMessage(resourceResponse);
  480. return;
  481. }
  482.  
  483. foreach (var itemToTake in itemsToTake)
  484. {
  485. player.TakeItem(itemToTake.Key, itemToTake.Value);
  486. }
  487. }
  488.  
  489. if (AllowTimer)
  490. {
  491. bgradePlayer.UpdateTime();
  492. }
  493.  
  494. buildingBlock.SetGrade((BuildingGrade.Enum)playerGrade);
  495. buildingBlock.SetHealthToMax();
  496. buildingBlock.StartBeingRotatable();
  497. buildingBlock.SendNetworkUpdate();
  498. buildingBlock.UpdateSkin();
  499. buildingBlock.ResetUpkeepTime();
  500. buildingBlock.GetBuilding()?.Dirty();
  501. }
  502.  
  503. private object OnPayForPlacement( BasePlayer player, Planner planner, Construction component )
  504. {
  505. if ( planner.isTypeDeployable )
  506. {
  507. return null;
  508. }
  509.  
  510. if ( !BGradePlayer.Players.ContainsKey( player ) )
  511. {
  512. return null;
  513. }
  514.  
  515. if ( !player.HasPluginPerm( "nores" ) )
  516. {
  517. return null;
  518. }
  519.  
  520. var bgradePlayer = BGradePlayer.Players[player];
  521. if ( bgradePlayer.GetGrade() == 0 )
  522. {
  523. return null;
  524. }
  525.  
  526. return false;
  527. }
  528.  
  529. private void OnEntityDeath(BuildingBlock buildingBlock, HitInfo info)
  530. {
  531. var attacker = info?.InitiatorPlayer;
  532. if (attacker == null)
  533. {
  534. return;
  535. }
  536.  
  537. if (info.damageTypes.GetMajorityDamageType() == DamageType.Explosion)
  538. {
  539. _lastAttacked[buildingBlock.transform.position] = Facepunch.Math.Epoch.Current + UpgradeCooldown;
  540. }
  541. }
  542.  
  543. private void OnPlayerDisconnected(BasePlayer player)
  544. {
  545. BGradePlayer bgradePlayer;
  546. if ( !BGradePlayer.Players.TryGetValue( player, out bgradePlayer ) )
  547. {
  548. return;
  549. }
  550.  
  551. bgradePlayer.Destroy();
  552. }
  553.  
  554. #endregion
  555.  
  556. #region Commands
  557.  
  558. private void BGradeCommand(BasePlayer player, string command, string[] args)
  559. {
  560. if (!player.HasAnyPermission(_registeredPermissions))
  561. {
  562. player.ChatMessage("Permission".Lang(player.UserIDString));
  563. return;
  564. }
  565.  
  566. if (args.Length == 0)
  567. {
  568. player.ChatMessage("Error.InvalidArgs".Lang(player.UserIDString, command));
  569. return;
  570. }
  571.  
  572. var chatMsgs = new List<string>();
  573.  
  574. switch (args[0].ToLower())
  575. {
  576. case "0":
  577. {
  578. player.ChatMessage("Notice.Disabled".Lang(player.UserIDString));
  579. BGradePlayer bgradePlayer;
  580. if ( BGradePlayer.Players.TryGetValue( player, out bgradePlayer ) )
  581. {
  582. bgradePlayer.DestroyTimer();
  583. bgradePlayer.SetGrade( 0 );
  584. }
  585. return;
  586. }
  587.  
  588. case "1":
  589. case "2":
  590. case "3":
  591. case "4":
  592. {
  593. if (!player.HasPluginPerm("all") && !player.HasPluginPerm(args[0]))
  594. {
  595. player.ChatMessage("Permission".Lang(player.UserIDString));
  596. return;
  597. }
  598.  
  599. var grade = Convert.ToInt32(args[0]);
  600.  
  601. BGradePlayer bgradePlayer;
  602. if ( !BGradePlayer.Players.TryGetValue( player, out bgradePlayer ) )
  603. {
  604. bgradePlayer = player.gameObject.AddComponent<BGradePlayer>();
  605. }
  606.  
  607. bgradePlayer.SetGrade(grade);
  608. var time = bgradePlayer.GetTime();
  609. chatMsgs.Add("Notice.SetGrade".Lang(player.UserIDString, grade));
  610.  
  611. if (AllowTimer && time > 0)
  612. {
  613. chatMsgs.Add("Notice.Time".Lang(player.UserIDString, time));
  614. }
  615.  
  616. player.ChatMessage(string.Join("\n", chatMsgs.ToArray()));
  617. return;
  618. }
  619.  
  620. case "t":
  621. {
  622. if ( !AllowTimer )
  623. {
  624. return;
  625. }
  626.  
  627. if ( args.Length == 1 )
  628. {
  629. goto default;
  630. }
  631.  
  632. int time;
  633. if (!int.TryParse(args[1], out time) || time <= 0)
  634. {
  635. player.ChatMessage("Error.InvalidTime".Lang(player.UserIDString, args[1]));
  636. return;
  637. }
  638.  
  639. if (time > MaxTimer)
  640. {
  641. player.ChatMessage("Error.TimerTooLong".Lang(player.UserIDString, MaxTimer));
  642. return;
  643. }
  644.  
  645. BGradePlayer bgradePlayer;
  646. if ( !BGradePlayer.Players.TryGetValue( player, out bgradePlayer ) )
  647. {
  648. bgradePlayer = player.gameObject.AddComponent<BGradePlayer>();
  649. }
  650.  
  651. player.ChatMessage("Notice.SetTime".Lang(player.UserIDString, time));
  652. bgradePlayer.SetTime(time);
  653. return;
  654. }
  655.  
  656. case "help":
  657. {
  658. chatMsgs.Add("Command.Help".Lang(player.UserIDString));
  659. if (AllowTimer)
  660. {
  661. chatMsgs.Add("Command.Help.T".Lang(player.UserIDString, command));
  662. chatMsgs.Add("Command.Help.0".Lang(player.UserIDString, command));
  663. }
  664.  
  665. for (var i = 1; i < 5; i++)
  666. {
  667. if (player.HasPluginPerm(i.ToString()) || player.HasPluginPerm("all"))
  668. chatMsgs.Add($"Command.Help.{i}".Lang(player.UserIDString, command));
  669. }
  670.  
  671. if (chatMsgs.Count <= 3 && !player.HasPluginPerm("all"))
  672. {
  673. player.ChatMessage("Permission".Lang(player.UserIDString));
  674. return;
  675. }
  676.  
  677. BGradePlayer bgradePlayer;
  678. if ( BGradePlayer.Players.TryGetValue( player, out bgradePlayer ) )
  679. {
  680. chatMsgs.Add( "Command.Settings".Lang( player.UserIDString ) );
  681. if ( AllowTimer )
  682. {
  683. chatMsgs.Add( "Command.Settings.Timer".Lang( player.UserIDString, bgradePlayer.GetTime( false ) ) );
  684. }
  685.  
  686. var fetchedGrade = bgradePlayer.GetGrade();
  687. chatMsgs.Add( "Command.Settings.Grade".Lang( player.UserIDString, fetchedGrade == 0 ? "Words.Disabled".Lang( player.UserIDString ) : fetchedGrade.ToString() ) );
  688. }
  689.  
  690. player.ChatMessage(string.Join("\n", chatMsgs.ToArray()));
  691. return;
  692. }
  693.  
  694. default:
  695. {
  696. player.ChatMessage("Error.InvalidArgs".Lang(player.UserIDString, command));
  697. return;
  698. }
  699. }
  700. }
  701.  
  702. private void BGradeUpCommand(ConsoleSystem.Arg arg)
  703. {
  704. var player = arg?.Player();
  705. if (player == null)
  706. {
  707. return;
  708. }
  709.  
  710. if (!player.HasAnyPermission(_registeredPermissions))
  711. {
  712. player.ChatMessage("Permission".Lang(player.UserIDString));
  713. return;
  714. }
  715.  
  716. BGradePlayer bgradePlayer;
  717. if ( !BGradePlayer.Players.TryGetValue( player, out bgradePlayer ) )
  718. {
  719. bgradePlayer = player.gameObject.AddComponent<BGradePlayer>();
  720. }
  721. var grade = bgradePlayer.GetGrade() + 1;
  722. var count = 0;
  723.  
  724. if (!player.HasPluginPerm("all"))
  725. {
  726. while (!player.HasPluginPerm(grade.ToString()))
  727. {
  728. var newGrade = grade++;
  729. if (newGrade > 4)
  730. {
  731. grade = 1;
  732. }
  733.  
  734. if (count > bgradePlayer.GetGrade() + 4)
  735. {
  736. player.ChatMessage("Permission".Lang(player.UserIDString));
  737. return;
  738. }
  739. }
  740. }
  741. else if (grade > 4) grade = 1;
  742.  
  743. var chatMsgs = new List<string>();
  744. bgradePlayer.SetGrade(grade);
  745. var time = bgradePlayer.GetTime();
  746.  
  747. chatMsgs.Add("Notice.SetGrade".Lang(player.UserIDString, grade));
  748. if (AllowTimer && time > 0)
  749. {
  750. chatMsgs.Add("Notice.Time".Lang(player.UserIDString, time));
  751. }
  752.  
  753. player.ChatMessage(string.Join("\n", chatMsgs.ToArray()));
  754. }
  755.  
  756. #endregion
  757. }
  758. }
  759.  
  760. namespace Oxide.Plugins.BGradeExt
  761. {
  762. public static class BGradeExtensions
  763. {
  764. private static readonly Permission permission = Interface.Oxide.GetLibrary<Permission>();
  765. private static readonly Lang lang = Interface.Oxide.GetLibrary<Lang>();
  766.  
  767. public static bool HasAnyPermission(this BasePlayer player, ListHashSet<string> perms)
  768. {
  769. foreach (var perm in perms)
  770. {
  771. if (!player.HasPermission(perm))
  772. {
  773. continue;
  774. }
  775.  
  776. return true;
  777. }
  778.  
  779. return false;
  780. }
  781.  
  782. public static bool HasPermission(this BasePlayer player, string perm)
  783. {
  784. return permission.UserHasPermission(player.UserIDString, perm);
  785. }
  786.  
  787. public static bool HasPluginPerm(this BasePlayer player, string perm)
  788. {
  789. return permission.UserHasPermission(player.UserIDString, BGrade.Instance.Name.ToLower() + "." + perm);
  790. }
  791.  
  792. public static string Lang(this string key, string id = null, params object[] args)
  793. {
  794. return string.Format(lang.GetMessage(key, BGrade.Instance, id), args);
  795. }
  796.  
  797. public static bool HasItemAmount(this BasePlayer player, int itemId, int itemAmount)
  798. {
  799. var items = new List<Item>();
  800. player.inventory.GetAllItems(items);
  801. var count = items.Where(item => item.info.itemid == itemId).Sum(item => item.amount);
  802.  
  803. return count >= itemAmount;
  804. }
  805.  
  806. public static bool HasItemAmount(this BasePlayer player, int itemId, int itemAmount, out int amountGot)
  807. {
  808. var items = new List<Item>();
  809. player.inventory.GetAllItems(items);
  810. var count = items.Where(item => item.info.itemid == itemId).Sum(item => item.amount);
  811.  
  812. amountGot = count;
  813. return count >= itemAmount;
  814. }
  815.  
  816. public static void TakeItem(this BasePlayer player, int itemId, int itemAmount)
  817. {
  818. if (player.inventory.Take(null, itemId, itemAmount) > 0)
  819. {
  820. player.SendConsoleCommand("note.inv", itemId, itemAmount * -1);
  821. }
  822. }
  823. }
  824. }
Advertisement
Add Comment
Please, Sign In to add comment