Skip to content

Injecting an entity

After completing the project setup, creating a bootstrapper and writing your entity class, you’re ready to register your entity injection in your bootstrapper’s bootstrap() function. This is done using:

Bestium.injector().register(Supplier<EntityInjection>)

The Supplier provides an EntityInjection instance. Since this registration happens very early in server startup (before Bukkit or CraftBukkit are initialized) you can’t create the EntityInjection immediately. The Supplier is only resolved once the internals are ready.

An EntityInjection is created using:

EntityInjection.builder(...).build()

You must provide four required parameters:

  1. A unique entity key, use your plugin namespace and a unique entity name:

    Key.key("my_plugin", "capybara")
  2. The entity class you’ve created:

    Capybara::class.java
  3. A factory constructing the entity from an EntityType and a Level, usually the constructor reference of your entity:

    ::Capybara
  4. A convert function that converts our NMS entity into a Bukkit type. This is usually the constructor of a CraftBukkkit entity wrapper class and depends on the base abstract class you extended:

    Base Bestium classCraftBukkit wrapper class
    CustomAbstractFishCraftFish
    CustomAbstractGolemCraftGolem
    CustomAbstractSchoolingFishPaperSchoolableFish
    CustomAbstractVillagerCraftAbstractVillager
    CustomAgeableMobCraftAgeable
    CustomAgeableWaterCreaturemissing, use CraftAgeable
    CustomAmbientCreatureCraftAmbient
    CustomAnimalCraftAnimals
    CustomEntityCraftEntity
    CustomLivingEntityCraftLivingEntity
    CustomMobCraftMob
    CustomMonsterCraftMonster
    CustomPathfinderMobCraftCreature
    CustomShoulderRidingEntitymissing, use CraftTameableAnimal
    CustomTamableAnimalCraftTameableAnimal
    CustomWaterAnimalCraftWaterMob

    Our capybara extends CustomAnimal, so we reference the constructor of CraftAnimals:

    ::CraftAnimals

After the required parameters, you can chain several additional methods:

Determines the entity our capybara ‘disguises as’. This is what the client will see.

.backingType(EntityType.PIG)

Defaults to: EntityType.SILVERFISH

Defines the default entity attributes, you should call the static attributes method created earlier.

.attributes(Capybara.createAttributes())

Defaults to: LivingEntity.createLivingAttributes() if the entity is living entity otherwise to null

Sets the category this mob belongs to, this defines whether the mob is friendly or hostile, how many instances there can be per chunk, whether it persists in world or how far from the player it despawns.

Mob categoryDescriptionMax per chunkFriendlyPersistentDespawn distance
MONSTERmonsters like Zombies or Skeletons70falsefalse128
CREATUREfriendly mobs like Sheep or Villagers10truetrue128
AMBIENTambient surface creatures like Bats15truefalse128
AXOLOTLSaxolotls5truefalse128
WATER_CREATUREfriendly water mobs like Dolphins and Squids5truefalse128
UNDERGROUND_WATER_CREATUREfriendly water mobs underground like Glow Squids5truefalse128
WATER_AMBIENTambient water creatures like Cods and Salmon20truefalse64
MISCentities that don’t naturally spawn like Boats, TNT or Projectiles-truetrue128

Our capybara is a friendly mob, so we use CREATURE:

.mobCategory(MobCategory.CREATURE)

Defaults to: MobCategory.MISC

Sets the entity model displayed if BetterModel is installed on the server. The file must be a .bbmodel (BlockBench model) bundled inside the plugin’s JAR:

.model(this, "capybara.bbmodel")

Defaults to: no model

Sets the entity model displayed if BetterModel is installed on the server. The file must be a .bbmodel (BlockBench model) file provided as an external java.io.File.

Defaults to: no model

The final capybara injection registration looks like this:

MyPluginBootstrapper.kt
override fun bootstrap(context: BootstrapContext) {
Bestium.injector().register {
EntityInjection.builder(
Key.key("my_plugin", "capybara"),
Capybara::class.java,
::Capybara,
::CraftAnimals
)
.backingType(EntityType.PIG)
.attributes(Capybara.createAttributes())
.mobCategory(MobCategory.CREATURE)
.model(this, "capybara.bbmodel")
.build()
}
}