Solved getOnlinePlayers() doesnt work!?

Discussion in 'Plugin Development' started by Schaakmatth, May 27, 2014.

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

    Necrodoom

    Schaakmatth And now what if are you adding an else for exactly?
     
  2. Offline

    Schaakmatth

  3. Offline

    Necrodoom

    you have an else. else executes when ever the if condition before the else fails. Where is the if at?
    If you dont know, please read up on how if-elses work.
     
  4. Offline

    Schaakmatth

    i think i understand you: here is the if:
    Code:java
    1. @EventHandler
    2. public void PlayerJoin(PlayerJoinEvent e) {
    3. final Player p = e.getPlayer();
    4. final World world1 = p.getWorld();
    5. Location loc = new Location(world1, 112, 1, 66);
    6. p.teleport(loc);
    7.  
    8. //waitTimer
    9. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    10. public void run() {
    11. if(Bukkit.getServer().getOnlinePlayers().length == 2) {
    12. Location loca = new Location(world1, 182, 1, 71);
    13. p.teleport(loca);
    14. Bukkit.broadcastMessage(ChatColor.GREEN + "FIGHT!!!");
    15. }
    16.  
    17. }
    18. }, 20 * 10);
    19. } else {
    20. if(Bukkit.getServer().getOnlinePlayers().length == 2) {
    21. Bukkit.broadcastMessage("You will be teleported in 10 seconds...");
    Necrodoom
     
  5. Offline

    Necrodoom

    Schaakmatth ...Which you have put inside the else.
     
  6. Offline

    Schaakmatth

    ok heres the code again:
    Code:java
    1. } else {
    2. if(Bukkit.getServer().getOnlinePlayers().length == 2) {
    3. Bukkit.broadcastMessage("You will be teleported in 10 seconds...");
    4. } else {
    5. //wait
    6. if(Bukkit.getServer().getOnlinePlayers().length == 1) {
    7. Bukkit.broadcastMessage(ChatColor.RED + "Need 1 Player");
    8.  
    9. }
    10. }
    11.  
    12.  
    13.  
    14.  
    15. }
    16. }


    forget to tahg Necrodoom post above

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  7. Offline

    Necrodoom

    Schaakmatth Please read a java tutorial on how if elses work, because it doesnt seem like you understand them.
     
  8. Offline

    Schaakmatth

    changed the code but i still get a error maybe its better to say whats not right Necrodoom
    Code:java
    1. } else if(Bukkit.getServer().getOnlinePlayers().length == 2) { //error on else syntax error on else { expected
    2. Bukkit.broadcastMessage("You will be teleported in 10 seconds...");
    3.  
    4. } else if(Bukkit.getServer().getOnlinePlayers().length == 1) {
    5. //wait
    6. Bukkit.broadcastMessage(ChatColor.RED + "Need 1 Player");
     
  9. Schaakmatth It's hard to say what's not right - firstly, you're only posting part of the code there, have you changed any of the parts you haven't posted? Secondly, the main problem is, as Necrodoom pointed out, that you don't really understand how if-then-else statements work, which is a pretty basic Java knowledge. You need to learn this, otherwise we can't help you. It's possible that you'll get it right eventually by following this randomly moving things around strategy, but it will be luck and you won't know how to replicate it. Please just learn Java? I promise it will make things easier. :)
     
  10. Offline

    Schaakmatth

    ok im gonna learn if elses but the code is not changed so if you only write the problem im gonna write it in code ok? AdamQpzm
     
  11. Schaakmatth The problem is that your else matches nothing. It needs to be the first statement after an if statement, or it needs to not exist.
     
  12. Offline

    Schaakmatth

    i followed a few tutorials but still dont know i always get that error on else why? AdamQpzm
     
  13. Schaakmatth As I said, it matches nothing. Don't just read a tutorial to try and solve your problem, learn Java. :)
     
  14. Offline

    Schaakmatth

  15. Offline

    Azubuso

    Schaakmatth
    As harsh as it may sound, you don't seem to have a fundamental understanding of Java either, not just how to use else/if statements. You also need to learn how to properly look over your code and find errors on your own, which I honestly wonder how you miss, since every IDE out there will correct the errors you're getting. Especially these little things, where all you're doing wrong is misplacing if/else statements.
     
    AdamQpzm likes this.
  16. Offline

    Schaakmatth

    nice all that feedbacks but why i always get that error WHY?! Azubuso
     
  17. Schaakmatth Your else does not come right after an if! Ugh!
     
  18. Offline

    Azubuso

    Schaakmatth Hover over the highlighted error...It literally will tell you what's going wrong.
     
  19. Offline

    Schaakmatth

    Syntax error on token "else" { expected Azubuso

    thats the error in the code AdamQpzm post above

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  20. Offline

    1Rogue

  21. Offline

    Schaakmatth

    i am dutch not so good with english so this is not working for me

    please spoonfeed 1Rogue

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  22. Offline

    1Rogue


    No
     
    MrEinStain, Necrodoom and AdamQpzm like this.
  23. Offline

    Schaakmatth

    please cant understand you :( 1Rogue
     
  24. Offline

    1Rogue

    Use a translator, such as http://translate.google.com/

    This is an english site, I can't really do much more than explain what I already have. Those tutorials explain everything in excruciating detail, which you can also throw in a translator.
     
  25. Offline

    ZRC2011

    If this is still an issue the way I generally go about this would be to:

    Code:java
    1. OfflinePlayer[] allPlayers = this.getServer().getOfflinePlayers();
    2. List<Player> onlinePlayers = new ArrayList<Player>();
    3.  
    4. for (OfflinePlayer offPlayer : allPlayers)
    5. {
    6. if (offPlayer.isOnline())
    7. {
    8. Player p = (Player)offPlayer;
    9. onlinePlayers.add(p);
    10. }
    11. }


    and then where ever you need this, compare against your onlinePlayers List.

    Also on player join (Look into Listeners if you don't know how to do this) and add it to the List
    and again on player leave (Same as above) remove the player from the list
     
  26. ZRC2011 That's a horrible way to do it when there's nothing wrong with Bukkit.getOnlinePlayers()
     
  27. Offline

    ZRC2011

    Considering he said it doesn't work, I gave an alternative. Plus its not Bukkit.getOnlinePlayers()

    this.getServer().getOnlinePlayers()

    Wow learn java
     
  28. ZRC2011 You obviously didn't actually look at the thread - don't assume that the person asking for help is fully accurate with their title. The actual problem was with his syntax.

    Was the learn Java comment aimed at me? It would be learn the Bukkit API, not Java, since getOnlinePlayers() isn't a part of Java. I would also say that you should learn it yourself since Bukkit.getOnlinePlayers() is perfect valid. In fact, your suggestion for this.getServer().getOnlinePlayers() won't work in some cases in which Bukkit.getOnlinePlayers() will, and Bukkit.getOnlinePlayers() will work in all cases in which this.getServer().getOnlinePlayers()

    Next time you see someone refer to a method that you have never heard of before, especially when the person is actually fairly experienced with the API, it would be best to have a quick glance at the JavaDocs to make sure that it doesn't exist, rather that just falsely asserting that it doesn't :)
     
    Gnat008 likes this.
  29. Offline

    ZRC2011

    Not being funny but I'm currently using that exact method and it works perfectly fine.
     
  30. Offline

    Schaakmatth

    thanks but now is the problem elses so nobody wants to help me Adam i really dont understand you and rogue1 im dutch and do not understand english good AdamQpzm
     
Thread Status:
Not open for further replies.

Share This Page