Delay "teleportation" of a player while players near

Discussion in 'Plugin Development' started by girardcome, Nov 27, 2014.

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

    girardcome

    Wow ive really asked alot today :L but this is only my second actual plugin xD (first one was just a fix of an existing one)

    So, how can i delay a teleport to a player if there is another player near (15 or less blocks)?

    This is the code i have for teleport, but since an ennemi is too far from me (more than 15 blocks), it's running the teleportation delay ...
    Code:java
    1. Set<String> keys = plugin.getConfig().getConfigurationSection("Warps").getKeys(false);
    2. for (String str : keys) {
    3. if (str.equalsIgnoreCase(kit)) {
    4. found = true;
    5. String stringyaw = plugin.getConfig().getString("Warps." + args[0] + ".Yaw");
    6. String stringpitch = plugin.getConfig().getString("Warps." + args[0] + ".Pitch");
    7. Float yaw = Float.parseFloat(stringyaw);
    8. Float pitch = Float.parseFloat(stringpitch);
    9. final Location loc = new Location(Bukkit.getWorld(plugin.getConfig().getString("Warps." + args[0] + ".World")), plugin.getConfig().getDouble("Warps." + args[0] + ".X"), plugin.getConfig().getDouble("Warps." + args[0] + ".Y"), plugin.getConfig().getDouble("Warps." + args[0] + ".Z"), yaw, pitch);
    10. Random random = new Random();
    11. Player player2 = null;
    12. Player[] players = Bukkit.getServer().getOnlinePlayers();
    13. for (int i = 0; i < 15; i++) {
    14. int randomNumber = random.nextInt(players.length);
    15. player2 = players[randomNumber];
    16. if (!player2.equals(p)) break;
    17. }
    18. Location loc1 = p.getLocation();
    19. Location loc2 = player2.getLocation();
    20.  
    21. int x1 = loc1.getBlockX();
    22. int y1 = loc1.getBlockY();
    23. int z1 = loc1.getBlockZ();
    24.  
    25. int x2 = loc2.getBlockX();
    26. int y2 = loc2.getBlockY();
    27. int z2 = loc2.getBlockZ();
    28.  
    29. int distance = (int) Math.sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2);
    30. if (distance <= 15) {
    31. if(player2.getName().equalsIgnoreCase(p.getName())) {
    32. p.teleport(loc);
    33. p.sendMessage("§aYou have been teleported to §7" + args[0] + " §a!");
    34. return false;
    35. } else {
    36. p.sendMessage("§cThere is §7" + player2.getName() + " §cnear you!");
    37. p.sendMessage("§6You will be teleported in 10 seconds.");
    38. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    39. public void run() {
    40. p.teleport(loc);
    41. p.sendMessage("§aYou have been teleported to §7" + args[0] + " §a!");
    42. }
    43. }, 200L);
    44. }
    45. } else {
    46. p.teleport(loc);
    47. p.sendMessage("§aYou have been teleported to §7" + args[0] + " §a!");
    48. return false;
    49. }
    50. return true;
    51.  
    52. } else {
    53. p.sendMessage("§cThe warp name " + args[0] + " doesn't exist!");
    54. return false;
    55. }
    56.  
    57. }
     
  2. Offline

    drpk

    Code:java
    1. for (int i = 0; i < 15; i++) {
    2. int randomNumber = random.nextInt(players.length);
    3. player2 = players[randomNumber];
    4. if (!player2.equals(p)) break;
    5. }


    Can't you just use Player#getNearbyEntities and check if it's an instanceof Player?
     
  3. Offline

    girardcome

    what do you mean?
     
  4. Offline

    drpk

    girardcome I'm pretty sure you could do Player#getNearbyEntities to get the entities nearby, rather than checking every single player on the server. Then you could add a delay and stuff.
     
  5. Offline

    teej107

  6. Offline

    BeastCraft3

    girardcome
    I don't got the biggest amount of time, but something like this.
    Something like:
    Code:java
    1. for (Entity nearby : p.getNearbyEntities(15D, 15D, 15D)) {
    2. if(nearby instanceof Player) {
    3. //TODO
    4. }
    5. }
     
Thread Status:
Not open for further replies.

Share This Page