Solved How to repeat code until success?

Discussion in 'Plugin Development' started by Urag, Aug 5, 2015.

Thread Status:
Not open for further replies.
  1. For example:
    Code:
    if (cmd.getName().equalsIgnoreCase("rtp"){
        if (player is stupid){
            player.sendMessage("You can not be stupid!");
            return true;
        }
        player.sendMessage("Good job, you are not stupid!");
        return true;
    }
    ========================================================================================
    How to make this code repeating until player won't be stupid?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Urag Depends, how much do you want to spam the user?
    And on a side note: words in if statements don't do well in java.
     
    Urag likes this.
  3. @timtower
    So how to change this code to find another random location if player hit on not allowed biome?
    Code:
            if (cmd.getName().equalsIgnoreCase("rtp")){
                World w = p.getWorld();
                Location location = new Location(w, 0, 0, 0);
              
                location.setX(settings.getData().getDouble("spawn.x") + Math.random() * 500 * 2 - 500);  //This get a Random with a MaxRange
                location.setZ(settings.getData().getDouble("spawn.z") + Math.random() * 500 * 2 - 500);
                location.setY(w.getHighestBlockAt(location.getBlockX(), location.getBlockZ() ).getY() );  //Get the Highest Block of the Location for Save Spawn.
                Biome biome = w.getBiome(location.getBlockX(), location.getBlockZ());
                if (biome == Biome.OCEAN || biome == Biome.DEEP_OCEAN || biome == Biome.RIVER){
                    p.sendMessage("§cNot allowed biome, try again");
                    return true;
                }
                p.teleport(location);
                p.sendMessage("§aTeleported!");
            }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Urag Now you are getting somewhere.
    Code:
    while(true){
    random location stuff here;
    if (!(biome == Biome.OCEAN || biome == Biome.DEEP_OCEAN || biome == Biome.RIVER)){
    random location found, teleport;
    return;
    }
    }
    Best to turn the random location stuff in its own method.
     
    Shortninja66 and Urag like this.
  5. @timtower
    How to do that? Can you give me a hint?
     
    Last edited: Aug 5, 2015
  6. @Urag
    He already explained it
    Code:
    while(true) { //Keep this thing going :O
      Location random = yourRandomLocation(); //Get a random location
      if(/*The Location is the one you want*/) {
        player.teleport(random);
        return true; //If you want to perform code after this loop, put 'break;' here, so the loop ends.
      } //If the location doesn't match the requirements, the next thing is called in the while loop, it will go into infinity until it finds the right location
    }
    [/code
     
    Urag likes this.
  7. @megamichiel
    Thank you! ;)

    Code:
            if (cmd.getName().equalsIgnoreCase("rtp")){
                while(true) { //Keep this thing going :O
                    World w = p.getWorld();
                    Location location = new Location(w, 0, 0, 0);
                  
                    location.setX(settings.getData().getDouble("spawn.x") + Math.random() * 500 * 2 - 500);  //This get a Random with a MaxRange
                    location.setZ(settings.getData().getDouble("spawn.z") + Math.random() * 500 * 2 - 500);
                    location.setY(w.getHighestBlockAt(location.getBlockX(), location.getBlockZ() ).getY() + 1);  //Get the Highest Block of the Location for Save Spawn.
                    Biome biome = w.getBiome(location.getBlockX(), location.getBlockZ());
                    if (!(biome == Biome.OCEAN || biome == Biome.DEEP_OCEAN || biome == Biome.RIVER)){
                        p.teleport(location);
                        p.sendMessage("§aGut biome");
                        return true;
                      }
                    p.sendMessage("§cBad biome");
                    }
            }
     
    Last edited: Aug 5, 2015
Thread Status:
Not open for further replies.

Share This Page