Guest User

Untitled

a guest
Oct 15th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.35 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using Oxide.Core;
  3. using Oxide.Core.Plugins;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityEngine;
  8.  
  9. namespace Oxide.Plugins
  10. {
  11. [Info("Boat Licence", "Sorrow", "0.0.1")]
  12. [Description("Allow player to buy boat and spawn or store this")]
  13.  
  14. class BoatLicence: RustPlugin
  15. {
  16. #region Fields
  17. [PluginReference]
  18. Plugin Economics;
  19.  
  20. private Dictionary<ulong, LisencedPlayer> _lisencedPlayer = new Dictionary<ulong, LisencedPlayer>();
  21. private Dictionary<uint, LisencedPlayer> _boatsCache = new Dictionary<uint, LisencedPlayer>();
  22.  
  23. private bool _canBuyRowBoat;
  24. private bool _canBuyRhibBoat;
  25.  
  26. private double _rowBoatCost;
  27. private double _rhibBoatCost;
  28.  
  29. private int _intervalToCheckBoat;
  30. private int _timeBeforeBoatWipe;
  31.  
  32. const string prefix = "<color='orange'>[Boat Licence]</color> ";
  33. const string rowBoatPrefab = "assets/content/vehicles/boats/rowboat/rowboat.prefab";
  34. const string rhibBoatPrefab = "assets/content/vehicles/boats/rhib/rhib.prefab";
  35. #endregion
  36.  
  37. #region uMod Hooks
  38. private void Loaded()
  39. {
  40. if (Economics == null)
  41. {
  42. PrintError("Economics is not loaded, get it at https://umod.org");
  43. }
  44. }
  45.  
  46. private void OnServerInitialized()
  47. {
  48. _canBuyRowBoat = Convert.ToBoolean(Config["Can buy rowing boat"]);
  49. _canBuyRhibBoat = Convert.ToBoolean(Config["Can buy RHIB"]);
  50.  
  51. _rowBoatCost = Convert.ToDouble(Config["Cost of rowing boat"]);
  52. _rhibBoatCost = Convert.ToDouble(Config["Cost of RHIB"]);
  53.  
  54. _intervalToCheckBoat = Convert.ToInt32(Config["Interval in minutes to check boat for wipe"]);
  55. _timeBeforeBoatWipe = Convert.ToInt32(Config["Time before boat wipe in minutes"]);
  56.  
  57. LoadData();
  58. CheckBoats();
  59. }
  60.  
  61. private void Unload()
  62. {
  63. foreach (var boat in _boatsCache.ToList())
  64. {
  65. RemoveBoat(boat.Key, boat.Value);
  66. }
  67.  
  68. SaveData();
  69. }
  70.  
  71. private void OnEntityDismounted(BaseMountable entity, BasePlayer player)
  72. {
  73. var vehicleParent = entity.VehicleParent();
  74. LisencedPlayer lisencedPlayer;
  75. if (!_boatsCache.TryGetValue(vehicleParent.net.ID, out lisencedPlayer)) return;
  76. lisencedPlayer.UpdateBoatLastDismount(vehicleParent.net.ID);
  77. }
  78.  
  79. private void OnEntityKill(BaseNetworkable entity)
  80. {
  81. LisencedPlayer lisencedPlayer;
  82. if (entity == null || !_boatsCache.TryGetValue(entity.net.ID, out lisencedPlayer)) return;
  83. lisencedPlayer.ResetBoat(entity.net.ID);
  84. _boatsCache.Remove(entity.net.ID);
  85. }
  86.  
  87. private void OnServerSave()
  88. {
  89. SaveData();
  90. }
  91.  
  92. private void OnServerShutdown()
  93. {
  94. SaveData();
  95. }
  96. #endregion
  97.  
  98. #region Commands
  99. [ChatCommand("boatlicence")]
  100. void cmdBoatLicence(BasePlayer player, string command, string[] args)
  101. {
  102. SendReply(player, Msg("helpBoatLicence", player.UserIDString));
  103. }
  104.  
  105. [ChatCommand("buyboat")]
  106. void cmdBuyRowBoat(BasePlayer player, string command, string[] args)
  107. {
  108. LisencedPlayer lisencedPlayer;
  109.  
  110. if (args.Length == 0) SendReply(player, Msg("helpBuyBoat", player.UserIDString));
  111. if (args.Length >= 1)
  112. {
  113. switch (args[0].ToLower())
  114. {
  115. case "row":
  116. if (!_canBuyRowBoat)
  117. {
  118. SendReply(player, Msg("rowBoatCannotBePurchased", player.UserIDString));
  119. break;
  120. }
  121. if (Economics.Call<double>("Balance", player.userID) >= _rowBoatCost)
  122. {
  123. if (!_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
  124. {
  125. lisencedPlayer = new LisencedPlayer(player.userID);
  126. lisencedPlayer.rowBoat.Buyed = true;
  127. _lisencedPlayer.Add(player.userID, lisencedPlayer);
  128. Economics.Call<bool>("Withdraw", player.userID, _rowBoatCost);
  129. }
  130. else if (!lisencedPlayer.rowBoat.Buyed)
  131. {
  132. if (Economics.Call<bool>("Withdraw", player.userID, _rowBoatCost)) lisencedPlayer.rowBoat.Buyed = true;
  133. }
  134. else
  135. {
  136. SendReply(player, Msg("boatAlreadyPurchased", player.UserIDString));
  137. }
  138. }
  139. else
  140. {
  141. Puts(Economics.Call<double>("Balance", player.userID).ToString() + " " + _rowBoatCost);
  142. SendReply(player, Msg("noMoney", player.UserIDString));
  143. }
  144. break;
  145. case "rhib":
  146. if (!_canBuyRhibBoat)
  147. {
  148. SendReply(player, Msg("rhibCannotBePurchased", player.UserIDString));
  149. break;
  150. }
  151. if (Economics.Call<double>("Balance", player.userID) >= _rhibBoatCost)
  152. {
  153. if (!_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
  154. {
  155. lisencedPlayer = new LisencedPlayer(player.userID);
  156. lisencedPlayer.rhibBoat.Buyed = true;
  157. _lisencedPlayer.Add(player.userID, lisencedPlayer);
  158. }
  159. else if (!lisencedPlayer.rhibBoat.Buyed)
  160. {
  161. if (Economics.Call<bool>("Withdraw", player.userID, _rhibBoatCost)) lisencedPlayer.rhibBoat.Buyed = true;
  162. }
  163. else
  164. {
  165. SendReply(player, Msg("boatAlreadyPurchased", player.UserIDString));
  166. }
  167. }
  168. else
  169. {
  170. SendReply(player, Msg("noMoney", player.UserIDString));
  171. }
  172. break;
  173. default:
  174. SendReply(player, Msg("helpOptionNotFound", player.UserIDString));
  175. break;
  176. }
  177. }
  178. }
  179.  
  180. [ChatCommand("spawnboat")]
  181. void cmdSpawnRowBoat(BasePlayer player, string command, string[] args)
  182. {
  183. Vector3 position = player.transform.position + (player.transform.forward * 3);
  184. LisencedPlayer lisencedPlayer;
  185.  
  186. if (args.Length == 0) SendReply(player, Msg("helpSpawnBoat", player.UserIDString));
  187. if (args.Length >= 1)
  188. {
  189. switch (args[0].ToLower())
  190. {
  191. case "row":
  192. if (_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
  193. {
  194. if (!lisencedPlayer.rowBoat.Buyed)
  195. {
  196. SendReply(player, Msg("didntBuyRowBoat", player.UserIDString));
  197. return;
  198. }
  199. else if (lisencedPlayer.rowBoat.Id != 0)
  200. {
  201. SendReply(player, Msg("alreadyRowBoatOut", player.UserIDString));
  202. return;
  203. }
  204. var entity = SpawnRowBoat(position, player.transform.rotation);
  205. lisencedPlayer.rowBoat.Id = entity.net.ID;
  206. lisencedPlayer.UpdateBoatLastDismount(entity.net.ID);
  207. _boatsCache.Add(lisencedPlayer.rowBoat.Id, lisencedPlayer);
  208. }
  209. else
  210. {
  211. Puts("Vous n'avez pas encore acheter de bateau");
  212. }
  213. break;
  214. case "rhib":
  215. if (_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
  216. {
  217. if (!lisencedPlayer.rhibBoat.Buyed)
  218. {
  219. SendReply(player, Msg("didntBuyRhib", player.UserIDString));
  220. return;
  221. }
  222. else if (lisencedPlayer.rhibBoat.Id != 0)
  223. {
  224. SendReply(player, Msg("alreadyRhibOut", player.UserIDString));
  225. return;
  226. }
  227. var entity = SpawnRhibBoat(position, player.transform.rotation);
  228. lisencedPlayer.rhibBoat.Id = entity.net.ID;
  229. lisencedPlayer.UpdateBoatLastDismount(entity.net.ID);
  230. _boatsCache.Add(lisencedPlayer.rhibBoat.Id, lisencedPlayer);
  231. }
  232. else
  233. {
  234. Puts("Vous n'avez pas encore acheter de bateau");
  235. }
  236. break;
  237. default:
  238. SendReply(player, Msg("helpOptionNotFound", player.UserIDString));
  239. break;
  240. }
  241. }
  242. }
  243.  
  244. [ChatCommand("recallboat")]
  245. void cmdRecallRowBoat(BasePlayer player, string command, string[] args)
  246. {
  247. LisencedPlayer lisencedPlayer;
  248.  
  249.  
  250. if (args.Length == 0) SendReply(player, Msg("helpRecallBoat", player.UserIDString));
  251. if (args.Length >= 1)
  252. {
  253. switch (args[0].ToLower())
  254. {
  255. case "row":
  256. if (_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
  257. {
  258. RemoveBoat(lisencedPlayer.rhibBoat.Id, lisencedPlayer);
  259. }
  260. break;
  261. case "rhib":
  262. if (_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
  263. {
  264. RemoveBoat(lisencedPlayer.rhibBoat.Id, lisencedPlayer);
  265. }
  266. break;
  267. default:
  268. SendReply(player, Msg("helpOptionNotFound", player.UserIDString));
  269. break;
  270. }
  271. }
  272. }
  273. #endregion
  274.  
  275. #region Functions
  276. private BaseEntity SpawnRowBoat(Vector3 position, Quaternion rotation = default(Quaternion))
  277. {
  278. BaseEntity entity = GameManager.server.CreateEntity(rowBoatPrefab, position + Vector3.up, rotation);
  279. entity.enableSaving = true;
  280. entity.Spawn();
  281.  
  282. return entity;
  283. }
  284.  
  285. private BaseEntity SpawnRhibBoat(Vector3 position, Quaternion rotation = default(Quaternion))
  286. {
  287. BaseEntity entity = GameManager.server.CreateEntity(rhibBoatPrefab, position + Vector3.up, rotation);
  288. entity.enableSaving = true;
  289. entity.Spawn();
  290.  
  291. return entity;
  292. }
  293.  
  294. private void RemoveBoat(uint boatID, LisencedPlayer lisencedPlayer)
  295. {
  296. BaseNetworkable.serverEntities.Find(boatID)?.Kill();
  297. lisencedPlayer.ResetBoat(boatID);
  298. _boatsCache?.Remove(boatID);
  299. }
  300.  
  301. private void UpdateLisencedPlayer(LisencedPlayer lisencedPlayer, uint boatID)
  302. {
  303. if (lisencedPlayer.rowBoat.Id == boatID) lisencedPlayer.rowBoat.Id = 0;
  304. if (lisencedPlayer.rhibBoat.Id == boatID) lisencedPlayer.rhibBoat.Id = 0;
  305. }
  306.  
  307. private void CheckBoats()
  308. {
  309. foreach (var boat in _boatsCache.ToList())
  310. {
  311. LisencedPlayer lisencedPlayer = boat.Value;
  312. var boatNetworkable = BaseNetworkable.serverEntities.Find(boat.Key);
  313. var boatEntity = boatNetworkable.GetComponent<BaseBoat>();
  314. if (boatEntity == null) continue;
  315. if (boatEntity.IsMounted()) continue;
  316. if (lisencedPlayer.rowBoat.Id == boat.Key)
  317. {
  318. if (BoatIsActive(lisencedPlayer.rowBoat.LastDismount)) continue;
  319. RemoveBoat(boat.Key, lisencedPlayer);
  320. }
  321. else if (lisencedPlayer.rhibBoat.Id == boat.Key)
  322. {
  323. if (BoatIsActive(lisencedPlayer.rhibBoat.LastDismount)) continue;
  324. RemoveBoat(boat.Key, lisencedPlayer);
  325. }
  326. }
  327.  
  328. timer.Once(_intervalToCheckBoat * 60f, () => CheckBoats());
  329. }
  330.  
  331. private bool BoatIsActive(DateTime lastDismount)
  332. {
  333. return lastDismount.Ticks >= DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(_timeBeforeBoatWipe)).Ticks;
  334. }
  335. #endregion
  336.  
  337.  
  338. #region Localization
  339. private string Msg(string key, string playerId = null) => prefix + lang.GetMessage(key, this, playerId);
  340.  
  341. protected override void LoadDefaultMessages()
  342. {
  343. lang.RegisterMessages(new Dictionary<string, string>
  344. {
  345. ["helpBoatLicence"] = "These are the available commands: \n/buyboat -- To buy a boat \n/spawnboat -- To spawn a boat \n/recallboat -- To recall a boat",
  346. ["helpBuyBoat"] = "These are the available commands: \n/buyboat row -- To buy a rowing boat \n/buyboat rhib -- To buy a RHIB.",
  347. ["helpSpawnBoat"] = "These are the available commands: \n/spawnboat row -- To spawn a rowing boat \n/spawnboat rhib -- To spawn a RHIB.",
  348. ["helpRecallBoat"] = "These are the available commands: \n/recallboat row -- To recall a rowing boat \n/recallboat rhib -- To recall a RHIB.",
  349. ["helpOptionNotFound"] = "This option doesn't exist.",
  350. ["boatAlreadyPurchased"] = "You have already purchased this boat.",
  351. ["rowBoatCannotBeBuyed"] = "You can't buy a rowing boat.",
  352. ["rhibCannotBeBuyed"] = "You can't buy a RHIB.",
  353. ["noMoney"] = "You don't have enough money.",
  354. ["didntBuyRowBoat"] = "You didn't purchase a rowing boat.",
  355. ["didntBuyRhib"] = "You didn't purchase a RHIB.",
  356. ["alreadyRowBoatOut"] = "You already have a rowing boat outside.",
  357. ["alreadyRhibOut"] = "You already have a RHIB outside.",
  358. ["boatNotYetPurchased"] = "You have not yet purchased a boat.",
  359. }, this);
  360.  
  361. lang.RegisterMessages(new Dictionary<string, string>
  362. {
  363. ["helpBoatLicence"] = "Voici les commandes disponibles : \n/buyboat -- Pour acheter un bateau \n/spawnboat -- Pour faire apparaître un bateau \n/recallboat -- Pour ranger un bateau",
  364. ["helpBuyBoat"] = "Voici les commandes disponibles : \n/buyboat row -- Pour acheter un bateau à rames \n/buyboat rhib -- Pour acheter un RHIB.",
  365. ["helpSpawnBoat"] = "Voici les commandes disponibles : \n/spawnboat row -- Pour faire apparaître un bateau à rames \n/spawnboat rhib -- Pour faire apparaître un RHIB.",
  366. ["helpRecallBoat"] = "Voici les commandes disponibles : \n/recallboat row -- Pour ranger un bateau à rames \n/recallboat rhib -- Pour ranger un RHIB.",
  367. ["helpOptionNotFound"] = "Cette option n'existe pas.",
  368. ["boatAlreadyPurchased"] = "Vous avez déjà acheté ce bateau.",
  369. ["rowBoatCannotBeBuyed"] = "On ne peut pas acheter un bateau à rames.",
  370. ["rhibCannotBeBuyed"] = "Vous ne pouvez pas acheter un RHIB.",
  371. ["noMoney"] = "Vous n'avez pas assez d'argent.",
  372. ["didntBuyRowBoat"] = "Vous n'avez pas acheté de bateau à rames.",
  373. ["didntBuyRhib"] = "Vous n'avez pas acheté de RHIB.",
  374. ["alreadyRowBoatOut"] = "Vous avez déjà un bateau à rames à l'extérieur.",
  375. ["alreadyRhibOut"] = "Vous avez déjà un RHIB à l'extérieur.",
  376. ["boatNotYetPurchased"] = "Vous n'avez pas encore acheté de bateau.",
  377. }, this, "fr");
  378. }
  379. #endregion
  380.  
  381. #region Config
  382. private new void LoadDefaultConfig()
  383. {
  384. PrintWarning("Creating a new configuration file");
  385. Config.Clear();
  386.  
  387. Config["Can buy rowing boat"] = true;
  388. Config["Can buy RHIB"] = true;
  389. Config["Cost of rowing boat"] = 1000;
  390. Config["Cost of RHIB"] = 5000;
  391. Config["Interval in minutes to check boat for wipe"] = 5;
  392. Config["Time before boat wipe in minutes"] = 15;
  393.  
  394. SaveConfig();
  395. }
  396.  
  397. private void LoadData()
  398. {
  399. _lisencedPlayer = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, LisencedPlayer>>("BoatLicence");
  400. }
  401.  
  402. private void SaveData() => Interface.Oxide.DataFileSystem.WriteObject("BoatLicence", _lisencedPlayer);
  403. #endregion
  404.  
  405. #region Class
  406. class LisencedPlayer
  407. {
  408. private readonly ulong userid;
  409. public RowBoat rowBoat { get; }
  410. public RhibBoat rhibBoat { get; }
  411.  
  412. public LisencedPlayer(ulong userid)
  413. {
  414. this.userid = userid;
  415. rowBoat = new RowBoat();
  416. rhibBoat = new RhibBoat();
  417. }
  418.  
  419. [JsonIgnore]
  420. public ulong Userid
  421. {
  422. get
  423. {
  424. return userid;
  425. }
  426. }
  427.  
  428. public void ResetBoat(uint boatID)
  429. {
  430. if (rowBoat.Id == boatID)
  431. {
  432. rowBoat.Id = 0;
  433. rowBoat.LastDismount = DateTime.MinValue;
  434. }
  435. else if (rhibBoat.Id == boatID)
  436. {
  437. rhibBoat.Id = 0;
  438. rhibBoat.LastDismount = DateTime.MinValue;
  439. }
  440. }
  441.  
  442. public void UpdateBoatLastDismount(uint boatID)
  443. {
  444. if (rowBoat.Id == boatID) rowBoat.LastDismount = DateTime.UtcNow;
  445. if (rhibBoat.Id == boatID) rhibBoat.LastDismount = DateTime.UtcNow;
  446. }
  447. }
  448.  
  449. class RowBoat
  450. {
  451. private bool buyed;
  452. private uint id;
  453. private DateTime lastDismount;
  454.  
  455. public RowBoat()
  456. {
  457. LastDismount = DateTime.MinValue;
  458. }
  459.  
  460. public bool Buyed
  461. {
  462. get
  463. {
  464. return buyed;
  465. }
  466.  
  467. set
  468. {
  469. buyed = value;
  470. }
  471. }
  472.  
  473. public uint Id
  474. {
  475. get
  476. {
  477. return id;
  478. }
  479.  
  480. set
  481. {
  482. id = value;
  483. }
  484. }
  485.  
  486. public DateTime LastDismount
  487. {
  488. get
  489. {
  490. return lastDismount;
  491. }
  492.  
  493. set
  494. {
  495. lastDismount = value;
  496. }
  497. }
  498. }
  499.  
  500. class RhibBoat
  501. {
  502. private bool buyed;
  503. private uint id;
  504. private DateTime lastDismount;
  505.  
  506. public RhibBoat()
  507. {
  508. LastDismount = DateTime.MinValue;
  509. }
  510.  
  511. public bool Buyed
  512. {
  513. get
  514. {
  515. return buyed;
  516. }
  517.  
  518. set
  519. {
  520. buyed = value;
  521. }
  522. }
  523.  
  524. public uint Id
  525. {
  526. get
  527. {
  528. return id;
  529. }
  530.  
  531. set
  532. {
  533. id = value;
  534. }
  535. }
  536.  
  537. public DateTime LastDismount
  538. {
  539. get
  540. {
  541. return lastDismount;
  542. }
  543.  
  544. set
  545. {
  546. lastDismount = value;
  547. }
  548. }
  549.  
  550. public void UpdateLastDismount(DateTime dateTime)
  551. {
  552. LastDismount = dateTime;
  553. }
  554. }
  555. #endregion
  556. }
  557. }
Advertisement
Add Comment
Please, Sign In to add comment