Random Teleport function not working

Discussion in 'Plugin Development' started by Ghilliedrone, Sep 11, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Ghilliedrone

    My random teleport function doesn't work. It makes the player freeze and unable to interact, then it kicks them and wont let them join. Can anyone spot an error?
    Code:java
    1. public void Random_Teleport_World(Player player){
    2. int rand_x,rand_z;
    3. rand_x = generator.nextInt(1000);
    4. rand_z = generator.nextInt(1000);
    5. int y = 0;
    6. boolean safe = false;
    7. Location loc;
    8. World world = player.getWorld();
    9. while(!safe){
    10. y++;
    11. loc = new Location(world, rand_x, y, rand_z);
    12. if(!(loc.subtract(0, 1, 0).getBlock().getType().equals(Material.AIR)||loc.subtract(0, 1, 0).getBlock().getType().equals(Material.LAVA))){
    13. if(loc.getBlock().getType().equals(Material.AIR) && loc.add(0,1,0).getBlock().getType().equals(Material.AIR)){
    14. safe = true;
    15. player.teleport(loc);
    16. }
    17. }else if(y >= 254){
    18. y = 0;
    19. rand_x = generator.nextInt(1000);
    20. rand_z = generator.nextInt(1000);
    21. }
    22. }
    23.  
    24. }
     
  2. Offline

    RunningStudios

    Wait... I'm confused. You're trying to make a random teleport function, but you want it freeze, and kick them? I don't see anything in the code about kicking the player. I'm just probably not seeing it :p
     
  3. Offline

    WauloK

    I think he means he wants to teleport them somewhere there's no lava and just air in the two blocks he's checking.
    Instead of doing that, it freezes the player for a bit and they get kicked from the server.
     
  4. Offline

    Ghilliedrone

  5. Offline

    blablubbabc

    Your condition is never reached, because a block can't be air and not-air at the same time (this is what you are checking here at some point) -> endless loop -> server freezes

    The problem is, that location.subtract(..) and location.add(..) modify the location object, therefore your conditions don't work like you probably expect them to work..

    Step by step explanation: you call:
    !(loc.subtract(0, 1, 0).getBlock().getType().equals(Material.AIR))
    -> loc.subtract(0, 1, 0) -> loc is now at y - 1 and there is no air
    !(loc.subtract(0, 1, 0).getBlock().getType().equals(Material.LAVA))
    -> loc is now at y - 2 and there is no lava
    loc.getBlock().getType().equals(Material.AIR)
    -> loc is still at y - 2 and there is air (air != lava so this is possible)
    loc.add(0,1,0).getBlock().getType().equals(Material.AIR)
    -> loc is now again at y - 1 and there has to be air in order for your condition to be successfull
    However: there can't be air because the first condition (see above at y - 1) checked, that there is no air..

    -> endless loop

    You could add a .clone() whenever you call subtract or add on loc, or you get the block at your newly created location and call something like block.getRelative(BlockFace.DOWN or BlockFace.UP).getType()..
     
    Vekh and WauloK like this.
  6. Offline

    WauloK

    So y=0 which is bedrock. Add 1 so y=1.
    This:
    Code:java
    1. if(!(loc.subtract(0, 1, 0).getBlock().getType().equals(Material.AIR)||loc.subtract(0, 1, 0).getBlock().getType().equals(Material.LAVA))){


    Pseudo-code:


    This could be the problem. You only get teleported if the block at under feet is not air or it is lava.
    At bedrock level, feet are on bedrock and face is at dirt level.
    I could be wrong. My brain hurts with this code XD
     
  7. Offline

    Ghilliedrone

    Not sure anymore if the problem is this function. My other function do the same thing
    Edit: nevermind
     
  8. Offline

    blablubbabc

    Code:java
    1.  
    2. public void Random_Teleport_World(Player player) {
    3. int rand_x = generator.nextInt(1000);
    4. int rand_z = generator.nextInt(1000);
    5. int y = 0;
    6. boolean safe = false;
    7. Location loc;
    8. World world = player.getWorld();
    9. while (!safe){
    10. y++;
    11. loc = new Location(world, rand_x, y, rand_z);
    12. Block block = loc.getBlock();
    13. Material downType = block.getRelative(BlockFace.DOWN).getType();
    14. if (downType != Material.AIR && downType != Material.LAVA) {
    15. if (block.getType() == Material.AIR && block.getRelative(BlockFace.UP).getType() == Material.AIR) {
    16. safe = true;
    17. player.teleport(loc);
    18. }
    19. } else if (y >= 254){
    20. y = 0;
    21. rand_x = generator.nextInt(1000);
    22. rand_z = generator.nextInt(1000);
    23. }
    24. }
    25. }
    26.  
     
Thread Status:
Not open for further replies.

Share This Page