Class VariableString

java.lang.Object
ch.njol.skript.lang.VariableString
All Implemented Interfaces:
Debuggable, Expression<String>, SyntaxElement

public class VariableString extends Object implements Expression<String>
Represents a string that may contain expressions, and is thus "variable".
  • Method Details

    • newInstance

      public static @Nullable VariableString newInstance(String s)
      Prints errors
    • isQuotedCorrectly

      public static boolean isQuotedCorrectly(String s, boolean withQuotes)
      Tests whether a string is correctly quoted, i.e. only has doubled double quotes in it. Singular double quotes are only allowed between percentage signs.
      Parameters:
      s - The string
      withQuotes - Whether s must be surrounded by double quotes or not
      Returns:
      Whether the string is quoted correctly
    • unquote

      public static String unquote(String s, boolean surroundingQuotes)
      Removes quoted quotes from a string.
      Parameters:
      s - The string
      surroundingQuotes - Whether the string has quotes at the start & end that should be removed
      Returns:
      The string with double quotes replaced with signle ones and optionally with removed surrounding quotes.
    • newInstance

      public static @Nullable VariableString newInstance(String orig, StringMode mode)
      Creates an instance of VariableString by parsing given string. Prints errors and returns null if it is somehow invalid.
      Parameters:
      orig - Unquoted string to parse.
      Returns:
      A new VariableString instance.
    • nextVariableBracket

      public static int nextVariableBracket(String s, int start)
      Copied from SkriptParser#nextBracket(String, char, char, int, boolean), but removed escaping & returns -1 on error.
      Parameters:
      s -
      start - Index after the opening bracket
      Returns:
      The next closing curly bracket
    • makeStrings

      public static VariableString[] makeStrings(String[] args)
    • makeStringsFromQuoted

      public static @Nullable VariableString[] makeStringsFromQuoted(List<String> args)
      Parameters:
      args - Quoted strings - This is not checked!
      Returns:
      a new array containing all newly created VariableStrings, or null if one is invalid
    • toString

      public String toString(@Nullable org.bukkit.event.Event e)
      Parses all expressions in the string and returns it. If this is a simple string, the event may be null.
      Parameters:
      e - Event to pass to the expressions.
      Returns:
      The input string with all expressions replaced.
    • toUnformattedString

      public String toUnformattedString(org.bukkit.event.Event e)
      Parses all expressions in the string and returns it. Does not parse formatting codes!
      Parameters:
      e - Event to pass to the expressions.
      Returns:
      The input string with all expressions replaced.
    • getMessageComponents

      public List<MessageComponent> getMessageComponents(org.bukkit.event.Event e)
      Gets message components from this string. Formatting is parsed only in simple parts for security reasons.
      Parameters:
      e - Currently running event.
      Returns:
      Message components.
    • getMessageComponentsUnsafe

      public List<MessageComponent> getMessageComponentsUnsafe(org.bukkit.event.Event e)
      Gets message components from this string. Formatting is parsed everywhere, which is a potential security risk.
      Parameters:
      e - Currently running event.
      Returns:
      Message components.
    • toChatString

      public String toChatString(org.bukkit.event.Event e)
      Parses all expressions in the string and returns it in chat JSON format.
      Parameters:
      e - Event to pass to the expressions.
      Returns:
      The input string with all expressions replaced.
    • toString

      public String toString()
      Description copied from interface: Debuggable
      Should return toString(null, false)
      Specified by:
      toString in interface Debuggable
      Overrides:
      toString in class Object
    • toString

      public String toString(@Nullable org.bukkit.event.Event e, boolean debug)
      Use toString(Event) to get the actual string
      Specified by:
      toString in interface Debuggable
      Parameters:
      e - The event to get information to. This is always null if debug == false.
      debug - If true this should print more information, if false this should print what is shown to the end user
      Returns:
      String representation of this object
    • getDefaultVariableName

      public String getDefaultVariableName()
    • isSimple

      public boolean isSimple()
    • getMode

      public StringMode getMode()
    • setMode

      public VariableString setMode(StringMode mode)
    • init

      public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult)
      Description copied from interface: SyntaxElement
      Called just after the constructor.
      Specified by:
      init in interface SyntaxElement
      Parameters:
      exprs - all %expr%s included in the matching pattern in the order they appear in the pattern. If an optional value was left out it will still be included in this list holding the default value of the desired type which usually depends on the event.
      matchedPattern - The index of the pattern which matched
      isDelayed - Whether this expression is used after a delay or not (i.e. if the event has already passed when this expression will be called)
      parseResult - Additional information about the match.
      Returns:
      Whether this expression was initialised successfully. An error should be printed prior to returning false to specify the cause.
      See Also:
      ParserInstance.isCurrentEvent(Class...)
    • getSingle

      public String getSingle(org.bukkit.event.Event e)
      Description copied from interface: Expression
      Get the single value of this expression.

      This method may only return null if it always returns null for the given event, i.e. it is equivalent to getting a random element out of Expression.getAll(Event) or null iff that array is empty.

      Do not use this in conditions, use Expression.check(Event, Checker, boolean) instead.

      Specified by:
      getSingle in interface Expression<String>
      Parameters:
      e - The event
      Returns:
      The value or null if this expression doesn't have any value for the event
    • getArray

      public String[] getArray(org.bukkit.event.Event e)
      Description copied from interface: Expression
      Get all the values of this expression. The returned array is empty if this expression doesn't have any values for the given event.

      The returned array must not contain any null values.

      Do not use this in conditions, use Expression.check(Event, Checker, boolean) instead.

      Specified by:
      getArray in interface Expression<String>
      Parameters:
      e - The event
      Returns:
      An array of values of this expression which must neither be null nor contain nulls, and which must not be an internal array.
    • getAll

      public String[] getAll(org.bukkit.event.Event e)
      Description copied from interface: Expression
      Gets all possible return values of this expression, i.e. it returns the same as Expression.getArray(Event) if Expression.getAnd() is true, otherwise all possible values for Expression.getSingle(Event).
      Specified by:
      getAll in interface Expression<String>
      Parameters:
      e - The event
      Returns:
      An array of all possible values of this expression for the given event which must neither be null nor contain nulls, and which must not be an internal array.
    • isSingle

      public boolean isSingle()
      Specified by:
      isSingle in interface Expression<String>
      Returns:
      true if this expression will ever only return one value at most, false if it can return multiple values.
    • check

      public boolean check(org.bukkit.event.Event e, Checker<? super String> c, boolean negated)
      Description copied from interface: Expression
      Checks this expression against the given checker. This is the normal version of this method and the one which must be used for simple checks, or as the innermost check of nested checks.

      Usual implementation (may differ, e.g. may return false for nonexistent values independent of negated):

       return negated ^ Expression.check(Event, Checker);
       
      Specified by:
      check in interface Expression<String>
      Parameters:
      e - The event
      c - A checker
      negated - The checking condition's negated state. This is used to invert the output of the checker if set to true (i.e. negated ^ checker.check(...))
      Returns:
      Whether this expression matches or doesn't match the given checker depending on the condition's negated state.
      See Also:
      SimpleExpression.check(Object[], Checker, boolean, boolean)
    • check

      public boolean check(org.bukkit.event.Event e, Checker<? super String> c)
      Description copied from interface: Expression
      Checks this expression against the given checker. This method must only be used around other checks, use Expression.check(Event, Checker, boolean) for a simple ckeck or the innermost check of a nested check.
      Specified by:
      check in interface Expression<String>
      Parameters:
      e - The event
      c - A checker
      Returns:
      Whether this expression matches the given checker
      See Also:
      SimpleExpression.check(Object[], Checker, boolean, boolean)
    • getConvertedExpression

      public <R> @Nullable Expression<? extends R> getConvertedExpression(Class<R>... to)
      Description copied from interface: Expression
      Tries to convert this expression to the given type. This method can print an error prior to returning null to specify the cause.

      Please note that expressions whose returnType is not Object will not be parsed at all for a certain class if there's no converter from the expression's returnType to the desired class. Thus this method should only be overridden if this expression's returnType is Object.

      The returned expression should delegate this method to the original expression's method to prevent excessive converted expression chains (see also ConvertedExpression).

      Specified by:
      getConvertedExpression in interface Expression<String>
      Parameters:
      to - The desired return type of the returned expression
      Returns:
      Expression with the desired return type or null if the expression can't be converted to the given type. Returns the expression itself if it already returns the desired type.
      See Also:
      Converter, ConvertedExpression
    • getReturnType

      public Class<? extends String> getReturnType()
      Description copied from interface: Expression
      Gets the return type of this expression.
      Specified by:
      getReturnType in interface Expression<String>
      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<String>
      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) throws UnsupportedOperationException
      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<String>
      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.
      Throws:
      UnsupportedOperationException - (optional) - If this method was called on an unsupported ChangeMode.
    • 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<String>
      Returns:
      Whether this expression returns all values at once or only part of them.
    • setTime

      public boolean setTime(int time)
      Description copied from interface: Expression
      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).

      Specified by:
      setTime in interface Expression<String>
      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...), ParserInstance.isCurrentEvent(Class...)
    • getTime

      public int getTime()
      Specified by:
      getTime in interface Expression<String>
      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<String>
      Returns:
      Whether is is the return types' default expression
    • iterator

      public Iterator<? extends String> 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<String>
      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.
    • isLoopOf

      public boolean isLoopOf(String s)
      Description copied from interface: Expression
      Checks whether the given 'loop-...' expression should match this loop, e.g. loop-block matches any loops that loop through blocks and loop-argument matches an argument loop.

      You should usually just return false as e.g. loop-block will automatically match the expression if its returnType is Block or a subtype of it.

      Specified by:
      isLoopOf in interface Expression<String>
      Parameters:
      s - The entered string
      Returns:
      Whether this loop matches the given string
    • getSource

      public Expression<?> getSource()
      Description copied from interface: Expression
      Returns the original expression that was parsed, i.e. without any conversions done.

      This method is undefined for simplified expressions.

      Specified by:
      getSource in interface Expression<String>
      Returns:
      The unconverted source expression of this expression or this expression itself if it was never converted.
    • setStringMode

      public static <T> Expression<T> setStringMode(Expression<T> e, StringMode mode)
    • simplify

      public Expression<String> 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<String>
      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!