CommandAPI 9.6.0
An API for the command UI introduced in Minecraft 1.13
com.mojang.brigadier.suggestion.Suggestion Class Reference

Represents a suggestion for a possible next value as well as an optional tooltip. More...

+ Inheritance diagram for com.mojang.brigadier.suggestion.Suggestion:

Public Member Functions

 Suggestion (final StringRange range, final String text)
 Creates a new Suggestion spanning the given string range in the input and that has a given text it suggests. More...
 
 Suggestion (final StringRange range, final String text, final Message tooltip)
 Creates a new Suggestion spanning the given string range in the input, with a given text it suggests and a tooltip it provides. More...
 
StringRange getRange ()
 Returns the range in the input string this suggestion is applicable in. More...
 
String getText ()
 Returns the text this suggestion suggests. More...
 
Message getTooltip ()
 Returns the tooltip with some explanatory message for the user. More...
 
String apply (final String input)
 Applies the suggestion to the input. More...
 
boolean equals (final Object o)
 
int hashCode ()
 
String toString ()
 
int compareTo (final Suggestion o)
 Compares the getText() of this suggestion to the getText() of the passed suggestion. More...
 
int compareToIgnoreCase (final Suggestion b)
 Compares the getText() of this suggestion to the getText() of the passed suggestion, ignoring case. More...
 
Suggestion expand (final String command, final StringRange range)
 Expands this suggestion by changing the range to the passed one and filling everything that lies outside getText() with characters from the passed command. More...
 

Detailed Description

Represents a suggestion for a possible next value as well as an optional tooltip.

An example would be tab completion showing you the suggestions on the fly.


Instances of this class are compared based on the natural order of getText(). A case insensitive comparison method is provided as compareToIgnoreCase(Suggestion).

Constructor & Destructor Documentation

◆ Suggestion() [1/2]

com.mojang.brigadier.suggestion.Suggestion.Suggestion ( final StringRange  range,
final String  text 
)

Creates a new Suggestion spanning the given string range in the input and that has a given text it suggests.

Parameters
rangethe range in the input it is applicable in
textthe replacement it suggests

◆ Suggestion() [2/2]

com.mojang.brigadier.suggestion.Suggestion.Suggestion ( final StringRange  range,
final String  text,
final Message  tooltip 
)

Creates a new Suggestion spanning the given string range in the input, with a given text it suggests and a tooltip it provides.

Parameters
rangethe range in the input it is applicable in
textthe replacement it suggests
tooltipsome explanatory tooltip to show to the user, before they apply the suggestion

Member Function Documentation

◆ apply()

String com.mojang.brigadier.suggestion.Suggestion.apply ( final String  input)

Applies the suggestion to the input.

This method will replace or insert the getText() into the passed input string.


An Example with the text fizz:

  • Input: buzz, range 0-4
    Output: fizz
  • Input: buzz, range 0-0
    Output: fizzbuzz
  • Input: buzz, range 4-4
    Output: buzzfizz
Parameters
inputthe input string to apply it to
Returns
the result of applying the suggestion to the input
Exceptions
StringIndexOutOfBoundsExceptionif the range is not contained in the input string

◆ compareTo()

int com.mojang.brigadier.suggestion.Suggestion.compareTo ( final Suggestion  o)

Compares the getText() of this suggestion to the getText() of the passed suggestion.

This method is case sensitive, see compareToIgnoreCase(Suggestion) if you need it to the ignore the case.

Parameters
othe suggestion to compare it to
Returns
-1 if this suggestion is less than the other, 0 if they are equal or 1 if this suggestion should appear behind the passed one
See also
compareToIgnoreCase
String::compareTo

Reimplemented in com.mojang.brigadier.suggestion.IntegerSuggestion.

◆ compareToIgnoreCase()

int com.mojang.brigadier.suggestion.Suggestion.compareToIgnoreCase ( final Suggestion  b)

Compares the getText() of this suggestion to the getText() of the passed suggestion, ignoring case.

Parameters
bthe suggestion to compare it to
Returns
-1 if this suggestion is less than the other, 0 if they are equal or 1 if this suggestion should appear behind the passed one
See also
String::compareToIgnoreCase
compareTo

Reimplemented in com.mojang.brigadier.suggestion.IntegerSuggestion.

◆ expand()

Suggestion com.mojang.brigadier.suggestion.Suggestion.expand ( final String  command,
final StringRange  range 
)

Expands this suggestion by changing the range to the passed one and filling everything that lies outside getText() with characters from the passed command.


Examples with text = ---- and range = 0 and the text to apply it to as abcdefghi:

  • expand("123", StringRange.at(0):
    ----abcdefghi, as the ranges matched and so nothing was changed
  • expand("123", StringRange.at(1):
    a----1bcdefghi, as the passed range (1) was taken and one character from the command was used to fill up the index 1, as the range of the original suggestion was 0
  • expand("123", StringRange.between(1, 3):
    a----123defghi, as the passed range (1,3) was taken and 3 characters from the command were used to fill up the indices 1, 2 and 3, as the range of the original suggestion was 0. It is then inserted after the first character in the text it is applied to (after the a) and replaces everything that lies in its interval (bc).
Parameters
commandthe command to fill the range up with
rangethe range to widen it to
Returns
the expanded suggestion

◆ getRange()

StringRange com.mojang.brigadier.suggestion.Suggestion.getRange ( )

Returns the range in the input string this suggestion is applicable in.

Returns
the range in the input it is applicable in

◆ getText()

String com.mojang.brigadier.suggestion.Suggestion.getText ( )

Returns the text this suggestion suggests.

Returns
the text this suggestion suggests

◆ getTooltip()

Message com.mojang.brigadier.suggestion.Suggestion.getTooltip ( )

Returns the tooltip with some explanatory message for the user.

Returns
the tooltip with some explanatory message for the user or null if not set