Run code only if 1+ players are on

Discussion in 'Plugin Development' started by x86cam, May 14, 2014.

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

    x86cam

    I am trying to run a scheduler upon start, but it will only echo a message IF there is one or more players on the server.

    I looked all over the web for something like this, but no help.

    I know how to get all of the online players, but none for if at least one player is on.

    My code for onEnable {
    Code:java
    1. public void onEnable(){
    2. loadConfiguration();
    3. long repeatTime = getConfig().getLong("announce.minutes");
    4. getLogger().info(prefix + pName + " version " + v + " has been enabled.");
    5. boolean repeat = getConfig().getBoolean("announce.enable");
    6. if(repeat) {
    7. runScheduler(repeatTime);
    8. }
    9. }

    }

    Here is my code for the scheduler{
    Code:java
    1. public void runScheduler(long repeatMinutes) {
    2. long repeatToTicks = repeatMinutes * 60 * 20;
    3. Bukkit.getScheduler().runTaskTimerAsynchronously(this, new Runnable(){
    4. @Override
    5. public void run() {
    6. String host = getConfig().getString("host");
    7. String port = getConfig().getString("port");
    8. String mount = getConfig().getString("mount");
    9. String radioName = getConfig().getString("radio-name");
    10. Bukkit.broadcastMessage(ChatColor.GRAY + prefix + "Information on " + radioName);
    11. Bukkit.broadcastMessage(ChatColor.GRAY + "====================================");
    12. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
    13. player.playSound(player.getLocation(), Sound.NOTE_PIANO, 10, 1);
    14. }
    15.  
    16. try {
    17. Scraper scraper = new ShoutCastScraper();
    18. List<Stream> streams = scraper.scrape(new URI("http://"+host+":"+port+"/"+mount));
    19. for (Stream stream: streams) {
    20. if(getConfig().getBoolean("show.song")){
    21. Bukkit.broadcastMessage(ChatColor.YELLOW + "Song: " + stream.getCurrentSong());
    22. }
    23. if(getConfig().getBoolean("show.listeners")){
    24. //String listenerCountOld = "" + stream.getCurrentListenerCount();
    25. //String listenerCountNew = listenerCountOld.replace("-", "");
    26. //Bukkit.broadcastMessage(ChatColor.GREEN + "Listeners: " + listenerCountNew);
    27. }
    28. if(getConfig().getBoolean("show.genre")){
    29. Bukkit.broadcastMessage(ChatColor.GREEN + "Listeners: " + stream.getGenre());
    30. }
    31. if(getConfig().getBoolean("show.uri")){
    32. Bukkit.broadcastMessage(ChatColor.GREEN + "Listeners: " + stream.getUri());
    33. }
    34. }
    35. String listenLink = getConfig().getString("listen-link");
    36. if(getConfig().getBoolean("show.listenlink")){
    37. Bukkit.broadcastMessage(ChatColor.AQUA + "To listen, click the link: "+ ChatColor.UNDERLINE + listenLink);
    38. }
    39. Bukkit.broadcastMessage(ChatColor.BLUE + "To show this whenever, type /radio");
    40. } catch (ScrapeException e) {
    41. e.printStackTrace();
    42. Bukkit.broadcastMessage(ChatColor.RED+"[MineRadio] You have configured this plugin incorrectly.");
    43. Bukkit.broadcastMessage(ChatColor.RED+"[MineRadio] Modify the configuration file.");
    44. Bukkit.broadcastMessage(ChatColor.RED+"[MineRadio] When you are done, type '/radio reload'");
    45. } catch (URISyntaxException e) {
    46. e.printStackTrace();
    47. Bukkit.broadcastMessage(ChatColor.RED+"[MineRadio] You have configured this plugin incorrectly.");
    48. Bukkit.broadcastMessage(ChatColor.RED+"[MineRadio] Modify the configuration file.");
    49. Bukkit.broadcastMessage(ChatColor.RED+"[MineRadio] When you are done, type '/radio reload'");
    50. }
    51. }
    52. }, 1, repeatToTicks);
    53. }

    }

    Any help?
     
  2. Offline

    hofma100

    If you want to run a task if one or more players are online, then the code below should work for you.
    Code:java
    1. if (Bukkit.getServer().getOnlinePlayers().length >= 1) {
    2. //Do your stuff here...
    3. }
     
Thread Status:
Not open for further replies.

Share This Page