How to broadcast a message when reached player limit

Discussion in 'Plugin Development' started by Crafted Evil, Jan 4, 2014.

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

    Crafted Evil

    Hello, I've been trying to make a plugin when there is a certain amount of players online a message will broadcast but I can't seem to get it to work. Can anyone help me? :c
    Code:java
    1. package craftedevil.plugin;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.PlayerJoinEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.scheduler.BukkitRunnable;
    10.  
    11. public class PlayerCountDown extends JavaPlugin implements Listener {
    12.  
    13. int countdown;
    14. int id;
    15.  
    16. @EventHandler
    17. public void OnPlayerJoinEvent(PlayerJoinEvent e) {
    18. if(Bukkit.getServer().getOnlinePlayers().length >= 1);
    19. getServer().broadcastMessage(ChatColor.DARK_GREEN + "" + ChatColor.BOLD + "[" + ChatColor.YELLOW + "Plugin" + ChatColor.DARK_GREEN + "" + ChatColor.BOLD + "]" + ChatColor.GRAY + "Lobby mode will end in 30 seconds.");
    20. if(Bukkit.getServer().getOnlinePlayers().length >= 1);
    21. getServer().broadcastMessage(ChatColor.GREEN + "Started");
    22. countdown = 10;
    23. id = getServer().getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
    24. @Override
    25. public void run() {
    26. getServer().broadcastMessage(ChatColor.DARK_GREEN + "" + ChatColor.BOLD + "[" + ChatColor.YELLOW + "Plugin" + ChatColor.DARK_GREEN + "" + ChatColor.BOLD + "]" + ChatColor.DARK_GRAY + "[" + countdown + ChatColor.DARK_GRAY + "]");
    27. if(countdown == 0) {
    28. Bukkit.getScheduler().cancelTask(id);
    29. getServer().broadcastMessage(ChatColor.GREEN + ".o.");
    30. }
    31. countdown--;
    32. }
    33. }, 0L, 20L);
    34. }
    35.  
    36. }
     
  2. Offline

    Dako

    Something like this?

    Code:java
    1.  
    2. @EventHandler
    3. public void onJoin(PlayerJoinEvent event){
    4. switch(Bukkit.getServer().getOnlinePlayers().length){
    5.  
    6. case 10: Bukkit.broadcastMessage("10 Players are online! Wow!");
    7. break;
    8.  
    9. case 11: Bukkit.broadcastMessage("11 Players are online! One More<3");
    10. break;
    11. }
    12. }


    case 10: means 10 players. case 1: would mean 1 player:).
     
    Crafted Evil likes this.
  3. Offline

    Crafted Evil

    Dako Didn't work :c
     
  4. Offline

    sgavster

    Crafted Evil Because your events aren't registered.....
    Code:java
    1. public void onEnable() {
    2. Bukkit.getPluginManager().registerEvents(this, this);
    3. }
     
    Crafted Evil likes this.
  5. Offline

    Crafted Evil

    sgavster Derp slurp, forgot to do that >.>
     
  6. Offline

    Dako

    saw this now :D
     
    Crafted Evil likes this.
  7. Offline

    Crafted Evil

  8. Offline

    Dako

    after a case is ended you have to write break; or return;(ends void)

    Example
    Code:java
    1. switch(Bukkit.getServer().getOnlinePlayers().length){
    2. case 1: getServer().broadcastMessage(ChatColor.GREEN + "Started");
    3. id = getServer().getScheduler().scheduleSyncRepeatingTask(this, new BukkitRunnable() {
    4. @Override
    5. public void run() {
    6. getServer().broadcastMessage(ChatColor.GOLD + "Time: " + countdown);
    7. if(countdown == 0) {
    8. Bukkit.getScheduler().cancelTask(id);
    9. }
    10. countdown--;
    11. }
    12. }, 0L, 20L);
    13. break;
    14.  
    15. case 2: .....
    16. break;
    17. }
     
  9. Offline

    Crafted Evil

  10. Offline

    PhilipsNostrum

    You can just use if if you only have a condition, switch is normally used when you have a bunch of them
     
  11. Offline

    BenRush

    You're talking about a record? When a new record of people are online then it broadcasts to server and put new record to config or somewhere?
     
  12. Offline

    Crafted Evil

    No, when a certain amount of people are online it should broadcast a message.
     
  13. Offline

    BenRush

    and amount of certain people online should be in config?
     
  14. Offline

    Crafted Evil

    BenRush No, when the desired player number is reached a message is broadcasted and a countdown to display another message begins.
    Code:
    switch(Bukkit.getServer().getOnlinePlayers().length){
            case 1:    getServer().broadcastMessage(ChatColor.GREEN + "Started");
     
  15. Offline

    BenRush

    maybe something like this?
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent event){
    3. int num= 0;
    4. for(int i = 0; i < Bukkit.getServer().getOnlinePlayers().length; i++) {
    5. num= i;
    6. }
    7. if(num == 1){
    8. getServer().broadcastMessage("GOOD");
    9. }
    10. }
     
  16. Offline

    PhilipsNostrum

    That for loop is completely unnecessary just do if(getServer().getOnlinePlayers().length() == (your number){
    //do something
    }
     
  17. Offline

    Crafted Evil

  18. Offline

    BenRush

    Your error is NPE, its about this line:
    Code:java
    1. case 1: getServer().broadcastMessage(ChatColor.GREEN + "Started");

    try using PhilipsNostrum example.
     
  19. Offline

    Crafted Evil

    Bump, still dosen't work.
     
Thread Status:
Not open for further replies.

Share This Page