Java Reference
In-Depth Information
in our main class, but we aren't going to do that with schedule-able tasks;
we'll define that code in its own class.
Mechanically, making another source-code file is easy. If you can hit your
text editor's File -> Save (or something similar), you can make a new file. As
long as you've saved it in the correct directory, the build system will see the
new file and know what to do. But that's not enough. You need to know how
to tell the plugin about the new file, and the new file about the plugin.
What Should I Put in a Class?
Beginning programmers might look at a class as just a box to toss functions
and data into. Overall, that's a bad idea—just like a messy attic or garage,
you end up with a lot of boxes full of junk spilling out and tangled up in each
Instead, any class you create should be responsible for just one thing. In
other words, any class should have only one reason to change. If it's respon-
sible for a lot of different things, then suddenly it's got a lot of reasons it may
need to change: it's fragile, and fragile code leads to suffering.
In this case, the Canary API guides us in the right direction. We will be
making our plugin class, but in order to make a scheduled task, we need to
make a second class for it as well, so we'll end up with two classes: the plugin
and the task. We'll need to get them to work with each other as well.
So let's see what it takes to make a task that we can schedule to run (when
the server thinks it's okay to run it) sometime in the future. We'll look at the
bits and pieces first, and then put it all together in a cool plugin.
Make a Runnable Task
Here's the simplest code for a task that just sends a broadcast message. It
may not do much, but you can use this piece of code as a starting template
when making your own tasks.
Just add the code you want down in the body of the run function, change the
package name, and add any imports you may need. (Remember, the common
ones are listed in Appendix 7, Common Imports , on page 253 , and you can find
others in the Canary or Java docs.)
package examplepackage;
import net.canarymod.Canary;
import net.canarymod.tasks.ServerTask;
import com.pragprog.ahmine.ez.EZPlugin;
Search WWH ::

Custom Search