Skip to content

Creating an Entity type

Elliot Ford edited this page Jun 30, 2015 · 11 revisions

What is an Entity type?

This is a subclass of Entity (outlined below). An Entity type can be used to 'cast' an entity to a different type - allowing entities to be used in an OO fashion.

By default when proxied (via DefaultEntityProxyFactory) Entity type methods are resolved into EntityMethods. This is done by EntityFunctionResolver which reads the annotations from the Entity annotations package.

What makes an Entity type?

An Entity type is a subclass of the interface Entity (see EntityFunctionResolver API docs).

An Entity type must be a subclass of Entity or another Entity type (interface). As well as default methods the following methods with annotations will resolve:

In most cases a UUID parameter can be substituted for an EntityID annotation (see API docs). This annotation is repeatable and provides several means to create a new UUID instance (constructor, random or fromString()).

@EntityID(mostSigBits = 0, leastSigBits = 1)
public Hero getHero();

Example Entity types

The most basic types can be used to mark or group entities for processing - so require no methods at all.

public interface Animal extends Entity{}
public interface FlyingAnimal extends Animal{}

Entity types can be bean-like with getters and setters.

public interface Friend extends Entity {

  @GetAttribute
  String getName();

  @SetAttribute
  void setName(String name);
}

Entities can manage other entities (which may be typed).

public interface FriendshipCircle extends Entity {

  @StreamEntities
  Stream<Friend> streamFriends();

  @GetEntities
  Set<Friend> getFriends();

  @NewEntity
  Friend newFriend();

  @KillEntities
  void killFriends();

  @KillEntity
  boolean killFriend(UUID id);

  @GetEntity
  Friend getFriend(UUID id);
}

Default methods allow Entity types to be used similarly to concrete classes.

public interface WingedBeast extends Entity {

  default void takeFlight() {
    /* Set flying */
  }
}

Marking an Entity as type

An Entity can be marked as multiple types (for easy filtering).

Entity e; // Previously created entity
e.markAsType(FlyingAnimal.class);

Entity type inheritance still applies:

assert e.isMarkedAsType(Animal.class);

Marked entities can be used to filter entities of that type:

jalse.streamEntitiesOfType(Animal.class).forEach(/* feed */);

Marking and unmarking entities with different types trigger EntityTypeEvents (see Creating an EntityTypeListener).

'casting' an Entity as type.

Entities can be 'cast' to a particular type and used as such without error. This is because Entity type methods are translated into AttributeContainer and EntityContainer methods.

Entity e; // Previously created entity
FlyingAnimal fa = Entities.asType(e, FlyingAnimal.class);

or

FlyingAnimal fa = e.asType(FlyingAnimal.class(;

Multiple Entity types

Entities can have many types and these can be easily obtained:

Set<Class<? extends Entity>> types = e.getMarkedAsTypes();

Stream<Class<? extends Entity>> typeStream = e.streamMarkedAsTypes();

All types can also easily be dropped:

e.unmarkAsAllTypes();

Clone this wiki locally