- java.lang.Object
-
- aeonics.entity.Registry<T>
-
- Type Parameters:
T
- the entity category
- All Implemented Interfaces:
Exportable
,java.lang.Iterable<T>
public class Registry<T extends Entity> extends java.lang.Object implements java.lang.Iterable<T>, Exportable
The registry contains the list of allEntity
that have been populated based on user input. The registry is organised byEntity.category()
.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <U extends Entity>
Uadd(U entity)
Adds the specified entity to its matching registryEntity.category()
.static java.lang.Iterable<Registry<? extends Entity>>
all()
Returns an iterator over all registriesjava.lang.String
category()
Returns the registry category.void
clear()
Removes all entities from this registry.void
clear(java.util.function.Predicate<T> comparator)
Removes all entities from this registry matching the specified criteria.boolean
contains(java.lang.String id)
Checks whether the specified entity exists in this registry.Data
export()
Renders this class instance to a simple data structure for rendering client-side.<U extends T>
Uget(java.lang.String id)
Fetches the entity based on its id or `name` property.<U extends T>
Uget(java.util.function.Predicate<T> comparator)
Fetches the first entity that matches the specified criteria.static boolean
has(java.lang.String type)
Checks if the registry of the given entity category exists.java.util.Iterator<T>
iterator()
Returns an iterator over all entities registered in this registrystatic <U extends Entity>
Registry<U>of(java.lang.Class<? extends Item<U>> category)
Fetches the registry of the given entity category.static <U extends Entity>
Registry<U>of(java.lang.String type)
Fetches the registry of the given entity category.Callback<Entity,Registry<T>>
onAdd()
Event callback called every time an entity is added to this registry.Callback<Entity,Registry<T>>
onRemove()
Event callback called every time an entity is removed from this registry.<U extends T>
Uput(U entity)
Adds the specified entity into this registry.<U extends T>
Uremove(java.lang.String id)
Removes the specified entity.<U extends T>
Uremove(java.util.function.Predicate<T> comparator)
Removes the first entity matching the specified criteria.int
size()
Returns the number of entities in this registry.
-
-
-
Method Detail
-
of
public static <U extends Entity> Registry<U> of(java.lang.Class<? extends Item<U>> category)
Fetches the registry of the given entity category. This is the same as callingof(StringUtils.toLowerCase(category))
- Type Parameters:
U
- The entity category- Parameters:
category
- The entity category- Returns:
- The registry containing all
Entity
for the specified category
-
of
public static <U extends Entity> Registry<U> of(java.lang.String type)
Fetches the registry of the given entity category.- Type Parameters:
U
- The entity category- Parameters:
type
- The entity category (should be or will be converted to lower case)- Returns:
- The registry containing all
Entity
for the specified category
-
has
public static boolean has(java.lang.String type)
Checks if the registry of the given entity category exists.- Parameters:
type
- The entity category (should be or will be converted to lower case)- Returns:
- true if the registry of the given entity category exists
-
add
public static <U extends Entity> U add(U entity)
Adds the specified entity to its matching registryEntity.category()
.- Type Parameters:
U
- the entity category- Parameters:
entity
- the entity- Returns:
- the entity
-
all
public static java.lang.Iterable<Registry<? extends Entity>> all()
Returns an iterator over all registries- Returns:
- a registry iterator
-
onAdd
public Callback<Entity,Registry<T>> onAdd()
Event callback called every time an entity is added to this registry. You shouldCallback.then(aeonics.util.Functions.BiConsumer)
this event handler to subscribe to events.- Returns:
- the onAdd event handler
-
onRemove
public Callback<Entity,Registry<T>> onRemove()
Event callback called every time an entity is removed from this registry. You shouldCallback.then(aeonics.util.Functions.BiConsumer)
this event handler to subscribe to events.- Returns:
- the onRemove event handler
-
category
public java.lang.String category()
Returns the registry category.- Returns:
- the registry category
-
contains
public boolean contains(java.lang.String id)
Checks whether the specified entity exists in this registry. The id is checked first and is a very cheap lookup. If nothing matches, all entities are checked to find the first `name` property that matches.- Parameters:
id
- the entity id- Returns:
- true if the entity is found, false otherwise
-
get
public <U extends T> U get(java.lang.String id)
Fetches the entity based on its id or `name` property. The id is checked first and is a very cheap lookup. If nothing matches, all entities are checked to find the first `name` property that matches.- Type Parameters:
U
- the entity type- Parameters:
id
- the entity id- Returns:
- the matching entity or null if no such entity is found
-
get
public <U extends T> U get(java.util.function.Predicate<T> comparator)
Fetches the first entity that matches the specified criteria.- Type Parameters:
U
- the entity type- Parameters:
comparator
- the matching criteria- Returns:
- the matching entity or null if no such entity is found
-
put
public <U extends T> U put(U entity)
Adds the specified entity into this registry. TheEntity.category()
should match this registry's category.- Type Parameters:
U
- the entity type- Parameters:
entity
- the entity- Returns:
- the entity
- Throws:
java.lang.IllegalArgumentException
- if the entity is null or if the category does not match
-
remove
public <U extends T> U remove(java.lang.String id)
Removes the specified entity. The id is checked first and is a very cheap lookup. If nothing matches, all entities are checked to find the first `name` property that matches. If the entity isCloseable
, it is closed.- Type Parameters:
U
- the entity type- Parameters:
id
- the entity id- Returns:
- the removed entity or null if no entity was found
-
remove
public <U extends T> U remove(java.util.function.Predicate<T> comparator)
Removes the first entity matching the specified criteria. If the entity isCloseable
, it is closed.- Type Parameters:
U
- the entity type- Parameters:
comparator
- the matching criteria- Returns:
- the removed entity or null if no entity was found
-
clear
public void clear()
Removes all entities from this registry. If the entity isCloseable
, it is closed.
-
clear
public void clear(java.util.function.Predicate<T> comparator)
Removes all entities from this registry matching the specified criteria. If the entity isCloseable
, it is closed.- Parameters:
comparator
- the matching criteria
-
size
public int size()
Returns the number of entities in this registry.- Returns:
- the number of entities in this registry
-
iterator
public java.util.Iterator<T> iterator()
Returns an iterator over all entities registered in this registry
-
export
public Data export()
Description copied from interface:Exportable
Renders this class instance to a simple data structure for rendering client-side.- Specified by:
export
in interfaceExportable
- Returns:
- a public data representation of this class instance
-
-