Hello, I just wanted to share some useful functions with you that I made.
I hope they are useful
If you use those in your plugin, credit me please (Comment in plugin, in plugin overview)
Code://---------------------------> Player finding <---------------------------// BasePlayer GetPlayer(string searchedPlayer, BasePlayer executer, string prefix) { BasePlayer targetPlayer = null; List<string> foundPlayers = new List<string>(); string searchedLower = searchedPlayer.ToLower(); foreach (BasePlayer player in BasePlayer.activePlayerList) { string display = player.displayName; string displayLower = display.ToLower(); if (!displayLower.Contains(searchedLower)) { continue; } if (displayLower.Contains(searchedLower)) { foundPlayers.Add(display); } } var matchingPlayers = foundPlayers.ToArray(); if (matchingPlayers.Length == 0) { SendChatMessage(executer, prefix, "No matching players found!"); } if (matchingPlayers.Length > 1) { SendChatMessage(executer, prefix, "Multiple players found:"); string multipleUsers = ""; foreach (string matchingplayer in matchingPlayers) { if (multipleUsers == "") { multipleUsers = "<color=yellow>" + matchingplayer + "</color>"; continue; } if (multipleUsers != "") { multipleUsers = multipleUsers + ", " + "<color=yellow>" + matchingplayer + "</color>"; } } SendChatMessage(executer, prefix, multipleUsers); } if (matchingPlayers.Length == 1) { targetPlayer = BasePlayer.Find(matchingPlayers[0]); } return targetPlayer; } //----------------------------> Converting <----------------------------// string ArrayToString(string[] array, int first) { int count = 0; string output = array[first]; foreach (string current in array) { if (count <= first) { count++; continue; } output = output + " " + current; count++; } return output; } //----------------------------> Chat Sending <----------------------------// void BroadcastChat(string prefix, string msg) { PrintToChat("<color=orange>" + prefix + "</color>: " + msg); } void SendChatMessage(BasePlayer player, string prefix, string msg) { SendReply(player, "<color=orange>" + prefix + "</color>: " + msg); } //------------------------------ by LaserHydra -------------------------//
Player finding, stringArray2string & chat
Discussion in 'Rust Development' started by LaserHydra, Jun 30, 2015.
-
You could probably just use .ToString() on your array to get the same result..
-
didnt test that, also with my function you should also be able to say it should start at a specific member. for example mainly for getting a message. If you want to do /say <player> <message>, then you gotta use 1 as the 2. argument of the function as that is the arg where the message begins.
-
Don't loop player list 2 times. Just how it's can be done (maybe not the best, but...):
Code:List<BasePlayer> FindPlayer(string find) { find = find.ToUpper(); var NamesList = new List<BasePlayer>(); foreach(var ply in BasePlayer.activePlayerList) if (ply.displayName.ToUpper().Contains(find)) NamesList.Add(ply); return NamesList; } void SomeFunction() { var list = FindPlayer("Name"); if (target.Count == 1) //list target[0] is your player list[0].ChatMessage("Hello!"); else if (list.Count == 0) player.ChatMessage("Can't find player with this name!"); else player.ChatMessage("Found multiple players: " + list.Select(ply => ply.displayName).Aggregate((name1, name2) => name1 + ", " + name2)); // Also you can use -> String.Join(", ", list.Select(ply => ply.displayName)); } -
or better yet:
Code:List<BasePlayer> FindPlayer(string find) { return BasePlayer.activePlayerList.Where(p => p.displayName.IndexOf(find, StringComparison.OrdinalIgnoreCase) >= 0); } -
I just advanced it as much as I could, so you don't need to set all the messages extra. As you see my function also send messages and stuff
-
Oh I was just talking about the first function, the one that gets the player matches.
-
you mean the PART of the function
-
Sorry I was quoting Bombardir's version of the code, hence the confusion

So yes, if referring to your code it would be part of the function. -
yeah I was a bit confused ^^
