Java Reference
In-Depth Information
The parseArgs Function
Since there are a couple of things to check for when the user types in a
command, I've split that out into its own function instead of doing it right in
the signsCommand function.
Notice we're using a parameter from our command function that we haven't
used before: String[]args . These are any arguments that the player types in with
the command in the chat window. For example, if we type in
that will be passed to signsCommand in the args array as args[0] ; args will have a
length of 1. If we type
/signs set one Hello!
then args[0] will still be "signs" , args[1] will be "set" , args[2] will be "one" , and args[3]
will be "Hello!" .
We know that we need at least three values in the args array: it will either be
"signsnewname" or "signssetnamesomething" .
Remember that with arrays, you have to check that the array is long enough;
otherwise you'll get a lengthy error message from the server that ends up with
something like this:
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at namedsigns.NamedSigns.makeSign(
at namedsigns.NamedSigns.signsCommand(
... 15 more
We're checking the length of the arguments ourselves. But we could also set
the " min == 3 " part in the @Command annotation. That way if we don't have at
least three arguments, the system will send a nice message to the player with
our toolTip as a help message.
Safe in the knowledge that we have at least three values in the args array to
work with, let's see what the player actually typed in.
The “/signs new” Command
Here's the part for the "new" command:
// signs new sign_name
private void makeNewSign(Player me, String [] args) {
Location loc = me.getLocation();
loc.setX(loc.getX() + 1); // Not right on top of player
int y = loc.getWorld().getHighestBlockAt(( int )loc.getX(),( int )loc.getZ());
Search WWH ::

Custom Search