TGAM123

Whisper plugin update

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