Cooldown Timer In PlayerListener

Discussion in 'Plugin Development' started by stantheman68, Nov 12, 2012.

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

    stantheman68

    Hi,

    For some reason I am getting an error when I try to add a timer in my Player Listener and config, here is my player listener code,
    Code:
    package me.stantheman68.chatcooldown;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
     
    public class PlayerListener implements Listener {
       
        public static ChatCooldown plugin;
        public static Long interval = getConfig().getLong("Seconds");
     
        public PlayerListener(ChatCooldown instance) {
            plugin = instance;
        }
        List<String> cooling = new ArrayList<String>();
       
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {
            // TODO: event checks
       
            String name = e.getPlayer().getDisplayName();
       
            // Cancel event if player is cooling down
            if(cooling.contains(name)) {
                e.getPlayer().sendMessage("I am sorry you must wait " + getConfg().getPath("Seconds: " + " More seconds!"));
                e.setCancelled(true);
            } else {
                cooling.add(name);
            }
        }
    }
    How may I fix this?
     
    kevc45 likes this.
  2. Offline

    fireblast709

    Code:java
    1. package me.stantheman68.chatcooldown;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.AsyncPlayerChatEvent;
    9.  
    10. public class PlayerListener implements Listener {
    11.  
    12. public static ChatCooldown plugin;
    13. public static long interval;
    14.  
    15. public PlayerListener(ChatCooldown instance) {
    16. plugin = instance;
    17. interval = plugin.getConfig().getLong("Seconds", 0);
    18. }
    19. List<String> cooling = new ArrayList<String>();
    20.  
    21. @EventHandler
    22. public void onChat(AsyncPlayerChatEvent e) {
    23. // TODO: event checks
    24.  
    25. String name = e.getPlayer().getDisplayName();
    26.  
    27. // Cancel event if player is cooling down
    28. if(cooling.contains(name)) {
    29. e.getPlayer().sendMessage("I am sorry you must wait " + getConfg().getLong("Seconds", 0) + " More seconds!"));
    30. e.setCancelled(true);
    31. } else {
    32. cooling.add(name);
    33. }
    34. }
    35. }

    I fixed some stuff in your code, but note that this still does not work properly. Like you never remove players, the message you send is incorrect, etc.
     
  3. Offline

    stantheman68

    How can I add a timer though?
     
  4. Offline

    fireblast709

  5. Offline

    stantheman68

  6. Offline

    cman1885

    Use System.currentTimeMillis in a hashmap and compare values.
     
Thread Status:
Not open for further replies.

Share This Page