Whisper plugin update

Download Raw Clone


  1. ?using System.Collections.Generic;
  2. using Oxide.Core;
  3. using System.Linq;
  4. using Oxide.Core.Libraries.Covalence;
  5. namespace Oxide.Plugins
  6. {
  7. [Info("Whisper", "Ryan/mTrX", "1.1.1")]
  8. class Whisper : CovalencePlugin
  9. {
  10. #region Declaration
  11. private const string UsePerm = "whisper.use";
  12. private const string ReplyPerm = "whisper.reply";
  13. private string Command;
  14. private string ReplyCmd;
  15. #endregion
  16. #region Configuration
  17. private void LoadDefaultConfig()
  18. {
  19. Puts("Generating default configuration file");
  20. Config["Command"] = Command = "whisper";
  21. Config["Reply Command"] = ReplyCmd = "wr";
  22. Config.Save();
  23. }
  24. #endregion
  25. #region Lang
  26. private struct Msg
  27. {
  28. public const string Prefix = "Prefix";
  29. public const string Message = "Message";
  30. public const string NoPermission = "NoPermission";
  31. public const string Whispered = "Whispered";
  32. public const string InvalidArgs = "InvalidArgs";
  33. public const string NotId = "NotId";
  34. public const string NoPlayer = "NoPlayer";
  35. public const string InvalidArgsReply = "InvalidArgsReply";
  36. public const string Replied = "Replied";
  37. public const string Reply = "Reply;";
  38. }
  39. private void LoadDefaultMessages()
  40. {
  41. lang.RegisterMessages(new Dictionary<string, string>
  42. {
  43. [Msg.Prefix] = "[#ff6666][WHISPER][/#] ",
  44. [Msg.Message] = "[#c0c0c0ff]{0}[/#]",
  45. [Msg.NoPermission] = "You don't have permission to use that commannd",
  46. [Msg.Whispered] = "Whispered {0} ({1}) with message '{2}'",
  47. [Msg.InvalidArgs] = "Invalid arguments. Usage: '{0} <id> <message>'",
  48. [Msg.InvalidArgsReply] = "Invalid arguments. Usage '{0} <message>'",
  49. [Msg.NotId] = "The ID you entered doesn't seem to be a valid Steam ID",
  50. [Msg.NoPlayer] = "No online player found with that Steam ID",
  51. [Msg.Replied] = "You have sent your reply",
  52. [Msg.Reply] = "[REPLY] {0} replied with message '{1}'"
  53. }, this);
  54. }
  55. #endregion
  56. #region Methods
  57. private string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
  58. private ulong FindPlayersSingleId(string nameOrIdOrIp, IPlayer player)
  59. {
  60. var targets = FindPlayers(nameOrIdOrIp);
  61. if (targets.Count > 1)
  62. {
  63. player.Reply("MultiplePlayers", string.Join(", ", targets.Select(p => p.displayName).ToArray()));
  64. return 0;
  65. }
  66. ulong userId;
  67. if (targets.Count <= 0)
  68. {
  69. if (ulong.TryParse(nameOrIdOrIp, out userId)) return userId;
  70. player.Reply("PlayerNotFound");
  71. return 0;
  72. }
  73. else
  74. userId = targets.First().userID;
  75. return userId;
  76. }
  77. private static HashSet<BasePlayer> FindPlayers(string nameOrIdOrIp)
  78. {
  79. var foundPlayers = new HashSet<BasePlayer>();
  80. if (string.IsNullOrEmpty(nameOrIdOrIp)) return foundPlayers;
  81. foreach (var activePlayer in BasePlayer.activePlayerList)
  82. {
  83. if (activePlayer.UserIDString.Equals(nameOrIdOrIp))
  84. foundPlayers.Add(activePlayer);
  85. else if (!string.IsNullOrEmpty(activePlayer.displayName) && activePlayer.displayName.ToLower().IndexOf(nameOrIdOrIp) != -1)
  86. foundPlayers.Add(activePlayer);
  87. else if (activePlayer.net?.connection != null && activePlayer.net.connection.ipaddress.Equals(nameOrIdOrIp))
  88. foundPlayers.Add(activePlayer);
  89. }
  90. return foundPlayers;
  91. }
  92. #endregion
  93. #region Hooks
  94. private void Init()
  95. {
  96. permission.RegisterPermission(UsePerm, this);
  97. permission.RegisterPermission(ReplyPerm, this);
  98. Command = (string) Config["Command"];
  99. ReplyCmd = (string) Config["Reply Command"];
  100. if (string.IsNullOrEmpty(Command) || string.IsNullOrEmpty(ReplyCmd))
  101. {
  102. LogWarning("Configuration file not valid");
  103. LoadDefaultConfig();
  104. }
  105. AddCovalenceCommand(Command, "WhisperCommand");
  106. AddCovalenceCommand(ReplyCmd, "ReplyCommand");
  107. }
  108. #endregion
  109. #region Commands
  110. private void WhisperCommand(IPlayer player, string command, string[] args)
  111. {
  112. if (!player.IsAdmin && !player.HasPermission(UsePerm))
  113. {
  114. player.Reply(Lang(Msg.NoPermission, player.Id));
  115. return;
  116. }
  117. if (args.Length < 2)
  118. {
  119. player.Reply(Lang(Msg.InvalidArgs, player.Id, Command));
  120. return;
  121. }
  122. string userId = args[0];
  123. if (!userId.IsSteamId())
  124. {
  125. ulong playerID = FindPlayersSingleId(userId, player);
  126. if (playerID == 0)
  127. {
  128. player.Reply(Lang(Msg.NoPlayer, player.Id));
  129. return;
  130. }
  131. userId = playerID.ToString();
  132. }
  133. var foundPlayer = players.FindPlayerById(userId);
  134. string foundPlayerName = FindPlayers(userId).First().displayName;
  135. if (foundPlayer == null || !foundPlayer.IsConnected)
  136. {
  137. player.Reply(Lang(Msg.NoPlayer, player.Id));
  138. return;
  139. }
  140. var fullArgs = string.Join(" ", args.ToList().Skip(1).ToArray());
  141. player.Reply(Lang(Msg.Whispered, player.Id, foundPlayer.Name, foundPlayer.Id, fullArgs));
  142. foundPlayer.Reply(covalence.FormatText(Lang(Msg.Prefix, player.Id) + foundPlayerName + ": " + Lang(Msg.Message, player.Id, fullArgs)));
  143. }
  144. private void ReplyCommand(IPlayer player, string command, string[] args)
  145. {
  146. if (!player.IsAdmin && !player.HasPermission(ReplyPerm))
  147. {
  148. player.Reply(Lang(Msg.NoPermission, player.Id));
  149. return;
  150. }
  151. if (args.Length < 1)
  152. {
  153. player.Reply(Lang(Msg.InvalidArgsReply, player.Id, ReplyCmd));
  154. return;
  155. }
  156. var fullArgs = string.Join(" ", args);
  157. Puts(Lang(Msg.Reply, null, player.Name, fullArgs));
  158. player.Reply(Lang(Msg.Replied, player.Id));
  159. }
  160. #endregion
  161. }
  162. }

Raw paste data: