Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using Oxide.Core;
- using Oxide.Core.Plugins;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- namespace Oxide.Plugins
- {
- [Info("Boat Licence", "Sorrow", "0.0.1")]
- [Description("Allow player to buy boat and spawn or store this")]
- class BoatLicence: RustPlugin
- {
- #region Fields
- [PluginReference]
- Plugin Economics;
- private Dictionary<ulong, LisencedPlayer> _lisencedPlayer = new Dictionary<ulong, LisencedPlayer>();
- private Dictionary<uint, LisencedPlayer> _boatsCache = new Dictionary<uint, LisencedPlayer>();
- private bool _canBuyRowBoat;
- private bool _canBuyRhibBoat;
- private double _rowBoatCost;
- private double _rhibBoatCost;
- private int _intervalToCheckBoat;
- private int _timeBeforeBoatWipe;
- const string prefix = "<color='orange'>[Boat Licence]</color> ";
- const string rowBoatPrefab = "assets/content/vehicles/boats/rowboat/rowboat.prefab";
- const string rhibBoatPrefab = "assets/content/vehicles/boats/rhib/rhib.prefab";
- #endregion
- #region uMod Hooks
- private void Loaded()
- {
- if (Economics == null)
- {
- PrintError("Economics is not loaded, get it at https://umod.org");
- }
- }
- private void OnServerInitialized()
- {
- _canBuyRowBoat = Convert.ToBoolean(Config["Can buy rowing boat"]);
- _canBuyRhibBoat = Convert.ToBoolean(Config["Can buy RHIB"]);
- _rowBoatCost = Convert.ToDouble(Config["Cost of rowing boat"]);
- _rhibBoatCost = Convert.ToDouble(Config["Cost of RHIB"]);
- _intervalToCheckBoat = Convert.ToInt32(Config["Interval in minutes to check boat for wipe"]);
- _timeBeforeBoatWipe = Convert.ToInt32(Config["Time before boat wipe in minutes"]);
- LoadData();
- CheckBoats();
- }
- private void Unload()
- {
- foreach (var boat in _boatsCache.ToList())
- {
- RemoveBoat(boat.Key, boat.Value);
- }
- SaveData();
- }
- private void OnEntityDismounted(BaseMountable entity, BasePlayer player)
- {
- var vehicleParent = entity.VehicleParent();
- LisencedPlayer lisencedPlayer;
- if (!_boatsCache.TryGetValue(vehicleParent.net.ID, out lisencedPlayer)) return;
- lisencedPlayer.UpdateBoatLastDismount(vehicleParent.net.ID);
- }
- private void OnEntityKill(BaseNetworkable entity)
- {
- LisencedPlayer lisencedPlayer;
- if (entity == null || !_boatsCache.TryGetValue(entity.net.ID, out lisencedPlayer)) return;
- lisencedPlayer.ResetBoat(entity.net.ID);
- _boatsCache.Remove(entity.net.ID);
- }
- private void OnServerSave()
- {
- SaveData();
- }
- private void OnServerShutdown()
- {
- SaveData();
- }
- #endregion
- #region Commands
- [ChatCommand("boatlicence")]
- void cmdBoatLicence(BasePlayer player, string command, string[] args)
- {
- SendReply(player, Msg("helpBoatLicence", player.UserIDString));
- }
- [ChatCommand("buyboat")]
- void cmdBuyRowBoat(BasePlayer player, string command, string[] args)
- {
- LisencedPlayer lisencedPlayer;
- if (args.Length == 0) SendReply(player, Msg("helpBuyBoat", player.UserIDString));
- if (args.Length >= 1)
- {
- switch (args[0].ToLower())
- {
- case "row":
- if (!_canBuyRowBoat)
- {
- SendReply(player, Msg("rowBoatCannotBePurchased", player.UserIDString));
- break;
- }
- if (Economics.Call<double>("Balance", player.userID) >= _rowBoatCost)
- {
- if (!_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
- {
- lisencedPlayer = new LisencedPlayer(player.userID);
- lisencedPlayer.rowBoat.Buyed = true;
- _lisencedPlayer.Add(player.userID, lisencedPlayer);
- Economics.Call<bool>("Withdraw", player.userID, _rowBoatCost);
- }
- else if (!lisencedPlayer.rowBoat.Buyed)
- {
- if (Economics.Call<bool>("Withdraw", player.userID, _rowBoatCost)) lisencedPlayer.rowBoat.Buyed = true;
- }
- else
- {
- SendReply(player, Msg("boatAlreadyPurchased", player.UserIDString));
- }
- }
- else
- {
- Puts(Economics.Call<double>("Balance", player.userID).ToString() + " " + _rowBoatCost);
- SendReply(player, Msg("noMoney", player.UserIDString));
- }
- break;
- case "rhib":
- if (!_canBuyRhibBoat)
- {
- SendReply(player, Msg("rhibCannotBePurchased", player.UserIDString));
- break;
- }
- if (Economics.Call<double>("Balance", player.userID) >= _rhibBoatCost)
- {
- if (!_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
- {
- lisencedPlayer = new LisencedPlayer(player.userID);
- lisencedPlayer.rhibBoat.Buyed = true;
- _lisencedPlayer.Add(player.userID, lisencedPlayer);
- }
- else if (!lisencedPlayer.rhibBoat.Buyed)
- {
- if (Economics.Call<bool>("Withdraw", player.userID, _rhibBoatCost)) lisencedPlayer.rhibBoat.Buyed = true;
- }
- else
- {
- SendReply(player, Msg("boatAlreadyPurchased", player.UserIDString));
- }
- }
- else
- {
- SendReply(player, Msg("noMoney", player.UserIDString));
- }
- break;
- default:
- SendReply(player, Msg("helpOptionNotFound", player.UserIDString));
- break;
- }
- }
- }
- [ChatCommand("spawnboat")]
- void cmdSpawnRowBoat(BasePlayer player, string command, string[] args)
- {
- Vector3 position = player.transform.position + (player.transform.forward * 3);
- LisencedPlayer lisencedPlayer;
- if (args.Length == 0) SendReply(player, Msg("helpSpawnBoat", player.UserIDString));
- if (args.Length >= 1)
- {
- switch (args[0].ToLower())
- {
- case "row":
- if (_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
- {
- if (!lisencedPlayer.rowBoat.Buyed)
- {
- SendReply(player, Msg("didntBuyRowBoat", player.UserIDString));
- return;
- }
- else if (lisencedPlayer.rowBoat.Id != 0)
- {
- SendReply(player, Msg("alreadyRowBoatOut", player.UserIDString));
- return;
- }
- var entity = SpawnRowBoat(position, player.transform.rotation);
- lisencedPlayer.rowBoat.Id = entity.net.ID;
- lisencedPlayer.UpdateBoatLastDismount(entity.net.ID);
- _boatsCache.Add(lisencedPlayer.rowBoat.Id, lisencedPlayer);
- }
- else
- {
- Puts("Vous n'avez pas encore acheter de bateau");
- }
- break;
- case "rhib":
- if (_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
- {
- if (!lisencedPlayer.rhibBoat.Buyed)
- {
- SendReply(player, Msg("didntBuyRhib", player.UserIDString));
- return;
- }
- else if (lisencedPlayer.rhibBoat.Id != 0)
- {
- SendReply(player, Msg("alreadyRhibOut", player.UserIDString));
- return;
- }
- var entity = SpawnRhibBoat(position, player.transform.rotation);
- lisencedPlayer.rhibBoat.Id = entity.net.ID;
- lisencedPlayer.UpdateBoatLastDismount(entity.net.ID);
- _boatsCache.Add(lisencedPlayer.rhibBoat.Id, lisencedPlayer);
- }
- else
- {
- Puts("Vous n'avez pas encore acheter de bateau");
- }
- break;
- default:
- SendReply(player, Msg("helpOptionNotFound", player.UserIDString));
- break;
- }
- }
- }
- [ChatCommand("recallboat")]
- void cmdRecallRowBoat(BasePlayer player, string command, string[] args)
- {
- LisencedPlayer lisencedPlayer;
- if (args.Length == 0) SendReply(player, Msg("helpRecallBoat", player.UserIDString));
- if (args.Length >= 1)
- {
- switch (args[0].ToLower())
- {
- case "row":
- if (_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
- {
- RemoveBoat(lisencedPlayer.rhibBoat.Id, lisencedPlayer);
- }
- break;
- case "rhib":
- if (_lisencedPlayer.TryGetValue(player.userID, out lisencedPlayer))
- {
- RemoveBoat(lisencedPlayer.rhibBoat.Id, lisencedPlayer);
- }
- break;
- default:
- SendReply(player, Msg("helpOptionNotFound", player.UserIDString));
- break;
- }
- }
- }
- #endregion
- #region Functions
- private BaseEntity SpawnRowBoat(Vector3 position, Quaternion rotation = default(Quaternion))
- {
- BaseEntity entity = GameManager.server.CreateEntity(rowBoatPrefab, position + Vector3.up, rotation);
- entity.enableSaving = true;
- entity.Spawn();
- return entity;
- }
- private BaseEntity SpawnRhibBoat(Vector3 position, Quaternion rotation = default(Quaternion))
- {
- BaseEntity entity = GameManager.server.CreateEntity(rhibBoatPrefab, position + Vector3.up, rotation);
- entity.enableSaving = true;
- entity.Spawn();
- return entity;
- }
- private void RemoveBoat(uint boatID, LisencedPlayer lisencedPlayer)
- {
- BaseNetworkable.serverEntities.Find(boatID)?.Kill();
- lisencedPlayer.ResetBoat(boatID);
- _boatsCache?.Remove(boatID);
- }
- private void UpdateLisencedPlayer(LisencedPlayer lisencedPlayer, uint boatID)
- {
- if (lisencedPlayer.rowBoat.Id == boatID) lisencedPlayer.rowBoat.Id = 0;
- if (lisencedPlayer.rhibBoat.Id == boatID) lisencedPlayer.rhibBoat.Id = 0;
- }
- private void CheckBoats()
- {
- foreach (var boat in _boatsCache.ToList())
- {
- LisencedPlayer lisencedPlayer = boat.Value;
- var boatNetworkable = BaseNetworkable.serverEntities.Find(boat.Key);
- var boatEntity = boatNetworkable.GetComponent<BaseBoat>();
- if (boatEntity == null) continue;
- if (boatEntity.IsMounted()) continue;
- if (lisencedPlayer.rowBoat.Id == boat.Key)
- {
- if (BoatIsActive(lisencedPlayer.rowBoat.LastDismount)) continue;
- RemoveBoat(boat.Key, lisencedPlayer);
- }
- else if (lisencedPlayer.rhibBoat.Id == boat.Key)
- {
- if (BoatIsActive(lisencedPlayer.rhibBoat.LastDismount)) continue;
- RemoveBoat(boat.Key, lisencedPlayer);
- }
- }
- timer.Once(_intervalToCheckBoat * 60f, () => CheckBoats());
- }
- private bool BoatIsActive(DateTime lastDismount)
- {
- return lastDismount.Ticks >= DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(_timeBeforeBoatWipe)).Ticks;
- }
- #endregion
- #region Localization
- private string Msg(string key, string playerId = null) => prefix + lang.GetMessage(key, this, playerId);
- protected override void LoadDefaultMessages()
- {
- lang.RegisterMessages(new Dictionary<string, string>
- {
- ["helpBoatLicence"] = "These are the available commands: \n/buyboat -- To buy a boat \n/spawnboat -- To spawn a boat \n/recallboat -- To recall a boat",
- ["helpBuyBoat"] = "These are the available commands: \n/buyboat row -- To buy a rowing boat \n/buyboat rhib -- To buy a RHIB.",
- ["helpSpawnBoat"] = "These are the available commands: \n/spawnboat row -- To spawn a rowing boat \n/spawnboat rhib -- To spawn a RHIB.",
- ["helpRecallBoat"] = "These are the available commands: \n/recallboat row -- To recall a rowing boat \n/recallboat rhib -- To recall a RHIB.",
- ["helpOptionNotFound"] = "This option doesn't exist.",
- ["boatAlreadyPurchased"] = "You have already purchased this boat.",
- ["rowBoatCannotBeBuyed"] = "You can't buy a rowing boat.",
- ["rhibCannotBeBuyed"] = "You can't buy a RHIB.",
- ["noMoney"] = "You don't have enough money.",
- ["didntBuyRowBoat"] = "You didn't purchase a rowing boat.",
- ["didntBuyRhib"] = "You didn't purchase a RHIB.",
- ["alreadyRowBoatOut"] = "You already have a rowing boat outside.",
- ["alreadyRhibOut"] = "You already have a RHIB outside.",
- ["boatNotYetPurchased"] = "You have not yet purchased a boat.",
- }, this);
- lang.RegisterMessages(new Dictionary<string, string>
- {
- ["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",
- ["helpBuyBoat"] = "Voici les commandes disponibles : \n/buyboat row -- Pour acheter un bateau à rames \n/buyboat rhib -- Pour acheter un RHIB.",
- ["helpSpawnBoat"] = "Voici les commandes disponibles : \n/spawnboat row -- Pour faire apparaître un bateau à rames \n/spawnboat rhib -- Pour faire apparaître un RHIB.",
- ["helpRecallBoat"] = "Voici les commandes disponibles : \n/recallboat row -- Pour ranger un bateau à rames \n/recallboat rhib -- Pour ranger un RHIB.",
- ["helpOptionNotFound"] = "Cette option n'existe pas.",
- ["boatAlreadyPurchased"] = "Vous avez déjà acheté ce bateau.",
- ["rowBoatCannotBeBuyed"] = "On ne peut pas acheter un bateau à rames.",
- ["rhibCannotBeBuyed"] = "Vous ne pouvez pas acheter un RHIB.",
- ["noMoney"] = "Vous n'avez pas assez d'argent.",
- ["didntBuyRowBoat"] = "Vous n'avez pas acheté de bateau à rames.",
- ["didntBuyRhib"] = "Vous n'avez pas acheté de RHIB.",
- ["alreadyRowBoatOut"] = "Vous avez déjà un bateau à rames à l'extérieur.",
- ["alreadyRhibOut"] = "Vous avez déjà un RHIB à l'extérieur.",
- ["boatNotYetPurchased"] = "Vous n'avez pas encore acheté de bateau.",
- }, this, "fr");
- }
- #endregion
- #region Config
- private new void LoadDefaultConfig()
- {
- PrintWarning("Creating a new configuration file");
- Config.Clear();
- Config["Can buy rowing boat"] = true;
- Config["Can buy RHIB"] = true;
- Config["Cost of rowing boat"] = 1000;
- Config["Cost of RHIB"] = 5000;
- Config["Interval in minutes to check boat for wipe"] = 5;
- Config["Time before boat wipe in minutes"] = 15;
- SaveConfig();
- }
- private void LoadData()
- {
- _lisencedPlayer = Interface.Oxide.DataFileSystem.ReadObject<Dictionary<ulong, LisencedPlayer>>("BoatLicence");
- }
- private void SaveData() => Interface.Oxide.DataFileSystem.WriteObject("BoatLicence", _lisencedPlayer);
- #endregion
- #region Class
- class LisencedPlayer
- {
- private readonly ulong userid;
- public RowBoat rowBoat { get; }
- public RhibBoat rhibBoat { get; }
- public LisencedPlayer(ulong userid)
- {
- this.userid = userid;
- rowBoat = new RowBoat();
- rhibBoat = new RhibBoat();
- }
- [JsonIgnore]
- public ulong Userid
- {
- get
- {
- return userid;
- }
- }
- public void ResetBoat(uint boatID)
- {
- if (rowBoat.Id == boatID)
- {
- rowBoat.Id = 0;
- rowBoat.LastDismount = DateTime.MinValue;
- }
- else if (rhibBoat.Id == boatID)
- {
- rhibBoat.Id = 0;
- rhibBoat.LastDismount = DateTime.MinValue;
- }
- }
- public void UpdateBoatLastDismount(uint boatID)
- {
- if (rowBoat.Id == boatID) rowBoat.LastDismount = DateTime.UtcNow;
- if (rhibBoat.Id == boatID) rhibBoat.LastDismount = DateTime.UtcNow;
- }
- }
- class RowBoat
- {
- private bool buyed;
- private uint id;
- private DateTime lastDismount;
- public RowBoat()
- {
- LastDismount = DateTime.MinValue;
- }
- public bool Buyed
- {
- get
- {
- return buyed;
- }
- set
- {
- buyed = value;
- }
- }
- public uint Id
- {
- get
- {
- return id;
- }
- set
- {
- id = value;
- }
- }
- public DateTime LastDismount
- {
- get
- {
- return lastDismount;
- }
- set
- {
- lastDismount = value;
- }
- }
- }
- class RhibBoat
- {
- private bool buyed;
- private uint id;
- private DateTime lastDismount;
- public RhibBoat()
- {
- LastDismount = DateTime.MinValue;
- }
- public bool Buyed
- {
- get
- {
- return buyed;
- }
- set
- {
- buyed = value;
- }
- }
- public uint Id
- {
- get
- {
- return id;
- }
- set
- {
- id = value;
- }
- }
- public DateTime LastDismount
- {
- get
- {
- return lastDismount;
- }
- set
- {
- lastDismount = value;
- }
- }
- public void UpdateLastDismount(DateTime dateTime)
- {
- LastDismount = dateTime;
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment