Util Task Manager: Easy Schedulers!

Discussion in 'Resources' started by JPG2000, May 20, 2015.

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

    JPG2000

    Hello,

    I've created this resource to eliminate the need for Bukkit schedulers, and to allow programmers to make scheduler tasks easier and more efficiently. In effect, this means very easy creation of repeating or delayed tasks.

    CODE EXAMPLES (open)
    Below is a task that is delayed, and will broadcast the message "Hello" in 10 seconds:
    Code:
            new Task(Task.DELAYED, 20 * 10) {
                public void update() {
                    Bukkit.broadcastMessage("Hello!");
                }
            };

    Below is a task that is repeating every second, and will broadcast the message "HI!" at this interval:
    Code:
            new Task(Task.REPEATING, 1 * 20) {
                public void update() {
                    Bukkit.broadcastMessage("Hi!");
                }
            };

    Below is a repeating task that will broadcast a message every second, then auto cancel in 10 seconds:
    Code:
            new Task(Task.REPEATING, 1 * 20, 10 * 20) {
                public void update() {
                    Bukkit.broadcastMessage("Number: " + count);
                }
            };
    Lastly I have included a countdown timer. This will countdown from 10 to 0, then cancel:

    Code:
     new Task(Task.REPEATING, 1 * 20, 10 * 20) {
                public void update() {
                    for (Player player: Bukkit.getOnlinePlayers()) {
                        player.sendMessage("Countdown: " + (count - 10));
                       
                        if (count == 10) player.sendMessage("Lift off!");
                    }
                }
            };


    CODE INDEPTH (open)
    Firstly, this resource is very efficient. It only creates one scheduler that is repeating, and has all of those schedulers working on that loop.

    Here all all of the different tasks you can create:
    Code:
    new Task(Task.DELAYED, timeInTicks);
    new Task(Task.REPEATING), timeToRepeatInTicks);
    newtask(Task.REPEATING, timeToRepeatInTicks, autoCanceInTicksl);
    It is all self explanatory. The first argument is the task type, being delayed or repeating. The second is the time, in ticks. 20 ticks is one second. The third is the autoCancel, this is only availble in repeating task types. This is the time, in seconds, that this task will auto cancel it self in. Very useful for countdowns, etc.

    To cancel a task, just call the "cancel" method in a task like below:
    Code:
    public void update() {
    cancel(); //This will cancel  the task
    }
    Lastly, you have access to the "count" variable in the update method. This variable is an integer that keeps track of every time the update method is called in your task. If your repeating task is repeating every second, then this variable will increase by one, every second. This is useful in many ways, like countdowns (just subtract the number you want from the count variable, like 100 - count).

    SETUP (open)
    To use this resource, copy and paste these to classes in your project:

    Task
    TaskManager

    Then, go into the Task class, and change line 7. Make the static variable Plugin equal to the instance of Plugin in your project.
    To do this, if you have not already, make a static Plugin variable in your main class, and make "plugin = this" in your onEnable method.

    And that's it!


    I hope you guys find this as useful as I do. If you found it useful, leave a like, or check out the Tunnels servers (say I sent you!).

    - Jake
     
    Last edited: May 26, 2015
    Monkey_Swag and ChipDev like this.
  2. Offline

    RingOfStorms

    I feel like this isn't much better or easier than the current API, all you've really done is write a wrapper over the old one with an enum for the types rather than different method names. It's good, but I wouldn't say it is better or worth using vs calling "new BukkitRunnable(){}.runTaskLater()".

    Also, is there a reason that you've opted for using seconds rather than ticks? I would find this API rather restricting since you can't use ticks which allow more precise and shorter times.
     
  3. Offline

    Monkey_Swag

    I would also like to see it use ticks instead of seconds. Other than that it seems pretty solid to me. Good job.
     
  4. Offline

    JPG2000

    Thank you for your suggestions, I'll update this soon.
     
  5. Offline

    JPG2000

    I have updated the resource and it now is in ticks, and not seconds. Hope this helps you guys!
     
  6. Offline

    teej107

    @JPG2000 Now why would want me to use your resource over using the BukkitRunnables and schedulers?
     
  7. Offline

    xTrollxDudex

    I really only see it is shorter

    And you've got to add classes to your project.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Sep 3, 2019
  8. Offline

    JPG2000

    Honestly, it's just easier. I don't like referencing Bukkit Schedulers too much because the code is long, and I feel like some of the features included are useful. Sure it isn't game changing or ground breaking in any way, it's just superior to schedulers in my opinion.
     
  9. Offline

    nverdier

    It's really just a wrapper of the included task scheduler from Bukkit... But I guess I see your point.
     
  10. Offline

    JPG2000

    I never said it was anything but a wrapper, as it is totally a wrapper. I called it a resource because it's something that you can reference and use that's not in Bukkit. In my opinion, I prefer these, though I do see your point of it not adding many features.

    How do you guys think I can improve this? What features would you like to see?
     
  11. Offline

    nverdier

    @JPG2000 People may want to see some kind of countdown thing...
     
    xTrollxDudex likes this.
  12. Offline

    JPG2000

    Thanks for your input! I have included a countdown timer as an example. It's very simple...:)
     
  13. @JPG2000 Well let me see here...

    Your wrapper:
    Code:
    new Task(Task.DELAYED, 20 * 10) {
                public void update() {
                    Bukkit.broadcastMessage("Hello!");
                }
            };
    Total lines: 5
    Total characters (excluding indentation): 92

    Bukkit code:
    Code:
    new BukkitRunnable() {
        public void run() {
            Bukkit.broadcastMessage("Hello!");
        }
    }.runTaskLater(plugin, 20 * 10);
    Total lines: 5
    Total characters (excluding indentation): 105

    Benefits:
    Total characters saved (excluding the amount for the extra two classes): 13

    Problems:
    - Additional dependency (must add 2 classes to each project)
    - Dependency relies on deprecated methods
    - Plugin must be called Main and have a static getPlugin() method in order to function without change
    - Dependency is lacking in features (such as async)
    - Task uses an "update" method, despite the fact that a Runnable uses run() - the difference is unnecessary and may cause some to accidentally type run() out of habit.

    So all in all I'd rate this wrapper "useless" in its current form. The Bukkit API covers all of what your wrapper does, without the problems I have mentioned, with more features... I see no reason this wrapper is "superior" unless you want to argue the 13 characters you've saved, at which point there's probably something wrong with the way you're viewing programming anyway.
     
    teej107, nverdier and RainoBoy97 like this.
  14. Offline

    leet4044

    add async pls
     
  15. Offline

    teej107

  16. Offline

    JPG2000

    Well thanks for the feedback, I understand now. I'll try to edit and add some features to improve. I never claimed this was extraordinary or game changing, I just simply prefer ed it over Bukkit. For newish people too, this is easier to use. I'll make some changes, based on your suggestions, however.

    By the way, how would you go about handling a resource like this? I'm not questioning you, I'd like to know more ways to improve this!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  17. Offline

    nverdier

    Well you can't really improve it, it's just a wrapper... The only thing to do is just add features which aren't in Bukkit, I suppose.
     
    JPG2000 likes this.
  18. Offline

    JPG2000

    I'll look into a non wrapper resource. I just hate Bukkit Schedulers xD
     
  19. Offline

    GeekWithAChick

    @JPG2000

    I'm a newbie and your resource is easier to understand for me ;) Can you add cooldowns? I have no idea if it has anything to do with this.
     
  20. This seems a pretty irrational hate to me ;)

    @GeekWithAChick Cooldowns in what sense? That's something you'd have to make yourself, and it wouldn't be a great idea to use schedulers for cooldowns.
     
  21. Offline

    GeekWithAChick

    @AdamQpzm
    Yea, I thought it wouldn't xd
     
Thread Status:
Not open for further replies.

Share This Page