Java Reference
In-Depth Information
A
Player
, on the other hand, does not use
Ageable
, so you can't turn players into
babies, even if they're acting like them. Instead, a
Player
has a whole different
set of functions available, including functions to set and get the player's
experience level, food level, inventory, and so on.
Spawn Entities
You can use several functions to spawn different entities and creatures, as
well as game objects—like an Ender Pearl.
2
To create new things in the world,
we'll use functions defined in our
EZPlugin
helper instead of writing out all the
code directly. It's not that the code is particularly complicated or hard to
understand; it's just that these couple of lines of code will always be the same,
so it makes sense to use a helper function. That way you only need to use
the one-line helper function call, instead of using several lines of duplicated
code. Let's take a close look at what those helper functions actually do.
Here's the method we've been using to spawn cows, squid, and such:
public static
EntityLiving spawnEntityLiving(Location loc, EntityType type) {
EntityFactory factory = Canary.factory().getEntityFactory();
EntityLiving thing = factory.newEntityLiving(type, loc);
thing.spawn();
return
thing;
}
It's a little messy, maybe, but it's a common Java pattern. The idea is that
first you obtain a factory object, in this case, an
EntityFactory
. The factory works
as the name implies; it generates things for you. Here, it's generating new
EntityLiving
objects. But just creating a new object (even a
Cow
object) isn't
enough to make it exist in the Minecraft world. You need to tell the object to
spawn
itself.
Spawning particles is a little easier:
public static void
spawnParticle(Location loc, Particle.Type type) {
loc.getWorld().spawnParticle(
new
Particle(loc.getX(),
loc.getY(), loc.getZ(), type));
}
Here we just need to use the
spawnParticle
function in
World
, and pass it a new
Particle
initialized with individual coordinates x, y, and z. Again, it's not com-
plicated, but a helper function literally “helps” us keep code cleaner and
easier to read.
2.
In survival mode, right-clicking on an Ender Pearl will transport you to where it lands.