BKCommonLib/Controller

From BergerHealer Wiki
Jump to navigation Jump to search

« Go back

Introduction

The EntityController class contains various methods a plugin can override to alter entity behaviour.

Functionalities

  • Altering, cancelling or completely rewriting entity movement physics
  • Handling block collisions, cancelling them
  • Handling entity collisions, cancelling them
  • Handling onTick to disable or completely alter Entity logic
  • Handling damage dealt to the Entity
  • Handling entity interaction (by players)
  • Altering the name of an Entity
  • Handling when an Entity dies

How to use

Entity Controllers are used in combination with Common Entities. Assigning one is fairly simple: use setController. There is no need to manually bind the entity to the controller, setController takes care of that for you.

For example, here we assign an onBlockCollision handler for items to create an explosion.

Item item = ...;
CommonItem entity = CommonEntity.get(item);
entity.setController(new EntityController<CommonItem>() {
    @Override
    public boolean onBlockCollision(Block block, BlockFace hitFace) {
        block.getWorld().createExplosion(block.getLocation(), 5.0f);
        return false;
    }
});

What you put in the (overridden) methods is completely up to you. You can (literally) make pigs fly.

Replacing all entities

If you wish to replace all entities globally, so set controllers whenever a new Entity is added to the server, you can use the EntityAdd event provided by BKCommonLib:

@EventHandler(priority = EventPriority.LOWEST)
public void onEntityAdd(EntityAddEvent event) {
    if (!(event.getEntity() instanceof Item)) {
        return;
    } 
    CommonEntity.get((Item) event.getEntity()).setController(new CustomItemController());
}