Help with while loop crashing game

Discussion in 'Spigot Plugin Development' started by AppleMonJeans, Jun 30, 2017.

  1. I am making a lava bucket that when ever you place it, it will turn the lava into cobblestone and start making cobblestone all the way down untill it hits a block. I have some of the plugin made but i don't know whats wrong with my while loop. When ever someone places a genbucket (which is what im calling these items) the server crashes.
  2. Try checking if
    Code (Java):
    loc.getBlock().getRelative(BlockFace.DOWN).getType() != null) { }
    and remove the semi colon after the if statement.
  3. Choco


    The block from Block#getRelative(), nor its type, can be null. Check whether the type is solid
    Code (Java):
    while (!loc.getBlock().getRelative(BlockFace.DOWN).getType().isSolid()) {

    • Agree Agree x 1
  4. You really shouldn't be using a while loop in on the main thread . Just do all the pre calculations and place all the blocks. Like in a for loop. Keep decreasing the y axis and place a block until it hits the ground.
  5. Choco


    What? Why would you not use a while loop in this case?
    #5 Choco, Jun 30, 2017
    Last edited: Jun 30, 2017
  6. There's not really anything wrong in running a while loop in the main thread, as long as it's not infinite ofcourse, since that'll crash the server yes. As long as your while loop doesn't loop thousands of times, it shouldn't be any issue.
  7. You can make something like this:
    Code (Text):
    int x = loc.getX();
    int z = loc.getZ()
    int y = loc.getY();// or something like this, writting on the phone
    for(int i ; i< y - 1; i++){
       // test if the block bellow the y level of the lava - i is air, if it returns true, place a cobblestone, if not, break the loop then replace the lava with a cobblestone as well
  8. I meant: use a for loop.
  9. Praya


    the main problem is in the while loop:

    Code (Text):
    while (loc.getBlock().getRelative(BlockFace.DOWN) != null); { //this is where everything goes wrong
                    if (event.getBlock().equals(Material.AIR))
    the location is always in the same position that causing an infinite loop, you need to change the location to -1 y-axis when block below is solid until you get the block is non-solid.

    but remember to check the y coordinate is equals or above 0.
  10. this is what i have now but its not working it places one cobblestone block then does nothing (i have switched to the PlayerBukketEmptyEvent listener)