Adding a timer before teleporting

Discussion in 'Plugin Development' started by nite, Jul 4, 2014.

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

    nite

    Hi,
    I want to add a timer somewhere in this code so that after the duel is over it counts down from 25 seconds until it teleports the Player out of the arena.

    Code:java
    1. public void endDuel(Player player)
    2. {
    3. FileManager fm = this.plugin.getFileManager();
    4. ItemManager im = this.plugin.getItemManager();
    5. String playerName = player.getName();
    6.  
    7. DuelArena arena = getPlayersArena(playerName);
    8. arena.removePlayer(playerName);
    9. player.teleport(fm.getLobbySpawnLocation());
    10. if (this.plugin.isUsingSeperatedInventories()) {
    11. restoreInventory(player);
    12. }
    13. if (arena.getPlayers().size() == 1) {
    14. im.rewardPlayer(arena);
    15. }
    16. }
    17.  
    18. public void endDuel(DuelArena arena)
    19. {
    20. ItemManager im = this.plugin.getItemManager();
    21. DuelManager dm = this.plugin.getDuelManager();
    22. FileManager fm = this.plugin.getFileManager();
    23. if (arena.getPlayers().size() == 1)
    24. {
    25. im.rewardPlayer(arena);
    26. return;
    27. }
    28. for (String player : arena.getPlayers())
    29. {
    30. if (isFrozen(player)) {
    31. removeFrozenPlayer(player);
    32. }
    33. Player playerOut = Bukkit.getPlayer(player);
    34. if (playerOut != null)
    35. {
    36. playerOut.teleport(fm.getLobbySpawnLocation());
    37. if (this.plugin.isUsingSeperatedInventories()) {
    38. restoreInventory(playerOut);
    39. }
    40. Util.sendMsg(playerOut, ChatColor.DARK_GRAY +""+ ChatColor.BOLD +""+ "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.RED + "Duel was forcefully cancelled!");
    41. }
    42. arena.getPlayers().remove(player);
    43. }
    44. arena.getPlayers().clear();
    45. arena.setDuelState(DuelState.WAITING);
    46. }
    47. }


    Code:java
    1. @EventHandler(priority=EventPriority.HIGH)
    2. public void onPlayerDeath(PlayerDeathEvent e)
    3. {
    4. Player player = e.getEntity();
    5. String playerName = player.getName();
    6.  
    7. DuelManager dm = this.plugin.getDuelManager();
    8. FileManager fm = this.plugin.getFileManager();
    9. MySql mySql = this.plugin.getMySql();
    10. if (dm.isInDuel(playerName))
    11. {
    12. if (fm.isMySqlEnabled()) {
    13. mySql.addPlayerKillDeath(playerName, FieldName.DEATH);
    14. }
    15. dm.endDuel(player);
    16. if ((e.getEntity().getKiller() instanceof Player))
    17. {
    18. Player killer = e.getEntity().getKiller();
    19. String killerName = killer.getName();
    20. if (fm.isMySqlEnabled()) {
    21. mySql.addPlayerKillDeath(killerName, FieldName.KILL);
    22. }
    23. if ((!fm.isDropsOnDeathEnabled()) && ((e.getEntity() instanceof Player)) && ((e.getEntity().getKiller() instanceof Player)))
    24. {
    25. Player p = e.getEntity();
    26. Player killer1 = p.getKiller();
    27. for (ItemStack item : player.getInventory().getContents())
    28. {
    29. for(ItemStack armor : player.getInventory().getArmorContents())
    30. if (item != null) {
    31. if(armor != null)
    32. killer1.getInventory().addItem(new ItemStack[] { item });
    33. killer1.getInventory().addItem(new ItemStack[] { armor});
    34. }
    35. player.getInventory().clear();
    36. e.getDrops().removeAll(e.getDrops());
    37. }
    38. if (!fm.isDeathMessagesEnabled())
    39. {
    40. e.setDeathMessage("");
    41. return;
    42. }
    43. e.setDeathMessage(ChatColor.DARK_GRAY +""+ ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.AQUA + player.getName() + ChatColor.RED + " was killed in a duel by " + ChatColor.AQUA + killer.getName());
    44. }
    45. else
    46. {
    47. if (!fm.isDeathMessagesEnabled())
    48. {
    49. e.setDeathMessage("");
    50. return;
    51. }
    52. e.setDeathMessage(ChatColor.DARK_GRAY +""+ ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.AQUA + player.getName() + ChatColor.RED + " was killed in a duel!");
    53. }
    54. }
    55. }
    56. }
    57.  
    58. @EventHandler(priority=EventPriority.HIGH)
    59. public void onPlayerRespawn(PlayerRespawnEvent e)
    60. {
    61. Player player = e.getPlayer();
    62. String playerName = player.getName();
    63. DuelManager dm = this.plugin.getDuelManager();
    64. FileManager fm = this.plugin.getFileManager();
    65. if (dm.isDeadPlayer(playerName))
    66. {
    67. e.setRespawnLocation(fm.getLobbySpawnLocation());
    68. if (this.plugin.isUsingSeperatedInventories()) {
    69. DuelManager.restoreInventory(player);
    70. }
    71. dm.removedDeadPlayer(playerName);
    72. }
    73. }
    74.  


    I would also like to have it send a message to the player in the arena saying something like "Teleporting in <Time>"

    Thanks!

    Also if you can show a example please do and try and show me where i would put it in the code
     
  2. Offline

    dsouzamatt

  3. Offline

    MeRPG

  4. Offline

    nite

    MeRPG I have this but im not sure if this will work
    Code:java
    1. Bukkit.getScheduler().runTaskLater(plugin, new BukkitRunnable(){
    2. public void run(){
    3. }
    4. }, 600L);


    sorry for the late response
     
  5. Offline

    Gater12

    nite
    You should create annonymous BukkitRunnable.
     
  6. Offline

    UaVxChallenge

    Do this instead of where you have arena.getPlayers().clear(); it should work since there it isn't going to be ran constantly and only after there is one player left. At the top of EndDuel you can also add

    Code:java
    1. if(! arena.getPlayers().getSize() == 1) return;


    Code:java
    1. Bukkit.getScheduler().scheduleDelayedTask(plugin, new BukkitRunnable(){
    2.  
    3. public void run(){
    4. arena.getPlayers().clear();
    5. }
    6.  
    7. }, 20 * 25);
    8.  
    9.  


    This may not work because I'm doing this freehand and don't have anything to correct it but its worth a try!

    If this doesn't work you cant also try to do it on this:

    arena.getPlayers().remove(player);
     
  7. Offline

    nite

    How would i do that and then add it to my current code?
     
  8. Offline

    Mr360zack

    Code:java
    1. public void teleportCountdown(Player p, Location loc, int time){
    2. Bukkit.getScheduler().runTaskTimer(plugin, new Runnable(){
    3.  
    4. public void run(){
    5.  
    6. int i = 0;
    7.  
    8. if(i>=time){
    9. p.teleport(loc);
    10. }
    11. else{
    12. i++;
    13. p.sendMessage(time-i);
    14. }
    15.  
    16.  
    17. }
    18.  
    19. }, 20L, 20L);
    20.  
    21. }


    then use teleportCountdown(player, locToTeleportTo, 25);

    hand wrote this did not test... i hope this is what you are looking for (will teleport a player after specified amount of seconds)
     
  9. Offline

    nite

    Mr360zack I tried this and it kept giving me errors on "p" "time" "loc"

    I did this
    Code:java
    1. public void endDuel(Player player) {
    2. FileManager fm = this.plugin.getFileManager();
    3. ItemManager im = this.plugin.getItemManager();
    4. String playerName = player.getName();
    5.  
    6. if(! arena.getPlayers().getSize() == 1) return;
    7. Bukkit.getScheduler().scheduleDelayedTask(plugin, new BukkitRunnable(){
    8.  
    9. public void run(){
    10. arena.getPlayers().clear();
    11. }
    12.  
    13. }, 20 * 25);
    14.  
    15.  
    16. final DuelArena arena = getPlayersArena(playerName);
    17. arena.removePlayer(playerName);
    18. player.teleport(fm.getLobbySpawnLocation());
    19. if (this.plugin.isUsingSeperatedInventories()) {
    20. restoreInventory(player);
    21. }
    22. if (arena.getPlayers().size() == 1) {
    23. im.rewardPlayer(arena);
    24.  
    25. }
    26. }
    27.  
    28. public void endDuel(DuelArena arena) {
    29. ItemManager im = this.plugin.getItemManager();
    30. DuelManager dm = this.plugin.getDuelManager();
    31. FileManager fm = this.plugin.getFileManager();
    32. if (arena.getPlayers().size() == 1) {
    33. im.rewardPlayer(arena);
    34. return;
    35. }
    36. for (String player : arena.getPlayers()) {
    37. if (isFrozen(player)) {
    38. removeFrozenPlayer(player);
    39. }
    40. Player playerOut = Bukkit.getPlayer(player);
    41. if (playerOut != null) {
    42. playerOut.teleport(fm.getLobbySpawnLocation());
    43. if (this.plugin.isUsingSeperatedInventories()) {
    44. restoreInventory(playerOut);
    45. }
    46. Util.sendMsg(playerOut, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "" + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.RED + "Duel was forcefully cancelled!");
    47. }
    48. if(! arena.getPlayers().getSize() == 1) return;
    49. Bukkit.getScheduler().scheduleDelayedTask(plugin, new BukkitRunnable(){
    50.  
    51. public void run(){
    52. arena.getPlayers().clear();
    53. }
    54.  
    55. }, 20 * 25);
    56. }
    57. arena.setDuelState(DuelState.WAITING);
    58. }
    59.  
    60. }


    But I keep getting errors on "getSize" "scheduleDelayedTask" "arena".Players().clear();

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

    Wizehh

    "getSize" should be "size", and "scheduleDelayedTask" "scheduleSyncDelayedTask". As for the last method, I don't know; that looks fine to me.
     
Thread Status:
Not open for further replies.

Share This Page