Solved Problem with cooldown

Discussion in 'Plugin Development' started by BDKing88, Apr 23, 2014.

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

    BDKing88

    Hey everyone, I'm having a slight problem with the cooldown in a plugin I'm making. When I type in the random teleport command, it only works once and then it says it's still cooling down at which point it should not be... If someone could help figure this out, it'd be much appreciated!
    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. ArrayList<Player> cooldown = new ArrayList<Player>();
    4.  
    5. public void onEnable() {
    6. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    7. getConfig().options().copyDefaults(true);
    8. saveConfig();
    9. }
    10.  
    11. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    12. final Player p = (Player) sender;
    13. Location originalLocation = p.getLocation();
    14.  
    15. if(cmd.getName().equalsIgnoreCase("randomlocation")) {
    16.  
    17. Random r = new Random();
    18.  
    19. Location tploc = null;
    20.  
    21. int x = r.nextInt(100) + 1;
    22. int y = 150;
    23. int z = r.nextInt(100) + 1;
    24.  
    25. boolean isOnLand = false;
    26.  
    27. while (isOnLand == false) {
    28. tploc = new Location(p.getWorld(), x, y, z);
    29. if(tploc.getBlock().getType() != Material.AIR) {
    30. isOnLand = true;
    31. } else y--;
    32. }
    33. p.getLocation().getWorld().playEffect(p.getLocation(), Effect.MOBSPAWNER_FLAMES, 8);
    34.  
    35. p.teleport(new Location(p.getWorld(), tploc.getX(), tploc.getY() + 1, tploc.getZ()));
    36.  
    37. p.sendMessage(ChatColor.AQUA + "You have been teleported to a random location!");
    38.  
    39. p.getLocation().getWorld().playEffect(p.getLocation(), Effect.SMOKE, 8);
    40. cooldown.add(p);
    41. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    42. public void run() {
    43. cooldown.remove(p);
    44. }
    45. }, getConfig().getInt("CooldownTime") * 20);
    46. return false;
    47. }
    48.  
    49. if(cooldown.contains(p)) {
    50. p.sendMessage(ChatColor.RED + "Teleportation machine cooling down!");
    51. p.teleport(originalLocation);
    52. }
    53.  
    54. if(!(sender instanceof Player)) {
    55. sender.sendMessage(ChatColor.RED + "You can only use this command ingame");
    56. return true;
    57. }
    58.  
    59. if(!(sender.hasPermission("srl.use"))) {
    60. sender.sendMessage(ChatColor.RED + "You don't have permission");
    61.  
    62. }
    63. return false;
    64.  
    65. }
    66.  
    67. }
    68.  
     
  2. Offline

    stink123456

    Not sure but note that you add the player to the cooldown and then check if he is added, this will result in him teleporting back instantly
     
  3. Offline

    Zwander

    Does your scheduled task actually run? Thats the first thing to check.

    On a side note, I would use System.currentTimeMillis(). Hashmap PlayerID against the time the last used the command, and then when they try and run the command, make sure that the time difference is greater than the cooldown. Much cleaner that way.
     
  4. Offline

    BDKing88

    Zwander
    The runnable is working, and I don't think I need to focus on cleaning it up just yet, but thanks for the tips!
     
  5. Offline

    Zwander

    BDKing88
    The other thing is, you shouldn't be storing players, you should be storing the IDs as strings.

    Actually, 2 more things.
    1. Why is your cooldown check outside of your command check? Your cooldown check will run no matter what command you run which is... Silly.
    2. As stink pointed out, you add your player to the cooldown list when they run the command, then teleport them back if they are added to it! So they will always be teleported back and denied the command.
    Your code structure is completely out of wack. It should be:
    Code:
    if (player is cooled down){
      teleport
    }
    else{
      Send denial message.
    }
     
    
    The way you have it is also inefficient, if every player on the server spammed the command, your code would have them teleporting all over the place and causing lag. My version would just be sending the message.
     
  6. Offline

    BDKing88

    Zwander
    Well, if it you just sent them a message saying they couldn't tp, they'd still teleport and all they would get is a message saying they couldn't? How would I go about adding a player AND a string to a hashmap? What do I put for the String?

    I fixed it, thanks for the help guys!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page