Record Class Result<T>
java.lang.Object
java.lang.Record
dev.jorel.commandapi.arguments.parser.Result<T>
- Type Parameters:
T- The type of object held as a return result.- Record Components:
value- The result of parsing input, if the parse was successful.exception- TheCommandSyntaxExceptionthrown, if the parse failed.suggestionsStart- The point in the inputStringReaderwhere the suggestions should be placed.suggestions- TheSuggestionProviderobject that will generate the suggestions.
public record Result<T>(T value, com.mojang.brigadier.exceptions.CommandSyntaxException exception, int suggestionsStart, SuggestionProvider suggestions)
extends Record
Holds the information that results from a
Parser parsing input. A parse either creates an object
or throws a CommandSyntaxException explaining why it couldn't create its object. The result may
optionally provide suggestions, which includes the point in the string where the suggestions should be
placed and the SuggestionProvider that will generate the suggestions.
Results can be constructed with the following methods:
withValue(Object)withValueAndSuggestions(Object, int, SuggestionProvider)withVoidValue()withVoidValueAndSuggestions(int, SuggestionProvider)withException(CommandSyntaxException)withExceptionAndSuggestions(CommandSyntaxException, int, SuggestionProvider)
Result object with the same value but different
suggestions can be created using withSuggestions(int, SuggestionProvider).
The information of a Result can be accessed directly using value(), exception(),
suggestionsStart(), or suggestions(). Additionally, the information can be interacted with
using throwOrReturn() or continueWith(Function, Function).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAFunctionalInterfacewith abstract methodResult.ThrowableFunction.apply(Object).static classA placeholder object that represents a successful parse that didn't return a specific object. -
Constructor Summary
ConstructorsConstructorDescriptionResult(T value, com.mojang.brigadier.exceptions.CommandSyntaxException exception, int suggestionsStart, SuggestionProvider suggestions) Creates an instance of aResultrecord class. -
Method Summary
Modifier and TypeMethodDescription<R> Result<R> continueWith(Function<T, Result<R>> success) This method works the same ascontinueWith(Function, Function), but thefailureFunctionis given asResult::withException.<R> Result<R> continueWith(Function<T, Result<R>> success, Function<com.mojang.brigadier.exceptions.CommandSyntaxException, Result<R>> failure) final booleanIndicates whether some other object is "equal to" this one.com.mojang.brigadier.exceptions.CommandSyntaxExceptionReturns the value of theexceptionrecord component.final inthashCode()Returns a hash code value for this object.Returns the value of thesuggestionsrecord component.intReturns the value of thesuggestionsStartrecord component.final StringtoString()Returns a string representation of this record class.value()Returns the value of thevaluerecord component.static <T> Result<T> withException(@NotNull com.mojang.brigadier.exceptions.CommandSyntaxException exception) static <T> Result<T> withExceptionAndSuggestions(@NotNull com.mojang.brigadier.exceptions.CommandSyntaxException exception, int suggestionsStart, SuggestionProvider suggestions) withSuggestions(int suggestionsStart, SuggestionProvider suggestions) Keeps the same value or exception but overrides any suggestions information.static <T> Result<T> withValue(T value) static <T> Result<T> withValueAndSuggestions(T value, int suggestionsStart, SuggestionProvider suggestions) static Result<Result.Void> static Result<Result.Void> withVoidValueAndSuggestions(int suggestionsStart, SuggestionProvider suggestions) wrapFunctionResult(Result.ThrowableFunction<T, R> original)
-
Constructor Details
-
Result
public Result(T value, com.mojang.brigadier.exceptions.CommandSyntaxException exception, int suggestionsStart, SuggestionProvider suggestions) Creates an instance of aResultrecord class.- Parameters:
value- the value for thevaluerecord componentexception- the value for theexceptionrecord componentsuggestionsStart- the value for thesuggestionsStartrecord componentsuggestions- the value for thesuggestionsrecord component
-
-
Method Details
-
withValue
-
withVoidValue
- Returns:
- A new
Resultwith aResult.Voidvalue.
-
withException
public static <T> Result<T> withException(@NotNull @NotNull com.mojang.brigadier.exceptions.CommandSyntaxException exception) - Type Parameters:
T- The type of object held as a return value.- Parameters:
exception- TheCommandSyntaxExceptionexplaining why a value object could not be created from the given input.- Returns:
- A new
Resultobject with the given information.
-
withValueAndSuggestions
public static <T> Result<T> withValueAndSuggestions(@NotNull T value, int suggestionsStart, SuggestionProvider suggestions) - Type Parameters:
T- The type of object held as a return value.- Parameters:
value- The object resulting from parsing input.suggestionsStart- The point in the input where suggestions should be placed.suggestions- TheSuggestionProviderobject that will generate the suggestions.- Returns:
- A new
Resultobject with the given information.
-
withVoidValueAndSuggestions
public static Result<Result.Void> withVoidValueAndSuggestions(int suggestionsStart, SuggestionProvider suggestions) - Parameters:
suggestionsStart- The point in the input where suggestions should be placed.suggestions- TheSuggestionProviderobject that will generate the suggestions.- Returns:
- A new
Resultobject with aResult.Voidvalue and the given suggestions.
-
withExceptionAndSuggestions
public static <T> Result<T> withExceptionAndSuggestions(@NotNull @NotNull com.mojang.brigadier.exceptions.CommandSyntaxException exception, int suggestionsStart, SuggestionProvider suggestions) - Type Parameters:
T- The type of object held as a return value.- Parameters:
exception- TheCommandSyntaxExceptionexplaining why a value object could not be created from the given input.suggestionsStart- The point in the input where suggestions should be placed.suggestions- TheSuggestionProviderobject that will generate the suggestions.- Returns:
- A new
Resultobject with the given information.
-
withSuggestions
Keeps the same value or exception but overrides any suggestions information.- Parameters:
suggestionsStart- The point in the input where suggestions should be placed.suggestions- TheSuggestionProviderobject that will generate the suggestions.- Returns:
- A new
Resultobject with the given information.
-
throwOrReturn
- Returns:
value()if the parse was successful.- Throws:
com.mojang.brigadier.exceptions.CommandSyntaxException-exception()if the parse failed.
-
continueWith
public <R> Result<R> continueWith(Function<T, Result<R>> success, Function<com.mojang.brigadier.exceptions.CommandSyntaxException, Result<R>> failure) Uses thisResultto generate a finalResultwith a value of typeR. If this parse was successful, then the givensuccessFunctionis used to generate the newResult. If this parse failed, then the givenfailureFunctionis used to generate the newResult.If the new
Resultdoes not have anysuggestions(), then the finalResultreturned by this method will have thesuggestionsStart()andsuggestions()of thisResult.Note that the
wrapFunctionResult(ThrowableFunction)method may be useful for defining thesuccessandfailureFunctions.- Type Parameters:
R- The type of object held as thevalue()of the finalResult.- Parameters:
success- TheFunctionto use to generate the newResultwhen this parse was successful. The argument passed toFunction.apply(Object)will be thevalue()of thisResult.failure- TheFunctionto use to generate the newResultwhen this parse failed. The argument passed toFunction.apply(Object)will be theexception()of thisResult.- Returns:
- The final
Resultobject.
-
continueWith
This method works the same ascontinueWith(Function, Function), but thefailureFunctionis given asResult::withException. This has the effect of passing the exception and suggestions of thisResultwithout changes if this parse failed. If this parse succeeded, thesuccessFunctionis applied as usual. -
wrapFunctionResult
public static <T,R> Function<T,Result<R>> wrapFunctionResult(Result.ThrowableFunction<T, R> original) Converts aResult.ThrowableFunction<T, R>into aFunction<T, Result<R>>. The returnedFunctionwill pass its argument unchanged to the givenResult.ThrowableFunction. The outcome of theResult.ThrowableFunction- either returning an object of typeRor throwing aCommandSyntaxException- will be wrapped into aResult<R>as appropriate.- Type Parameters:
T- The type of the function parameter.R- The function return type.- Parameters:
original- TheResult.ThrowableFunctionbeing wrapped.- Returns:
- A
Functionthat wraps the outcome of the givenResult.ThrowableFunctionin aResult.
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
value
-
exception
-
suggestionsStart
public int suggestionsStart()Returns the value of thesuggestionsStartrecord component.- Returns:
- the value of the
suggestionsStartrecord component
-
suggestions
Returns the value of thesuggestionsrecord component.- Returns:
- the value of the
suggestionsrecord component
-