Java Reference
In-Depth Information
Try This Yourself
Well, that was fun! Let's try something a little different: adding a new command
to this plugin, all on your own. Add a new command to the
SkyCmd
plugin that
creates ten squid—a squid bomb.
You'll add annotations for a command named
"squidbomb"
, and you'll use a
for
loop and one of our helper functions.
The
spawnEntityLiving
is documented to take a location and the thing you want
to spawn:
spawnEntityLiving(newloc, EntityType.SQUID);
spawnEntityLiving
returns an
Entity
, but we won't be using that right now.
For instance, to spawn a squid, you need to
importnet.canarymod.api.entity.EntityType
up at the top of the file. Then later in your function, pass
EntityType.SQUID
to
the
spawnEntityLiving
function. With that we can make a simple “squid bomb”:
import
net.canarymod.api.entity.EntityType;
//... other parts not shown
// Spawning some squid. Derp.
for
(
int
i = 0; i < 10; i++) {
spawnEntityLiving(location, EntityType.SQUID);
}
Use that in your new command in our
SkyCmd
plugin. Don't forget to do the
following:
•Add your new command (
squidbomb
) to
SkyCmd
using the
@Command
annota-
tion as we've seen previously, and your new function.
•Recompile and install using
build.sh
.
•Stop and restart the server to pick up the change.
You'll need to add a new command annotation, which looks like this:
@Command(aliases = {
"squidbomb"
},
description =
"Drop a fixed number of squid on your head."
,
permissions = {
""
},
toolTip =
"/squidbomb"
)
Now, that's a little bit boring—all the squid kind of pile on top of each other.
It might be better to randomize the location for each squid. Java provides a
function,
Math.random()
, that will give us a random number that ranges from 0
up to (but not including) 1.