Teleporting to random locations

Discussion in 'Plugin Development' started by kreashenz, Apr 15, 2013.

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

    kreashenz

    I have read the other posts, and still have no idea how to get it to teleport you to a random location. This plugin was for a request ( via Skype ) for someone. I just can't get it to teleport whatsoever..
    Code:java
    1. package stuntguy3000.kreashenz.wilderness;
    2.  
    3. import java.util.Random;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.Sign;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.block.SignChangeEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class Wilderness extends JavaPlugin implements Listener {
    15.  
    16. public void onEnable() {
    17. getLogger().info("Wilderness has been enabled");
    18. getServer().getPluginManager().registerEvents(this,this);
    19. }
    20.  
    21. @EventHandler
    22. public void onSignInteract(PlayerInteractEvent e){
    23. Player p = e.getPlayer();
    24. if(e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock() != null){
    25. if(e.getClickedBlock().getType() == Material.SIGN_POST
    26. || e.getClickedBlock().getType() == Material.WALL_SIGN){
    27. Sign s = (Sign)e.getClickedBlock().getState();
    28. String[] lines = s.getLines();
    29. if(lines.length == 0 && lines[0].equalsIgnoreCase("§1[Wilderness]")){
    30. Random r = new Random();
    31. int loc = r.nextInt(3000) + 8000;
    32. }
    33. }
    34. }
    35. }
    36.  
    37. @EventHandler
    38. public void onSignCreate(SignChangeEvent e){
    39. Sign s = (Sign)e.getBlock();
    40. String[] lines = s.getLines();
    41. if(lines.length == 0 && lines[0].equalsIgnoreCase("[Wilderness]")){
    42. if(e.getPlayer().hasPermission("wilderness.signcreate")){
    43. if(lines[0].equalsIgnoreCase("[Wilderness]")){
    44. e.setLine(0, "§1[Wilderness]");
    45. }
    46. } else {
    47. e.getBlock().breakNaturally();
    48. e.getPlayer().sendMessage("§cYou do not have permission to create Wilderness signs.");
    49. }
    50. }
    51. }
    52. }

    Thanks..
     
  2. Offline

    surtic

  3. Offline

    kreashenz

    surtic
    So, just a little guess here, but
    Code:
    Location x = r.nextInt()+1;
    would be one?
     
  4. Offline

    surtic

    Not really then X is a Int not a Location :)

    Here a Snipped from my Code
    Code:
    Location location = new Location(world, 0, 0, 0); // New Location in the right World you want
     
    location.setX( spawn.getX() + Math.random() * maxRadius * 2 - maxRadius); // This get a Random with a MaxRange
    location.setZ( spawn.getZ() + Math.random() * maxRadius * 2 - maxRadius);
     
    location.setY( world.getHighestBlockAt(location.getBlockX(), location.getBlockZ() ).getY() ); // Get the Highest Block of the Location for Save Spawn.
    
    also you can make for Max Distance

    r.nextInt(range)
     
    jetp250 likes this.
  5. Offline

    kreashenz

    surtic I have a new code now, would this work?
    Code:java
    1. package stuntguy3000.kreashenz.wilderness;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.Material;
    7. import org.bukkit.block.Sign;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.block.SignChangeEvent;
    13. import org.bukkit.event.player.PlayerInteractEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class Wilderness extends JavaPlugin implements Listener {
    17.  
    18. public void onEnable() {
    19. getLogger().info("Wilderness has been enabled");
    20. getServer().getPluginManager().registerEvents(this,this);
    21. }
    22.  
    23. @EventHandler
    24. public void onSignInteract(PlayerInteractEvent e){
    25. Player p = e.getPlayer();
    26. if(e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getClickedBlock() != null){
    27. if(e.getClickedBlock().getType() == Material.SIGN_POST
    28. || e.getClickedBlock().getType() == Material.WALL_SIGN){
    29. Sign s = (Sign)e.getClickedBlock().getState();
    30. String[] lines = s.getLines();
    31. if(lines.length == 0 && lines[0].equalsIgnoreCase("§1[Wilderness]")){
    32. Random r = new Random();
    33. double x = r.nextDouble() + 1;
    34. double y = p.getWorld().getMaxHeight();
    35. double z = r.nextDouble() + 1;
    36. Location loc = new Location(p.getWorld(), x,y,z);
    37. p.teleport(loc);
    38. }
    39. }
    40. }
    41. }
    42.  
    43. @EventHandler
    44. public void onSignCreate(SignChangeEvent e){
    45. Sign s = (Sign)e.getBlock();
    46. String[] lines = s.getLines();
    47. if(lines.length == 0 && lines[0].equalsIgnoreCase("[Wilderness]")){
    48. if(e.getPlayer().hasPermission("wilderness.signcreate")){
    49. if(lines[0].equalsIgnoreCase("[Wilderness]")){
    50. e.setLine(0, "§1[Wilderness]");
    51. }
    52. } else {
    53. e.getBlock().breakNaturally();
    54. e.getPlayer().sendMessage("§cYou do not have permission to create Wilderness signs.");
    55. }
    56. }
    57. }
    58. }
     
  6. Offline

    surtic

    Why you asking my about Would this Work?

    Test it and you see how it Works.

    One thing is really bad... you get the Highest Point of the World and not of the Postion you want Teleport... so the Player will Teleportet to 254 ore so and Fall down to Earth.
     
    kreashenz likes this.
  7. Offline

    kaskoepoes112

    kreashenz why the hell are you doing r.nextDouble() + 1;
    You better use int:

    double x = r.nextInt(10); // will retrun a number between 0 and 9

    you must set something between the () things (forgot the word for that).
     
  8. Offline

    kreashenz

    surtic Haha, true. Gimme a sec.. *After a couple secs* It turns the first line to a black line, and then I added some to check if colour was working, and why the hell the first line isn't setting to &1[Wilderness]. It turns out like this.[​IMG]
    1st line SHOULD equal lines[0], shouldn't it? That's what I have, and that's not working, and that's causing the rest of the code not to work, so I can't check.. :/

    kaskoepoes112 Can you tell me why my above post is happening?
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  9. Offline

    kaskoepoes112

    kreashenz

    if(lines.length==0&& lines[0].equalsIgnoreCase("[Wilderness]")){

    this will always return false because first you're asking if lines.length == 0 and then you're asking if line[0] is equal to wilderniss. That's just wrong..
     
  10. Offline

    kreashenz

    kaskoepoes112 That's true.. But it's still not setting the colour?! Why?
     
  11. Offline

    kaskoepoes112


    Have you fixed it?
     
  12. Offline

    kreashenz

    kaskoepoes112 I haven't fixed the colour, I don't know whats causing it not to colour..
     
  13. Offline

    kaskoepoes112


    I mean the wrong if statements..
     
  14. Offline

    kreashenz

    kaskoepoes112 Uhm, I think so.. I removed the lines.length==0&& from the if, so I'm not sure..
     
Thread Status:
Not open for further replies.

Share This Page