-
Notifications
You must be signed in to change notification settings - Fork 8
Working with 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.
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.
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);
}
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);
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.
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);
(c) 2019-2021 pxav.
Kelp is an open-source project maintained by multiple developers. If you have additions/questions/problems to report about the wiki, feel free to create an issue here on GitHub or join the Discord
- SQL Module coming soon
- Documentation in progress