Package redempt.redlib.misc
Class EntityPersistor<T extends org.bukkit.entity.Entity>
java.lang.Object
redempt.redlib.misc.EntityPersistor<T>
An Entity in Spigot may not persist if the entity it refers to is unloaded, then loaded again at a later time.
This can make development very annoying, as you have to constantly check whether the Entity is still valid or not.
EntityPersistor wraps an Entity using a proxy, and anytime a method is called on the Entity, it will check if
the Entity instance is still valid. If it isn't, it will attempt to replace it with a valid instance by re-fetching
the Entity from Bukkit.
-
Method Summary
Modifier and TypeMethodDescriptionget()
Gets the Entity held in this EntityPersistor.static <T extends org.bukkit.entity.Entity>
Tpersist
(T entity) Wraps an Entity object with a proxy which will attempt to ensure the Entity object remains valid even if the entity's chunk is unloaded, then loaded again.static <T extends org.bukkit.entity.Entity>
EntityPersistor<T>wrap
(T entity) Wraps an Entity in an EntityPersistor.
-
Method Details
-
persist
public static <T extends org.bukkit.entity.Entity> T persist(T entity) Wraps an Entity object with a proxy which will attempt to ensure the Entity object remains valid even if the entity's chunk is unloaded, then loaded again. Helpful if you need a reference to an Entity over a long period of time which must not be broken. Note that any wrapped Entity will not interact withObject.equals(Object)
reflexively. You must call .equals() on the Entity which has been wrapped, not on another Entity comparing it to this one. This could not be avoided, unfortunately, but as long as you are aware of that, it should work fine. Seems to break in 1.8 because of API fuckery. Usewrap(Entity)
- Type Parameters:
T
- The type of the Entity- Parameters:
entity
- The Entity to wrap- Returns:
- The wrapped Entity
-
wrap
Wraps an Entity in an EntityPersistor. Callingget()
will refresh the reference if it is invalid. Use for 1.8- Type Parameters:
T
- The type of the Entity- Parameters:
entity
- The Entity to wrap- Returns:
- An EntityPersistor wrapping the given Entity
-
get
Gets the Entity held in this EntityPersistor. If the reference is invalid, the EntityPersistor will attempt to refresh it.- Returns:
- The wrapped Entity
-