Package org.spongepowered.api.event
Class Cause
- java.lang.Object
-
- org.spongepowered.api.event.Cause
-
@DoNotStore public final class Cause extends Object implements Iterable<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 Optional<?>
after(Class<?> clazz)
Gets the object immediately after the object that is an instance of theClass
passed in.List<Object>
all()
<T> List<T>
allOf(Class<T> target)
Optional<?>
before(Class<?> clazz)
Gets the object immediately before the object that is an instance of theClass
passed in.static Cause.Builder
builder()
Creates a newCause.Builder
to make a newCause
.boolean
contains(Object object)
Checks if this cause contains of any of the providedObject
.boolean
containsType(Class<?> target)
Returns whether the target class matches any object of thisCause
.EventContext
context()
Gets the event context relating to this cause.boolean
equals(@Nullable Object object)
<T> Optional<T>
first(Class<T> target)
Gets the firstT
object of thisCause
, if available.int
hashCode()
Iterator<Object>
iterator()
<T> Optional<T>
last(Class<T> target)
Gets the last object instance of theClass
of typeT
.List<Object>
noneOf(Class<?> ignoredClass)
static Cause
of(EventContext ctx, Iterable<Object> iterable)
Constructs a new cause with the specified event context and causes.static Cause
of(EventContext ctx, Object cause)
Constructs a new cause with the specified event context and cause.static Cause
of(EventContext ctx, Object cause, Object... causes)
Constructs a new cause with the specified event context and causes.Object
root()
Gets the rootObject
of this cause.String
toString()
Cause
with(Iterable<Object> iterable)
Creates a newCause
where the objects are added at the end of the cause array of objects.Cause
with(Object additional)
Creates a newCause
where the objects are added at the end of the cause array of objects.Cause
with(Object additional, Object... additionals)
Creates a newCause
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
-
builder
public static Cause.Builder builder()
Creates a newCause.Builder
to make a newCause
.- Returns:
- The new builder
-
of
public static Cause of(EventContext ctx, Object cause)
Constructs a new cause with the specified event context and cause.- Parameters:
ctx
- The event contextcause
- The direct object cause- Returns:
- The constructed cause
-
of
public static Cause of(EventContext ctx, Object cause, Object... causes)
Constructs a new cause with the specified event context and causes.- Parameters:
ctx
- The event contextcause
- The direct object causecauses
- Other associated causes- Returns:
- The built cause
-
of
public static Cause of(EventContext ctx, Iterable<Object> iterable)
Constructs a new cause with the specified event context and causes.- Parameters:
ctx
- The event contextiterable
- 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 Object root()
Gets the rootObject
of this cause.- Returns:
- The root object cause for this cause
-
first
public <T> Optional<T> first(Class<T> target)
Gets the firstT
object of thisCause
, 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> Optional<T> last(Class<T> target)
Gets the last object instance of theClass
of typeT
.- 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 Optional<?> before(Class<?> clazz)
Gets the object immediately before the object that is an instance of theClass
passed in.- Parameters:
clazz
- The class of the object- Returns:
- The object
-
after
public Optional<?> after(Class<?> clazz)
Gets the object immediately after the object that is an instance of theClass
passed in.- Parameters:
clazz
- The class to type check- Returns:
- The object after, if available
-
containsType
public boolean containsType(Class<?> target)
Returns whether the target class matches any object of thisCause
.- Parameters:
target
- The class of the target type- Returns:
- True if found, false otherwise
-
contains
public boolean contains(Object object)
Checks if this cause contains of any of the providedObject
. This is the equivalent to checking based onequals(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> List<T> allOf(Class<T> target)
- 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 List<Object> noneOf(Class<?> ignoredClass)
- Parameters:
ignoredClass
- The class of object types to ignore- Returns:
- The list of objects not an instance of the provided class
-
with
public Cause with(Object additional)
Creates a newCause
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(Object additional, Object... additionals)
Creates a newCause
where the objects are added at the end of the cause array of objects.- Parameters:
additional
- The additional object to addadditionals
- The remaining objects to add- Returns:
- The new cause
-
with
public Cause with(Iterable<Object> iterable)
Creates a newCause
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
-
-