Class EntityPersistor<T extends org.bukkit.entity.Entity>

java.lang.Object
redempt.redlib.misc.EntityPersistor<T>

public class EntityPersistor<T extends org.bukkit.entity.Entity> extends Object
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 Type
    Method
    Description
    get()
    Gets the Entity held in this EntityPersistor.
    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.
    static <T extends org.bukkit.entity.Entity>
    EntityPersistor<T>
    wrap(T entity)
    Wraps an Entity in an EntityPersistor.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 with Object.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. Use wrap(Entity)
      Type Parameters:
      T - The type of the Entity
      Parameters:
      entity - The Entity to wrap
      Returns:
      The wrapped Entity
    • wrap

      public static <T extends org.bukkit.entity.Entity> EntityPersistor<T> wrap(T entity)
      Wraps an Entity in an EntityPersistor. Calling get() 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

      public T get()
      Gets the Entity held in this EntityPersistor. If the reference is invalid, the EntityPersistor will attempt to refresh it.
      Returns:
      The wrapped Entity