I'm trying to set a block at a location right above ground level of where the player is standing. So if the player is flying, it would get the block right above ground level, and set a block at that location. Here's what I have now: Code:java int x = player.getLocation().getBlockX(); int y = player.getLocation().getBlockY(); int z = player.getLocation().getBlockZ(); World world = player.getLocation().getWorld(); Location loc = new Location(world, x, y, z); Material air = new ItemStack(Material.AIR).getType(); Block block = player.getWorld().getBlockAt(loc); if (block.getType() == air) { System.out.println("worked1"); Block below = block.getRelative(BlockFace.DOWN); if (below.getType() == Material.AIR) { for (int i = below.getY(); below.getType() == Material.AIR; i--) { System.out.println("worked2"); if (!(below.getType() == Material.AIR)) { below.setType(Material.CARPET); } Bukkit.getServer().getScheduler().runTaskLater(Main.getInstance(), new Runnable() { public void run() { below.setType(air); System.out.println("worked3"); } }, 20 * 10); } } else { block.setType(Material.CARPET); } Bukkit.getServer().getScheduler().runTaskLater(Main.getInstance(), new Runnable() { public void run() { block.setType(air); System.out.println("worked3"); } }, 20 * 10); } However, the loop just continues on forever and never stops, crashing the server. Any help is appreciated!
I'm gonna guess that below.getType() always returns AIR. Also you shouldn't be using a for loop if you are using a condition like that. Instead you would want to use a sentinel loop like while or do while. Edit: If all you are doing is trying to get the highest block at the player's position, simply use World.getHighestBlockAt(playerLocation);
Thanks, I'll look into using a while loop. I cant use getHighestBlockAt since they could be in a building, etc. New code with while loop: Code:java int x = player.getLocation().getBlockX(); int y = player.getLocation().getBlockY(); int z = player.getLocation().getBlockZ(); World world = player.getLocation().getWorld(); Location loc = new Location(world, x, y, z); Material air = new ItemStack(Material.AIR).getType(); Block block = player.getWorld().getBlockAt(loc); if (block.getType() == air) { System.out.println("worked1"); Block below = block.getRelative(BlockFace.DOWN); if (below.getType() == Material.AIR) { System.out.println("worked2"); while (below.getType() == Material.AIR) { System.out.println("worked3"); int belowy = below.getY(); belowy--; } } else { block.setType(Material.CARPET); } Bukkit.getServer().getScheduler().runTaskLater(Main.getInstance(), new Runnable() { public void run() { block.setType(air); System.out.println("worked4"); } }, 20 * 10); } Same result, it just repeats the while loop over and over and crashes the server. @uksspy New code @uksspy Code:java int x = player.getLocation().getBlockX(); int y = player.getLocation().getBlockY(); int z = player.getLocation().getBlockZ(); World world = player.getLocation().getWorld(); Location loc = new Location(world, x, y, z); int by = y - 1; Location bloc = new Location(world, x, by, z); Material air = new ItemStack(Material.AIR).getType(); Block block = player.getWorld().getBlockAt(loc); Block below = player.getWorld().getBlockAt(bloc); if (block.getType() == air) { System.out.println("worked1"); if (below.getType() == Material.AIR) { System.out.println("worked2"); while (below.getType() == Material.AIR) { by = by - 1; System.out.println("worked3"); if (below.getType() != Material.AIR) { by++; } } } else { block.setType(Material.CARPET); } Bukkit.getServer().getScheduler().runTaskLater(Main.getInstance(), new Runnable() { @Override public void run() { block.setType(air); System.out.println("worked4"); } }, 20 * 10); } Same issue.
@Signatured Of course there is! This code runs over and over again if below is AIR: Code: while (below.getType() == Material.AIR) { by = by - 1; System.out.println("worked3"); if (below.getType() != Material.AIR) { by++; } } Inside the loop you must somehow modify the variable below.
That's why when I check if below is air, if it is, it subtracts 1 from the y, and continues to do so until below does not equal air.
Did that, didn't help. Code:java int x = player.getLocation().getBlockX(); int y = player.getLocation().getBlockY(); int z = player.getLocation().getBlockZ(); World world = player.getLocation().getWorld(); Location loc = new Location(world, x, y, z); int by = y - 1; Location bloc = new Location(world, x, by, z); Material air = new ItemStack(Material.AIR).getType(); Block block = player.getWorld().getBlockAt(loc); Block below = player.getWorld().getBlockAt(bloc); if (block.getType() == air) { System.out.println("worked1"); if (below.getType() == Material.AIR) { System.out.println("worked2"); while (below.getType() == Material.AIR) { by = by - 1; bloc.setY(by); System.out.println("worked3"); } if (below.getType() != Material.AIR) { by++; bloc.setY(by); } } else { block.setType(Material.CARPET); } Bukkit.getServer().getScheduler().runTaskLater(Main.getInstance(), new Runnable() { @Override public void run() { block.setType(air); System.out.println("worked4"); } }, 20 * 30); } Code:java int x = player.getLocation().getBlockX(); int y = player.getLocation().getBlockY(); int z = player.getLocation().getBlockZ(); World world = player.getLocation().getWorld(); Location loc = new Location(world, x, y, z); int by = y - 1; Location bloc = new Location(world, x, by, z); Material air = new ItemStack(Material.AIR).getType(); Block block = player.getWorld().getBlockAt(loc); Block below = player.getWorld().getBlockAt(bloc); if (block.getType() == air) { System.out.println("worked1"); if (below.getType() == Material.AIR) { System.out.println("worked2"); for (int i = by; player.getWorld().getBlockAt(bloc).getType() == Material.AIR; i--) { by = i; player.getWorld().getBlockAt(bloc).setType(Material.CARPET); } } else { block.setType(Material.CARPET); } Bukkit.getServer().getScheduler().runTaskLater(Main.getInstance(), new Runnable() { @Override public void run() { block.setType(air); System.out.println("worked4"); } }, 20 * 30); } We're getting somewhere. It no longer gets stuck and lags out the server. However, if I'm in the air, it sets a carpet and the block under my player location (even though it's still air) and doesn't put it on the ground. Any ideas?
@Signatured Changing the value of by does NOTHING to change anything else! You NEED to do Location.setY(by); for any effect. When you initialize an integer it basically clones it, so modifying it only helps to modify it in that one specific place, if that makes sense.