Random Teleport except in Water

Discussion in 'Spigot Plugin Development' started by KollegahDerBoss, May 1, 2015.

  1. Hey everyone,
    let's assume I have a random-generated world which consists primarily of water.

    I want to teleport the player to a random coordinate, which is NOT on WATER.

    How can I find that random location - WITHOUT DOING a while Loop which could possibly run for hours?

    Thanks.
     
    #1 KollegahDerBoss, May 1, 2015
    Last edited: May 1, 2015
  2. just add a line like this:
    Code (Java):

    if (!newlocation.getBlock().getType() == Material.WATER) {
    teleportplayer();
    } else {
    calculatenewpos();
    }
     
     
  3. When a teleport is executed check if the player is in water, if yes teleport to safe land.
     
  4. I appreciate the answers, however none of these answers have fully answered my question.

    1 Answer)
    What you are giving me there is a recursive method, which is the same as a while-loop.

    2 Answer)
    Sure, but that random "safe land" can be water as well, so this does not fix the problem.
     
  5. I don't think you can achieve this without any extra data or a recursive method/loop..
    Random coords will be random, there is no guarantee your first roll will be on land.

    If your world contains islands, you could save one location per island (center). Then select a random island and find a random coord around that. That will highly increase the chance of a 'safe' roll, but requires extra setting up and a tiny bit of extra memory
     
    #5 Flamedek, May 1, 2015
    Last edited: May 1, 2015
  6. Check if the block you're teleporting to is solid by using the method isSolid(), if it's not then simply change the block or repeat the teleport.
     
  7. Basically an extend to my answer.
    You teleport then then check is the block is solid if it isn't teleport them again, if it is then do nothing.