Java Reference
In-Depth Information
To get a random number from 0 up to 5, just multiply
Math.random()
by 5. So
for instance, to get a new x-coordinate you might use an expression like
loc.getX() + (Math.random() * 5)
. When multiplying and adding, parentheses are
usually a good idea—in this case we want to multiply the random 0..1 by 5,
then add that to the original x.
Now it's your turn again: improve the squid bomb by making a new location
based on the player's location that you already have, and add a bit of random-
ness to the x- and z-coordinates. To get the squid to drop on you from above,
add 10 to the y-coordinate.
Try going through this exercise all by yourself first. In case you get stuck and
need some help, I made a whole new plugin for the squid bomb. You can see
my code and config file in
code/SquidBomb
.
Find Nearby Blocks or Entities
Canary provides a very handy feature in
net.canarymod.BlockIterator
. A
BlockIterator
lets you find all the blocks along a line in the game. Most useful is probably
the version where you pass in a
LineTracer
(made from a
Player
) and a boolean,
which is declared in the Canary API like this:
2
public
BlockIterator(LineTracer tracer,
boolean
include_air)
That gives you a
BlockIterator
object, which you can use to retrieve blocks along
the line of sight from that entity. The
boolean
flag says to include
Air
blocks,
and it works like this:
BlockIterator sightItr =
new
BlockIterator(
new
LineTracer(me), true);
while
(sightItr.hasNext()) {
Block b = sightItr.next();
// do something with this block, b
}
You might check each block along this player's line of sight and find the first
block that isn't
Air
. That would be the player's “target.” Or you could set fire
to each block along the way and then turn that target into
Lava
.
Here's a plugin that does exactly that.
Plugin: LavaVision
This plugin runs a
BlockIterator
for the player, and checks each block along the
way, setting a flame effect. The first block that isn't
Air
is the target, so we'll
set that to
Lava
.
2.
This is a recent addition to CanaryMod, and doesn't yet appear in their docs.