Class WrapperExpression<T>

java.lang.Object
ch.njol.skript.lang.util.SimpleExpression<T>
ch.njol.skript.expressions.base.WrapperExpression<T>
All Implemented Interfaces:
Debuggable, Expression<T>, SyntaxElement
Direct Known Subclasses:
ExprBlock, ExprEventExpression, ExprLocation, ExprLocationOf, ExprTimeState

public abstract class WrapperExpression<T> extends SimpleExpression<T>
Represents an expression which is a wrapper of another one. Remember to set the wrapped expression in the constructor (WrapperExpression(SimpleExpression)) or with setExpr(Expression) in init().
If you override get(Event) you must override iterator(Event) as well.
  • Constructor Details

    • WrapperExpression

      protected WrapperExpression()
    • WrapperExpression

      public WrapperExpression(SimpleExpression<? extends T> expr)
  • Method Details

    • setExpr

      protected void setExpr(Expression<? extends T> expr)
      Sets wrapped expression. Parser instance is automatically copied from this expression.
      Parameters:
      expr - Wrapped expression.
    • getExpr

      public Expression<?> getExpr()
    • getConvertedExpr

      protected <R> @Nullable ConvertedExpression<T,​? extends R> getConvertedExpr(Class<R>... to)
      Description copied from class: SimpleExpression
      Converts this expression to another type. Unless the expression is special, the default implementation is sufficient.

      This method is never called with a supertype of the return type of this expression, or the return type itself.

      Overrides:
      getConvertedExpr in class SimpleExpression<T>
      Parameters:
      to - The desired return type of the returned expression
      Returns:
      Expression with the desired return type or null if it can't be converted to the given type
      See Also:
      Expression.getConvertedExpression(Class...), ConvertedExpression.newInstance(Expression, Class...), Converter
    • get

      protected T[] get(org.bukkit.event.Event e)
      Description copied from class: SimpleExpression
      This is the internal method to get an expression's values.
      To get the expression's value from the outside use SimpleExpression.getSingle(Event) or SimpleExpression.getArray(Event).
      Specified by:
      get in class SimpleExpression<T>
      Parameters:
      e - The event
      Returns:
      An array of values for this event. May not contain nulls.
    • iterator

      public @Nullable Iterator<? extends T> iterator(org.bukkit.event.Event e)
      Description copied from interface: Expression
      Returns the same as Expression.getArray(Event) but as an iterator. This method should be overriden by expressions intended to be looped to increase performance.
      Specified by:
      iterator in interface Expression<T>
      Overrides:
      iterator in class SimpleExpression<T>
      Parameters:
      e - The event
      Returns:
      An iterator to iterate over all values of this expression which may be empty and/or null, but must not return null elements.
    • isSingle

      public boolean isSingle()
      Returns:
      true if this expression will ever only return one value at most, false if it can return multiple values.
    • getAnd

      public boolean getAnd()
      Description copied from interface: Expression
      Returns true if this expression returns all possible values, false if it only returns some of them.

      This method significantly influences Expression.check(Event, Checker), Expression.check(Event, Checker, boolean) and CondIsSet and thus breaks conditions that use this expression if it returns a wrong value.

      This method must return true if this is a single expression. // TODO make this method irrelevant for single expressions

      Specified by:
      getAnd in interface Expression<T>
      Overrides:
      getAnd in class SimpleExpression<T>
      Returns:
      Whether this expression returns all values at once or only part of them.
    • getReturnType

      public Class<? extends T> getReturnType()
      Description copied from interface: Expression
      Gets the return type of this expression.
      Returns:
      A supertype of any objects returned by Expression.getSingle(Event) and the component type of any arrays returned by Expression.getArray(Event)
    • acceptChange

      public @Nullable Class<?>[] acceptChange(Changer.ChangeMode mode)
      Description copied from interface: Expression
      Tests whether this expression supports the given mode, and if yes what type it expects the delta to be.

      Use Changer.ChangerUtils.acceptsChange(Expression, ChangeMode, Class...) to test whether an expression supports changing, don't directly use this method!

      Please note that if a changer is registered for this expression's returnType this method does not have to be overridden. If you override it though make sure to return super.acceptChange(mode), and to handle the appropriate ChangeMode(s) in Expression.change(Event, Object[], ChangeMode) with super.change(...).

      Unlike Changer.acceptChange(ChangeMode) this method may print errors.

      Specified by:
      acceptChange in interface Expression<T>
      Overrides:
      acceptChange in class SimpleExpression<T>
      Returns:
      An array of types that Expression.change(Event, Object[], ChangeMode) accepts as its delta parameter (which can be arrays to denote that multiple of that type are accepted), or null if the given mode is not supported. For Changer.ChangeMode.DELETE and Changer.ChangeMode.RESET this can return any non-null array to mark them as supported.
    • change

      public void change(org.bukkit.event.Event e, @Nullable Object[] delta, Changer.ChangeMode mode)
      Description copied from interface: Expression
      Changes the expression's value by the given amount. This will only be called on supported modes and with the desired delta type as returned by Expression.acceptChange(ChangeMode)
      Specified by:
      change in interface Expression<T>
      Overrides:
      change in class SimpleExpression<T>
      delta - An array with one or more instances of one or more of the the classes returned by Expression.acceptChange(ChangeMode) for the given change mode (null for Changer.ChangeMode.DELETE and Changer.ChangeMode.RESET). This can be a Object[], thus casting is not allowed.
    • setTime

      public boolean setTime(int time)
      Description copied from class: SimpleExpression
      Sets the time of this expression, i.e. whether the returned value represents this expression before or after the event.

      This method will not be called if this expression is guaranteed to be used after a delay (an error will be printed immediately), but will be called if it only can be after a delay (e.g. if the preceding delay is in an if or a loop) as well as if there's no delay involved.

      If this method returns false the expression will be discarded and an error message is printed. Custom error messages must be of ErrorQuality.SEMANTIC_ERROR to be printed (NB: Skript.error(String) always creates semantic errors).

      This implementation sets the time but returns false.

      Specified by:
      setTime in interface Expression<T>
      Overrides:
      setTime in class SimpleExpression<T>
      Parameters:
      time - -1 for past or 1 for future. 0 is never passed to this method as it represents the default state.
      Returns:
      Whether this expression has distinct time states, e.g. a player never changes but a block can. This should be sensitive for the event (using ParserInstance.isCurrentEvent(Class)).
      See Also:
      SimpleExpression.setTime(int, Class, Expression...), SimpleExpression.setTime(int, Expression, Class...)
    • getTime

      public int getTime()
      Specified by:
      getTime in interface Expression<T>
      Overrides:
      getTime in class SimpleExpression<T>
      Returns:
      The value passed to Expression.setTime(int) or 0 if it was never changed.
      See Also:
      Expression.setTime(int)
    • isDefault

      public boolean isDefault()
      Description copied from interface: Expression
      Returns whether this value represents the default value of its type for the event, i.e. it can be replaced with a call to event.getXyz() if one knows the event & value type.

      This method might be removed in the future as it's better to check whether value == event.getXyz() for every value an expression returns.

      Specified by:
      isDefault in interface Expression<T>
      Overrides:
      isDefault in class SimpleExpression<T>
      Returns:
      Whether is is the return types' default expression
    • simplify

      public Expression<? extends T> simplify()
      Description copied from interface: Expression
      Simplifies the expression, e.g. if it only contains literals the expression may be simplified to a literal, and wrapped expressions are unwrapped.

      After this method was used the toString methods are likely not useful anymore.

      This method is not yet used but will be used to improve efficiency in the future.

      Specified by:
      simplify in interface Expression<T>
      Overrides:
      simplify in class SimpleExpression<T>
      Returns:
      A reference to a simpler version of this expression. Can change this expression directly and return itself if applicable, i.e. no references to the expression before this method call should be kept!
    • beforeChange

      public @Nullable Object[] beforeChange(Expression<?> changed, @Nullable Object[] delta)
      Description copied from interface: Expression
      This method is called before this expression is set to another one. The return value is what will be used for change. You can use modified version of initial delta array or create a new one altogether

      Default implementation will convert slots to items when they're set to variables, as specified in Skript documentation.

      Parameters:
      changed - What is about to be set.
      delta - Initial delta array.
      Returns:
      Delta array to use for change.