ArrayLists and Cooldowns

Discussion in 'Plugin Development' started by benzimmer123, Feb 25, 2014.

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

    benzimmer123

    Please tell me what is wrong with this code, i have been trying to fix it for a very long time...
    Thanks in advance;)

    CODE:

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e) {
    3. Player p = e.getPlayer();
    4. if (tpcooldown.contains(p)) {
    5. tpcooldown.remove(p);
    6. }
    7. }
    8.  
    9. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    10. final Player p = (Player) sender;
    11. if (commandLabel.equalsIgnoreCase("spawn")) {
    12. p.sendMessage(ChatColor.GOLD + "Teleportation commencing in 5 seconds...");
    13. tpcooldown.add(p);
    14. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    15. public void run() {
    16. if (!tpcooldown.contains(p)) {
    17. p.sendMessage(ChatColor.RED + "You must have moved.");
    18. } else {
    19. p.teleport(p.getWorld().getSpawnLocation());
    20. p.sendMessage(ChatColor.GREEN + "Teleported to spawn!");
    21. tpcooldown.remove(p);
    22. }
    23. }
    24. }, 100);
    25. }
    26. return false;
    27. }
     
  2. Offline

    Traks

    What is the problem? If it's not removing players, try using the name of the players in the list instead of the player instances
     
  3. Offline

    benzimmer123

    Traks
    Underlined red...
    Code:java
    1. tpcooldown.add(p.getName());
     
  4. Offline

    Alshain01

    Show us the declaration.
     
  5. Offline

    benzimmer123

  6. Offline

    Alshain01

    That isn't the declaration.
     
  7. Offline

    Traks

    Change your declaration of tpcooldown to use a String instead of a Player
     
  8. Offline

    benzimmer123

    Traks
    Done and makes no difference. I added debug messages like this...
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e) {
    3. Player p = e.getPlayer();
    4. if (tpcooldown.contains(p.getName())) {
    5. tpcooldown.remove(p.getName());
    6. p.sendMessage("Test1");
    7. } else {
    8. p.sendMessage("Test2");
    9. }
    10. }
    11.  
    12. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    13. final Player p = (Player) sender;
    14. if (commandLabel.equalsIgnoreCase("spawn")) {
    15. p.sendMessage(ChatColor.GOLD + "Teleportation commencing in 5 seconds...");
    16. tpcooldown.add(p.getName());
    17. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    18. public void run() {
    19. if (!tpcooldown.contains(p.getName())) {
    20. p.sendMessage("Test3");
    21. } else {
    22. p.teleport(p.getWorld().getSpawnLocation());
    23. p.sendMessage(ChatColor.GREEN + "Teleported to spawn!");
    24. tpcooldown.remove(p.getName());
    25. }
    26. }
    27. }, 100);
    28. }
    29. return false;
    30. }


    P.S The one that gets sent is test2
     
  9. Offline

    Konkz


    Can you show us how you are declaring the ArrayList? It should be something such as

    Code:java
    1. ArrayList<String> tpcooldown = new ArrayList<String>();
     
  10. Offline

    benzimmer123

    Konkz
    Exactly that...
    Code:java
    1. ArrayList<String> tpcooldown = new ArrayList<String>();
     
  11. Offline

    KingKillerCZ

    Hi , [diamond]
    Please Can I Got That Plugin ?
    Spawn - Teleportion In Cooldown


    Thank You ,
    Donga - KingKillerCZ
     
  12. Offline

    tamajpm

    If you use a ArrayList instead of a HashMap you can get problems with the players. Becouse i think you want for a player 15 seconds cooldown for example. If there are 10 seconds over in the delayed task the player gets a cooldown of 5 seconds. Becouse the 10 seconds are already done. If i make a cooldown i use a hashmap with the values String for the name of the player and Integer for the amount of cooldown. Then i use a repeating task and get all the players in the hashmap. If the players cooldown is >= 1 i set the cooldown from the player to the currentcooldown - 1. If the cooldown == 1 i remove the player from the hashmap.
     
  13. Offline

    KingKillerCZ

    tamajpm [diamond]

    Please Can , You Create SpawnCooldown Plugin ?



    Thank You ,
    Donga - KingKillerCZ
     
  14. Offline

    tamajpm

    Do you want i create a plugin so you can do /spawn and it is going to give you a cooldown?
     
  15. Offline

    KingKillerCZ

    yeah , but i wana /setspawn and /spawn :D with cooldown :D
     
  16. Offline

    benzimmer123

    tamajpm
    An ArrayList should work fine, you will hardly ever get problems with the players because each player has its own separate instance. Using an ArrayList will also be less costly.
     
  17. Offline

    badboystee

    Just a quick tip at the end of the boolean were return false; is change to return true; :)
     
Thread Status:
Not open for further replies.

Share This Page