Java Reference
In-Depth Information
to the plugin. For example, in an object function (a method ) like your construc-
tor or enable , you'd set plugin=this .
You might want to hang on to the task variable, but it's not necessary. With
or without it, you've created an ExampleTask which will schedule the run function
to be executed sometime in the future.
Schedule to Run Once, or Keep Running
You can specify how your task is run by setting a delay until it starts, and
setting whether it should be run just once or keep running. Have a look at
the first thing we do in our constructor: the call to super at . The call to super
will in turn call the constructor in the parent class, ServerTask , with the needed
information.
The ServerTask constructor takes three arguments: the server itself, a delay to
wait before running, and a boolean flag that should be true if this task should
keep running continuously, or false if it should be run only once (a one-shot).
If you set it to run continuously, the task will run forever until you cancel it.
We could change the previous example to delay for 60 seconds before running
once:
super(Canary.getServer(), 1200, false);
At 20 ticks per second, 1200 ticks will be about 60 seconds (one minute).
Plugin: CowShooter
Now we have enough parts to make a really fun plugin: the CowShooter . If you
have a piece of leather in your hand in the game world, you can click to shoot
a flaming cow out into the world. When the cow hits the ground, it will explode
in a ball of flame, fire, and hamburger.
This plugin is a little different from what we've seen so far; there's no @Command
section at all. It's driven entirely by events.
One problem with a flaming cow is that it won't stay flaming forever, or even
for very long. Normally a flaming cow will catch fire, then the cow dies and
the fire goes out. That's not quite suitable for our purposes: we need the cow
to stay on fire as long as it's flying through the air.
To make that happen, we'll schedule a task. The task will keep repeating as
long as the cow is in the air, and in that task we can keep the cow alive and
on fire until it hits the ground.
Here's the code for the main event listener:
 
 
Search WWH ::




Custom Search