Check how many players there are in a world, if a certain number of players, issue a command

Discussion in 'Plugin Development' started by Jabbers9999, Aug 30, 2015.

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

    Jabbers9999

    Hi,
    Am making plugin, need to check how many players are in a world, and if there are 2 players, it will teleport them.
    Here is my java code:
    Code:Java
    1.  
    2. package me.bukkit.jabbers9999;
    3.  
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.World;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14.  
    15. public class Survive extends JavaPlugin {
    16.  
    17. @Override
    18. public void onEnable() {
    19. getLogger().info("Survive has been enabled!");
    20. }
    21.  
    22.  
    23. @Override
    24. public void onDisable() {
    25.  
    26. }
    27.  
    28. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    29. if(commandLabel.equalsIgnoreCase("survjoin")){
    30. Player player = (Player) sender;
    31. World world = Bukkit.getWorld("survive");
    32. Location loc = new Location(world, -391.874,4,883.654);
    33. loc.add(0.5, 0, 0.5);
    34. loc.setPitch((float)4);
    35. loc.setYaw((float) -179.4);
    36. player.teleport(loc);
    37. player.sendMessage(ChatColor.BLUE + "Joining a game of survive...");
    38.  
    39. for(Player p : Bukkit.getWorld("survive").getPlayers()){
    40. p.sendMessage(sender.getName() + " has joined survive!");
    41.  
    42.  
    43. if (world.getPlayers().size() >= 2) {
    44. for(Player player1 : Bukkit.getWorld("survive").getPlayers()){
    45. player1.sendMessage ("round has begun!");
    46. }
    47.  
    48.  
    49.  
    50. }
    51.  
    52. }
    53.  
    54. }{
    55.  
    56.  
    57.  
    58.  
    59. }
    60. return true;}}
    61.  
    62.  

    Thanks
    Joey
     
    Last edited: Sep 17, 2015
  2. Offline

    au2001

    @Jabbers9999 Please. Don't sleep the main thread (especially for 15+ minutes...)

    Also, you might want to learn how to format Java code (or any langage actually), with indents and line breaks.

    You check for the the size of the players in the InterruptedException catch.
    Don't, just put the code after the try/catch (and remove the whole try/catch).

    And you don't do anything with the size of the players, try this:
    Code:
    if (world.getPlayers().size() >= 2) {
        // Do something
    } else {
        // Do something else
    }
    You can either execute a command (as the console) like that:
    Code:
    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "command without slash");
    or teleport the players like you did with Player#teleport(Location).
     
  3. Offline

    Jabbers9999

    @au2001, thanks, anyway I could split to players up? Could I possibly make it so player 1 teleports here player 2 teleports here, etc.
    Thanks
     
  4. I don't see how 1000 milliseconds (1 second) are 15+ minutes for you..
     
    teej107 likes this.
  5. Offline

    au2001

    @FisheyLP Oh, I thought it was in seconds...

    @Jabbers9999 Yes, create two locations, teleport payer1 to location1 and player2 to location2.
     
  6. Offline

    Jabbers9999

    @au2001 how would i define player 1 as player 1 and player 2 as player 2?
    Because normally you would just do:
    Code:
    player.teleport(<LOCATION>)
    
    Just saying, here is my code now:
    Code:Java
    1.  
    2. public class Survive extends JavaPlugin {
    3.  
    4.  
    5. @Override
    6.  
    7. public void onEnable() {
    8.  
    9. getLogger().info("Survive has been enabled!");
    10.  
    11. }
    12.  
    13.  
    14.  
    15. @Override
    16.  
    17. public void onDisable() {
    18.  
    19.  
    20. }
    21.  
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    24.  
    25. if(commandLabel.equalsIgnoreCase("survjoin")){
    26.  
    27. Player player = (Player) sender;
    28.  
    29. World world = Bukkit.getWorld("survive");
    30.  
    31. Location loc = new Location(world, -391.874,4,883.654);
    32.  
    33. loc.add(0.5, 0, 0.5);
    34.  
    35. loc.setPitch((float)4);
    36.  
    37. loc.setYaw((float) -179.4);
    38.  
    39. player.teleport(loc);
    40.  
    41. player.sendMessage(ChatColor.BLUE + "Joining a game of survive...");
    42.  
    43.  
    44.  
    45. for(Player p : Bukkit.getWorld("survive").getPlayers()){
    46.  
    47. p.sendMessage(sender.getName() + " has joined survive!");
    48.  
    49.  
    50.  
    51.  
    52.  
    53. if (world.getPlayers().size() >= 4) {
    54. //Here I would like it to count down from 20, then teleport player 1 to location 1 and player 2 to location 2.
    55. for(Player player1 : Bukkit.getWorld("survive").getPlayers()){
    56.  
    57. player1.sendMessage(sender.getName() + " round has begun!");
    58.  
    59. }
    60.  
    61.  
    62.  
    63.  
    64.  
    65.  
    66.  
    67. }
    68.  
    69.  
    70.  
    71. }
    72.  
    73.  
    74.  
    75. }{
    76.  
    77.  
    78.  


    Errrr ive changed the '4' to 2 amd ive got my friend to join. It does not send the message 'round has begun!'
    Code:
              if (world.getPlayers().size() >= 2) {
                    for(Player player1 : Bukkit.getWorld("survive").getPlayers()){
    
                            player1.sendMessage(sender.getName() + " round has begun!");
    
                        }
    
                              
    
                              
    
    
     
    Last edited: Aug 31, 2015
  7. Offline

    DuaneTheDev_

    @Jabbers9999

    Make sure you're importing world as "org.bukkit". Also sender.getName() only gets the player who sent the command. So upon reaching the world size the last person who joined will have their name displayed to everyone playing.
     
  8. Offline

    Jabbers9999

    @DuaneTheDev_
    I am importing all as bukkit. Here are all my imports:
    Code:
    import org.bukkit.Bukkit;
    
    import org.bukkit.ChatColor;
    
    import org.bukkit.Location;
    
    import org.bukkit.World;
    
    import org.bukkit.command.Command;
    
    import org.bukkit.command.CommandSender;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    So its not that. And I want it to count down from ten or something when the last player joins.
     
  9. Offline

    au2001

  10. Offline

    Jabbers9999

    @au2001 I have now formatted, so can you please tell me how to define player 1 as player 1 and player 2 as player 2
     
  11. Offline

    RoboticPlayer

    A) Remove your onEnable and onDisable, you don't need them for what you are doing.
    B) The reason you don't need your onEnable is because Bukkit automatically prints when the plugin is enabled.
    C) Use cmd.getName().equalsIgnoreCase instead of commandLabel.equalsIgnoreCase, this allows for aliases and is just good practice
    D) Don't assume that the sender is a player. Check before casting sender to player.
    E) Check to see if the world "survive" actually exists. If it doesn't right now, you will get an NPE.
    F) DON'T USE .bukkit for your package! Use me.(your name).(project name) instead. Using .bukkit implies that you own the domain, or you are a bukkit staff member.
     
  12. Offline

    Jabbers9999

    @henderry2019 Ok i have removed OnEnable and onDisable, and changed the package name, and changed the commandLabel.equalsIgnoreCase to cmd.getName() .gonna test it out.

    @henderry2019 Ok Thanks! But now it says (username) round has begun.
    What I want is 'Round has begun!' once to everyone on that world
    Here is my code:
    Code:Java
    1.  
    2. package me.Jabbers9999.Survive;
    3.  
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.World;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14.  
    15. public class Survive extends JavaPlugin {
    16.  
    17. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    18. if(cmd.getName().equalsIgnoreCase("survjoin")){
    19. Player player = (Player) sender;
    20. World world = Bukkit.getWorld("survive");
    21. Location loc = new Location(world, -391.874,4,883.654);
    22. loc.add(0.5, 0, 0.5);
    23. loc.setPitch((float)4);
    24. loc.setYaw((float) -179.4);
    25. player.teleport(loc);
    26. player.sendMessage(ChatColor.BLUE + "Joining a game of survive...");
    27.  
    28. for(Player p : Bukkit.getWorld("survive").getPlayers()){
    29. p.sendMessage(sender.getName() + " has joined survive!");
    30.  
    31.  
    32. if (world.getPlayers().size() >= 2) {
    33. for(Player player1 : Bukkit.getWorld("survive").getPlayers()){
    34. player1.sendMessage ("round has begun!");
    35. }
    36.  
    37.  
    38.  
    39. }
    40.  
    41. }
    42.  
    43. }{
    44.  
    45.  
    46.  
    47.  
    48. }
    49. return true;}}
    50.  
    51.  

    Thanks

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 17, 2015
  13. Offline

    RoboticPlayer

    A) Package name should be all lowercase
    B) You haven't actually done steps D or E from my original post.
     
  14. Offline

    Jabbers9999

    @henderry2019 The world survive does exist and can you give me some basic code for step D?
     
  15. Offline

    timtower Administrator Administrator Moderator

    @henderry2019 A) It is a naming convention, not a requirement, will work the same way regardless upper or lowercase.
    @Jabbers9999 instanceof
     
  16. Offline

    Jabbers9999

    @timtower So how would I use instanceof?
     
  17. Offline

    timtower Administrator Administrator Moderator

  18. Offline

    Jabbers9999

  19. Don't do the checks inside a loop, also you're looping again inside the loop.
     
  20. Offline

    timtower Administrator Administrator Moderator

    @Jabbers9999 Don't google "bukkit instanceof", try "java instanceof" instead.
     
  21. Offline

    DoggyCode™

    int i = 0;
    for(i; Bukkit.getServer().getWorld().getPlayers().size()){
    i++
    }
    if( > 4){
    //do stuff
    }
     
  22. Why do you use a for-loop?!
    Just use
    Code:
    if (world.getPlayers().size() > 4) {
     
  23. Offline

    Jabbers9999

    Thanks everyone for the support!
    I won't be needing the 'for(i; Bukkit.getServer().getWorld().getPlayers()' as I have switched to bungeecord.
    So I have switched to bukkit.broadcastMessage().

    @FisheyLP
    Err so yeah:) I just realised that switching to bungeecord would mean doing the /server command.
    Anyway of making the player issue that command, without having permissions or seeing the message?
    Thanks:)

    EDIT: I could just have bungeeportals, thats what I will probably do, ignore this post

    Also (One last thing, I promise) on the other server, it says 'An internal error occured while attempting to perform this command'. Any ideas? Also, on my console it came up with an error - http://pastebin.com/2mwtckpu
     
    Last edited by a moderator: Oct 13, 2015
  24. Offline

    boomboompower

  25. I wouldn't say that in the bukkit forums... (Angry moderators incoming that want to lock this thread because of bungeecord/offline mode in 3.. 2.. 1.. :p)
     
    Last edited: Oct 13, 2015
  26. Tactical Moderators inbound!

    Locked, offline mode is not supportd here.
     
    mine-care and DoggyCode™ like this.
Thread Status:
Not open for further replies.

Share This Page