How can i get a false if the array list is empty?

Discussion in 'Plugin Development' started by Funky StylzZz, Apr 15, 2014.

Thread Status:
Not open for further replies.
  1. Hey Guys what i want to make is a little support command that informs a group of player with a specific permission if someone type in /support but if no one with the permission is online the player shall get a messager that no Supporter is online and the he should open a support ticket.

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. Player p = null;
    4. if(sender instanceof Player){
    5. p = (Player) sender;
    6. }
    7.  
    8. if(p!= null) {
    9.  
    10. if(args.length == 0) {
    11.  
    12. p.sendMessage("A Supporter has ben informed about your request!");
    13.  
    14. for(Player players : Bukkit.getServer().getOnlinePlayers()) {
    15. if(players.hasPermission("supportix.supporter")) {
    16. players.sendMessage("The player" + p.getName() + " needs your help! Have a look on his request");
    17. }
    18. }
    19. }
    20. else {
    21. return false;
    22. }
    23. return true;
    24. }
    25. else {
    26. sender.sendMessage("This command can only executed by a Player!");
    27. return true;
    28. }
    29. }
    30.  
    31. }
    32.  



    This works fine. Now i tried to give the player another message when no "supporter" is online.

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    3. Player p = null;
    4. if(sender instanceof Player){
    5. p = (Player) sender;
    6. }
    7.  
    8. int supporters = 0;
    9. if(p!= null) {
    10.  
    11. if(args.length == 0) {
    12. if(supporters == 0) {
    13. //No supporters online
    14. }
    15. else {
    16. p.sendMessage("A Supporter has ben informed about your request!");
    17. }
    18. for(Player players : Bukkit.getServer().getOnlinePlayers()) {
    19. if(players.hasPermission("supportix.supporter")) {
    20. players.sendMessage("The player" + p.getName() + " needs your help! Have a look on his request");
    21. supporter++
    22. }
    23. }
    24. }
    25. else {
    26. return false;
    27. }
    28. return true;
    29. }
    30. else {
    31. sender.sendMessage("This command can only executed by a Player!");
    32. return true;
    33. }
    34. }
    35.  
    36. }


    I'm a beginner in java and i have no clue how to implement this. I hope you can help me :)

    regards Funky
     
  2. Offline

    coasterman10

    Your logic seems right except that you're calculating the number of supporters after checking. Just before if (supporters == 0) is where you should loop through all the players and total up the number of ones with the necessary permission.

    Speaking of ArrayLists, if you are using any Collection (which includes all Sets, Lists, etc), there is a convenient size() function which will tell you how many elements are in the collection.
     
  3. Looks good, here is a proper and simple solution ;)
    Code:java
    1.  
    2. ArrayList<Player> supporters = new ArrayList<Player>();
    3. for(Player players : Bukkit.getServer().getOnlinePlayers())
    4. {
    5. if(players.hasPermission("supportix.supporter"))
    6. supporters.add(players);
    7. }
    8. for(Player supporter : supporters)
    9. supporter.sendMessage("The player" + p.getName() + " needs your help! Have a look on his request");
    10. if(supporters.size() == 0)
    11. p.sendMessage("Sorry, no supporters online!");
    12. else
    13. p.sendMessage("Supporters have been informed!");
    14.  
     
  4. ohh Thanks!:) So my problem was that i checked the amount of supporters before the loop can increase the number of the supporter variable? And i looked up the size() function and i will use it next time Thanks for your supporting :D!!




    Thank you very much for your help :) I'll gettin better and better :))
     
Thread Status:
Not open for further replies.

Share This Page