<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.traincarts.net/index.php?action=history&amp;feed=atom&amp;title=BKCommonLib%2FTask</id>
	<title>BKCommonLib/Task - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.traincarts.net/index.php?action=history&amp;feed=atom&amp;title=BKCommonLib%2FTask"/>
	<link rel="alternate" type="text/html" href="https://wiki.traincarts.net/index.php?title=BKCommonLib/Task&amp;action=history"/>
	<updated>2026-05-09T11:24:44Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.13</generator>
	<entry>
		<id>https://wiki.traincarts.net/index.php?title=BKCommonLib/Task&amp;diff=7850&amp;oldid=prev</id>
		<title>RyanDo at 09:47, 12 October 2025</title>
		<link rel="alternate" type="text/html" href="https://wiki.traincarts.net/index.php?title=BKCommonLib/Task&amp;diff=7850&amp;oldid=prev"/>
		<updated>2025-10-12T09:47:06Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 09:47, 12 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[BKCommonLib|« Go back]]&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Introduction==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Introduction==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The Task class is a wrapper class for starting Bukkit scheduler tasks (synchronized only). They can be used to execute a task with a certain tick delay, or with a certain tick interval. They can easily be started and stopped.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;The Task class is a wrapper class for starting Bukkit scheduler tasks (synchronized only). They can be used to execute a task with a certain tick delay, or with a certain tick interval. They can easily be started and stopped.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key pulseco1_mediawiki:diff::1.12:old-1546:rev-7850 --&gt;
&lt;/table&gt;</summary>
		<author><name>RyanDo</name></author>
	</entry>
	<entry>
		<id>https://wiki.traincarts.net/index.php?title=BKCommonLib/Task&amp;diff=1546&amp;oldid=prev</id>
		<title>RyanDo: Created page with &quot;« Go back  ==Introduction== The Task class is a wrapper class for starting Bukkit scheduler tasks (synchronized only). They can be used to execute a task with...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.traincarts.net/index.php?title=BKCommonLib/Task&amp;diff=1546&amp;oldid=prev"/>
		<updated>2021-06-09T19:05:30Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&lt;a href=&quot;/p/BKCommonLib&quot; title=&quot;BKCommonLib&quot;&gt;« Go back&lt;/a&gt;  ==Introduction== The Task class is a wrapper class for starting Bukkit scheduler tasks (synchronized only). They can be used to execute a task with...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[BKCommonLib|« Go back]]&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
The Task class is a wrapper class for starting Bukkit scheduler tasks (synchronized only). They can be used to execute a task with a certain tick delay, or with a certain tick interval. They can easily be started and stopped.&lt;br /&gt;
&lt;br /&gt;
==Usage==&lt;br /&gt;
Tasks are abstract implementations - you need to make a new Class that extends the Task class and implement the run() method. In there you can execute your logic. In almost all cases, you will want to do something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Task myTask = new Task(MyPlugin.plugin) {&lt;br /&gt;
    @Override&lt;br /&gt;
    public void run() {&lt;br /&gt;
        Bukkit.broadcastMessage(&amp;quot;Test after 10 ticks!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}.start(10);&amp;lt;/pre&amp;gt;&lt;br /&gt;
As you can see, all start() and stop() methods return the &amp;#039;this&amp;#039; Task instance, allowing calls to be chained and the Task to be stored in a single line. In case of a task in your own plugin, this is a general way of doing it:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;public class MyPlugin extends JavaPlugin {&lt;br /&gt;
    private Task myTask;&lt;br /&gt;
&lt;br /&gt;
    @Override&lt;br /&gt;
    public void onEnable() {&lt;br /&gt;
        myTask = new Task(this) {&lt;br /&gt;
            @Override&lt;br /&gt;
            public void run() {&lt;br /&gt;
                Bukkit.broadcastMessage(&amp;quot;This message is displayed about every second&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
        }.start(20, 20);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Override&lt;br /&gt;
    public void onDisable() {&lt;br /&gt;
        Task.stop(myTask);&lt;br /&gt;
        myTask = null;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
It is possible to use myTask.stop() instead, but in the event that myTask is not initialized (and is null) Task.stop is more secure, as it includes a null check. Who knows, perhaps there was an error while enabling or for some other reason your task ended up nulled sooner.&lt;br /&gt;
&lt;br /&gt;
==Start types==&lt;br /&gt;
You can start in multiple ways:&lt;br /&gt;
&lt;br /&gt;
* Task.start() - runs the task a single time at the end of the current tick&lt;br /&gt;
* Task.start(10) - runs the task a single time in 10 ticks&lt;br /&gt;
* Task.start(10, 20) - runs the task every 20 ticks, with a delay of 10 ticks before starting to repeat&lt;br /&gt;
==Next-tick tasks==&lt;br /&gt;
Since you have to start these tasks so often, there is an alternative to using the Task class for executing next-tick tasks: CommonUtil.nextTick. It allows you to schedule a Runnable to execute at the end of the current tick, similar to how Task.start() works. Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;CommonUtil.nextTick(new Runnable() {&lt;br /&gt;
    @Override&lt;br /&gt;
    public void run() {&lt;br /&gt;
        Bukkit.broadcastMessage(&amp;quot;Next tick message!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
});&amp;lt;/pre&amp;gt;&lt;br /&gt;
You do not have to pass in a Plugin instance, and it is also impossible to cancel the task once scheduled. When BKCommonLib disables all remaining tasks are discarded - only use this to schedule tasks while the server is enabling or running, not when disabling.&lt;br /&gt;
&lt;br /&gt;
==Tips for passing in parameters==&lt;br /&gt;
It is possible that you want to use data obtained in an event or function to pass into the scheduled task - so how to easily use it? Making your own Task wrapper class with all these variables stored is possible, but also very inefficient. Java has a nice trick for that: final variables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;@EventHandler&lt;br /&gt;
public void onPlayerJoin(PlayerJoinEvent event) {&lt;br /&gt;
    final Player player = event.getPlayer();&lt;br /&gt;
    new Task(plugin) {&lt;br /&gt;
        @Override&lt;br /&gt;
        public void run() {&lt;br /&gt;
            player.sendMessage(&amp;quot;Welcome to our server!&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }.start(40);&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Be very careful with this, as it IS prone to memory leaks. As a rule of thumb, don&amp;#039;t use final variables to pass them into repeating tasks, as these variables then never get garbage collected. Even better would be to use primitive types only - a player name instead of a Player.&lt;/div&gt;</summary>
		<author><name>RyanDo</name></author>
	</entry>
</feed>