1. iShot

    iShot Scavenger

    Dear,

    I'm sorry if this question is the most simple thing you've seen today but I'm reasonably new in the world of writing Oxide plugins. I'm currently trying to write a console command that gives ServerReward points to a player, I was hoping I can do that for both online and offline players (as long as they are registered once).

    Is there a way to create a BasePlayer using a steam64 ID even if the player is offline? That way I could forward that BasePlayer to the following command even when he's offline?

    Code (Text):
    ServerRewards?.Call("AddPoints", new object[] { player.userID, 5 });

    Thanks in advance,

    iShot
     
  2. Wulf

    Wulf Community Admin Community Admin Oxide Developer

    If they are dead, they won't have a BasePlayer. If they are sleeping, they'd be in the sleeper list. The better way would be to use Oxide's Covalence methods to get all players from it instead via players.All (or covalence.Players.All if your plugin isn't a Covalence plugin.)
     
    iShot likes this.
  3. Vlad-00003

    Vlad-00003 Master Researcher Plugin Developer

    As Wulf said - you can use covalense library. Here is a quick example(made on fly, no idea if there is a mistakes =D)

    Code (Text):
    using Oxide.Core.Libraries.Covalence;
    namespace Oxide.Plugins
         public class CoolPlugin : RustPlugin
         {
           //This function will return list of all founded players within Dictionary<ID, Name>
            private Dictionary<ulong, string> GetPlayers(string NameOrID)
            {
                var pl = covalence.Players.FindPlayers(NameOrID).ToList();
                return pl.Select(p => new KeyValuePair<ulong, string>(ulong.Parse(p.Id), p.Name)).ToDictionary(x => x.Key, x => x.Value);
            }
             pirvate void AddPoints(string UserID, int points)
             {
                ServerRewards?.Call("AddPoints", new object[] {UserID, points});
             }
             [ChatCommand("Add")
             void Add(BasePlayer player, string command, string[] args)
             {
             if(args.Lenght < 2) return;
             var recivers = GetPlayers(arg[0]);
             if(recivers == null || recivers.Count == 0)
             {
                      SendReply(player,"Player {0} can not be found!", args[0]);
                      return;
             }
             if(ecivers.Count > 1)
             {
                      SendReply(player,"Multiply players found:\n{0}",string.Join("\n",recivers..Select(p => $"{p.Value} ({p.Key})").ToArray()));
                      return;
             }
             var target = recivers.First();
             //Don't forget to check agrs[1] for being in of course =D
             AddPoints(target.Key,args[1]);
             SendReply(player,"Successfully added {0} point to the player {1}", target.Value, args[1]);
             }
            }
         }
    This example works with both input - partial name or id.