Hey guys,
Is there a way to read the native Rust banlist and safe it to an array? I've tried looking everywhere but I can't find how people have read it, just a lot of custom solutions.
Thanks for your help.
Getting native ban list?
Discussion in 'Rust Development' started by Kinesis, Jul 18, 2017.
-
Code:
ServerUsers.GetAll(ServerUsers.UserGroup.Banned);
-
-
Code:var bannedUsers = ServerUsers.GetAll(ServerUsers.UserGroup.Banned); if (bannedUsers.Count < 1) return; var bannedArray = new ulong[bannedUsers.Count]; for (int i = 0; i < bannedUsers.Count; i++) { var user = bannedUsers[i]; bannedArray[i] = user.steamid; }
-
Code:List<ulong> GetBannedUsers() { List<ulong> ret = new List<ulong>(); foreach (var _user in ServerUsers.GetAll(ServerUsers.UserGroup.Banned).ToArray()) ret.Add(_user.steamid); return ret; }
You should use foreach loops instead of for loops when iterating through arrays, that way you don't need to keep track of a key and use extra lines of code.
Also, the usage of .ToArray() in my function will copy the current values for use in the loop to avoid enumeration errors.
If you still wanted to set the capacity of the list/array when constructing it (no major requirement in an Oxide script,) you can construct it like this instead:
Code:List<ulong> ret = new List<ulong>(ServerUsers.GetAll(ServerUsers.UserGroup.Banned).Count);
-
Lastly, he specifically asked for an array (more than once), and your snippets are lists. -
Code:return ServerUsers.GetAll(ServerUsers.UserGroup.Banned).ToArray();
-
Regardless, I'll stop here because it's quickly going off topic. -
This seems so pointless that I would love to weigh in. One line
Code:ServerUsers.GetAll(ServerUsers.UserGroup.Banned).Select(x=>x.steamid).ToArray();