using System.Collections.Generic; using System.Linq; using System.Text; using Oxide.Core; namespace Oxide.Plugins { [Info("RenameName", "Sparkless", "0.0.1")] public class RenameName : RustPlugin { #region Var public string Permission = "renamename.use"; #endregion #region Lang public static StringBuilder sb = new StringBuilder(); public string GetLang(string LangKey, string userID = null, params object[] args) { sb.Clear(); if (args != null) { sb.AppendFormat(lang.GetMessage(LangKey, this, userID), args); return sb.ToString(); } return lang.GetMessage(LangKey, this, userID); } private void LoadMessages() { lang.RegisterMessages(new Dictionary { ["SetName"] = "You have a nickname installed - {0}", ["SetOldName"] = "You have returned your old nickname - {0}", ["NotFoundOldName"] = "The old nickname was not found! You may not have changed your nickname yet", } , this); } #endregion void OnServerInitialized() { try { OldName = Interface.GetMod().DataFileSystem.ReadObject>(Name); if (OldName == null) OldName = new Dictionary(); } catch { OldName = new Dictionary(); } if (!permission.PermissionExists(Permission, this)) permission.RegisterPermission(Permission, this); LoadMessages(); } void Unload() { Interface.Oxide.DataFileSystem.WriteObject(Name, OldName); } void OnServerSave() { Interface.Oxide.DataFileSystem.WriteObject(Name, OldName); } public Dictionary OldName = new Dictionary(); [ChatCommand("rename")] void RenameFoNick(BasePlayer player, string command, string[] args) { if (!permission.UserHasPermission(player.UserIDString, Permission)) return; var message = string.Join(" ", args.Skip(0).ToArray()); if (string.IsNullOrEmpty(message)) return; if (!OldName.ContainsKey(player.userID)) { OldName.Add(player.userID, player.displayName); } Rename(player, message); player.ChatMessage(GetLang("SetName", player.UserIDString, message)); } [ChatCommand("renameold")] void RenameOldNick(BasePlayer player) { if (!permission.UserHasPermission(player.UserIDString, Permission)) return; if (!OldName.ContainsKey(player.userID)) { player.ChatMessage(GetLang("NotFoundOldName", player.UserIDString)); return; } Rename(player, OldName[player.userID]); player.ChatMessage(GetLang("SetOldName", player.UserIDString, OldName[player.userID])); OldName.Remove(player.userID); } public void Rename(BasePlayer player, string name) { name = (string.IsNullOrEmpty(name.Trim()) ? player.displayName : name); player.net.connection.username = name; player.displayName = name; player._name = name; player.SendNetworkUpdateImmediate(false); player.IPlayer.Name = name; permission.UpdateNickname(player.UserIDString, name); } } }