Random teleport

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

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

    AppleMen

    Hello,

    I am making simple code that will teleport you to a random location on command.

    But how? I got the onCommand stuff already :p

    thanks!
     
  2. Offline

    Erigitic

    AppleMen Use this to teleport players
    Code:
    player.teleport(location);
    And set the locations x, y, z values to random numbers.
     
  3. Offline

    AppleMen

    What I want is, each time they perform the command, the plugin will teleport them to a random location. Not just to 1 location.
     
  4. Offline

    Erigitic

    AppleMen I know. What I meant by random numbers is you would use randomly generated numbers every time the command is run. I think this code will create random numbers:
    Code:java
    1. Random r = new Random();
    2.  
    3. int randomNum = r.nextInt(500) + 1;//Will return a number between 1 and 501
     
  5. Offline

    AppleMen

    And thats for X, Y and Z? And if I put that code under the onCommand section, will the player get teleported to a random place then?
     
  6. Offline

    Erigitic

    AppleMen You would want three variables that each have their own random value. Then with those three variables create a Location.
     
  7. Offline

    AppleMen

    I'm a bit confused, how can I put it into the onCommand section? And Do I just copy and paste
    Code:java
    1. int randomNum = r.nextInt(500) + 1;
    3 times?
     
  8. Offline

    Erigitic

    AppleMen When the command is run you want to set 3 different variables with different names. For example,
    Code:java
    1. int x = randNum;
    2. int y = randNum;
    3. int z = randNum;


    After this you would create a Location variable and put those three variables in for the x, y, and z values of the location.
     
  9. Offline

    AppleMen

    So like this?
    Code:java
    1. int x = 1000;
    2. int y = 1050;
    3. int z = 2000;
    4.  
    5. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    6. Player player = (Player) sender;
    7. Location rp = new Location (Bukkit.getServer().getWorld("CastleCrystalsSpawnTemp"), x, y, z);
    8. if(sender.hasPermission("server.teleport.random")) {
    9. if(cmd.getName().equalsIgnoreCase("randomteleport")) {
    10. player.teleport(rp);
    11. }
    12. }
    13. return false;
    14. }
     
  10. Offline

    Erigitic

    AppleMen Yes except you want to use randomly generated numbers and set the values of the variables when the command is run. And same thing with the location. You want to set it when the command is run.
    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    3. Player player = (Player) sender;
    4.  
    5. if(sender.hasPermission("server.teleport.random")) {
    6. if(cmd.getName().equalsIgnoreCase("randomteleport")) {
    7. int x = randomlyGeneratedNumberHere;
    8. int y = randomlyGeneratedNumberHere;
    9. int z = randomlyGeneratedNumberHere;
    10.  
    11. Location rp = new Location (Bukkit.getServer().getWorld("CastleCrystalsSpawnTemp"), x, y, z);
    12.  
    13. player.teleport(rp);
    14. }
    15. }
    16. return false;
    17. }
    18.  
     
  11. Offline

    AppleMen

    So it should now teleport me to random places every time I do /randomteleport?
     
  12. Offline

    Erigitic

  13. Offline

    AppleMen


    Code:java
    1. int x = 1000;
    2. int y = 1050;
    3. int z = 2000;
    4.  
    5. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    6. Player player = (Player) sender;
    7. Location rp = new Location (Bukkit.getServer().getWorld("CastleCrystalsSpawnTemp"), x, y, z);
    8.  
    9. if(sender.hasPermission("server.teleport.random")) {
    10. if(cmd.getName().equalsIgnoreCase("randomteleport")) {
    11. player.teleport(rp);
    12. }
    13. }
    14. return false;
    15. }
     
  14. Offline

    Erigitic

    AppleMen
     
  15. Offline

    AppleMen

    I got a internal error when I try to execute the command:

    Code:
    18:33:04 [INFO] matthijs110 issued server command: /randomteleport
    18:33:04 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'randomteleport' in plugin CastleCrystals v1.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:532)
        at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:986)
        at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:897)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:838)
        at net.minecraft.server.v1_6_R3.Packet3Chat.handle(Packet3Chat.java:47)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:237)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:117)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:131)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:604)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:493)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:425)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_6_R3.entity.CraftPlayer.teleport(CraftPlayer.java:402)
        at org.bukkit.craftbukkit.v1_6_R3.entity.CraftEntity.teleport(CraftEntity.java:199)
        at me.matthijs110.CastleCrystals.Main.onCommand(Main.java:76)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    This is my code now:
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    2. Player player = (Player) sender;
    3.  
    4. if(sender.hasPermission("server.teleport.random")) {
    5. if(cmd.getName().equalsIgnoreCase("randomteleport")) {
    6. int x = 1000;
    7. int y = 1050;
    8. int z = 2000;
    9. Location rp = new Location (Bukkit.getServer().getWorld("CastleCrystalsSpawnTemp"), x, y, z);
    10.  
    11. player.teleport(rp);
    12. player.sendMessage(ChatColor.BLUE + "You are randomly teleported!");
    13. }
    14. } else {
    15. player.sendMessage(ChatColor.RED + "You don't have permissions!");
    16. }
    17. return false;
    18. }
     
  16. Offline

    Erigitic

    AppleMen Line 4
    Code:java
    1. if (sender.hasPermission("server.teleport.random")
    . The hasPermission method can only be used on a Player. You have it being used on a CommandSender.
     
  17. Offline

    AppleMen

    After changing sender to player I still got this error:

    Code:
    18:41:46 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'rp' in plugin CastleCrystals v1.1
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:532)
        at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:986)
        at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:897)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:838)
        at net.minecraft.server.v1_6_R3.Packet3Chat.handle(Packet3Chat.java:47)
        at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:237)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:117)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java:131)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:604)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:240)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:493)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:425)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.NullPointerException
        at org.bukkit.craftbukkit.v1_6_R3.entity.CraftPlayer.teleport(CraftPlayer.java:402)
        at org.bukkit.craftbukkit.v1_6_R3.entity.CraftEntity.teleport(CraftEntity.java:199)
        at me.matthijs110.CastleCrystals.Main.onCommand(Main.java:76)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
     
  18. Offline

    Erigitic

  19. Offline

    AppleMen

    Code:java
    1. player.teleport(rp);
     
  20. Offline

    Erigitic

    AppleMen Instead of CastleCrystalsSpawnTemp use world. Try that and see what happens. I just tested the code and it worked fine for me.
     
  21. Offline

    AppleMen

    It works for me now, but it isn't random.. It is teleporting me to the X, Y and Z int. How can I do it randomly??
     
  22. Offline

    Erigitic

    AppleMen I already gave you code to do randomized numbers. Scroll up a bit. Also, when you do Bukkit.getServer().getWorld(worldName) the worldName has to be the same as the folder that contains all of the world information. Unless you changed the name of that folder it will always be "world".
     
  23. Offline

    AppleMen

    How do I this part then?

    Code:java
    1. int randomNum = r.nextInt(500) + 1;
    2. Location rp = new Location (Bukkit.getServer().getWorld("world"), x, y, z);
     
  24. Offline

    Erigitic

    AppleMen In r.nextInt(num), num is the range from 0-num. What you have above would range from 0-500. When you add the 1 it changes that range from 1-501. So just put a number in for num that best suits your needs.
     
  25. Offline

    AppleMen

    And how about the Location part?
    Code:java
    1. Location rp = new Location (Bukkit.getServer().getWorld("world"), x, y, z);
     
  26. Offline

    Erigitic

    AppleMen What do you need to know about?
     
  27. Offline

    AppleMen

    Do I need to use that or not?
     
  28. Offline

    Erigitic

  29. Offline

    AppleMen

    And what do I need to put in here?
    Code:java
    1. , x, y, z);
     
  30. Offline

    Erigitic

    AppleMen Exactly what you have. Just test it out. If it works then mark this post as solved. Otherwise come back and I will try and help.
     
Thread Status:
Not open for further replies.

Share This Page