Code:void UseUI(BasePlayer player, string msg) { var elements = new CuiElementContainer(); var mainName = elements.Add(new CuiPanel { Image = { Color = "0.1 0.1 0.1 1" }, RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" }, CursorEnabled = true }, "Overlay", "Npctp"); if(backroundimage == true) { elements.Add(new CuiElement { Parent = "Npctp", Components = { new CuiRawImageComponent { Url = backroundimageurl, Sprite = "assets/content/textures/generic/fulltransparent.tga" }, new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1" } } }); } var Agree = new CuiButton { Button = { Close = mainName, Color = "0 255 0 1" }, RectTransform = { AnchorMin = "0.2 0.16", AnchorMax = "0.45 0.2" }, Text = { Text = "I Agree", FontSize = 22, Align = TextAnchor.MiddleCenter } }; var Disagree = new CuiButton { Button = { Command = "global.hardestcommandtoeverguess", Close = mainName, Color = "255 0 0 1" }, RectTransform = { AnchorMin = "0.5 0.16", AnchorMax = "0.75 0.2" }, Text = { Text = "I Disagree", FontSize = 22, Align = TextAnchor.MiddleCenter } }; elements.Add(new CuiLabel { Text = { Text = msg, FontSize = 22, Align = TextAnchor.MiddleCenter }, RectTransform = { AnchorMin = "0 0.20", AnchorMax = "1 0.9" } }, mainName); elements.Add(Agree, mainName); elements.Add(Disagree, mainName); CuiHelper.AddUi(player, elements); } void DisplayUI(BasePlayer player) { string msg = ""; msg = msg + lang.GetMessage("chargeyesno", this, player.UserIDString); UseUI(player, msg.ToString()); }Code:if (useUI) DisplayUI(player); { }
how would i make it so that it will return true; if agree and return false; if disagree
right now it is displaying the ui and still continuing the script with no input.
Help with UI return true/false
Discussion in 'Rust Development' started by Ts3hosting, Jan 29, 2017.
-
remember that if you want to return true/false, you have to make your function a bool and not void
Code:bool UseUI(BasePlayer player, string msg) { if(CuiHelper.AddUi(player, elements)) return true; return false; }
-
where would i add in the
Code:if(CuiHelper.AddUi(player, elements)) return true; return false;
-
-
i tried that with no luck the ui pops up and still runs with no input to the UI
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; using System.Linq; using Oxide.Game.Rust.Cui;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 bool backroundimage; private bool Changed; private string text; private bool displayoneveryconnect; private string kickmsg; private string backroundimageurl; private static int cooldownTime = 3600; private static int auth = 2; private static bool noAdminCooldown = false; 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!" }, {"chargeyesno", "I will charge you {0} to talk to me!" }, }; #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 bool useUI; 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); if (amount == 0) return true; if (amount > 0) 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); if (amount > 0) SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("charged", this, player.UserIDString), (int)(amount))); return true; if (amount == 0) return true; } SendReply(player, string.Format(lang.GetMessage("title", this) + lang.GetMessage("nomoney", this, player.UserIDString), (int)(amount))); } return false; } bool UseUI(BasePlayer player, string msg) { var elements = new CuiElementContainer(); var mainName = elements.Add(new CuiPanel { Image = { Color = "0.1 0.1 0.1 1" }, RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" }, CursorEnabled = true }, "Overlay", "Npctp"); if(backroundimage == true) { elements.Add(new CuiElement { Parent = "Npctp", Components = { new CuiRawImageComponent { Url = backroundimageurl, Sprite = "assets/content/textures/generic/fulltransparent.tga" }, new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1" } } }); } var Agree = new CuiButton { Button = { Close = mainName, Color = "0 255 0 1" }, RectTransform = { AnchorMin = "0.2 0.16", AnchorMax = "0.45 0.2" }, Text = { Text = "I Agree", FontSize = 22, Align = TextAnchor.MiddleCenter } }; var Disagree = new CuiButton { Button = { Close = mainName, Color = "255 0 0 1" }, RectTransform = { AnchorMin = "0.5 0.16", AnchorMax = "0.75 0.2" }, Text = { Text = "I Disagree", FontSize = 22, Align = TextAnchor.MiddleCenter } }; elements.Add(new CuiLabel { Text = { Text = msg, FontSize = 22, Align = TextAnchor.MiddleCenter }, RectTransform = { AnchorMin = "0 0.20", AnchorMax = "1 0.9" } }, mainName); elements.Add(Agree, mainName); elements.Add(Disagree, mainName); if(CuiHelper.AddUi(player, elements)) return true; return false; } void DisplayUI(BasePlayer player) { string msg = ""; msg = msg + lang.GetMessage("chargeyesno", this, player.UserIDString); UseUI(player, msg.ToString()); } #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", useUI = false, UseCommand = false, CommandOnPlayer = false, Command = "say", Arrangements = "this is a test" }; var setups = new NPCInfo { Cost = Cost, CanUse = true, SpawnFile = SpawnFiles, permission = "npctp.default", useUI = false, 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; var useUI = npcData.NpcTP[npcId].useUI; string Command = npcData.NpcTP[npcId].Command; string Arrangements = npcData.NpcTP[npcId].Arrangements; object newpos = null; var bad = "somthing is not right somewhere is your spawnfile name correct"; string spawn = npcData.NpcTP[npcId].SpawnFile; string msg = ""; 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 (useUI) msg = msg + lang.GetMessage("chargeyesno", this, player.UserIDString); UseUI(player, msg.ToString()); { } 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 } } }
-
You need to use a create a console command and assign it to the button using the "Command" field in CUIButton
-
Code:void OnUseNPC(BasePlayer npc, BasePlayer player, Vector3 destination) { string msg = ""; msg = msg + lang.GetMessage("chargeyesno", this, player.UserIDString); var elements = new CuiElementContainer(); var mainName = elements.Add(new CuiPanel { Image = { Color = "0.1 0.1 0.1 1" }, RectTransform = { AnchorMin = "0 0", AnchorMax = "1 1" }, CursorEnabled = true }, "Overlay", "Npctp"); if(backroundimage == true) { elements.Add(new CuiElement { Parent = "Npctp", Components = { new CuiRawImageComponent { Url = backroundimageurl, Sprite = "assets/content/textures/generic/fulltransparent.tga" }, new CuiRectTransformComponent { AnchorMin = "0 0", AnchorMax = "1 1" } } }); } var Agree = new CuiButton { Button = { Command = "global.hardestcommandtoeverguessnpctp", Close = mainName, Color = "0 255 0 1" }, RectTransform = { AnchorMin = "0.2 0.16", AnchorMax = "0.45 0.2" }, Text = { Text = "I Agree", FontSize = 22, Align = TextAnchor.MiddleCenter } }; var Disagree = new CuiButton { Button = { Close = mainName, Color = "255 0 0 1" }, RectTransform = { AnchorMin = "0.5 0.16", AnchorMax = "0.75 0.2" }, Text = { Text = "I Disagree", FontSize = 22, Align = TextAnchor.MiddleCenter } }; elements.Add(new CuiLabel { Text = { Text = msg, FontSize = 22, Align = TextAnchor.MiddleCenter }, RectTransform = { AnchorMin = "0 0.20", AnchorMax = "1 0.9" } }, mainName); elements.Add(Agree, mainName); elements.Add(Disagree, mainName); CuiHelper.AddUi(player, elements); }
Code:[ConsoleCommand("hardestcommandtoeverguessnpctp")] void OnUseNPC1(BasePlayer npc, BasePlayer player, Vector3 destination) {
-
I see now after doing some research this can not be done thanks.
-
ok so im trying like this witch should work but i must be missing somthing
Code:[ConsoleCommand("hardestcommandtoeverguessnpctp")] void cmdRun(ConsoleSystem.Arg arg, string[] args) { BasePlayer player = (BasePlayer)arg.connection.player; string npcId = args[0]; string spawn = npcData.NpcTP[npcId].SpawnFile; var cooldownTime = npcData.NpcTP[npcId].Cooldown; var UseCommand = npcData.NpcTP[npcId].UseCommand; ulong playerId = player.userID; var bad = "somthing is not right somewhere"; double timeStamp = GrabCurrentTime(); if (UseCommand == false) { 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 } }
and the call isCode:Command = $"hardestcommandtoeverguessnpctp {playerId} {npcId}",
what am i missing..Code:Failed to call hook 'cmdRun' on plugin 'Npctp v2.2.7' (NullReferenceException: Object reference not set to an instance of an object)
-
Ok New problem so with the UI command all works fine
Command = $"hardestcommandtoeverguessnpctp {playerId} {npcId}",
but if i try to run with out the UI with this call it fails.
rust.RunServerCommand("hardestcommandtoeverguessnpctp {playerId} {npcId}");
also tried rust.RunServerCommand($"hardestcommandtoeverguessnpctp {playerId} {npcId}");
Code:[ConsoleCommand("hardestcommandtoeverguessnpctp")] void cmdRun(ConsoleSystem.Arg arg) { var player = arg.Player(); var npcId = arg.Args[1];
player.SendConsoleCommand($"hardestcommandtoeverguessnpctp {playerId} {npcId}"); -
maybe
Code:player.SendConsoleCommand("hardestcommandtoeverguessnpctp", playerId, npcId);