Interface CommandCause

  • All Superinterfaces:
    Contextual, Subject, SubjectProxy

    @DoNotStore
    public interface CommandCause
    extends SubjectProxy
    The CommandCause represents the Cause of a command, and also contains utility methods to obtain key information about said cause.

    In line with causes used in events, you may assume that the Cause.root() (from cause()) is the direct invoker of the command, though it should also be noted that the invoker and intended target of a command may be different, which will be indicated by entries in the Cause.context()

    It is very important to note that no object in the Cause is guaranteed to be a traditional "command source" - a plugin may invoke a command without pushing anything to the cause stack and thus the PluginContainer of the plugin in question will be the root of the cause.

    In the case of a command being executed as a "proxy", such as a command block executing a command by virtue of an entity stepping on a pressure plate, the direct cause will be the command block. However, the player in question will also be present in the cause stack, allowing command providers to obtain richer information about the invocation of their command. This is inline with how Causes work in Sponge and its events, for more information about how Causes work, see the Cause and CauseStackManager javadocs and associated documentation.

    The EventContext that is attached to Cause.context() may offer other indications as to how the command should be handled, in addition to using the provided cause stack:

    This object acts as a Subject, and simply redirects all calls inherited by this interface to the subject returned by subject().

    There are utility methods on this interface that provide hints as to what the implementation will select for different tasks, for example, the implementation will use the result of subject() for permission checks. Third party command consumers are under no obligation to use these utility methods as all methods obtain their information from the Cause.

    No method on this interface, apart from cause(), should be taken a guarantee of what may be present, however, they indicate what typically would be of interest to command API consumers.

    • Method Detail

      • cause

        Cause cause()
        Gets the Cause of the command invocation.
        Returns:
        The cause of the invocation.
      • contextCause

        default Cause contextCause()
        Description copied from interface: Contextual
        Get the cause describing the current state of this subject.

        This is often not based on current game state, but rather the last known state of this subject. If a subject refers to a game object that is not active in the world, the cause may be a global cause.

        Specified by:
        contextCause in interface Contextual
        Specified by:
        contextCause in interface Subject
        Specified by:
        contextCause in interface SubjectProxy
        Returns:
        the active cause
      • root

        default java.lang.Object root()
        Returns:
        The root object cause for this cause
        See Also:
        Cause.root()
      • first

        default <T> java.util.Optional<T> first​(java.lang.Class<T> target)
        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
        See Also:
        Cause.first(Class)
      • last

        default <T> java.util.Optional<T> last​(java.lang.Class<T> target)
        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
        See Also:
        Cause.last(Class)
      • before

        default java.util.Optional<?> before​(java.lang.Class<?> clazz)
        Parameters:
        clazz - The class of the object
        Returns:
        The object
        See Also:
        Cause.before(Class)
      • after

        default java.util.Optional<?> after​(java.lang.Class<?> clazz)
        Parameters:
        clazz - The class to type check
        Returns:
        The object after, if available
        See Also:
        Cause.after(Class)
      • containsType

        default boolean containsType​(java.lang.Class<?> target)
        Parameters:
        target - The object to check if it is contained
        Returns:
        True if the object is contained within this cause
        See Also:
        Cause.contains(Object)
      • contains

        default boolean contains​(java.lang.Object object)
        Parameters:
        object - The object to check if it is contained
        Returns:
        True if the object is contained within this cause
        See Also:
        Cause.contains(Object)
      • allOf

        default <T> java.util.List<T> allOf​(java.lang.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
        See Also:
        Cause.allOf(Class)
      • noneOf

        default java.util.List<java.lang.Object> noneOf​(java.lang.Class<?> ignoredClass)
        Parameters:
        ignoredClass - The class of object types to ignore
        Returns:
        The list of objects not an instance of the provided class
        See Also:
        Cause.noneOf(Class)
      • all

        default java.util.List<java.lang.Object> all()
        Returns:
        An immutable list of all the causes
        See Also:
        Cause.all()
      • subject

        Subject subject()
        Get the Subject that will be selected for permission checks during command execution (by default).

        The Subject will be selected in the following way from the Cause in cause():

        This subject may present a different view of default context than calls to Subject methods directly on this cause.

        Note: while it might be tempting to use this as the invoker of the command, the Cause.root() and this might be different. Command executors should generally use the root of the Cause as the target of their command.

        Specified by:
        subject in interface SubjectProxy
        Returns:
        The Subject responsible, if any.