
de.placeblock.commandapi.core.parameter.EnumParameter Maven / Gradle / Ivy
package de.placeblock.commandapi.core.parameter;
import de.placeblock.commandapi.core.SuggestionBuilder;
import de.placeblock.commandapi.core.exception.CommandParseException;
import de.placeblock.commandapi.core.exception.InvalidParameterValueException;
import de.placeblock.commandapi.core.parser.ParsedCommandBranch;
import lombok.RequiredArgsConstructor;
/**
* Author: Placeblock
*/
@RequiredArgsConstructor
public class EnumParameter> implements Parameter {
private final Class enumClass;
private final E[] enumValues;
@SuppressWarnings("unused")
public static > EnumParameter enumparam(Class enumClass, E[] enumValues) {
return new EnumParameter<>(enumClass, enumValues);
}
@Override
public E parse(ParsedCommandBranch command, S source) throws CommandParseException {
String word = command.getReader().readUnquotedString();
try {
if (CommandEnum.class.isAssignableFrom(this.enumClass)) {
for (E enumValue : this.enumValues) {
if (word.equals(((CommandEnum) enumValue).getDisplayName())) {
return enumValue;
}
}
throw new InvalidParameterValueException(word);
} else {
return Enum.valueOf(this.enumClass, word.toUpperCase());
}
} catch (IllegalArgumentException ex) {
throw new InvalidParameterValueException(word);
}
}
@Override
public void getSuggestions(SuggestionBuilder suggestionBuilder) {
String partial = suggestionBuilder.getRemaining();
for (E enumValue : this.enumValues) {
String displayName;
if (enumValue instanceof CommandEnum commandEnumValue) {
displayName = commandEnumValue.getDisplayName();
} else {
displayName = enumValue.name();
}
if (displayName.toLowerCase().startsWith(partial.toLowerCase())) {
suggestionBuilder.withSuggestion(displayName);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy