Bukkit get online players get only 1 player?

Discussion in 'Plugin Development' started by EnchantedMiners, Aug 20, 2014.

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

    EnchantedMiners

    Hello, i am having a problem with my code specially when i loop all bukkit players
    as it says its suppose to get all players and send them message or give items to them but when i do that it only give or send message to one of the players online a random one when i leave and join it get another player idk why is that... here is my code:
    NOTE: Everything is working except for the getonlineplayers
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("giveall")){
    2. if(args.length == 0){
    3. sender.sendMessage("§4Arguments must be: §c/giveall <ITEM_NAME> <AMOUNT 1-32>");
    4. } else if(args.length > 1){
    5. if(sender.hasPermission("prison.staff")){
    6. Material items = Material.getMaterial(args[0]);
    7. if(items != null){
    8. int amount = Integer.parseInt(args[1]);
    9. if(amount >= 1 && amount <= 32){
    10. for(Player player : Bukkit.getOnlinePlayers()){
    11. if(!player.hasPermission("prison.vip")){
    12. player.getInventory().addItem(new ItemStack(items, amount));
    13. player.sendMessage("§7§l>> §aYou have received §7[§dx§5" + amount + " §e" + items.toString().toUpperCase() + "§7] §afrom §c" + sender.getName());
    14. player.updateInventory();
    15. sender.sendMessage("§aYou sended the blocks successfully!");
    16. } else {
    17. player.getInventory().addItem(new ItemStack(items, amount * 2));
    18. player.sendMessage("§7§l>§2✪§7§l> §aYou have received §ex2 §7[§dx§5" + amount * 2 + " §e" + items.toString().toUpperCase() + "§7] §afrom §c" + sender.getName());
    19. player.updateInventory();
    20. sender.sendMessage("§aYou sended the blocks successfully!");
    21. }
    22. }
    23. } else {
    24. sender.sendMessage("§4Wrong item amount!");
    25. }
    26. } else {
    27. sender.sendMessage("§4Wrong item name!");
    28. }
    29. } else {
    30. sender.sendMessage(ChatColor.RED + sender.getName() + " §4silly player you cant do this :D");
    31. }
    32. }
    33. return true;
    34. }
     
  2. Offline

    unon1100

    EnchantedMiners

    new ArrayList(Bukkit.getOnlinePlayers()).get(index);

    Another way you can do it is CraftBukkit.getOnlinePlayers if you compile with CraftBukkit
     
  3. Offline

    EnchantedMiners

    unon1100 wait how would i use the array one one my code ?
     
  4. Offline

    Garris0n

    ...what?

    @OP Print out the result of getOnlinePlayers and post what it says.
     
  5. Offline

    mythbusterma

    unon1100

    Why unnecessarily copy the Collection? Iterating over Bukkit.getOnlinePlayers() works just fine, he's doing something else wrong.
     
  6. Offline

    EnchantedMiners

    Garris0n can you tell me another way of getting all online players so my code works correct?

    yes that's what i though too i think i have something wrong there but i have no idea never seen this before and is weird because if i log out and login it chose a random player sometimes me sometimes another player,
    Maybe because i am using Spigot ?

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

    mbaxter ʇıʞʞnq ɐ sɐɥ ı

    Seek support where you acquired your server mod.
     
Thread Status:
Not open for further replies.

Share This Page