[Utility] TimeTracker

A lot of people end up using a Scheduler where it isn’t needed, or for those who understand what-is/how-to-use a Delayed Task always have to keep coding their own. No longer!

I’ve developed a little Utility (does not require a complementary plugin) which does everything you’d need a Delayed Task to do.

Say, for instance, you want a command to have a cooldown of 5 minutes?

    private TimeTracker tracker = new TimeTracker(TimeUnit.MINUTES, 5);;
    private Map<Player, TimeTracker> trackerMap = new HashMap<Player, TimeTracker>();

    public void someCommand(Player player) {
        TimeTracker playerTracker = this.trackerMap.get(player);

        if (playerTracker == null) {
            // Do Command Stuff

            this.trackerMap.put(player, this.tracker.duplicate().startTimer());
            return;
        }

        if (!playerTracker.hasEnoughTimePassed()) {
            player.sendMessage("You can't use this command again so quickly!!");
            return;
        }

        // Do Command Stuff
        this.trackerMap.put(player, playerTracker.restart());
    }

Thoughts? :smile:

4 Likes

why not implement Cloneable if you have a duplicate() method anyway (will have to rename to clone())

Because TimeTracker#duplicate() isn’t an actual clone of the class. It just instantiates a new TimeTracker object with the measurement and the waitTime. Cloning the class would involve actually passing over the startTime, as well, which I don’t actually want to do.

Something will still need to periodically call the method in which checks how much time has passed. Also- nothing new. Sorry but this isn’t all too revolutionary, and doesn’t seem like an adequate replacement to using a scheduling API. (Also I don’t mean to come off as rude).

This isn’t meant as a replacement for a Runnable. This is meant to correct cases where using a Runnable isn’t necessary, but often gets used. Did you look at the example? This is, essentially, a Utility for Delayed Tasking.

You do not need to have something periodically call it. That defeats the entire purpose, actually. And it’s not meant to be revolutionary. This is something which has been around for quite a while. It sounds as though, to me, you didn’t fully read my original post.

This is meant as a definitive way to replace individual instances of Delayed Tasks, in one easy to transport utility object.

I see, my apologies- my understanding was different.

Read through the example. :smile: There’s plenty of ways that you can use this utility, but I imagine that’ll be one of the most common.

Isn’t there a commons class (Stopwatch?) that does just that.

Not that I’m aware of. Do you know the link?

Here’s the javadoc: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/StopWatch.html

Ah, I see. I thought you had meant the SpongeCommons.

StopWatch does provide a similar foundation. However, my utility allows for a more direct approach, as well as the TimeTracker#duplicate method, which allows you to only need to create a single, originating TimeTracker, which you can then manipulate separate from the original.

hmm
could pull this into ServiceCommons if you’d like; it’d be a nice fit .3.
just have to do a little cleanup before you do

I’ll make a PR sometime, today. :smile:

Cloneable is evil. Really evil.

1 Like