Continue timer when player goes offline

Discussion in 'Plugin Development' started by nathanot14, Oct 24, 2015.

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

    nathanot14

    I am maken a plugin when a player types a command he needs to wait a couple of seconds to typ the command again. Everything works fine but when a player leaves and join again the cool down is over. How can I fix this? This is my code:

    Code:
    ArrayList<Player> cooldown = new ArrayList<Player>();
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (command.getName().equalsIgnoreCase("cooldown")) {
                    if (cooldown.contains(p)) {
                        p.sendMessage(ChatColor.GRAY + "You need to wait!");
                        return true;
                    } else {
                        cooldown.add(p);
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                            @Override
                            public void run() {
                                cooldown.remove(p);
                                p.sendMessage(ChatColor.GRAY + "You can typ the command again!");
                            }
                        }, 200);
                    }
    
                }
            }
            return true;
        }
     
  2. Offline

    PixelBeaver

    I'm not too sure about that.... Maybe you could try using
    Code:
    Bukkit.getPlayer(commandsender);
    or
    Code:
    Bukkit.getOfflinePlayer(sender);
     
  3. Offline

    Reynergodoy

    making instead of maken, and type instead of typ
     
  4. Offline

    elian1203

    Try making a new HashMap<Player, Runnable> and then
    cooldown.put(p);
    cooldowntask.put(p, new Runnable(){
    public void run()
    {
    ////
     
  5. @nathanot14
    When comparing entities (including players), their entity ids are compared, and when a player relogs, they have a different entity id, making them "removed" from the list. You need to make the type of the list UUID and store the player's UUID instead.
     
  6. Offline

    Scimiguy

    Basically what Mega said.

    Player objects are different through logins, and so storing a Player object won't be the same as the one a player will have when they log in again. If you are ever storing Player objects, make sure it is only for a very short time. IF you need to keep track of players for longer, use their UUID
     
  7. Offline

    rbrick

    @Scimiguy You should never store Player objects in the first place, because of memory leaks
     
  8. Offline

    Scimiguy

    @rbrick
    Storing them short term is fine, as long as you're handling removal properly
     
  9. Offline

    Epicballzy

Thread Status:
Not open for further replies.

Share This Page