use of org.spongepowered.common.accessor.ChatFormattingAccessor in project SpongeCommon by SpongePowered.
the class SpongeColorValueParameter method parseValue.
@Override
@NonNull
public Optional<? extends Color> parseValue(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable reader) throws ArgumentParseException {
final ArgumentReader.Immutable state = reader.immutable();
// First, is the argument type giving the correct return type?
try {
final ChatFormatting formatting = this.colorArgumentType.parse((StringReader) reader);
final Integer colorCode = ((ChatFormattingAccessor) (Object) formatting).accessor$color();
if (colorCode != null) {
return Optional.of(Color.ofRgb(colorCode));
}
// "reset" slips through the net here.
throw reader.createException(Component.text().content(String.format("%s is not a valid color", formatting.getName())).build());
} catch (final CommandSyntaxException e) {
// ignored
}
reader.setState(state);
// Hex codes and comma separated RGB values require commas, so we need to parse the string,
// rather than use the unquoted string the ColorArgument does.
final String string = reader.parseString();
// Hex code (with optional #)
final Matcher matcher = SpongeColorValueParameter.HEX_CODE.matcher(string);
if (matcher.matches()) {
try {
return Optional.of(Color.ofRgb(Integer.parseInt(matcher.group("colorcode"), 16)));
} catch (final NumberFormatException ex) {
// handled below
}
}
final String[] rgb = string.split(",", 3);
if (rgb.length == 3) {
final Optional<Color> result = this.checkIntConversion(rgb);
if (result.isPresent()) {
return result;
}
}
throw reader.createException(SpongeColorValueParameter.EXCEPTION_MESSAGE);
}
Aggregations