Class Cause

  • All Implemented Interfaces:
    java.lang.Iterable<java.lang.Object>

    @DoNotStore
    public final class Cause
    extends java.lang.Object
    implements java.lang.Iterable<java.lang.Object>
    A cause represents the reason or initiator of an event.

    For example, if a block of sand is placed where it drops, the block of sand would create a falling sand entity, which then would place another block of sand. The block place event for the final block of sand would have the cause chain of the block of sand -> falling sand entity.

    It is not possible to accurately describe the chain of causes in all scenarios so a best effort approach is generally acceptable. For example, a player might press a lever, activating a complex Redstone circuit, which would then launch TNT and cause the destruction of some blocks, but tracing this event would be too complicated and thus may not be attempted.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Cause.Builder  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Optional<?> after​(java.lang.Class<?> clazz)
      Gets the object immediately after the object that is an instance of the Class passed in.
      java.util.List<java.lang.Object> all()
      Gets an List of all causes within this Cause.
      <T> java.util.List<T> allOf​(java.lang.Class<T> target)
      Gets an ImmutableList of all objects that are instances of the given Class type T.
      java.util.Optional<?> before​(java.lang.Class<?> clazz)
      Gets the object immediately before the object that is an instance of the Class passed in.
      static Cause.Builder builder()
      Creates a new Cause.Builder to make a new Cause.
      boolean contains​(java.lang.Object object)
      Checks if this cause contains of any of the provided Object.
      boolean containsType​(java.lang.Class<?> target)
      Returns whether the target class matches any object of this Cause.
      EventContext context()
      Gets the event context relating to this cause.
      boolean equals​(@Nullable java.lang.Object object)  
      <T> java.util.Optional<T> first​(java.lang.Class<T> target)
      Gets the first T object of this Cause, if available.
      int hashCode()  
      java.util.Iterator<java.lang.Object> iterator()  
      <T> java.util.Optional<T> last​(java.lang.Class<T> target)
      Gets the last object instance of the Class of type T.
      java.util.List<java.lang.Object> noneOf​(java.lang.Class<?> ignoredClass)
      Gets an immutable List with all object causes that are not instances of the provided Class.
      static Cause of​(EventContext ctx, java.lang.Iterable<java.lang.Object> iterable)
      Constructs a new cause with the specified event context and causes.
      static Cause of​(EventContext ctx, java.lang.Object cause)
      Constructs a new cause with the specified event context and cause.
      static Cause of​(EventContext ctx, java.lang.Object cause, java.lang.Object... causes)
      Constructs a new cause with the specified event context and causes.
      java.lang.Object root()
      Gets the root Object of this cause.
      java.lang.String toString()  
      Cause with​(java.lang.Iterable<java.lang.Object> iterable)
      Creates a new Cause where the objects are added at the end of the cause array of objects.
      Cause with​(java.lang.Object additional)
      Creates a new Cause where the objects are added at the end of the cause array of objects.
      Cause with​(java.lang.Object additional, java.lang.Object... additionals)
      Creates a new Cause where the objects are added at the end of the cause array of objects.
      Cause with​(Cause cause)
      Merges this cause with the other cause.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Method Detail

      • of

        public static Cause of​(EventContext ctx,
                               java.lang.Object cause)
        Constructs a new cause with the specified event context and cause.
        Parameters:
        ctx - The event context
        cause - The direct object cause
        Returns:
        The constructed cause
      • of

        public static Cause of​(EventContext ctx,
                               java.lang.Object cause,
                               java.lang.Object... causes)
        Constructs a new cause with the specified event context and causes.
        Parameters:
        ctx - The event context
        cause - The direct object cause
        causes - Other associated causes
        Returns:
        The built cause
      • of

        public static Cause of​(EventContext ctx,
                               java.lang.Iterable<java.lang.Object> iterable)
        Constructs a new cause with the specified event context and causes.
        Parameters:
        ctx - The event context
        iterable - The associated causes
        Returns:
        The built cause
      • context

        public EventContext context()
        Gets the event context relating to this cause.
        Returns:
        The event context
      • root

        public java.lang.Object root()
        Gets the root Object of this cause.
        Returns:
        The root object cause for this cause
      • first

        public <T> java.util.Optional<T> first​(java.lang.Class<T> target)
        Gets the first T object of this Cause, if available.
        Type Parameters:
        T - The type of object being queried for
        Parameters:
        target - The class of the target type
        Returns:
        The first element of the type, if available
      • last

        public <T> java.util.Optional<T> last​(java.lang.Class<T> target)
        Gets the last object instance of the Class of type T.
        Type Parameters:
        T - The type of object being queried for
        Parameters:
        target - The class of the target type
        Returns:
        The last element of the type, if available
      • before

        public java.util.Optional<?> before​(java.lang.Class<?> clazz)
        Gets the object immediately before the object that is an instance of the Class passed in.
        Parameters:
        clazz - The class of the object
        Returns:
        The object
      • after

        public java.util.Optional<?> after​(java.lang.Class<?> clazz)
        Gets the object immediately after the object that is an instance of the Class passed in.
        Parameters:
        clazz - The class to type check
        Returns:
        The object after, if available
      • containsType

        public boolean containsType​(java.lang.Class<?> target)
        Returns whether the target class matches any object of this Cause.
        Parameters:
        target - The class of the target type
        Returns:
        True if found, false otherwise
      • contains

        public boolean contains​(java.lang.Object object)
        Checks if this cause contains of any of the provided Object. This is the equivalent to checking based on equals(Object) for each object in this cause.
        Parameters:
        object - The object to check if it is contained
        Returns:
        True if the object is contained within this cause
      • allOf

        public <T> java.util.List<T> allOf​(java.lang.Class<T> target)
        Gets an ImmutableList of all objects that are instances of the given Class type T.
        Type Parameters:
        T - The type of objects to query for
        Parameters:
        target - The class of the target type
        Returns:
        An immutable list of the objects queried
      • noneOf

        public java.util.List<java.lang.Object> noneOf​(java.lang.Class<?> ignoredClass)
        Gets an immutable List with all object causes that are not instances of the provided Class.
        Parameters:
        ignoredClass - The class of object types to ignore
        Returns:
        The list of objects not an instance of the provided class
      • all

        public java.util.List<java.lang.Object> all()
        Gets an List of all causes within this Cause.
        Returns:
        An immutable list of all the causes
      • with

        public Cause with​(java.lang.Object additional)
        Creates a new Cause where the objects are added at the end of the cause array of objects.
        Parameters:
        additional - The additional object to add
        Returns:
        The new cause
      • with

        public Cause with​(java.lang.Object additional,
                          java.lang.Object... additionals)
        Creates a new Cause where the objects are added at the end of the cause array of objects.
        Parameters:
        additional - The additional object to add
        additionals - The remaining objects to add
        Returns:
        The new cause
      • with

        public Cause with​(java.lang.Iterable<java.lang.Object> iterable)
        Creates a new Cause where the objects are added at the end of the cause array of objects.
        Parameters:
        iterable - The additional objects
        Returns:
        The new cause
      • with

        public Cause with​(Cause cause)
        Merges this cause with the other cause.
        Parameters:
        cause - The cause to merge with this
        Returns:
        The new merged cause
      • iterator

        public java.util.Iterator<java.lang.Object> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Object>
      • equals

        public boolean equals​(@Nullable java.lang.Object object)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object