Solved getOnlinePlayers() doesnt work!?

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

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

    Schaakmatth

    Just wanna check how much players there are online but it doesn't work?!
    code:
    Code:java
    1. Player p = (Player) sender;
    2. if (Bukkit.getServer().getOnlinePlayers().length == 1) {
    3. p.sendMessage(ChatColor.RED + "need more players!");
    4. } else {
    5.  
    6. Bukkit.getScheduler().runTaskLater(this, new Runnable() {
    7.  
    8. @Override
    9. public void run() {
    10. Player p = (Player) sender;
    11. if (Bukkit.getServer().getOnlinePlayers().length == 2) {
    12. Bukkit.broadcastMessage(ChatColor.GREEN + "DEBUG: WORKING");
    13. World world = p.getWorld();
    14. Location loc = new Location(world, 24, 1, 101);
    15. p.teleport(loc);
    16. }
    17.  
    18. }
    19.  
    20. }, 20 * 5);

    when i typ /bpjoin i get teleported with 1 second but it doesnt teleport me or send a broadcastMessage?
     
  2. Offline

    SnelleFrikandel

  3. Offline

    Schaakmatth

  4. Offline

    1Achmed1

  5. Offline

    fireblast709

    What exactly doesn't work...
    1Achmed1 learn how anonymous classes work ;3
     
  6. Offline

    itzrobotix

    Code:java
    1. final Player p = (Player) sender;
    2. final World world = p.getWorld();
    3. if (Bukkit.getServer().getOnlinePlayers().length == 1) {
    4. p.sendMessage(ChatColor.RED + "Need more players!");
    5. }
    6.  
    7. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    8. public void run() {
    9. if(Bukkit.getOnlinePlayers().length > 1) {
    10. Bukkit.broadcastMessage(ChatColor.GREEN + "DEBUG: WORKING");
    11. Location loc = new Location(world, 24.0, 1.0, 101.0);
    12. p.teleport(loc);
    13. }
    14. }
    15. }, 20 * 5);

    My attempt to tidy it up, if it doesn't work now, check you have got the commands in the plugin.yml and your { } are correct.
     
  7. Offline

    Niknea

    Schaakmatth add .length(); to the end of your code line.
     
  8. Offline

    Schaakmatth

    Ok gonna test it out
     
  9. Offline

    JimsHD

    Schaakmatth Getting the size of the online players:
    Code:java
    1. for(Player pl : Bukkit.getOnlinePlayers()){
    2. if(pl.equals(1)){
    3. //do stuff
    4. }
    5.  
     
  10. Offline

    Schaakmatth

    doesnt work this is my code:
    Code:java
    1. } else {
    2. final Player p = (Player) sender;
    3. final World world = p.getWorld();
    4. for(Player pl : Bukkit.getOnlinePlayers()) {
    5. if(pl.equals(1)) {
    6. Bukkit.broadcastMessage(ChatColor.RED + "Need 1 more player");
    7. }
    8.  
    9. }
    10.  
    11. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    12.  
    13. @Override
    14. public void run() {
    15. for(Player pl : Bukkit.getOnlinePlayers()) {
    16. if(pl.equals(2)) {
    17. Bukkit.broadcastMessage(ChatColor.GREEN + "DEBUG: working!");
    18. Location loc = new Location(world, 24.0, 1.0, 101.0);
    19. p.teleport(loc);
    20. }
    21. }
    22.  
    23. }
    24.  
    25. }, 20 * 5);
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35. return true;
    36.  
    37. }
    38. return true;
    39. }
    40. }


    JimsHD Niknea comment above

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

    Azubuso

    Schaakmatth See if this works for you
    PHP:
    *snip*
    EDIT: Removed spoonfeed.
     
  12. Offline

    JimsHD

    Schaakmatth
    Code:java
    1. if (pl.length() > 0) {
    2. //code
    3. }
     
  13. Offline

    Schaakmatth

  14. Offline

    JimsHD

  15. Offline

    Schaakmatth

  16. Offline

    Garris0n

    What is with all the spoonfeeding around here?

    Anyway, @OP, what doesn't work?

    "It doesn't work" is not at all descriptive.
     
    MayoDwarf and Gamecube762 like this.
  17. Offline

    Tomasko

    Code:java
    1. if(Bukkit.getServer().getOnlinePlayers().length > 1) {
    2. //start game
    3. } else {
    4. throw new Exeption("Only 1 player can't play game");
    5. }

    Try edit your first code :)
     
  18. Offline

    Necrodoom

    Oh my goodness.

    JimsHD please learn Java before trying to teach others. You are attempting to check the amount of players by checking if a random player object equals 1.. What.

    itzrobotix Azubuso Tomasko did any of you even made sure that you know what is the problem is? You don't even know what isn't working for OP.

    Schaakmatth go back to your code in the very first post, and explain what exactly is the problem, as Garris0n asked.
     
  19. Offline

    Azubuso

    Necrodoom Hmm...well this is embarrassing.
     
  20. Offline

    JimsHD

  21. Offline

    Schaakmatth

    ok that works but when someone join nothing happends so when someone join nothing happends...
    Code:java
    1. @EventHandler
    2. public void Join(final 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. //wait
    9. if(Bukkit.getServer().getOnlinePlayers().length == 1) {
    10. Bukkit.broadcastMessage(ChatColor.RED + "Need 1 Player");
    11.  
    12.  
    13. //waitTimer
    14. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    15. public void run() {
    16. if(Bukkit.getServer().getOnlinePlayers().length == 1) {
    17. Location loca = new Location(world1, 182, 1, 71);
    18. p.teleport(loca);
    19. Bukkit.broadcastMessage(ChatColor.GREEN + "FIGHT!!!");
    20.  
    21. }
    22.  
    23. }
    24.  
    25. }, 20 * 1);
    26. //messageTimer
    27. if(Bukkit.getServer().getOnlinePlayers().length == 2) {
    28. Bukkit.broadcastMessage("You will be teleported in 10 seconds...");
    29. }
    30. }
    31. }
    32.  
    33.  
    34. }
     
  22. Offline

    Necrodoom

  23. Offline

    Schaakmatth

    forget to say you will get the message need 1 player so that works but if 1 of my friends join nothing will happends Necrodoom
     
  24. Offline

    Necrodoom

    Your if on line 9 warps the whole timer so the code will only execute if only one player is in.
     
  25. Offline

    Schaakmatth

    so i have to place that to the place at the bottom?

    forget to tahg post above Necrodoom

    im out of ideas please help!

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

    Necrodoom

    You include the timer in the else of the if.
     
  27. Offline

    Schaakmatth

    ok i tried to set elses but get a red line under else Necrodoom
    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.  
    20. } else { //red line under else: syntax error on else delete this token.
    21.  
    22. //waitMessage
    23. if(Bukkit.getServer().getOnlinePlayers().length == 2) {
    24. Bukkit.broadcastMessage("You will be teleported in 10 seconds...");
    25. } else {
    26. //wait
    27. if(Bukkit.getServer().getOnlinePlayers().length == 1) {
    28. Bukkit.broadcastMessage(ChatColor.RED + "Need 1 Player");
    29.  
    30. }
    31. }
    32. }
    33.  
    34.  
    35. @EventHandler
    36. public void DeathLobby(PlayerDeathEvent ev) {
    37. Player killed = ev.getEntity();
    38. Bukkit.broadcastMessage(ChatColor.WHITE + killed.getDisplayName() + ChatColor.RED + "is killed!");
    39. World world = killed.getWorld();
    40. Location locat = new Location(world, 112, 1, 66);
    41. killed.teleport(locat);
    42.  
    43.  
    44. }
    45. }
     
  28. Offline

    Necrodoom

    I did not say include the if itself in an else.
    Do you know how if-else works?
    You want to do so that if the length is more than 1, then run the wait timer code. So do so.
    If you cant understand it, i recommend reading about how if-elses work.
     
  29. Offline

    Schaakmatth

    really dont know how to place that else

    what the F*CK does i need to do something like this but i get a error :
    Code:java
    1. } else if(Bukkit.getServer().getOnlinePlayers().length == 2) {
    2. Bukkit.broadcastMessage("You will be teleported in 10 seconds...");

    so something like that? Necrodoom

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page