Class Variable<T>

java.lang.Object
io.github.syst3ms.skriptparser.lang.Variable<T>
Type Parameters:
T - the common supertype of the possibly multiple values of the variable
All Implemented Interfaces:
Expression<T>, SyntaxElement

public class Variable<T> extends Object implements Expression<T>
A reference to a variable, whose value is only known at runtime. It can be local to the event, meaning it isn't defined outside of the event it was first defined in. It can also be a list of multiple values. It can also be both.
  • Constructor Details

    • Variable

      public Variable(VariableString name, boolean local, boolean list, Class<?> type)
  • Method Details

    • init

      @Contract("_, _, _ -> fail") public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext)
      Description copied from interface: SyntaxElement
      Initializes this SyntaxElement before being used. This method is always called before all the others in an extending class, the only exception being CodeSection.loadSection(FileSection, ParserState, SkriptLogger).
      Specified by:
      init in interface SyntaxElement
      Parameters:
      expressions - an array of expressions representing all the expressions that are being passed to this syntax element. As opposed to Skript, elements of this array can't be null.
      matchedPattern - the index of the pattern that was successfully matched. It corresponds to the order of the syntaxes in registration
      parseContext - an object containing additional information about the parsing of this syntax element, like regex matches and parse marks
      Returns:
      true if the syntax element was initialized successfully, false otherwise.
      See Also:
    • getValues

      public T[] getValues(TriggerContext ctx)
      Description copied from interface: Expression
      Retrieves all values of this Expression, accounting for possible modifiers. This means that if this is an or-list, it will choose a random value to return.
      Specified by:
      getValues in interface Expression<T>
      Parameters:
      ctx - the event
      Returns:
      an array of the values
      See Also:
    • getIndex

      public String getIndex(TriggerContext ctx)
      Parameters:
      ctx - the event
      Returns:
      the index of this Variable
    • getRaw

      public Optional<Object> getRaw(TriggerContext ctx)
      Returns the raw value stored inside the variable map. This can either be the object this Variable is referencing to or some sort of Map containing nested nodes.
      Parameters:
      ctx - the event
      Returns:
      the raw value
    • isSingle

      public boolean isSingle()
      Specified by:
      isSingle in interface Expression<T>
      Returns:
      whether this expression returns a single value. By default, this is defined on registration, but it can be overridden.
    • getReturnType

      public Class<? extends T> getReturnType()
      Specified by:
      getReturnType in interface Expression<T>
      Returns:
      the return type of this expression. By default, this is defined on registration, but, like Expression.isSingle(), can be overriden.
    • setReturnType

      public void setReturnType(Class<?> returnType)
    • acceptsChange

      public Optional<Class<?>[]> acceptsChange(ChangeMode mode)
      Description copied from interface: Expression
      Determines whether this expression can be changed according to a specific ChangeMode, and what type of values it can be changed with.
      Specified by:
      acceptsChange in interface Expression<T>
      Parameters:
      mode - the mode this Expression would be changed with
      Returns:
      an array of classes describing what types this Expression can be changed with, empty if it shouldn't be changed with the given change mode. If the change mode is ChangeMode.DELETE or ChangeMode.RESET, then an empty array should be returned.
    • change

      public void change(TriggerContext ctx, ChangeMode mode, Object[] changeWith) throws UnsupportedOperationException
      Description copied from interface: Expression
      Changes this expression with the given values according to the given mode
      Specified by:
      change in interface Expression<T>
      Parameters:
      ctx - the event
      mode - the mode of change
      changeWith - the values to change this Expression with
      Throws:
      UnsupportedOperationException
    • iterator

      public Iterator<T> iterator(TriggerContext ctx)
      Specified by:
      iterator in interface Expression<T>
      Parameters:
      ctx - the event
      Returns:
      an iterator of the values of this expression
    • variablesIterator

      public Iterator<Pair<String,Object>> variablesIterator(TriggerContext ctx)
      Parameters:
      ctx - the event
      Returns:
      an Iterator that iterates over pairs of indexes and values
    • convertExpression

      public <C> Optional<? extends Expression<C>> convertExpression(Class<C> to)
      Description copied from interface: Expression
      Converts this expression from it's current type (T) to another type, using converters.
      Specified by:
      convertExpression in interface Expression<T>
      Type Parameters:
      C - the type to convert this Expression to
      Parameters:
      to - the class of the type to convert this Expression to
      Returns:
      a converted Expression, or null if it couldn't be converted
    • isLoopOf

      public boolean isLoopOf(String s)
      Description copied from interface: Expression
      When this expression is looped, returns what the loop reference (as in loop-<reference>) should be in order to describe each element of the values of this expression.
      Specified by:
      isLoopOf in interface Expression<T>
      Parameters:
      s - the loop reference
      Returns:
      whether the given reference describes this expression's elements. By default, returns true if the parameter is "value"
      See Also:
    • isIndexLoop

      public boolean isIndexLoop(String s)
    • toString

      public String toString(TriggerContext ctx, boolean debug)
      Specified by:
      toString in interface SyntaxElement
      Parameters:
      ctx - the event
      debug - whether to show additional information or not
      Returns:
      a String that should aim to resemble what is written in the script as closely as possible