Solved onEnable how to get a Player variable without it returning null

Discussion in 'Plugin Development' started by TheDiamond06, Mar 18, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    TheDiamond06

    In my onEnable method I am calling a method that requires a Player variable. I've tried for quite some time and cannot find away to get a Player as a variable in the onEnable without it returning null. How would I do this?
    Code:java
    1. Player p = Bukkit.getOnlinePlayers();

    This seems to not work because this gets an array when I need one player.

    The method I am calling is going into a run method running a repeating task. The Player I am looking for is basically any Player on the server. If I use the getOnlinePlayers Method it will not work for the Player I am calling in the method.
     
    Last edited: Mar 18, 2015
  2. Offline

    Kassestral

    I would imagine that in your method you can check to see if player is equal to null, if it's not then do what you would normally do with your code, also doesn't Player p = Bukkit.getOnlinePlayers() return a arraylist of players?
     
  3. Offline

    TheDiamond06

    @Kassestral If I am making the variable of the Player in onEnable null, testing for null would be pointless in the method. It would always return null. Bukkit.getOnlinePlayers does return an ArrayList, but when I use that for the variable in the method it gives errors. It says cannot invoke type on Player[]
     
  4. Offline

    SuperOriginal

    What method are you trying to call?
     
  5. Offline

    Kassestral

    'If I am making the variable of the Player in onEnable null, testing for null would be pointless in the method'
    Why would you need a player if you are setting it to null?, also could you share the full code with us so we can get a grasp of what you are doing and how to fix it?
    @TheDiamond06
     
  6. Offline

    teej107

    @TheDiamond06 The getOnlinePlayers method returns a Collection. A Collection is not a Player and your code attempt shows that you are trying make it one.
     
  7. Offline

    xTigerRebornx

    @TheDiamond06 Chances are when onEnable() is called, there will be no players online anyway (unless the server was reloaded, which isn't a good situation to be in).
    Find an alternate place to call it, such as when the player joins. Either that, or make it not depend on a Player
     
  8. Offline

    Konato_K

    @TheDiamond06 The real question is why do you need a Player onEnable?
     
  9. Offline

    MCJoshua345

    @TheDiamond06
    You're trying to assign a variable to every player on the server, which I'm pretty sure is incompatible with a String.
    He's right. I think what you're looking for is this:
    Code:
    @EventHandler
    public void onPJoin(PlayerJoinEvent e){
     
          Player p = e.getPlayer();
    
    }
    
     
  10. Code:
    for(int i = 0; Bukkit.getOnlinePlayers(); i++){
    
    Player p = Bukkit.getPlayer(i);
    
    }
    
    That might work if you put it inside a scheduler :p.
     
  11. @TheDiamond06 Pretty sure the easiest way of doing this would be...
    Code:
    Bukkit.getOnlinePlayers().getPlayer("playerName");
    // OR
    Bukkit.getOnlinePlayers().getPlayerExact("ExactPlayerName");
     
  12. Offline

    teej107

    Player names aren't represented with ints. getPlayer(int) is a method that doesn't exist.

    OP wants any one player that is on. Not a certain Player.
     
    CodePlaysMinecraft likes this.
  13. Offline

    TheDiamond06

    @MCJoshua345 @xTigerRebornx Took my a while to get back to this forum but I will use this instead. The onPlayerJoinEvent would seem to work the same I guess if I am trying to use a player.
     
Thread Status:
Not open for further replies.

Share This Page