Random teleport

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

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

    AppleMen

    Erigitic
    How you mean with What I have?
    Where do I change X, Y and Z to?
     
  2. Offline

    Erigitic

    AppleMen Did you test your code? The value for x, y, and z in the Location should be set to the three random numbers you created.
     
  3. Offline

    AppleMen

    Its working now , but I have some issues with the Y. I am spawning in the ground. Or lower..

    I got my Y like this:
    Code:java
    1. int y = r.nextInt(67) + 1;
    I want that people spawn on the normal ground.
     
  4. Offline

    Erigitic

    AppleMen You could do a check to see if that location contains a block. If it does then have it create a new random Y value until the location does not contain a block.
     
  5. Offline

    AppleMen

    Can you show me how?
     
  6. Offline

    Erigitic

    AppleMen You need to try it first at least. You learn nothing by being spoon fed code, you learn by trying and attempting to solve it yourself. Try it. Show me the code and I will help you debug it.
     
  7. Offline

    AppleMen

    I tried it already, but got errors..
     
  8. Offline

    Erigitic

    AppleMen Show me the code and I will help debug it.
     
  9. Offline

    AppleMen

    I didn't made one. I tried it, but it was wrong. With getting the Y = to a block location...
     
  10. Offline

    Erigitic

    AppleMen I can't help unless I have code to see. Write some code that will check what block is at the teleport location.
     
  11. Offline

    AppleMen

    I just did some random things, and I know.. Its totally wrong.

    Code:java
    1. if (Bukkit.getServer().getLocation) = ItemStack block {
    2. int y = r.nextInt(67) + 1;
    3. }

    You'll probably laugh:p
     
  12. Offline

    Erigitic

    AppleMen You need to check if the location contains a block.
    Code:
    loc.getBlock();
    Google is your friend here. Their are a ton of tutorials online about getting a block at a specified location.
     
  13. Offline

    AppleMen

    And
    Where do I put that? Can't find it on Google..
     
  14. Offline

    Erigitic

  15. Offline

    AppleMen

    If I do this, it still teleports me into the ground:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    2. {
    3. Player player = (Player)sender;
    4. Location shoploc = new Location(Bukkit.getServer().getWorld("CastleCrystalSpawnTemp"), -9378.329008854484D, 12.0D, -3704.0366090418465D, 269.0F, 2.0F);
    5. if (sender.hasPermission("server.teleport.shop"))
    6. {
    7. if (cmd.getName().equalsIgnoreCase("shop"))
    8. {
    9. player.teleport(shoploc);
    10. player.sendMessage(ChatColor.GREEN + "Teleporting to the Shop");
    11. }
    12. }
    13. else {
    14. player.sendMessage(ChatColor.RED + "You don't have permissions!");
    15. }
    16. if (player.hasPermission("server.teleport.random"))
    17. {
    18. if (cmd.getName().equalsIgnoreCase("randomteleport"))
    19. {
    20. World currentWorld = player.getWorld();
    21. Location loc = player.getLocation();
    22. int locx = (int)loc.getX();
    23. int locy = 127;
    24. int locz = (int)loc.getZ();
    25. Location nloc = new Location(currentWorld, locx, locy, locz);
    26. Block b = nloc.getBlock();
    27. do
    28. {
    29. locy--;
    30. nloc.setY(locy);
    31. b = nloc.getBlock();
    32. if (!b.getType().equals(Material.AIR))
    33. {
    34. double flocx = nloc.getX();
    35. double flocy = nloc.getY();
    36. double flocz = nloc.getZ();
    37. flocx += 0.5D;
    38. flocz += 0.5D;
    39. flocy += 2.0D;
    40. Location floc = new Location(currentWorld, flocx, flocy, flocz);
    41. player.teleport(floc);
    42. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!");
    43. }
    44. } while (b.getType().equals(Material.AIR));
    45. return true;
    46. }
    47. }
    48. else {
    49. player.sendMessage(ChatColor.RED + "You don't have permissions!");
    50. }
    51. return false;
    52. }
     
  16. Offline

    Erigitic

    AppleMen What did you do with the randomization code now?
     
  17. Offline

    AppleMen

    I kinda lost it by accident. I was trying the thread you send me.
     
  18. Offline

    Erigitic

    AppleMen You copied the code practically line for line from what I sent you and it is not even doing what you want. I sent that to help you get an understanding of how to get the block at a location.
     
  19. Offline

    AppleMen

    I got this 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. rp.getBlock();
    10.  
    11. player.teleport(rp);
    12. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!");
    13. }


    But I lost the right part of
    Code:java
    1. int x = r.nextInt(500) + 1;
    2. int y = r.nextInt(67) + 1;
    3. int z = r.nextInt(500) + 1;
     
  20. Offline

    Erigitic

    AppleMen Now that you have that you want to check to see if the block at the location is Material.AIR. If it is then teleport otherwise create a new set of random numbers.
     
  21. Offline

    chrisman0091

    He also might want to check if the block above it is air(so its not a 1 thick airpocket, making them suffocate).

    Also OP, just popping in, if you want to get REALLY fancy you can make your X/Z randomnumber+0.5, so they won't be on the edge of the block but rather the middle(not really that fancy).
     
  22. Offline

    Erigitic

    @chisman0091 I am just taking it one thing at a time so he does not get to confused. But yes he should check if the block above is air.
     
  23. Offline

    AppleMen

    I think that I am almost there, still some issues:

    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--;
    12. rp.setY(r);
    13. b = rp.getBlock();
    14. }
    15.  
    16. player.teleport(rp);
    17. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!");
    18. }
     
  24. Offline

    Erigitic

    AppleMen You cannot put a Random into setY(). You need to do r.nextInt(). Also do not subtract anything from r.
     
  25. Offline

    AppleMen

    Wait.. I'm confused now, what do you mean with "Abstract from r"? I have this 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. }
     
  26. AppleMen
    You can't just throw in some lines, search your problem, and instead of copying the code you find (if any), read what they say, look at the code, and take only the needed part.

    Erigitic
    What about negative numbers? They deserve recognition too.
     
  27. Offline

    AppleMen

    The problem is.. I don't know the problem. Thats why I am asking it here.
     
  28. Offline

    Garris0n

    It's 0(inclusive) and 500(non-inclusive). So 0, 1...498, 499 are the numbers it chooses from.
     
  29. Offline

    Erigitic

    garrison My mistake. Thanks for correcting that.
    AppleMen What you have above should work. Just correct the stuff I said. And it is subtract not abstract.

    Assist I am just taking it one step at a time to not confuse him. But you are right negatives should not be excluded.

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

    AppleMen

    It is doing random locations now but still under ground.. I have some issues with Substracting the r :/
     
Thread Status:
Not open for further replies.

Share This Page