Targeting everyone.

Discussion in 'Plugin Development' started by xNamesi, Nov 23, 2014.

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

    xNamesi

    Hi so i have me some code that gives players that just joined the server items but how would i make my code so when a player joins it gives ALL the players on the server an item?
     
  2. Offline

    Funergy

    xNamesi You loop through the online players with a for loop and then give them the item
    I will give you a little thing.
    for(Player p : Bukkit.getOnlinePlayers()){}
     
  3. Offline

    xNamesi

    Heres my code
    Code:java
    1. //Player checking and countdown
    2. if(playerAmound >= 2) {
    3. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    4. public void run() {
    5. Player p = e.getPlayer();
    6. String players = Bukkit.getOnlinePlayers();
    7. p.setLevel(countdown);
    8. countdown = countdown - 1;
    9. int timeleft = countdown + 1;
    10. //Countdown 10 seconds left
    11. if(countdown < 10) {
    12. Bukkit.broadcastMessage(ChatColor.RED + "Game starting in " + ChatColor.GOLD + timeleft + ChatColor.RED + " seconds!");
    13.  
    14. Player[] Me = Bukkit.getOnlinePlayers();
    15.  
    16. //If counter hits 0
    17. if(countdown <= 0) {
    18. Bukkit.getScheduler().cancelAllTasks();
    19. p.setLevel(0);
    20. Bukkit.broadcastMessage(ChatColor.RED + "The game has now " + ChatColor.GOLD + "Started! " + ChatColor.RED + "Go, go, go.");
    21.  
    22. //Heal Player
    23. p.setFoodLevel(100);
    24. p.setHealth(20);
    25.  
    26. //Give player items
    27. ItemStack wSword = new ItemStack(Material.WOOD_SWORD, 1);
    28. p.getInventory().setItem(0, wSword);
    29. wSword.addEnchantment(Enchantment.ARROW_INFINITE , 1);
    30. ItemStack iBow = new ItemStack(Material.BOW, 1);
    31. p.getInventory().setItem(1, iBow);
    32.  
    33.  
    34.  
    35. }
    36. }
    37. }
    38. }, 20L, 20L);


    where it gives the players snone sword and bow i need it to give it to all the players?
     
  4. Offline

    mrCookieSlime

    xNamesi
    Funergy has already provided you all the Code you need for that.
    Just replace the Variable p with the for-loop.
     
  5. Offline

    xNamesi

    I replaced it but it dosnt work still.
     
  6. Offline

    mrCookieSlime

    xNamesi
    It should work.
    Please post your Code.
     
  7. Offline

    xNamesi

    //Player checking and countdown
    if(playerAmound >= 2) {
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    public void run() {
    Player p = e.getPlayer();
    for(Player pl : Bukkit.getOnlinePlayers()){
    p.setLevel(countdown);
    countdown = countdown - 1;
    int timeleft = countdown + 1;
    //Countdown 10 seconds left
    if(countdown < 10) {
    Bukkit.broadcastMessage(ChatColor.RED + "Game starting in " + ChatColor.GOLD + timeleft + ChatColor.RED + " seconds!");



    //If counter hits 0
    if(countdown <= 0) {
    Bukkit.getScheduler().cancelAllTasks();
    p.setLevel(0);
    Bukkit.broadcastMessage(ChatColor.RED + "The game has now " + ChatColor.GOLD + "Started! " + ChatColor.RED + "Go, go, go.");

    //Heal Player
    pl.setFoodLevel(100);
    pl.setHealth(20);

    //Give player items
    ItemStack wSword = new ItemStack(Material.WOOD_SWORD, 1);
    pl.getInventory().setItem(0, wSword);
    ItemStack iBow = new ItemStack(Material.BOW, 1);
    pl.getInventory().setItem(1, iBow);
    iBow.addEnchantment(Enchantment.ARROW_INFINITE , 1);



    }

    }

    }
    }
    }, 20L, 20L);
    }else {
    Bukkit.broadcastMessage(ChatColor.RED + "Players are still needed to play the game");
    }
     
  8. Offline

    mrCookieSlime

    xNamesi
    Please remove the Variable p and use pl instead.
     
  9. Offline

    xNamesi

    That dosnt make a difference?
     
  10. Offline

    mrCookieSlime

  11. Offline

    xNamesi

    It dosnt work because i removede it and tried
     
  12. Offline

    mrCookieSlime

    xNamesi
    Why did you remove your Code ?.?
     
  13. Offline

    xNamesi

    What, i didnt remove my code i removed the p variable like you said but it still dosnt work.
     
  14. Offline

    mrCookieSlime

  15. Offline

    Funergy

Thread Status:
Not open for further replies.

Share This Page