Code:using System.Collections.Generic; using System; using Oxide.Core; using Oxide.Core.Plugins; using Oxide.Core.Configuration; using Oxide.Game.Rust.Cui; using Newtonsoft.Json; using UnityEngine; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using Oxide.Core.Libraries.Covalence; using static UnityEngine.Vector3; using System.Reflection;namespace Oxide.Plugins { [Info("Npctp", "Ts3hosting", "2.2.6", ResourceId = 2229)] [Description("Some NPC Controle")] class Npctp : RustPlugin { #region Initialization [PluginReference] Plugin Spawns; [PluginReference] Plugin Economics; PlayerCooldown pcdData; NPCTPDATA npcData; private DynamicConfigFile PCDDATA; private DynamicConfigFile NPCDATA; private static int cooldownTime = 3600; private static int auth = 2; private static bool noAdminCooldown = false; private bool Changed; private string text; private bool displayoneveryconnect; private string Cost = "0"; private static bool useEconomics = false; private static bool useRewards = false; #region Localization Dictionary<string, string> messages = new Dictionary<string, string>() { {"title", "<color=orange>Npc</color> : "}, {"cdTime", "You must wait another {0} minutes and some seconds before using me again" }, {"noperm", "You do not have permissions to talk to me!" }, {"notenabled", "Sorry i am not enabled!" }, {"nomoney", "Sorry you need {0} to talk to me!" }, {"charged", "Thanks i only took {0} from you!" }, {"npcCommand", "I just ran a Command!" } }; #endregion void Loaded() { PCDDATA = Interface.Oxide.DataFileSystem.GetFile("NpcTp/NpcTP_Player"); NPCDATA = Interface.Oxide.DataFileSystem.GetFile("NpcTp/NpcTP_Data"); LoadData(); LoadVariables(); RegisterPermissions(); lang.RegisterMessages(messages, this); Puts("Thanks for using NPCTP drop me a line if you need anything added."); } object GetConfig(string menu, string datavalue, object defaultValue) { var data = Config[menu] as Dictionary<string, object>; if (data == null) { data = new Dictionary<string, object>(); Config[menu] = data; Changed = true; } object value; if (!data.TryGetValue(datavalue, out value)) { value = defaultValue; data[datavalue] = value; Changed = true; } return value; } private void RegisterPermissions() { permission.RegisterPermission("npctp.admin", this); permission.RegisterPermission("npctp.default", this); } private void CheckDependencies() { if (Economics == null) if (useEconomics) { PrintWarning($"Economics could not be found! Disabling money feature"); useEconomics = false; } if (Spawns == null) { PrintWarning($"Spawns Database could not be found!"); } } void LoadVariables() { useEconomics = Convert.ToBoolean(GetConfig("SETTINGS", "useEconomics", false)); useRewards = Convert.ToBoolean(GetConfig("SETTINGS", "useRewards", false)); if (Changed) { SaveConfig(); Changed = false; } } protected override void LoadDefaultConfig() { Puts("Creating a new configuration file!"); Config.Clear(); LoadVariables(); } #endregion #region Classes and Data Management void SaveNpcTpData() { NPCDATA.WriteObject(npcData); } class NPCTPDATA { public Dictionary<string, NPCInfo> NpcTP = new Dictionary<string, NPCInfo>(); public NPCTPDATA() { } } class NPCInfo { public string SpawnFile; public string Cooldown; public string CanUse; public string Cost; public string permission; public string UseCommand; public string CommandOnPlayer; public string Command; public string Arrangements; public NPCInfo() { } public NPCInfo(string cd1) { SpawnFile = cd1; Cooldown = cd1; CanUse = cd1; Cost = cd1; permission = cd1; UseCommand = cd1; CommandOnPlayer = cd1; Command = cd1; Arrangements = cd1; } } class PlayerCooldown { public Dictionary<ulong, PCDInfo> pCooldown = new Dictionary<ulong, PCDInfo>(); public PlayerCooldown() { } } class PCDInfo { public long Cooldown; public PCDInfo() { } public PCDInfo(long cd) { Cooldown = cd; } } void SaveData() { PCDDATA.WriteObject(pcdData); } void LoadData() { try { pcdData = Interface.GetMod().DataFileSystem.ReadObject<PlayerCooldown>("NpcTp/NpcTP_Player"); } catch { Puts("Couldn't load NPCTP data, creating new Playerfile"); pcdData = new PlayerCooldown(); } try { npcData = Interface.GetMod().DataFileSystem.ReadObject<NPCTPDATA>("NpcTp/NpcTP_Data"); } catch { Puts("Couldn't load NPCTP data, creating new datafile"); npcData = new NPCTPDATA(); } } #endregion #region Cooldown Management static double GrabCurrentTime() => DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds; #endregion private void TeleportPlayerPosition1(BasePlayer player, Vector3 destination) { if (player.net?.connection != null) player.ClientRPCPlayer(null, player, "StartLoading", null, null, null, null, null); StartSleeping(player); player.MovePosition(destination); if (player.net?.connection != null) player.ClientRPCPlayer(null, player, "ForcePositionTo", destination); if (player.net?.connection != null) player.SetPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot, true); player.UpdateNetworkGroup(); player.SendNetworkUpdateImmediate(false); if (player.net?.connection == null) return; try { player.ClearEntityQueue(null); } catch { } player.SendFullSnapshot(); } private void StartSleeping(BasePlayer player) { if (player.IsSleeping()) return; player.SetPlayerFlag(BasePlayer.PlayerFlags.Sleeping, true); if (!BasePlayer.sleepingPlayerList.Contains(player)) BasePlayer.sleepingPlayerList.Add(player); player.CancelInvoke("InventoryUpdate"); } private bool CheckPlayerMoney(BasePlayer player, int amount) { if (useEconomics) { double money = (double)Economics?.CallHook("GetPlayerMoney", player.userID); if (money >= amount) { money = money - amount; Economics?.CallHook("Set", player.userID, money); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("charged", this, player.UserIDString), (int)(amount))); return true; } SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("nomoney", this, player.UserIDString), (int)(amount))); } return false; } #region USENPC [ChatCommand("npctp_add")] void cmdNpcAdD(BasePlayer player, string command, string[] args) { var n = npcData.NpcTP; string IDNPC = ""; double timeStamp = GrabCurrentTime(); IDNPC = (args[0]); if (args.Length >= 1) if (!n.ContainsKey(IDNPC)) n.Add(IDNPC, new NPCInfo((string)Cost)); n[IDNPC].CanUse = "false"; n[IDNPC].SpawnFile = "none"; n[IDNPC].permission = "npctp.default"; n[IDNPC].UseCommand = "false"; n[IDNPC].CommandOnPlayer = "false"; n[IDNPC].Command = "say"; n[IDNPC].Arrangements = "this is a test"; SaveNpcTpData(); if (args.Length >= 2) if (!n.ContainsKey(IDNPC)) n.Add(IDNPC, new NPCInfo((string)Cost)); n[IDNPC].CanUse = "true"; n[IDNPC].SpawnFile = (args[1]); n[IDNPC].permission = "npctp.default"; n[IDNPC].UseCommand = "false"; n[IDNPC].CommandOnPlayer = "false"; n[IDNPC].Command = "say"; n[IDNPC].Arrangements = "this is a test"; SaveNpcTpData(); } void OnUseNPC(BasePlayer npc, BasePlayer player, Vector3 destination) { ulong playerID = player.userID; string lowername = npc.userID.ToString(); var d = pcdData.pCooldown; var np = npcData.NpcTP; var NPCUSER = npc.userID; ulong ID = player.userID; double timeStamp = GrabCurrentTime(); string CanUse = np[lowername].CanUse; var cooldownTime = Convert.ToInt32(np[lowername].Cooldown); var buyMoney1 = Convert.ToInt32(np[lowername].Cost); var UseCommand = Convert.ToBoolean(np[lowername].UseCommand); var CommandOnPlayer = Convert.ToBoolean(np[lowername].CommandOnPlayer); string Command = np[lowername].Command; string Arrangements = np[lowername].Arrangements; object newpos = null; var bad = "somthing is not right in command on player"; string spawn = np[lowername].SpawnFile; if (!d.ContainsKey(ID)) { d.Add(ID, new PCDInfo((long)timeStamp)); SaveData(); } if (np.ContainsKey(lowername)) { if (CanUse == "false") { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("notenabled", this))); return; } else { long time = d[ID].Cooldown; if (!permission.UserHasPermission(player.userID.ToString(), "npctp.default")) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("noperm", this))); return; } if (time > timeStamp && time != 0.0) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("cdTime", this, player.UserIDString), (int)(time - timeStamp) / 60)); return; } else if (!useEconomics && UseCommand == false) { d[ID].Cooldown = (long)timeStamp + cooldownTime; SaveData(); object success = Spawns.Call("GetRandomSpawn", spawn); if (success is Vector3) // Check if the returned type is Vector3 { Vector3 location = (Vector3)success; TeleportPlayerPosition1(player, (Vector3)success); } else PrintError((string)newpos); // Otherwise print the error message to console so server owners know there is a problem } if (useEconomics && UseCommand == false) if (CheckPlayerMoney(player, buyMoney1)) { d[ID].Cooldown = (long)timeStamp + cooldownTime; SaveData(); object success = Spawns.Call("GetRandomSpawn", spawn); if (success is Vector3) // Check if the returned type is Vector3 { Vector3 location = (Vector3)success; TeleportPlayerPosition1(player, (Vector3)success); } else PrintError((string)newpos); // Otherwise print the error message to console so server owners know there is a problem } if (!useEconomics && UseCommand == true && CommandOnPlayer == true) { d[ID].Cooldown = (long)timeStamp + cooldownTime; SaveData(); if (CommandOnPlayer) // Check if this is command on player { rust.RunServerCommand($"{Command} {player.userID} {Arrangements}"); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcCommand", this))); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } if (useEconomics && UseCommand == true && CommandOnPlayer == false) if (CheckPlayerMoney(player, buyMoney1)) { d[ID].Cooldown = (long)timeStamp + cooldownTime; SaveData(); if (!CommandOnPlayer) // Check if this is command on player { rust.RunServerCommand($"{Command} {Arrangements}"); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcCommand", this))); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } } } #endregion } } }
Trying to make datafile save in this format for player cooldowns. im lost and could use some help thank you.
Code:{ "pCooldown": { "USERSSTEAMID": { "NPCID": TimeStamp } } }
Help with datafile cooldowns
Discussion in 'Rust Development' started by Ts3hosting, Jan 28, 2017.
-
It wont look exactly like that but basically you need the datafile to contain a dictionary using the npcId as key, with the value as the cooldown. Then you will need to check the players data, and also the cooldown for the specified npc
Code:class PCDInfo { public Dictionary<string, long> npcCooldowns = new Dictionary<string, long>(); }
Code:class NPCInfo { public string SpawnFile; public int Cooldown; public bool CanUse; public float Cost; public string permission; public bool UseCommand; public bool CommandOnPlayer; public string Command; public string Arrangements; }
-
Code:
using System.Collections.Generic; using System; using Oxide.Core; using Oxide.Core.Plugins; using Oxide.Core.Configuration; using UnityEngine; using Newtonsoft.Json.Linq; using Oxide.Core.Libraries.Covalence; using static UnityEngine.Vector3;namespace Oxide.Plugins { [Info("Npctp", "Ts3hosting", "2.2.6", ResourceId = 2229)] [Description("Some NPC Controle")] class Npctp : RustPlugin { #region Initialization [PluginReference] Plugin Spawns; [PluginReference] Plugin Economics; PlayerCooldown pcdData; NPCTPDATA npcData; private DynamicConfigFile PCDDATA; private DynamicConfigFile NPCDATA; private static int cooldownTime = 3600; private static int auth = 2; private static bool noAdminCooldown = false; private bool Changed; private string text; private bool displayoneveryconnect; private float Cost = 0; private static bool useEconomics = false; private static bool useRewards = false; #region Localization Dictionary<string, string> messages = new Dictionary<string, string>() { {"title", "<color=orange>Npc</color> : "}, {"cdTime", "You must wait another {0} minutes and some seconds before using me again" }, {"noperm", "You do not have permissions to talk to me!" }, {"notenabled", "Sorry i am not enabled!" }, {"nomoney", "Sorry you need {0} to talk to me!" }, {"charged", "Thanks i only took {0} from you!" }, {"npcCommand", "I just ran a Command!" } }; #endregion void Loaded() { PCDDATA = Interface.Oxide.DataFileSystem.GetFile("NpcTp/NpcTP_Player"); NPCDATA = Interface.Oxide.DataFileSystem.GetFile("NpcTp/NpcTP_Data"); LoadData(); LoadVariables(); RegisterPermissions(); lang.RegisterMessages(messages, this); Puts("Thanks for using NPCTP drop me a line if you need anything added."); } object GetConfig(string menu, string datavalue, object defaultValue) { var data = Config[menu] as Dictionary<string, object>; if (data == null) { data = new Dictionary<string, object>(); Config[menu] = data; Changed = true; } object value; if (!data.TryGetValue(datavalue, out value)) { value = defaultValue; data[datavalue] = value; Changed = true; } return value; } private void RegisterPermissions() { permission.RegisterPermission("npctp.admin", this); permission.RegisterPermission("npctp.default", this); } private void CheckDependencies() { if (Economics == null) if (useEconomics) { PrintWarning($"Economics could not be found! Disabling money feature"); useEconomics = false; } if (Spawns == null) { PrintWarning($"Spawns Database could not be found!"); } } void LoadVariables() { useEconomics = Convert.ToBoolean(GetConfig("SETTINGS", "useEconomics", false)); useRewards = Convert.ToBoolean(GetConfig("SETTINGS", "useRewards", false)); if (Changed) { SaveConfig(); Changed = false; } } protected override void LoadDefaultConfig() { Puts("Creating a new configuration file!"); Config.Clear(); LoadVariables(); } #endregion #region Classes and Data Management void SaveNpcTpData() { NPCDATA.WriteObject(npcData); } class NPCTPDATA { public Dictionary<string, NPCInfo> NpcTP = new Dictionary<string, NPCInfo>(); public NPCTPDATA() { } } class NPCInfo { public string SpawnFile; public int Cooldown; public bool CanUse; public float Cost; public string permission; public bool UseCommand; public bool CommandOnPlayer; public string Command; public string Arrangements; } class PlayerCooldown { public Dictionary<ulong, PCDInfo> pCooldown = new Dictionary<ulong, PCDInfo>(); public PlayerCooldown() { } } class PCDInfo { public long Cooldown; public PCDInfo() { } public PCDInfo(long cd) { Cooldown = cd; } } void SaveData() { PCDDATA.WriteObject(pcdData); } void LoadData() { try { pcdData = Interface.GetMod().DataFileSystem.ReadObject<PlayerCooldown>("NpcTp/NpcTP_Player"); } catch { Puts("Couldn't load NPCTP data, creating new Playerfile"); pcdData = new PlayerCooldown(); } try { npcData = Interface.GetMod().DataFileSystem.ReadObject<NPCTPDATA>("NpcTp/NpcTP_Data"); } catch { Puts("Couldn't load NPCTP data, creating new datafile"); npcData = new NPCTPDATA(); } } #endregion #region Cooldown Management static double GrabCurrentTime() => DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds; #endregion private void TeleportPlayerPosition1(BasePlayer player, Vector3 destination) { if (player.net?.connection != null) player.ClientRPCPlayer(null, player, "StartLoading", null, null, null, null, null); StartSleeping(player); player.MovePosition(destination); if (player.net?.connection != null) player.ClientRPCPlayer(null, player, "ForcePositionTo", destination); if (player.net?.connection != null) player.SetPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot, true); player.UpdateNetworkGroup(); player.SendNetworkUpdateImmediate(false); if (player.net?.connection == null) return; try { player.ClearEntityQueue(null); } catch { } player.SendFullSnapshot(); } private void StartSleeping(BasePlayer player) { if (player.IsSleeping()) return; player.SetPlayerFlag(BasePlayer.PlayerFlags.Sleeping, true); if (!BasePlayer.sleepingPlayerList.Contains(player)) BasePlayer.sleepingPlayerList.Add(player); player.CancelInvoke("InventoryUpdate"); } private bool CheckPlayerMoney(BasePlayer player, int amount) { if (useEconomics) { double money = (double)Economics?.CallHook("GetPlayerMoney", player.userID); if (money >= amount) { money = money - amount; Economics?.CallHook("Set", player.userID, money); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("charged", this, player.UserIDString), (int)(amount))); return true; } SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("nomoney", this, player.UserIDString), (int)(amount))); } return false; } #region USENPC [ChatCommand("npctp_add")] void cmdNpcAdD(BasePlayer player, string command, string[] args) { var n = npcData.NpcTP; string IDNPC = ""; string SpawnFiles = ""; if (args.Length == 2) { SpawnFiles = (args[1]); } var setup = new NPCInfo { Cost = Cost, CanUse = false, permission = "npctp.default", UseCommand = false, CommandOnPlayer = false, Command = "say", Arrangements = "this is a test" }; var setups = new NPCInfo { Cost = Cost, CanUse = true, SpawnFile = SpawnFiles, permission = "npctp.default", UseCommand = false, CommandOnPlayer = false, Command = "say", Arrangements = "this is a test" }; IDNPC = (args[0]); if (args.Length == 1 && !n.ContainsKey(IDNPC)) n.Add(IDNPC, setup); SaveNpcTpData(); if (args.Length == 2 && !n.ContainsKey(IDNPC)) n.Add(IDNPC, setups); SaveNpcTpData(); } void OnUseNPC(BasePlayer npc, BasePlayer player, Vector3 destination) { ulong playerID = player.userID; string lowername = npc.userID.ToString(); var d = pcdData.pCooldown; var np = npcData.NpcTP; var NPCUSER = npc.userID; ulong ID = player.userID; double timeStamp = GrabCurrentTime(); var CanUse = np[lowername].CanUse; var cooldownTime = np[lowername].Cooldown; var buyMoney1 = Convert.ToInt32(np[lowername].Cost); var UseCommand = np[lowername].UseCommand; var CommandOnPlayer = np[lowername].CommandOnPlayer; string Command = np[lowername].Command; string Arrangements = np[lowername].Arrangements; object newpos = null; var bad = "somthing is not right in command on player"; string spawn = np[lowername].SpawnFile; if (!d.ContainsKey(ID)) { d.Add(ID, new PCDInfo((long)NPCUSER)); SaveData(); } if (np.ContainsKey(lowername)) { if (!CanUse) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("notenabled", this))); return; } else { long time = d[ID].Cooldown; if (!permission.UserHasPermission(player.userID.ToString(), "npctp.default")) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("noperm", this))); return; } if (time > timeStamp && time != 0.0) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("cdTime", this, player.UserIDString), (int)(time - timeStamp) / 60)); return; } else if (!useEconomics && UseCommand == false) { d[ID].Cooldown = (long)timeStamp + cooldownTime; SaveData(); object success = Spawns.Call("GetRandomSpawn", spawn); if (success is Vector3) // Check if the returned type is Vector3 { Vector3 location = (Vector3)success; TeleportPlayerPosition1(player, (Vector3)success); } else PrintError((string)newpos); // Otherwise print the error message to console so server owners know there is a problem } if (useEconomics && UseCommand == false) if (CheckPlayerMoney(player, buyMoney1)) { d[ID].Cooldown = (long)timeStamp + cooldownTime; SaveData(); object success = Spawns.Call("GetRandomSpawn", spawn); if (success is Vector3) // Check if the returned type is Vector3 { Vector3 location = (Vector3)success; TeleportPlayerPosition1(player, (Vector3)success); } else PrintError((string)newpos); // Otherwise print the error message to console so server owners know there is a problem } if (!useEconomics && UseCommand == true && CommandOnPlayer == true) { d[ID].Cooldown = (long)timeStamp + cooldownTime; SaveData(); if (CommandOnPlayer) // Check if this is command on player { rust.RunServerCommand($"{Command} {player.userID} {Arrangements}"); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcCommand", this))); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } if (useEconomics && UseCommand == true && CommandOnPlayer == false) if (CheckPlayerMoney(player, buyMoney1)) { d[ID].Cooldown = (long)timeStamp + cooldownTime; SaveData(); if (!CommandOnPlayer) // Check if this is command on player { rust.RunServerCommand($"{Command} {Arrangements}"); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcCommand", this))); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } } } #endregion } } }
still dont understand the go about of the cooldown i see what your saying save instead of pCooldown would be the npcid just unsure about the route to do that. i did fix the other Suggestions. -
well at the moment you have it saving 1 cooldown per player, and you want 1 cooldown per npc, per player. so do pretty much exactly what you are doing now, but 1 layer down if you will. When a player tp's you want to check and assign the cooldown to that npc's ID in that players data.
This could be cleaned up a bit but this is essentially what you want
Code:class PCDInfo { public Dictionary<string, long> npcCooldowns = new Dictionary<string, long>(); } void OnUseNPC(BasePlayer npc, BasePlayer player, Vector3 destination) { if (!npcData.NpcTP.ContainsKey(npc.UserIDString)) return; // Check if this NPC is registered ulong playerId = player.userID; string npcId = npc.UserIDString; double timeStamp = GrabCurrentTime(); var CanUse = npcData.NpcTP[npcId].CanUse; var cooldownTime = npcData.NpcTP[npcId].Cooldown; var buyMoney1 = npcData.NpcTP[npcId].Cost; var UseCommand = npcData.NpcTP[npcId].UseCommand; var CommandOnPlayer = npcData.NpcTP[npcId].CommandOnPlayer; string Command = npcData.NpcTP[npcId].Command; string Arrangements = npcData.NpcTP[npcId].Arrangements; object newpos = null; var bad = "somthing is not right in command on player"; string spawn = npcData.NpcTP[npcId].SpawnFile; if (!pcdData.pCooldown.ContainsKey(playerId)) { pcdData.pCooldown.Add(playerId, new PCDInfo()); //SaveData(); } if (!CanUse) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("notenabled", this))); return; } else { if (!permission.UserHasPermission(player.userID.ToString(), "npctp.default")) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("noperm", this))); return; } if (pcdData.pCooldown[playerId].npcCooldowns.ContainsKey(npcId)) // Check if the player already has a cooldown for this NPC { var cdTime = pcdData.pCooldown[playerId].npcCooldowns[npcId]; // Get the cooldown time of the NPC if (cdTime > timeStamp) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("cdTime", this, player.UserIDString), (int)(cdTime - timeStamp) / 60)); return; } } else if (!useEconomics && UseCommand == false) { pcdData.pCooldown[playerId].npcCooldowns[npcId] = (long)timeStamp + cooldownTime; // Store the new cooldown in the players data under the specified NPC SaveData(); object success = Spawns.Call("GetRandomSpawn", spawn); if (success is Vector3) // Check if the returned type is Vector3 { Vector3 location = (Vector3)success; TeleportPlayerPosition1(player, (Vector3)success); } else PrintError((string)newpos); // Otherwise print the error message to console so server owners know there is a problem } if (useEconomics && UseCommand == false) if (CheckPlayerMoney(player, (int)buyMoney1)) { pcdData.pCooldown[playerId].npcCooldowns[npcId] = (long)timeStamp + cooldownTime; SaveData(); object success = Spawns.Call("GetRandomSpawn", spawn); if (success is Vector3) // Check if the returned type is Vector3 { Vector3 location = (Vector3)success; TeleportPlayerPosition1(player, (Vector3)success); } else PrintError((string)newpos); // Otherwise print the error message to console so server owners know there is a problem } if (!useEconomics && UseCommand == true && CommandOnPlayer == true) { pcdData.pCooldown[playerId].npcCooldowns[npcId] = (long)timeStamp + cooldownTime; SaveData(); if (CommandOnPlayer) // Check if this is command on player { rust.RunServerCommand($"{Command} {player.userID} {Arrangements}"); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcCommand", this))); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } if (useEconomics && UseCommand == true && CommandOnPlayer == false) if (CheckPlayerMoney(player, (int)buyMoney1)) { pcdData.pCooldown[playerId].npcCooldowns[npcId] = (long)timeStamp + cooldownTime; SaveData(); if (!CommandOnPlayer) // Check if this is command on player { rust.RunServerCommand($"{Command} {Arrangements}"); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcCommand", this))); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } } #endregion }
-
you are a life saver.... thank you.
-
Code:
using System.Collections.Generic; using System; using Oxide.Core; using Oxide.Core.Plugins; using Oxide.Core.Configuration; using UnityEngine; using Newtonsoft.Json.Linq; using Oxide.Core.Libraries.Covalence; using static UnityEngine.Vector3;namespace Oxide.Plugins { [Info("Npctp", "Ts3hosting", "2.2.7", ResourceId = 2229)] [Description("Some NPC Controle Thanks Wulf and k1lly0u")] class Npctp : RustPlugin { #region Initialization [PluginReference] Plugin Spawns; [PluginReference] Plugin Economics; [PluginReference] Plugin ServerRewards; PlayerCooldown pcdData; NPCTPDATA npcData; private DynamicConfigFile PCDDATA; private DynamicConfigFile NPCDATA; private static int cooldownTime = 3600; private static int auth = 2; private static bool noAdminCooldown = false; private bool Changed; private string text; private bool displayoneveryconnect; private float Cost = 0; private static bool useEconomics = false; private static bool useRewards = false; #region Localization Dictionary<string, string> messages = new Dictionary<string, string>() { {"title", "<color=orange>Npc</color> : "}, {"cdTime", "You must wait another {0} minutes and some seconds before using me again" }, {"noperm", "You do not have permissions to talk to me!" }, {"notenabled", "Sorry i am not enabled!" }, {"nomoney", "Sorry you need {0} to talk to me!" }, {"charged", "Thanks i only took {0} from you!" }, {"npcCommand", "I just ran a Command!" }, {"npcadd", "Added npcID {0} to datafile and not enabled edit NpcTP_Data ." }, {"npcadds", "Added npcID {0} with spawnfile {1} to datafile and enabled edit NpcTP_Data for more options." }, {"npcerror", "error example /npctp_add <npcID> <spawnfile> or /npctcp_add <npcID>" }, {"nopermcmd", "You do not have permissions to use this command" } }; #endregion void Loaded() { PCDDATA = Interface.Oxide.DataFileSystem.GetFile("NpcTp/NpcTP_Player"); NPCDATA = Interface.Oxide.DataFileSystem.GetFile("NpcTp/NpcTP_Data"); LoadData(); LoadVariables(); RegisterPermissions(); CheckDependencies(); lang.RegisterMessages(messages, this); Puts("Thanks for using NPCTP drop me a line if you need anything added."); } object GetConfig(string menu, string datavalue, object defaultValue) { var data = Config[menu] as Dictionary<string, object>; if (data == null) { data = new Dictionary<string, object>(); Config[menu] = data; Changed = true; } object value; if (!data.TryGetValue(datavalue, out value)) { value = defaultValue; data[datavalue] = value; Changed = true; } return value; } private void RegisterPermissions() { permission.RegisterPermission("npctp.admin", this); permission.RegisterPermission("npctp.default", this); foreach (var perm in npcData.NpcTP.Values) { if (!string.IsNullOrEmpty(perm.permission) && !permission.PermissionExists(perm.permission)) permission.RegisterPermission(perm.permission, this); } } private void CheckDependencies() { if (Economics == null) if (useEconomics) { PrintWarning($"Economics could not be found! Disabling money feature"); useEconomics = false; } if (ServerRewards == null) if (useRewards) { PrintWarning($"ServerRewards could not be found! Disabling RP feature"); useRewards = false; } if (Spawns == null) { PrintWarning($"Spawns Database could not be found you only can use command NPC all other will fail!"); } } void LoadVariables() { useEconomics = Convert.ToBoolean(GetConfig("SETTINGS", "useEconomics", false)); useRewards = Convert.ToBoolean(GetConfig("SETTINGS", "useRewards", false)); if (Changed) { SaveConfig(); Changed = false; } } protected override void LoadDefaultConfig() { Puts("Creating a new configuration file!"); Config.Clear(); LoadVariables(); } #endregion #region Classes and Data Management void SaveNpcTpData() { NPCDATA.WriteObject(npcData); } class NPCTPDATA { public Dictionary<string, NPCInfo> NpcTP = new Dictionary<string, NPCInfo>(); public NPCTPDATA() { } } class NPCInfo { public string SpawnFile; public int Cooldown; public bool CanUse; public float Cost; public string permission; public bool UseCommand; public bool CommandOnPlayer; public string Command; public string Arrangements; } class PlayerCooldown { public Dictionary<ulong, PCDInfo> pCooldown = new Dictionary<ulong, PCDInfo>(); public PlayerCooldown() { } } class PCDInfo { public Dictionary<string, long> npcCooldowns = new Dictionary<string, long>(); public PCDInfo() { } public PCDInfo(long cd) { } } void SaveData() { PCDDATA.WriteObject(pcdData); } void LoadData() { try { pcdData = Interface.GetMod().DataFileSystem.ReadObject<PlayerCooldown>("NpcTp/NpcTP_Player"); } catch { Puts("Couldn't load NPCTP data, creating new Playerfile"); pcdData = new PlayerCooldown(); } try { npcData = Interface.GetMod().DataFileSystem.ReadObject<NPCTPDATA>("NpcTp/NpcTP_Data"); } catch { Puts("Couldn't load NPCTP data, creating new datafile"); npcData = new NPCTPDATA(); } } #endregion #region Cooldown Management static double GrabCurrentTime() => DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds; #endregion private void TeleportPlayerPosition1(BasePlayer player, Vector3 destination) { if (player.net?.connection != null) player.ClientRPCPlayer(null, player, "StartLoading", null, null, null, null, null); StartSleeping(player); player.MovePosition(destination); if (player.net?.connection != null) player.ClientRPCPlayer(null, player, "ForcePositionTo", destination); if (player.net?.connection != null) player.SetPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot, true); player.UpdateNetworkGroup(); player.SendNetworkUpdateImmediate(false); if (player.net?.connection == null) return; try { player.ClearEntityQueue(null); } catch { } player.SendFullSnapshot(); } private void StartSleeping(BasePlayer player) { if (player.IsSleeping()) return; player.SetPlayerFlag(BasePlayer.PlayerFlags.Sleeping, true); if (!BasePlayer.sleepingPlayerList.Contains(player)) BasePlayer.sleepingPlayerList.Add(player); player.CancelInvoke("InventoryUpdate"); } private bool CheckPlayerMoney(BasePlayer player, int amount) { if (useEconomics) { double money = (double)Economics?.CallHook("GetPlayerMoney", player.userID); if (money >= amount) { money = money - amount; Economics?.CallHook("Set", player.userID, money); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("charged", this, player.UserIDString), (int)(amount))); return true; } SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("nomoney", this, player.UserIDString), (int)(amount))); } return false; } private bool CheckPlayerRP(BasePlayer player, int amount) { if (useRewards) { var money = (int)ServerRewards.Call("CheckPoints", player.userID); if (money >= amount) { ServerRewards.Call("TakePoints", player.userID, amount); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("charged", this, player.UserIDString), (int)(amount))); return true; } SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("nomoney", this, player.UserIDString), (int)(amount))); } return false; } #region USENPC [ChatCommand("npctp_add")] void cmdNpcAdD(BasePlayer player, string command, string[] args) { var n = npcData.NpcTP; string IDNPC = ""; string SpawnFiles = ""; if (!permission.UserHasPermission(player.userID.ToString(), "npctp.admin")) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("nopermcmd", this))); return; } if (args.Length == 2) { SpawnFiles = (args[1]); } var setup = new NPCInfo { Cost = Cost, CanUse = false, permission = "npctp.default", UseCommand = false, CommandOnPlayer = false, Command = "say", Arrangements = "this is a test" }; var setups = new NPCInfo { Cost = Cost, CanUse = true, SpawnFile = SpawnFiles, permission = "npctp.default", UseCommand = false, CommandOnPlayer = false, Command = "say", Arrangements = "this is a test" }; if (args.Length <= 0) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcerror", this, player.UserIDString))); return; } IDNPC = (args[0]); if (args.Length == 1) { if (!n.ContainsKey(IDNPC)) n.Add(IDNPC, setup); SaveNpcTpData(); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcadd", this, player.UserIDString), (string)(IDNPC))); return; } if (args.Length == 2) { if (!n.ContainsKey(IDNPC)) n.Add(IDNPC, setups); SaveNpcTpData(); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcadds", this, player.UserIDString), (string)(IDNPC), (string)(SpawnFiles))); return; } } void OnUseNPC(BasePlayer npc, BasePlayer player, Vector3 destination) { if (!npcData.NpcTP.ContainsKey(npc.UserIDString)) return; // Check if this NPC is registered ulong playerId = player.userID; string npcId = npc.UserIDString; double timeStamp = GrabCurrentTime(); var CanUse = npcData.NpcTP[npcId].CanUse; var cooldownTime = npcData.NpcTP[npcId].Cooldown; var buyMoney1 = npcData.NpcTP[npcId].Cost; var UseCommand = npcData.NpcTP[npcId].UseCommand; var CommandOnPlayer = npcData.NpcTP[npcId].CommandOnPlayer; var Perms = npcData.NpcTP[npcId].permission; string Command = npcData.NpcTP[npcId].Command; string Arrangements = npcData.NpcTP[npcId].Arrangements; object newpos = null; var bad = "somthing is not right somewhere"; string spawn = npcData.NpcTP[npcId].SpawnFile; if (!pcdData.pCooldown.ContainsKey(playerId)) { pcdData.pCooldown.Add(playerId, new PCDInfo()); //SaveData(); } if (!CanUse) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("notenabled", this))); return; } else { if (!permission.UserHasPermission(player.userID.ToString(), Perms)) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("noperm", this))); return; } if (pcdData.pCooldown[playerId].npcCooldowns.ContainsKey(npcId)) // Check if the player already has a cooldown for this NPC { var cdTime = pcdData.pCooldown[playerId].npcCooldowns[npcId]; // Get the cooldown time of the NPC if (cdTime > timeStamp) { SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("cdTime", this, player.UserIDString), (int)(cdTime - timeStamp) / 60)); return; } } if (useRewards) if (CheckPlayerRP(player, (int)buyMoney1)) { } if (useEconomics) if (CheckPlayerMoney(player, (int)buyMoney1)) { } if (UseCommand == false) { pcdData.pCooldown[playerId].npcCooldowns[npcId] = (long)timeStamp + cooldownTime; // Store the new cooldown in the players data under the specified NPC SaveData(); object success = Spawns.Call("GetRandomSpawn", spawn); if (success is Vector3) // Check if the returned type is Vector3 { Vector3 location = (Vector3)success; TeleportPlayerPosition1(player, (Vector3)success); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } if (UseCommand == true && CommandOnPlayer == false) { pcdData.pCooldown[playerId].npcCooldowns[npcId] = (long)timeStamp + cooldownTime; SaveData(); if (CommandOnPlayer) // Check if this is command on player { rust.RunServerCommand($"{Command} {Arrangements}"); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcCommand", this))); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } if (UseCommand == true && CommandOnPlayer == true) { pcdData.pCooldown[playerId].npcCooldowns[npcId] = (long)timeStamp + cooldownTime; SaveData(); if (CommandOnPlayer) // Check if this is command on player { rust.RunServerCommand($"{Command} {player.userID} {Arrangements}"); SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("npcCommand", this))); } else PrintError((string)bad); // Otherwise print the error message to console so server owners know there is a problem } } #endregion } } }
everything seems to be ok now any more recommendation?