Solved How to execute command for everyone?

Discussion in 'Plugin Development' started by monkafynix, Dec 14, 2019.

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

    monkafynix

    Code:
    public class HalfHeartChallenge implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(sender instanceof Player) {   
                    Player player = (Player) sender;
                        player.setGameMode(GameMode.SURVIVAL);
                        player.setMaxHealth(1);   
                               
            }
           
            return false;
        }
    
    }
    I just want to execute this for everyone with kind of like
    Code:
    if(args[0].equalsIgnoreCase("@a"){
    
    allPlayer.setGameMode(...);
    allPlayer.setMaxHealth(1);
    
    
    }
     
  2. Offline

    knokko

    You can get a collection of all online players with Bukkit.getOnlinePlayers(). Then loop over every player and invoke the methods you need to invoke.
     
    Strahan likes this.
  3. Offline

    monkafynix

    Can you give me an example how I could do that , I' m nearly new to coding.
     
  4. Offline

    Strahan

    You should learn basic Java before attempting to muck with the API. But that said..

    A collection is an object that holds multiple pieces of data. If you do String name = "Strahan", that's a String variable. A single piece of data. If you do List<String> names = new ArrayList<String>() you have a "list" of Strings. That variable can hold many Strings. There are many other collections, that's just one.

    A loop means to process something a certain amount of times. Or infinitely, depending on how you build the loop. If you wanted to count to 10, for example, you could do for (int x=1; x<=10; x++) player.sendMessage("Number: " + x); That's a basic loop. In the for loop you set the initial counting variable, then you set the criteria for the end of the loop, then you set what to do with the counter for each loop. In this case, ++ means take the current value and add one. I could also do -- to decrement or x += 5 to add by 5 each time, etc etc.

    For the loop you need, you won't be using a counter. You'll use what other languages call a foreach loop which means foreach item in collection. Java doesn't call it that though. The collection is Bukkit.getOnlinePlayers(). The format for a Java foreach is for (DataType variable : collection). getOnlinePlayers stores Player objects, so Player is your data type. So you would do:

    Code:
    for (Player p : Bukkit.getOnlinePlayers()) {
    // do something
    }
    Within the { } of your loop, the variable you designated will have the data for the current item. In this case, "p" will be the current player. For example, this code:

    Code:
    for (Player p : Bukkit.getOnlinePlayers()) {
      p.sendMessage("Hi!");
    }
    will send each player a "Hi!" message. Now knowing that, you should be able to adapt that to your use.

    PS - one last thing, don't try to remove items from a collection that you are looping. So if you have, say, a List of Player objects named "players" and want to remove any who aren't in survival, don't do this:

    Code:
    for (Player p : players) {
      if (p.getGameMode() == GameMode.SURVIVAL) continue;
      players.remove(p);
    }
    That will crash it. You can't modify a list you are iterating over. To do that, you'd use an iterator:

    Code:
    Iterator<Player> it = players.iterator();
    while (it.hasNext()) {
      Player p = (Player)it.next();
      if (p.getGameMode() == GameMode.SURVIVAL) continue;
      it.remove();
    }
    I won't get into explaining the nuts and bolts of that, you can Google if you want to read more. Just wanted to mention that so you don't try modifying your collection whilst looping.
     
  5. Offline

    monkafynix

    Wow you just teached more then every other Video. Thx mate.
     
Thread Status:
Not open for further replies.

Share This Page