Timer Plugin

Discussion in 'Plugin Development' started by boduzapho, Jan 30, 2012.

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


    I am attempting to create a timer that will count down an amount of seconds when a player executes a slash command.

    Problem is when more than one player tries it, the last one who calls it works the others just stop.

    I had thought plugins were loaded per user, however Im now thinking they are loaded once and shared.

    In any case, has anyone else found a way to isolate this problem and fix it? I have tried scheduler, as well as normal java threads, but I get the same results.

    Here is my latest fail...

    public boolean onCommand(CommandSender sender, Command cmd,String CommandLabel, String[] args) {
            player = ((Player) sender);
            if (CommandLabel.equalsIgnoreCase("timer")) {
                if (args.length >= 1) {
                    int para = myStringToInteger(args[0]);
                        seconds = para * 20;
                        String sec = Integer.toString(seconds);
                        player.sendMessage("Server ticks (20 = 1 sec) : " + sec);
                        timerId = Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()
                            public void run()
                                seconds = (int) (seconds) % 60 ;
                                minutes = (int) (((seconds*1000) / (1000*60)) % 60);
                                String newTime = minutes+":"+seconds;
                                if (seconds <= 0)
                                    player.sendMessage("Times up boy!");
                        }, 20L, 20L);
                        return true;
                } else {
                    player.sendMessage("Requires seconds as integer parameter.");
            return true;
        public static int myStringToInteger(String str) {
            int answer = 0, factor = 1;
            for (int i = str.length() - 1; i >= 0; i--) {
                answer += (str.charAt(i) - '0') * factor;
                factor *= 10;
            return answer;
    Anyone got any ideas?

    Thanks in advance.
  2. Maybe create a class named Timer or something and store an instance for each player
    Hashtable<Player, Timer> list = new Hashtable<Player, Timer>();
  3. Offline


    I am guessing "timerId" is declared outside that function, like in global scope, right?

    If that's the case, that means you are only ever running one timer on the server. And yes, plugins are loaded once per Bukkit instance, not per-user. In this case, I would use a nice HashMap<Player,Runnable> variable so that all users can have their own timers, as well as only allowing ONE per player (two bugs with one stone, eh?). However, be wary that HashMaps like to return null values if an entry does not exist, causing NullPointerExceptions if you don't check for them.
Thread Status:
Not open for further replies.

Share This Page