Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace Oxide.Plugins
- {
- [Info("BlockedNames", "BadWolf", "1.0.0")]
- [Description("Prevents players from connecting with unwanted names.")]
- class BlockedNames : CovalencePlugin
- {
- // Do NOT edit this file, instead edit BlockedNames.json in oxide/config and BlockedNames.en.json in the oxide/lang directory,
- // or create a new language file for another language using the 'en' file as a default.
- #region Localization
- void LoadDefaultMessages()
- {
- lang.RegisterMessages(new Dictionary<string, string>
- {
- {"BlockedWords", "Name contains blocked words"},
- {"InvalidCharacters", "Invalid characters in name"},
- //{"InvalidCharactersLog", "{name} ({userid}) kicked for invalid characters in name"},
- {"UniqueNamesOnly", "Name must be unique"},
- //{"UniqueNamesOnlyLog", "{name} ({userid}) kicked for non-unique name"}
- }, this);
- }
- #endregion
- #region Configuration
- char[] AllowedCharacters => GetConfig("AllowedCharacters", "✪abcdefghijklmnopqrstuvwxyz1234567890|;:~[](){}>!@#$%^&_-=+. ").ToCharArray();
- List<object> BlockedWords => GetConfig("BlockedWords", new List<object> { "admin", "moderator", "owner", "server" });
- bool StripHtmlTags => GetConfig("StripHtmlTags", true);
- bool UniqueNamesOnly => GetConfig("UniqueNamesOnly", true);
- protected override void LoadDefaultConfig()
- {
- Config["AllowedCharacters"] = new string(AllowedCharacters);
- Config["BlockedWords"] = BlockedWords;
- Config["StripHtmlTags"] = StripHtmlTags;
- Config["UniqueNamesOnly"] = UniqueNamesOnly;
- SaveConfig();
- }
- #endregion
- #region Initialization
- void Init()
- {
- #if !HURTWORLD && !REIGNOFKINGS && !RUST && !RUSTLEGACY
- throw new NotSupportedException("This plugin does not support this game");
- #endif
- LoadDefaultConfig();
- LoadDefaultMessages();
- }
- #endregion
- #region Name Checking
- bool IsNameBlocked(string name) => BlockedWords.Any(w => name.ToLower().Contains(w.ToString())); // TODO: Check casing
- bool IsNameInvalid(string name) => !name.ToLower().All(AllowedCharacters.Contains);
- bool IsNameUnique(string name) => players.Online.Any(p => !p.BasePlayer.Nickname.Equals(name));
- string NameCheck(string name, string userId)
- {
- if (IsNameBlocked(name)) return GetMessage("BlockedWords", userId);
- if (IsNameInvalid(name)) return GetMessage("InvalidCharacters", userId);
- if (UniqueNamesOnly && IsNameUnique(name)) return GetMessage("UniqueNamesOnly", userId);
- return null;
- }
- #if HURTWORLD
- void OnPlayerConnected(PlayerSession session)
- {
- if (StripHtmlTags)
- {
- session.Name = session.Identity.Name = StripTags(session.Name);
- session.WorldPlayerEntity?.GetComponent<HurtMonoBehavior>().RPC("UpdateName", session.Player, session.Name);
- }
- var player = players.GetOnlinePlayer(session.SteamId.ToString());
- if (IsNameInvalid(session.Name)) player.Kick(GetMessage("InvalidCharacters", session.SteamId.ToString()));
- if (IsNameBlocked(session.Name)) player.Kick(GetMessage("BlockedWords", session.SteamId.ToString()));
- //if (UniqueNamesOnly && IsNameUnique(session.Name)) player.Kick(GetMessage("UniqueNamesOnly", session.SteamId.ToString()));
- }
- #endif
- #if REIGNOFKINGS
- object OnUserApprove(CodeHatch.Engine.Networking.Player player)
- {
- if (StripHtmlTags) player.Name = StripTags(player.Name);
- return NameCheck(player.Name, player.Id.ToString());
- }
- #endif
- #if RUST
- object CanClientLogin(Network.Connection connection)
- {
- if (StripHtmlTags) connection.username = StripTags(connection.username);
- return NameCheck(connection.username, connection.userid.ToString());
- }
- #endif
- #if RUSTLEGACY
- object CanClientLogin(Client.Connection connection)
- {
- if (StripHtmlTags) connection.username = StripTags(connection.username);
- return NameCheck(connection.username, connection.userid.ToString());
- }
- #endif
- #endregion
- #region Helper Methods
- T GetConfig<T>(string name, T defaultValue)
- {
- if (Config[name] == null) return defaultValue;
- return (T)Convert.ChangeType(Config[name], typeof(T));
- }
- string GetMessage(string key, string userId = null) => lang.GetMessage(key, this, userId);
- static string StripTags(string name) => Regex.Replace(name, "<.*?>", string.Empty);
- #endregion
- }
- }
Add Comment
Please, Sign In to add comment