Search in sources :

Example 1 with BannerPatternShape

use of org.spongepowered.api.data.type.BannerPatternShape in project SpongeCommon by SpongePowered.

the class SpongePatternLayerBuilder method buildContent.

@Override
protected Optional<PatternLayer> buildContent(DataView container) throws InvalidDataException {
    checkNotNull(container);
    if (!container.contains(DataQueries.BANNER_SHAPE_ID) || !container.contains(DataQueries.BANNER_COLOR)) {
        return Optional.empty();
    }
    String id = container.getString(DataQueries.BANNER_SHAPE_ID).get();
    // We can get these pattern shapes from the game registry willy nilly, however, we still need to validate
    // that the pattern exists, if it doesn't, well, thow an InvalidDataException!
    Optional<BannerPatternShape> shapeOptional = Sponge.getRegistry().getType(BannerPatternShape.class, id);
    if (!shapeOptional.isPresent()) {
        throw new InvalidDataException("The provided container has an invalid banner pattern shape entry!");
    }
    // Now we need to validate the dye color of course...
    String dyeColorId = container.getString(DataQueries.BANNER_COLOR).get();
    Optional<DyeColor> colorOptional = Sponge.getRegistry().getType(DyeColor.class, dyeColorId);
    if (!colorOptional.isPresent()) {
        throw new InvalidDataException("The provided container has an invalid dye color entry!");
    }
    return Optional.of(new SpongePatternLayer(shapeOptional.get(), colorOptional.get()));
}
Also used : InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) BannerPatternShape(org.spongepowered.api.data.type.BannerPatternShape) DyeColor(org.spongepowered.api.data.type.DyeColor) SpongePatternLayer(org.spongepowered.common.data.meta.SpongePatternLayer)

Aggregations

InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1 BannerPatternShape (org.spongepowered.api.data.type.BannerPatternShape)1 DyeColor (org.spongepowered.api.data.type.DyeColor)1 SpongePatternLayer (org.spongepowered.common.data.meta.SpongePatternLayer)1