Adding a timer before automatically teleporting A player in a arena

Discussion in 'Plugin Development' started by diamondcodes, Jul 26, 2014.

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

    diamondcodes

    HI,
    I really need help with this, I have public void endDuel So when the duel is over It teleports the last player in the arena to the spawn and it works just fine, But Then I put it on my server with combat log And now with combat log it wont teleport the last player because they are in combat, So i just need help with adding a working timer to this So it will teleport the player after 25 seconds. Heres my code
    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. final DuelArena arena = getPlayersArena(playerName);
    6. if(arena.getPlayers().size() == 1);
    7. player.teleport(fm.getLobbySpawnLocation());
    8. if (this.plugin.isUsingSeperatedInventories()) {
    9. restoreInventory(player);
    10. }
    11. im.rewardPlayer(arena);
    12. }
    13.  
    14.  
    15.  
    16. public void endDuel(final DuelArena arena) {
    17. ItemManager im = this.plugin.getItemManager();
    18. DuelManager dm = this.plugin.getDuelManager();
    19. FileManager fm = this.plugin.getFileManager();
    20. if (arena.getPlayers().size() == 1) {
    21. im.rewardPlayer(arena);
    22. return;
    23. }
    24. for (final String player : arena.getPlayers()) {
    25. if (isFrozen(player)) {
    26. removeFrozenPlayer(player);
    27. }
    28. Player playerOut = Bukkit.getPlayer(player);
    29. if (playerOut != null) {
    30. playerOut.teleport(fm.getLobbySpawnLocation());
    31. if (this.plugin.isUsingSeperatedInventories()) {
    32. restoreInventory(playerOut);
    33. }
    34. 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!");
    35. }
    36. arena.setDuelState(DuelState.WAITING);
    37. }
    38.  
    39. }


    I have tried adding a timer
    Code:java
    1. if(arena.getPlayers().size() == 1) return;
    2. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
    3.  
    4. public void run() {
    5. arena.getPlayers().clear();
    6. }
    7.  
    8. }, 500L);
    9. player.teleport(fm.getLobbySpawnLocation());
    10.  

    But that didnt work It just kept the player in the duel it didnt remove him from the duel arena or the duel its self
     
  2. Offline

    k9rosie

    you need to put the player.teleport() method inside the SyncDelayedTask
     
  3. Offline

    diamondcodes

    So Just?
    Code:java
    1. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, player.teleport(), new BukkitRunnable() {
     
  4. Offline

    k9rosie

    no, like this
    Code:java
    1. if(arena.getPlayers().size() == 1) return;
    2. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
    3.  
    4. public void run() {
    5. arena.getPlayers().clear();
    6. player.teleport(fm.getLobbySpawnLocation());
    7. }
    8.  
    9. }, 500L);
    10.  


    before, you had the player.teleport(fm.getLobbySpawnLocation()); outside of the scheduleSyncDelayedTask callback
     
  5. Offline

    diamondcodes

    I have tried that before But i get errors on "player" And "fm"
     
  6. Offline

    k9rosie


    what are the errors?
     
  7. Offline

    diamondcodes

    For "player" its "Variable 'player' is accessed from within inner class, needs to be declared final"

    And for "fm" its "Variable 'fm' is accessed from within inner class, needs to be declared final"
     
  8. Offline

    k9rosie


    are these errors that are being thrown from your IDE? or console? i would initiate a new player and filemanager class within the run(){} callback
     
  9. Offline

    diamondcodes

    They are from my IDE. How would i initiate a new player and filemanager class within the run(){} callback?
     
  10. Offline

    k9rosie

    can you show me your code?
     
  11. Offline

    diamondcodes

    Heres the Whole DuelManager Class
    Code:java
    1. package me.duel.duel.util;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7. import me.maple.duel.main.Duel;
    8. import me.maple.duel.threads.StartDuelThread;
    9. import org.bukkit.*;
    10. import org.bukkit.block.Block;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.PlayerInventory;
    14. import org.bukkit.scheduler.BukkitRunnable;
    15.  
    16. public class DuelManager {
    17. private Duel plugin;
    18. public Map<String, String> duelRequests;
    19. public List<String> spectatingPlayers;
    20. public List<String> frozenPlayers;
    21. public List<String> deadPlayers;
    22. public List<DuelArena> duelArenas;
    23. List<Player> namehere = new ArrayList<Player>();
    24. private static HashMap<String, ItemStack[]> inventories;
    25. private static HashMap<String, ItemStack[]> armour;
    26.  
    27. public DuelManager(Duel plugin) {
    28. this.plugin = plugin;
    29. this.duelRequests = new HashMap();
    30. this.spectatingPlayers = new ArrayList();
    31. this.frozenPlayers = new ArrayList();
    32. this.duelArenas = new ArrayList();
    33. this.deadPlayers = new ArrayList();
    34. inventories = new HashMap();
    35. armour = new HashMap();
    36. }
    37.  
    38. public List<DuelArena> getDuelArenas() {
    39. return this.duelArenas;
    40. }
    41.  
    42. public void addDuelArena(DuelArena da) {
    43. this.duelArenas.add(da);
    44. }
    45.  
    46. public DuelArena getDuelArenaByName(String duelArenaName) {
    47. for (DuelArena da : this.duelArenas) {
    48. if (da.getName().equalsIgnoreCase(duelArenaName)) {
    49. return da;
    50. }
    51. }
    52. return null;
    53. }
    54.  
    55. public boolean isInDuel(String playerName) {
    56. for (DuelArena a : getDuelArenas()) {
    57. if (a.getPlayers().contains(playerName)) {
    58. return true;
    59. }
    60. }
    61. return false;
    62. }
    63.  
    64. public String getPlayersArenaName(String playerName) {
    65. for (DuelArena a : getDuelArenas()) {
    66. if (a.getPlayers().contains(playerName)) {
    67. return a.getName();
    68. }
    69. }
    70. return null;
    71. }
    72.  
    73. public DuelArena getPlayersArena(String player1, String player2) {
    74. for (DuelArena a : getDuelArenas()) {
    75. List<String> players = a.getPlayers();
    76. if ((players.contains(player1)) && (players.contains(player2))) {
    77. return a;
    78. }
    79. }
    80. return null;
    81. }
    82.  
    83. public boolean isFrozen(String playerIn) {
    84. if (getFrozenPlayers().contains(playerIn)) {
    85. return true;
    86. }
    87. return false;
    88. }
    89.  
    90. public List<String> getFrozenPlayers() {
    91. return this.frozenPlayers;
    92. }
    93.  
    94. public void addFrozenPlayer(String playerName) {
    95. this.frozenPlayers.add(playerName);
    96. }
    97.  
    98. public void addFrozenPlayer(String senderName, String targetName) {
    99. this.frozenPlayers.add(senderName);
    100. this.frozenPlayers.add(targetName);
    101. }
    102.  
    103. public void removeFrozenPlayer(String playerName) {
    104. this.frozenPlayers.remove(playerName);
    105. }
    106.  
    107. public DuelArena getPlayersArena(String player) {
    108. for (DuelArena a : getDuelArenas()) {
    109. List<String> players = a.getPlayers();
    110. if (players.contains(player)) {
    111. return a;
    112. }
    113. }
    114. return null;
    115. }
    116.  
    117. public void sendRequest(Player duelSender, String duelTargetIn) {
    118. final String duelSenderName = duelSender.getName();
    119. if ((this.duelRequests.containsKey(duelSenderName)) && (this.duelRequests.containsValue(duelTargetIn))) {
    120. Util.sendMsg(duelSender, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.YELLOW + "You have already sent a request to " +
    121. ChatColor.AQUA + duelTargetIn + ".");
    122. return;
    123. }
    124. final Player duelTarget = Bukkit.getPlayer(duelTargetIn);
    125. if (duelTarget != null) {
    126. final String duelTargetName = duelTarget.getName();
    127. if (duelSenderName == duelTargetName) {
    128. Util.sendMsg(duelSender, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.RED + "You cannot duel yourself!");
    129. return;
    130. }
    131. Util.sendMsg(duelSender, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.GREEN + "You have sent a duel request to " + ChatColor.AQUA + duelTargetName + ".");
    132. Util.sendMsg(duelTarget, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.translateAlternateColorCodes('&', new StringBuilder("&aYou have been sent a duel request from &b").append(duelSenderName).toString()));
    133. Util.sendEmptyMsg(duelTarget, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.translateAlternateColorCodes('&', new StringBuilder("&ause &b/duel accept ").append(duelSenderName).append("&a, to accept the request. You now 30 seconds to accept!").toString()));
    134. this.duelRequests.put(duelSenderName, duelTargetName);
    135. Bukkit.getScheduler().runTaskLater(plugin, new BukkitRunnable() {
    136. public void run() {
    137. duelRequests.remove(duelSenderName);
    138. }
    139. }, 600L);
    140.  
    141. } else {
    142. Util.sendMsg(duelSender, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.AQUA + duelTargetIn + ChatColor.RED + " is not online! Did you type it correctly?");
    143. }
    144. }
    145.  
    146. public void acceptRequest(Player accepter, String senderIn) {
    147. if ((this.duelRequests.containsKey(senderIn)) && (this.duelRequests.containsValue(accepter.getName()))) {
    148. Player sender = Bukkit.getPlayer(senderIn);
    149. if (sender != null) {
    150. this.duelRequests.remove(senderIn);
    151.  
    152. startDuel(accepter, sender);
    153. return;
    154. }
    155. Util.sendMsg(accepter, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.YELLOW + "Duel sender " + senderIn + " has gone offline!, duel cancelled!");
    156.  
    157. this.duelRequests.remove(senderIn);
    158. return;
    159. }
    160. Util.sendMsg(accepter, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.RED + "You do not have any duel requests from " + ChatColor.AQUA + senderIn + ".");
    161. }
    162.  
    163. public void startDuel(Player accepter, Player sender) {
    164. String accepterName = accepter.getName();
    165. String senderName = sender.getName();
    166. List<DuelArena> arenas = getDuelArenas();
    167. FileManager fm = this.plugin.getFileManager();
    168. ItemManager im = this.plugin.getItemManager();
    169. if (arenas.size() <= 0) {
    170. Util.sendMsg(sender, Util.NO_ARENAS);
    171. Util.sendMsg(accepter, Util.NO_ARENAS);
    172. return;
    173. }
    174. for (DuelArena a : arenas) {
    175. if (a.getDuelState() == DuelState.WAITING) {
    176. a.setDuelState(DuelState.STARTING);
    177. if (fm.isDuelStartAnnouncementEnabled()) {
    178. Util.broadcastMessage(
    179.  
    180.  
    181. ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.GREEN + "A duel is Starting between " + ChatColor.AQUA + accepterName + ChatColor.GREEN + " and " + ChatColor.AQUA + senderName);
    182. }
    183. a.addPlayer(accepterName);
    184. a.addPlayer(senderName);
    185. if (fm.isUsingSeperateInventories()) {
    186. if (this.plugin.isDebugEnabled()) {
    187. SendConsoleMessage.debug("Storing inventories enabled. storing player inventories");
    188. }
    189. storeInventory(accepter);
    190. storeInventory(sender);
    191. }
    192. accepter.teleport(generateRandomLocation(a));
    193. sender.teleport(generateRandomLocation(a));
    194.  
    195. this.frozenPlayers.add(accepter.getName());
    196. this.frozenPlayers.add(sender.getName());
    197. if (fm.isUsingSeperateInventories()) {
    198. if (this.plugin.isDebugEnabled()) {
    199. SendConsoleMessage.debug("Storing inventories enabled, giving duel items.");
    200. }
    201. im.givePlayerDuelItems(accepter);
    202. im.givePlayerDuelItems(sender);
    203. }
    204. new StartDuelThread(this.plugin, sender, accepter, a).runTaskTimer(this.plugin, 20L, 20L);
    205. return;
    206. }
    207. }
    208. Util.sendMsg(accepter, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.YELLOW + "There are no free duel arenas, please try again later!");
    209. Util.sendMsg(sender, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.YELLOW + "There are no free duel arenas, please try again later!");
    210. }
    211.  
    212. private double randomGenRange(double arg0, double arg1) {
    213. double range = arg0 < arg1 ? arg1 - arg0 : arg0 - arg1;
    214. if (range < 1.0D) {
    215. return Math.floor(arg0) + 0.5D;
    216. }
    217. double min = arg0 < arg1 ? arg0 : arg1;
    218. return Math.floor(min + Math.random() * range) + 0.5D;
    219. }
    220.  
    221. private Location generateRandomLocation(DuelArena a) {
    222. World w = a.getPos1().getWorld();
    223. double x = randomGenRange(a.getPos1().getX(), a.getPos2().getX());
    224. double z = randomGenRange(a.getPos1().getZ(), a.getPos2().getZ());
    225. double y = randomGenRange(a.getPos1().getY(), a.getPos2().getY());
    226. return new Location(w, x, y + 0.5D, z);
    227. }
    228.  
    229. public void removeDuelArena(DuelArena daIn) {
    230. for (DuelArena da : getDuelArenas()) {
    231. if (da == daIn) {
    232. this.duelArenas.remove(daIn);
    233. return;
    234. }
    235. }
    236. }
    237.  
    238. public void addDeadPlayer(String playerName) {
    239. this.deadPlayers.add(playerName);
    240. }
    241.  
    242. public List<String> getDeadPlayers() {
    243. return this.deadPlayers;
    244. }
    245.  
    246. public void removedDeadPlayer(String playerName) {
    247. this.deadPlayers.remove(playerName);
    248. }
    249.  
    250. public boolean isDeadPlayer(String playerName) {
    251. if (getDeadPlayers().contains(playerName)) {
    252. return true;
    253. }
    254. return false;
    255. }
    256.  
    257. public static void storeInventory(Player p) {
    258. ItemStack[] inv = p.getInventory().getContents();
    259. ItemStack[] arm = p.getInventory().getArmorContents();
    260. inventories.put(p.getName(), inv);
    261. armour.put(p.getName(), arm);
    262. p.getInventory().clear(-1, -1);
    263. Util.sendMsg(p, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.GREEN + "Your inventory has been stored and will be restored after the Duel.");
    264. }
    265.  
    266. public static void restoreInventory(Player p) {
    267. p.getInventory().clear(-1, -1);
    268. if ((inventories.containsKey(p.getName())) && (armour.containsKey(p.getName()))) {
    269. p.getInventory().setContents((ItemStack[]) inventories.get(p.getName()));
    270. p.getInventory().setArmorContents((ItemStack[]) armour.get(p.getName()));
    271. inventories.remove(p.getName());
    272. armour.remove(p.getName());
    273. Util.sendMsg(p, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.GREEN + "Your inventory has been restored.");
    274. } else {
    275. Util.sendMsg(p, ChatColor.DARK_GRAY + "" + ChatColor.BOLD + "[" + ChatColor.DARK_PURPLE + ChatColor.BOLD + "Supreme" + ChatColor.LIGHT_PURPLE + ChatColor.BOLD + "Duel" + ChatColor.DARK_GRAY + ChatColor.BOLD + "] " + ChatColor.RED + "There was an error restoring your inventory!");
    276. }
    277. }
    278.  
    279. public void endDuel(Player player) {
    280. FileManager fm = this.plugin.getFileManager();
    281. ItemManager im = this.plugin.getItemManager();
    282. String playerName = player.getName();
    283. final DuelArena arena = getPlayersArena(playerName);
    284. if (arena.getPlayers().size() == 1) return;
    285. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new BukkitRunnable() {
    286.  
    287. public void run() {
    288. arena.getPlayers().clear();
    289. player.teleport(fm.getLobbySpawnLocation());
    290. }
    291.  
    292. }, 500L);
    293. if (this.plugin.isUsingSeperatedInventories()) {
    294. restoreInventory(player);
    295. }
    296. im.rewardPlayer(arena);
    297. }
    298.  
    299.  
    300. public void endDuel(final DuelArena arena) {
    301. ItemManager im = this.plugin.getItemManager();
    302. DuelManager dm = this.plugin.getDuelManager();
    303. FileManager fm = this.plugin.getFileManager();
    304. if (arena.getPlayers().size() == 1) {
    305. im.rewardPlayer(arena);
    306. return;
    307. }
    308. for (final String player : arena.getPlayers()) {
    309. if (isFrozen(player)) {
    310. removeFrozenPlayer(player);
    311. }
    312. Player playerOut = Bukkit.getPlayer(player);
    313. if (playerOut != null) {
    314. playerOut.teleport(fm.getLobbySpawnLocation());
    315. if (this.plugin.isUsingSeperatedInventories()) {
    316. restoreInventory(playerOut);
    317. }
    318. 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!");
    319. }
    320. arena.setDuelState(DuelState.WAITING);
    321. }
    322.  
    323. }
    324.  
     
  12. Offline

    Niknea

    diamondcodes Change line 167 to:

    PHP:
    final FileManager fm this.plugin.getFileManager();
    And line 279 to:

    PHP:
     public void endDuel(final Player player) {
     
  13. Offline

    diamondcodes

    Yes, thank you that works But Its teleports the player that died not the player still in the arena :/
     
  14. Offline

    Niknea

    diamondcodes What line are you activating the endDuel method? As I can't find it, also, why do you have two methods both named "endDuel"?
     
  15. Offline

    Mr360zack

    .. have you learned java before bukkit? or bukkit before java?
     
  16. Offline

    diamondcodes

    I am activating the endDuel method in a different class, The second endDuel method is used for when a player leaves the duel in the starting phase (Sorry for the late reply)
     
  17. Offline

    diamondcodes

Thread Status:
Not open for further replies.

Share This Page