I'm making a bedwars plugin for my minecraft server, actually i have to coding a generator for drop item every 1 secs, i have no idea how to make that, with internet i have done this code :
while(true) {
task = Bukkit.getServer().getScheduler().runTaskTimer(Main.plugin, () -> {
World w = null;
for(Player p : Bukkit.getServer().getOnlinePlayers()) {
w = p.getWorld();
}
String[] pos = Main.blue_spawn.split("\\*");
Location loc = new Location(w, Double.parseDouble(pos[0]), Double.parseDouble(pos[1]), Double.parseDouble(pos[2]));
w.dropItemNaturally(loc, new ItemStack(1));
}, 20, 20);
}
But i don't know how to drop iron ingot, beaucause do new ItemStack(Material.IRON_INGOT) just give me an error
Pls help
CodePudding user response:
You should NOT use while(true)
loop in minecraft. It will block the thread, which seems to be the main server thread. The scheduler is enough about staying running.
Then, it's not a good idea about the player loop for getting world. If someone is on the nether or something, it will not work. You can use Bukkit.getServer().getWorlds()[0]
or Bukkit.getServer().getWorld("world")
.
Also, the new ItemStack(Material.IRON_INGOT)
should work. Documentation about it.
Finally, the dropItemNaturally
method is good. You can see doc here