Solved Need help with my TeleportToRandomLocation plugin!

Discussion in 'Plugin Development' started by Peter25715, Aug 12, 2014.

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

    Peter25715

    Hey everybody, My name is PeterSa and I really need help with my new plugin which is TeleportToRandomLoaction.
    Everything is al-right in this plugin! Everything is fine.
    Whenever you do /tprandom it teleports you to a random location.
    and it will send you a message that everything is successfully done and how many blocks you are away from your previous location and how many blocks you are away from the spawnpoint.
    But my problem is, I want it to spawn on grass, sand, stone..
    Not water. So please help.
    Here's the code :
    Code:java
    1. package me.petersa.Teleport;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14.  
    15. public class TeleportPlayerRandomLocation extends JavaPlugin implements Listener {
    16. @Override
    17. public void onEnable(){
    18. System.out.println("[TeleportPlayerRandomLocation] teleport-random plugin by PeterSa has been enabled!");
    19. this.getServer().getPluginManager().registerEvents(this, this);
    20. }
    21. @Override
    22. public void onDisable(){
    23. System.out.println("[TeleportPlayerRandomLocation] teleport-random plugin by PeterSa has been disabled!");
    24. }
    25. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    26. if (cmd.getName().equalsIgnoreCase("tprandom")) {
    27. if (!sender.hasPermission("psa.tprandom")) {
    28. sender.sendMessage(ChatColor.RED + "You are not allowed to use that command! permission node: psa.tprandom");
    29. }else{
    30. Player player = (Player) sender;
    31. Location originalLocation = player.getLocation();
    32. Location spawnLocation = player.getWorld().getSpawnLocation();
    33.  
    34. Random random = new Random();
    35.  
    36. int x = random.nextInt(10000) + 1;
    37. int y = 100;
    38. int z = random.nextInt(10000) + 1;
    39.  
    40. Location teleportLocation = new Location(player.getWorld(), x, y, z);
    41.  
    42. player.teleport(teleportLocation);
    43.  
    44. player.sendMessage(ChatColor.GREEN + "You have been teleported " + ChatColor.RED + (int)teleportLocation.distance(originalLocation) + ChatColor.GREEN + " blocks away from your last location and " + ChatColor.RED + (int)teleportLocation.distance(spawnLocation) + ChatColor.GREEN + " blocks away from spawn!");
    45. player.sendMessage(ChatColor.GRAY + "this moment will lag you just a bit and everything will be alright then. Please let the chunks load for you! Also if you got spawned in the water, Just do the command again!");
    46.  
    47. return true;
    48. }
    49.  
    50. }
    51.  
    52. return false;
    53. }
    54. }
     
  2. Offline

    AoH_Ruthless

    Peter25715
    You could use a while loop. While the location is not grass,sand, or stone, recalculate x, y, and z.

    Or if you are preventing teleporting to water only (and probably lava), check if the block is liquid (Location#getBlock()#isLiquid() )

    That's just the first thought that came to my mind.
     
    Peter25715 likes this.
  3. Offline

    Peter25715

    Hmm.. @AoH_Ruthless,
    Thanks for replying to this thread, Can you please show me the (Location#getBlock()#isLiquid() ) in code? I mean How to block the teleporting if the block was liquid.
    Thanks,
    Peter.
     
  4. Offline

    PogoStick29

    Right after you instantiate the location, check to see if it's a liquid using location.getBlock().isLiquid().
     
    Peter25715 likes this.
  5. Offline

    Peter25715

    Also, @AoH_Ruthless, I don't want the code to be; If the block was liquid then stop everything. I want to make the player spawn on a block which not liquid :/

    @PogoStick29, I don't want the event to stop and the player should do the command again.. :\ What should I do?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  6. Peter25715 Make a if statement if its a liquid redo the random location and if its not teleport them.
     
    Peter25715 likes this.
  7. Offline

    ChipDev

    Maybe use a loop and see if the location (-1) is a liquid, Redo.

    Thats how I think :)

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

    PogoStick29

    Use a do...while loop.

    Code:java
    1. Location loc;
    2.  
    3. do {
    4. loc = new Location(...);
    5. } while (!loc.getBlock().isLiquid());
     
  9. Offline

    Peter25715

    Thanks a lot for helping ;)
     
Thread Status:
Not open for further replies.

Share This Page