Skip to content

Working with KelpEntities

PXAV edited this page Jun 30, 2020 · 1 revision

Why use KelpEntities?

KelpEntities are a way to make entities version independent, which is important in some cases:

  • EntityType names have changed in the different versions
  • Entity mechanics have changed: In 1.8 you could create an elder guardian by spawning a normal guardian and setting its property elder to true. But in newer versions, there is a separate class/entity type for elder guardian, which makes it impossible to create such entities across different versions.

If you ever face such problems, KelpEntities is the solution for you.

How to create KelpEntities?

Unlike normal bukkit entities, you do not have to create your entity by spawning it directly, but can first create an instance and then spawn it. For this, you have to inject the KelpEntityFactory class. I'll also use players to demonstrate some things, so I inject the KelpPlayerRepository as well.

private KelpPlayerRepository playerRepository;
private KelpEntityFactory entityFactory;

@Inject
public EntityTest(KelpPlayerRepository playerRepository, KelpEntityFactory entityFactory) {
  this.playerRepository = playerRepository;
  this.entityFactory = entityFactory
}

Then, inside a join listener, for example, you can create your entity instance:

KelpEntity entity = entityFactory.newKelpEntity(KelpEntityType.ZOMBIE, player.getLocation());

The first parameter is your entity type and the second one is the location, where it should be spawned later.

Spawning entities

To spawn an entity you can simply call its #spawn() method. This will make it visible for all players in the current world. Note that I have added a small delay in this case because you cannot spawn entities directly on a player's join.

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
  KelpPlayer player = playerRepository.getKelpPlayer(event.getPlayer());

  Bukkit.getScheduler().runTaskLater(plugin, () -> {
    KelpEntity entity = entityFactory.newKelpEntity(KelpEntityType.ZOMBIE, player.getLocation());
    entity.spawn();
  }, 10L);
}

Working with specific entity sub-types

As you might have realized, the normal KelpEntity class does not contain entity-specific methods like setBaby() for a zombie for example. To access such specific methods, you have to cast your entity to the desired sub-type:

KelpEntity entity = entityFactory.newKelpEntity(KelpEntityType.ZOMBIE, player.getLocation());
ZombieEntity zombie = (ZombieEntity) entity; 
zombie.setBaby(false);

Living entities

Another sub-type of the KelpEntity is the LivingKelpEntity which is assigned to all entities, which can be alive like Guardians, Sheeps, etc. (and even zombies! ^^). If you need methods provided by this class, simply cast your entity:

KelpEntity entity = entityFactory.newKelpEntity(KelpEntityType.ZOMBIE, player.getLocation());
LivingKelpEntity livingEntity = (LivingKelpEntity) entity;
livingEntity.getEyeLocation(); // an example method you can now call

Note: KelpPlayer is also a sub-type of a living entity.

Converting a bukkit entity to a KelpEntity

Sometimes, when the Bukkit-API returns you a normal bukkit entity and you want to convert it to a KelpEntity, you can simply use the KelpEntityFactory.

entityFactory.getKelpEntity(bukkitEntity);
Clone this wiki locally