Random teleport

Discussion in 'Plugin Development' started by AppleMen, Dec 1, 2013.

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

    shmkane

    Also, just a tip with that code, it might be smart to find ground level at the given X and Z coods and adjust your Y from there. What I mean by that is, you could get a Y value like 5, and die in the ground.
     
  2. Offline

    Henzz

    AppleMen
    On a side note, might want to check if sender is instanceof Player before casting sender to of type Player.
     
  3. Offline

    Erigitic

    AppleMen I already said not to subtract from r. Also show updated code.
     
  4. Offline

    AppleMen

    This is my code now:

    Code:java
    1. if(player.hasPermission("server.teleport.random")) {
    2. if(cmd.getName().equalsIgnoreCase("randomteleport")) {
    3. Random r = new Random();
    4. int x = r.nextInt(500) + 1;
    5. int y = r.nextInt(67) + 1;
    6. int z = r.nextInt(500) + 1;
    7. Location rp = new Location (Bukkit.getServer().getWorld("world"), x, y, z);
    8.  
    9. Block b = rp.getBlock();
    10. while(b.getType().equals(Material.AIR)){
    11. r.nextInt();
    12. b = rp.getBlock();
    13. }
    14.  
    15. player.teleport(rp);
    16. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!");
    17. }
    18. } else {
    19. player.sendMessage(ChatColor.RED + "You don't have permissions!");
    20. }
    21. return false;
    22. }

    ( And I told you that I don't know what you mean with "Subtract from r".
     
  5. Offline

    Erigitic

    AppleMen You need to change the values of x, y, z if the block does not equal air.
     
  6. Offline

    AppleMen

    I did, but this didn't worked tho:

    Code:java
    1. if(player.hasPermission("server.teleport.random")) {
    2. if(cmd.getName().equalsIgnoreCase("randomteleport")) {
    3. Random r = new Random();
    4. Block b = rp.getBlock();
    5. Location rp = new Location (Bukkit.getServer().getWorld("world"), x, y, z);
    6.  
    7. while(b.getType().equals(Material.AIR)){
    8. r.nextInt();
    9. b = rp.getBlock();
    10. } else {
    11. int x = r.nextInt(500) + 1;
    12. int y = r.nextInt(67) + 1;
    13. int z = r.nextInt(500) + 1;
    14. }
    15.  
    16. player.teleport(rp);
    17. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!");
    18. }
    19. } else {
    20. player.sendMessage(ChatColor.RED + "You don't have permissions!");
    21. }
    22. return false;
    23. }
     
  7. Offline

    Erigitic

    AppleMen You need to set the x, y, z values of the Location aswell. rp.setX(x).

    AppleMen Also you cannot have an else statement with a while statement.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  8. Offline

    AppleMen

    I got this now, but it is still teleporting me into the ground :(

    Code:java
    1. if(player.hasPermission("server.teleport.random")) {
    2. if(cmd.getName().equalsIgnoreCase("randomteleport")) {
    3. Random r = new Random();
    4. int x = r.nextInt(500) + 1;
    5. int y = r.nextInt(67) + 1;
    6. int z = r.nextInt(500) + 1;
    7. Location rp = new Location (Bukkit.getServer().getWorld("world"), x, y, z);
    8.  
    9. Block b = rp.getBlock();
    10. while(b.getType().equals(Material.AIR)){
    11. r.nextInt();
    12. b = rp.getBlock();
    13. rp.setX(x);
    14. rp.setX(y);
    15. rp.setX(z);
    16. }
    17. player.teleport(rp);
    18. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!");
    19. }
    20. } else {
    21. player.sendMessage(ChatColor.RED + "You don't have permissions!");
    22. }
    23. return false;
    24. }
     
  9. Offline

    Garris0n

    I'm sorry but do you have any idea what that code does anymore?

    1. Somebody types /randomteleport.
    2. You create a new random object.
    3. It creates a random x, y, and z and adds 1 to it. 1-500, 1-67, 1-500.
    4. It creates a location based on a world named "world" (you should not do that, but whatever) and the randoms.
    5. It loops through a while loop in which it checks if the randomly generated location's block is air. If so it calls r.nextInt(), which returns a random number that you don't even use, and then you proceed to set b to rp.getBlock() whithout changing the values of the location at all and then set the values of the location(after changing the block mind you) to the exact same values all over again. This entire part of the code is redundant on so many levels that I'm confused as to how it even came into existence.
    6. Then it teleports you to the location.

    Here's what you should actually be doing:
    1. Somebody types /randomteleport.
    2. You call a method that returns a random location using your random code. Make the method recursive so that if the location it's gonna return is air, it just returns itself.
    3. You now have a location that isn't air, so teleport the player.
     
  10. Offline

    AppleMen

    2. I don't know how to make it recursive..

    And what do I need to remove from the code then? This?:
    Code:java
    1. int x = r.nextInt(500) + 1;
    2. int y = r.nextInt(67) + 1;
    3. int z = r.nextInt(500) + 1;
     
  11. Offline

    Garris0n

    I'm sorry but if you don't understand that I can't explain it any better. You need to understand Java before you try to work with Bukkit or you will constantly be confused. If you understand basic programming/java this should be very simple to do.
     
    Erigitic likes this.
  12. Offline

    AppleMen

    I only know some basics, Never done this part (x, y, z and random) of coding before..
     
  13. Offline

    Garris0n

  14. Offline

    AppleMen

    The random part is working. The only problem is.. Its underground. I think it is the problem of the Y coordinate. But I tried every single thing I know out on it, but its just not working. And I also read the documentations you sent me.
     
  15. Offline

    Garris0n

    There is an entire block of the code you posted which makes absolutely no sense whatsoever. The reason you end up in the ground is because that is the code I assume you intended to prevent ending up in the ground, however I know if I give you the code to fix it you won't understand what was wrong any better. Look through all of your code and figure out what every single line does, and you will understand what the problem is.
     
  16. Offline

    AppleMen

    If... you send the right code, then I will understand what I did wrong. I just take a look into it, then fix the code by my own. Then I will see the difference and see what I did wrong.
     
  17. Offline

    Garris0n

    Look through every line of your code right now and comment what you think it does on the right of it, paste it in, and I'll explain anything that's incorrect. That should help you fix it.
     
  18. Offline

    AppleMen

    I'm sure this part is wrong:
    Code:java
    1. int x = r.nextInt(500) + 1;
    2. int y = r.nextInt(67) + 1;
    3. int z = r.nextInt(500) + 1;
    4. Location rp = new Location (Bukkit.getServer().getWorld("world"), x, y, z);


    And this should be good:
    Code:java
    1. player.teleport(rp);
    2. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!");
    3. }
    4. } else {
    5. player.sendMessage(ChatColor.RED + "You don't have permissions!");
    6. }
    7. return false;
    8. }


    Not sure about this :/:
    Code:java
    1. Location rp = new Location (Bukkit.getServer().getWorld("world"), x, y, z);
    2.  
    3. Block b = rp.getBlock();
    4. while(b.getType().equals(Material.AIR)){
    5. r.nextInt();
    6. b = rp.getBlock();
    7. rp.setX(x);
    8. rp.setX(y);
    9. rp.setX(z);
    10. }
     
  19. Offline

    Garris0n

    I mean like this:
    Code:java
    1. if(player.hasPermission("server.teleport.random")) { //checks for "server.teleport.random" permission
    2. if(cmd.getName().equalsIgnoreCase("randomteleport")) { //checks if the command is "randomteleport"
    3. Random r = new Random(); //creates a new Random object
    4.  
     
  20. Offline

    Erigitic

    AppleMen Look at your while loop. That is the problem. And yes like Garris0n said if you do not understand ask about it. You won't learn if you just write down stuff that you have no idea what it is doing.
     
  21. Offline

    AppleMen

    Oh like that..

    Code:java
    1. if(player.hasPermission("server.teleport.random")) { //Checks if the player has permissions
    2. if(cmd.getName().equalsIgnoreCase("randomteleport")) { //If so, and the player performs this command
    3. Random r = new Random(); //Creates a new random object
    4. int x = r.nextInt(500) + 1;
    5. int y = r.nextInt(67) + 1;
    6. int z = r.nextInt(500) + 1;
    7. Location rp = new Location (Bukkit.getServer().getWorld("world"), x, y, z); //*Creates a new location on a specified world and coordinates.
    8.  
    9. Block b = rp.getBlock();
    10. while(b.getType().equals(Material.AIR)){
    11. r.nextInt();
    12. b = rp.getBlock();
    13. rp.setX(x);
    14. rp.setX(y);
    15. rp.setX(z);
    16. }
    17. player.teleport(rp); //Teleports the player to the location*
    18. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!"); //And sends the player a message in Blue color.
    19. }
    20. } else { //If the player doesn't has the permissions..
    21. player.sendMessage(ChatColor.RED + "You don't have permissions!"); //..Then send this message to the player.
    22. }
    23. return false;
    24. }
     
  22. Offline

    Erigitic

    AppleMen How come you are putting the y and z values in for setX?
     
  23. Offline

    Garris0n

    You skipped quite a few lines there.
     
  24. Offline

    AppleMen

    Woops.. I changed it now, when I do /randomteleport, nothing happens (also not in the console)


    I don't know where those lines are standing for exactly..
     
  25. Offline

    Erigitic

    AppleMen Look at your while loop then post what is saying. You should be able to figure out why it is not working by doing that.
     
  26. Offline

    The_Doctor_123

  27. Offline

    Erigitic

    The_Doctor_123 I agree. AppleMen You really should polish your Java skills or learn Java before working with Bukkit. Like his signature said it will make your life a whole lot easier as well as the people helping you. You are making a lot of mistakes that should not be made.
     
  28. Offline

    AppleMen

    I am trying to learn it. But I can't find a good/clear place to learn it. (I looked at the Java Oracle site too)
     
  29. Offline

    The_Doctor_123

    Erigitic likes this.
Thread Status:
Not open for further replies.

Share This Page