Solved Bukkit.getPlayer(UUID) unavailable at login

Discussion in 'Spigot Plugin Development' started by ExpDev, Apr 22, 2017.

  1. Hey,

    I noticed that Bukkit.getPlayer(UUID) on the player that is currently logging in is unavailable, and I need it to be available. I am aware that waiting a tick is the solution (already tried), but any way around without having to deal with schedulers?

    Why?:
    I have a object representing data for the player. This object has a login() method, where the getPlayer() method is used.
    Code (Text):
    public void login() {
        System.out.println(getPlayer()==null);
        System.out.println(this.id);
        this.kills = getPlayer().getStatistic(Statistic.PLAYER_KILLS);
        this.deaths = getPlayer().getStatistic(Statistic.DEATHS);
    }
    Code (Text):
    public Player getPlayer() {
        return Bukkit.getPlayer(UUID.fromString(this.getId()));
    }
    getId is available and returns the player's correct id. This login() method is called onLoginEvent. However, the getPlayer returns null because Bukkit.getPlayer(their id) returns null.

    A little bit vague, but hope it makes sense.
     
  2. Listen to the PlayerJoinEvent instead?
     
    • Agree Agree x 1
  3. It's onPlayerJoin(PlayerJoinEvent e) {
    MyPlayer m = refToPlayer;
    m.login(); // here Bukkit.getPlayer(UUID) is called with e.getPlayer().getUniqueId() as Parameter. Yes, it's a correct uuid
    }
     
  4. That's what I'm doing. The login() method is to set variable values in my custom player object. However, the login method uses Bukkit.getPlayer with the object's Id (real player uuid). This I do onPlayerJoin
     
  5. Oops, I'm just using PlayerLoginEvent instead of PlayerJoinEvent. Just checked my code against it now. Really thought there was just one. Can someone clerify when both are called (differences?). I can just read the docs but
     
  6. PlayerLoginEvent
    PlayerJoinEvent
    - Source(Click)
     
    • Agree Agree x 1
    • Friendly Friendly x 1
  7. I'm not so sure Login is that early - ASyncPlayerPreLogin event is called before, and the LoginEvent will only call once that is finished.

    You should use ASyncPlayerPreLogin event 90% of the time for login checks like bans.

    Also, what's the issue with just making login accept a player parameter?
     
  8. Would be my solution, but calling PlayerJoinEvent works out excellent for this.
     
  9. I mean still - you're iterating through online players unnecessarily just to get a player object you already have a pointer to - why?