Running commands for offline & online players?

Discussion in 'Spigot Plugin Development' started by Moderance, Jun 11, 2016.

  1. So I am not the best coder out there but I definitely know the basics of java and bukkit. So for a while now on commands such as /ban or /deop if the player is offline then I just clone the method and use the cast OfflinePlayer instead of Player, there must be a more efficient, better way to do this as I really find it annoying as I'm having to repeat chunks of code just because of 1 cast.
  2. This just came into my head, not sure if it's efficient, but maybe do this:

    Every time someone does a command with an offline player as an argument, save the player in the config, with the command is that run.
    Once that player joins the server again, go into the config, and grab the player and the command that's supposed to be run, and run it.
    • Like Like x 1
  3. Bukkit.getOfflinePlayer(name);



    For a lot of things you can simply treat online players as offline objects if all you need is things the offline object holds (like name or uuid).

    If you need to check for offline players permissions then you can use Vault.
    • Like Like x 1
  4. Both are wonderful ideas, but @BillyGalbreath's doesn't work with commands like /give... (As he said)

    Sent from my iPhone using Tapatalk
  5. So in short, how can I use it in a command,

    Code (Text):
    Player player = (Player) args[0];

    if (player.isOffline) {
    } else {
    Will it work if 'methodToCall' has a Player cast?
  6. You only use it for commands where you expect an offline player to be supported. Like /ban or /unban. You cant give an offline player an item or teleport them anywhere because they are not online and thus do not exist.

    Code (Text):

    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (args.length == 0) {
            // do something to handle no args.
            return true;

        OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]);

        if (target == null) {
            // this should never happen, but plan for it anyways.
            // probably cause could be no player cache for an offline mode server or if the mojang session servers are down.
            // mostly just report an error here.
            return true;

        if (!target.isOnline()) {
            // the player is not currently online. do stuff here
            // for commands like /give or /teleport you can show an error message here.
            // for other commands like /ban or /unban you can do the action here
            return true;

        // the player is online. do stuff here.
        // you can do pretty much anything here for any command.
        // if you need an actual Player object you can get the online Player object from OfflinePlayer.
        Player onlinePlayer = target.getPlayer();

        return true;
    Basically, its all circumstantial to what you plan on doing with the command whether or not you want to use offline player objects or not.

    For the most part, the methods provided by OfflinePlayer are sufficient without needing to use getPlayer() first. You have remember that the Player object extends the OfflinePlayer object, which means a lot of Player's methods are actually coming from OfflinePlayer (or any of its higher interfaces).
    • Winner Winner x 1
  7. Thanks helped me out a lot