Java Reference
In-Depth Information
Compare this to our original, very simple file. Notice right at the
top, the package statement and later the public class statement now each refer
to SkyCmd instead of HelloWorld .
Let's take a closer look at how a plugin handles a chat command like /sky .
Handle Chat Commands
The bit with the @Command at tells the system that this function, skyCommand ,
is responsible for handling the /sky command. That is, when the player types
the /sky command, your skyCommand function will be called.
The first thing we need to check is a little awkward; it turns out that the
MessageReceiver that gets passed to us here may not be a Player . It could be a
Player object, or who knows what else. We want to make sure it's really a Player ,
so we'll check for that explicitly at , using the Java keyword instanceof . This
tests to see if the thing passed in is really a Player . If it is, then we're going to
do the bulk of the command starting at . (If it's not a Player , then it's probably
a console command, if you want to allow those.)
The skyCommand function begins with another bit of magic, just like we saw
with parent/child recipes at the end of Chapter 5, Plugins Have Objects , on
page 67 . Now that we've confirmed the variable caller is really of type Player (not
just a MessageReceiver or any other parent or child), we can convert it to the
type Player , using a cast operator.
So the expression (Player)caller returns the variable caller , converted with a cast
operator to the type Player so you can assign it to the variable me . It sounds
messy, and it is a bit, but it's also something you can just copy and paste,
as we'll be using this little recipe in almost every command plugin to get a
Player object.
Now that we have a real Player object referenced by me , we can get the list of
all living entities with me.getWorld().getEntityLivingList() , which will get us all the
living entities in this world and return them in a List that we'll go through with
a for loop.
We'll go over the details of lists in the next chapter, but first we'll look at how
Location objects work. In this case, we're setting the variable target to each entry
in the list of entities as we go through the for loop. If the target is not a fellow
player, then we want to fling it skyward, which we do by changing its location
with a teleportTo() .
Search WWH ::

Custom Search