Java Reference
In-Depth Information
In the
@Command
spec, we're setting the minimum number of arguments to
2
.
That way we don't have to write any code to check it ourselves; the system
will do it automatically. Then in
stuckCommand
itself, we'll try to get the named
player, which may or may not work. If it doesn't work (if there's no player
online with that name), we'll fall out of the
if
body and return without doing
anything.
If it does work (that is, if we found the player), then we'll go ahead and call
stuck
, passing in the player object that we got from the server.
Here's the beginning of the
stuck
function:
public void
stuck(Player player) {
Location loc = player.getLocation();
int
playerX = (
int
) loc.getX();
int
playerY = (
int
) loc.getY();
int
playerZ = (
int
) loc.getZ();
loc.setX(playerX + 0.5); loc.setY(playerY); loc.setZ(playerZ + 0.5);
player.teleportTo(loc);
The first thing we'll do inside of the
stuck
function is get the player's current
location in
loc
. Over the next few lines, we'll set up to teleport the player to
the center of the block he or she is stuck in right now. That makes it easier
to plunk blocks down all around the player.
And how are we going to do that, exactly? Well, we know that a player takes
up two blocks. The location we got for the player is really where the character's
legs and feet are. The block on top of that (y+1) is the player's head and chest.
So we want a bunch of blocks, arranged like a stack of two blocks on all four
sides of the player, plus a block underneath and one on top. That should be
ten blocks in all, as you can see in
Figure 3,
Trapping a player in blocks
, on
page 120
.
We know where each of those blocks goes, based on the player's location. So
we've got a case where we need ten sets of coordinates, each one offset from
the player's base block. We need a list of lists.
And that's what you'll see next. It's an
int
array of ten elements, and each
element is an
int
array of three offsets, one each for x, y, and z values:
int[][]
offsets = {
//x, y, z
{0, -1, 0},
{0, 2, 0},
{1, 0, 0},
{1, 1, 0},