Java Reference
In-Depth Information
You can also run some interesting commands. For instance, you can execute
a command as if you were Fred:
fred.executeCommand(
"tell mary179 I love you"
)
Now Mary will think that Fred sent her a love note. Let's play with Fred's
Location
and see what other mischief we can create.
Location where = fred.getLocation();
Now the variable we named
where
will point to a
Location
object that represents
Fred's location in the world.
Making Objects
You can also make a new location from scratch:
double
x, y, z;
x = 10;
y = 0;
z = 10;
Location whereNow =
new
Location(x, y, z);
Or do it in one step:
Location whereNow =
new
Location(10, 0, 10);
And that's how to make a new object in Java: by using the
new
keyword.
When you use
new
to create an object, Java will create the object for you and
run its
constructor
: a function that's named the same as the class (for instance,
publicLocation()
). The constructor gives you the chance to set up anything in the
object that needs setting. It doesn't return anything and isn't declared with
a return type; Java automatically returns the new object after you've done
your setup.
We'll discuss how to make our own object definitions a bit later in the topic,
but for now we'll use what Minecraft has given us plus our plugin skeleton.
And now armed with a location, you can whisk Fred away:
fred.teleportTo(whereNow);
Suddenly Fred will find himself…suffocating in bedrock (because
y
in this
location is zero). Ouch.