Java Reference
In-Depth Information
loc.setY(y);
signs.put(args[2], loc);
setBlockAt(loc, BlockType.SignPost);
}
First we grab a handy block next to the player (
getX() +1
) and get the highest
block at that location with
getHighestBlockAt()
. That way we won't be putting the
sign underwater or in bedrock or anything.
Next we save this block's location to the hash, using the
name
the player gave
us (
args[2]
).
Finally we set that block's type to
BlockType.SignPost
. Now it's a sign.
The “/signs set” Command
And here's the part for the
"set"
command:
// signs set sign_name line1
private void
setSign(Player me,
String []
args) {
String
name = args[2];
String
msg = args[3];
if
(!signs.containsKey(name)) {
// No such named sign
me.chat(
"No sign named "
+ name);
return
;
}
Location loc = signs.get(name);
World world = loc.getWorld();
Sign sign = (Sign)world.getTileEntity(world.getBlockAt(loc));
sign.setTextOnLine(msg, 0);
sign.update();
}
Notice that we're setting two local variables,
name
and
msg
, to
args[2]
and
args[3]
.
Why bother? Aren't they the same thing? Yes, they are, but it's a lot easier to
read
name
instead of reading
args[2]
and trying to remember that 2 is the name
and 3 is the message.
Next we'll check that we really have an entry in the hash for
name
, and if not
we'll complain to the player. Otherwise we can safely get the location for the
sign block.
Next is the bit of magic. We can get the block at the right location, but it's
still a block—a
net.canarymod.api.world.blocks.Block
. A
Block
doesn't know anything
about the functions of a
Sign
. A
Sign
is one kind of
Block
, so we'll have to convince
Java to make a
net.canarymod.api.world.blocks.Sign
out of it.