I Need Help with the 'getOnlinePlayers()' Variable from Bukkit

Discussion in 'Plugin Development' started by DibDibs, Dec 6, 2014.

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

    DibDibs

    Hiya, I've recently started project called "ServerHeal", it is going to be a lightweight healing plugin but I'm stuck on a string of code!
    Code:
            else if(commandLabel.equalsIgnoreCase("HealAll")|| commandLabel.equalsIgnoreCase("HA"));
                if(args.length == 0){
                    Player targetPlayer = Server.getOnlinePlayers();
                    targetPlayer.setHealth(20);
                    player.sendMessage(ChatColor.RED + "You Have Been Healed by" + player);
         
                }
            }
    I am trying to get it so that when you say "HealAll" or "HA", it will restore all online players' health to 20!

    The full code is here @ pastebin

    PS: I know the plugin isn't named 'ServerHeal' and I know how to change that so please don't comment on that ;)
     
  2. Offline

    Skionz

    DibDibs Create an enhanced for loop that iterates through the online players and set their health to 20.
     
    es359 likes this.
  3. Offline

    teej107

    What's this?
    Code:
    Server.getOnlinePlayers();
    Server to be exact.
     
  4. Offline

    coasterman10

    "Server" doesn't exist. Did you just copy and paste this code from somewhere? It should be getServer().getOnlinePlayers() or Bukkit.getOnlinePlayers().

    Also, don't add enable/disable messages, that functionality is built into Bukkit.
     
  5. Offline

    Skionz

    DibDibs Just looked at your code. Stop watching TheBCBroz, there is so much wrong with it.
     
    coasterman10 likes this.
  6. Offline

    ZeusAllMighty11

    Code:
    for(Player p : Bukkit.getOnlinePlayers())
    {
        p.setHealth(p.getMaxHealth());
    }
     
  7. Offline

    es359

    Don't use commandLabel. Use cmd.getName().
     
  8. Offline

    DibDibs

    Is there, thanks, I was watching them for all of that code I did! Thanks for the heads up :)

    What do you mean by that?

    Sorry, I'm new to Java :/

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

    Skionz

    DibDibs I suggest you learn Java to a higher extent before starting with Bukkit or else nothing will make sense.
     
    teej107 likes this.
  10. Offline

    DibDibs

    Skionz Some stuff makes sense to me like variables etc. because I know other coding languages like Python & HTML
     
  11. Offline

    Unica

    DibDibs

    A for-loop is pretty basic. Therefor learning some basic java could help you in the future.
     
  12. Offline

    mug561

    DibDibs an Enhanced for loop well, i'll take a shot at trying to explain it:
    Bukkit.getOnlinePlayers is an array

    (a collection of objects, for example if i did : String[] test; id be making an array of strings, the square brackets delcar that imma be making an array. if i did test[1] = "hi"; id be saying the SECOND(Computers like to start counting at zero) String in this array is hi. Than i could do system.out.println(test[1]); and itd print out hi)

    As i was saying, Bukkit.getOnlinePlayers() is an array of players who are online.
    what an enhanced for loop does is it goes through a collection(In this case an array), and assigns it a variable.

    Heres a demo:
    Code:java
    1. for(Player p : Bukkit.getOnlinePlayers())
    2. p.sendMessage("hi");
    3.  
    4. //this will send every online player the message "hi"
    5. //sorry if i dis the caps wrong. just doing this off what i remember :L


    the first part (BEFORE the ":") is the variable in which you will temporarly store the players in the array.
    the second part(AFTER the ":") is the collection you will be getting the players from.

    When using an enhanced for loop it does not DELETE what is in the array, it simply gets what is in the collection.

    If that made any sense at all ._.

    If you need further clarification on the terms: "array" and "enhanced for loop" google it :I
     
Thread Status:
Not open for further replies.

Share This Page