Java Reference
In-Depth Information
Let's start with a
Location
, and then a number of blocks square. The location
will be one corner of a large square, and the square will then extend for some
number in the x and z directions. So we'll change the function signature to
look like this:
public void
spawnCows(Location start,
int
size) {
Then we can decide later what to pass in. For testing, we can pass in your
Player
location and a number you pick. Let's wire that up now.
Ah, one small hitch: a command function passes us the arguments to a user
command as an array of
String
objects. We don't want a
string
to pass to
testSpawnCows()
, though—we want an
int
. How do you do that conversion, again?
A Google search for "java convert string to int" reveals the magic incantation:
int
foo =
Integer
.parseInt(
"1234"
);
Now we can finish up the command to call
spawnCows
from our test command:
@Command(aliases = {
"testspawncows"
},
description =
"Test cow spawning"
,
permissions = {
""
},
min = 2,
// Number of arguments
toolTip =
"/testspawncows <number to spawn>"
)
public void
testSpawnCommand(MessageReceiver caller,
String[]
args) {
if
(caller
instanceof
Player) {
Player me = (Player)caller;
Location loc = me.getLocation();
spawnCows(loc,
Integer
.parseInt(args[1]));
}
}
Okay,
now
it's safe to add the code to implement
spawnCows
, because we have
a way to check it. Professionals work like this: write some way of checking
your code before you write the code itself. We can't quite do it all automatically
—we still have to manually log in to the game and visually confirm that we're
spawning creeper cows, but it's a similar idea.
So we need to start making some cows! We'll use a
for
loop, the starting loca-
tion, and the size to create a new location to spawn each new cow. How many
cows to spawn? Oops. We didn't think about that. Better add that to the
function signature:
public void
spawnCows(Location start,
int
size,
int
number) {
and pass it in from the
testSpawnCommand()
for testing:
spawnCows(loc,
Integer
.parseInt(args[1]),
Integer
.parseInt(args[2]));