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?
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()){}
Heres my code Code:java //Player checking and countdown if(playerAmound >= 2) { Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { public void run() { Player p = e.getPlayer(); String players = 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!"); Player[] Me = Bukkit.getOnlinePlayers(); //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 p.setFoodLevel(100); p.setHealth(20); //Give player items ItemStack wSword = new ItemStack(Material.WOOD_SWORD, 1); p.getInventory().setItem(0, wSword); wSword.addEnchantment(Enchantment.ARROW_INFINITE , 1); ItemStack iBow = new ItemStack(Material.BOW, 1); p.getInventory().setItem(1, iBow); } } } }, 20L, 20L); where it gives the players snone sword and bow i need it to give it to all the players?
xNamesi Funergy has already provided you all the Code you need for that. Just replace the Variable p with the for-loop.
//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"); }
xNamesi I suggest you to read about for-loops and how they work here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html And it does make a difference. The Variable p represents the Player who joined. But the Variable pl represents all Players on the Server. So if you use pl instead, your Code will run for each Player on the Server.