Search in sources :

Example 1 with FireworkShape

use of org.spongepowered.api.item.FireworkShape in project SpongeCommon by SpongePowered.

the class FireworkUtil method fromCompound.

public static FireworkEffect fromCompound(final CompoundTag compound) {
    final FireworkEffect.Builder builder = new SpongeFireworkEffectBuilder();
    if (compound.contains(Constants.Item.Fireworks.FLICKER)) {
        builder.flicker(compound.getBoolean(Constants.Item.Fireworks.FLICKER));
    }
    if (compound.contains(Constants.Item.Fireworks.TRAIL)) {
        builder.trail(compound.getBoolean(Constants.Item.Fireworks.TRAIL));
    }
    if (compound.contains(Constants.Item.Fireworks.SHAPE_TYPE)) {
        final byte type = compound.getByte(Constants.Item.Fireworks.SHAPE_TYPE);
        final MappedRegistry<FireworkShape> registry = (MappedRegistry<FireworkShape>) (Object) Sponge.game().registry(RegistryTypes.FIREWORK_SHAPE);
        @Nullable final FireworkShape shape = registry.byId(type);
        if (shape != null) {
            builder.shape(shape);
        }
    }
    if (compound.contains(Constants.Item.Fireworks.COLORS)) {
        final List<Color> colors = Lists.newArrayList();
        final int[] colorsRaw = compound.getIntArray(Constants.Item.Fireworks.COLORS);
        for (final int color : colorsRaw) {
            colors.add(Color.ofRgb(color));
        }
        builder.colors(colors);
    }
    if (compound.contains(Constants.Item.Fireworks.FADE_COLORS)) {
        final List<Color> fades = Lists.newArrayList();
        final int[] fadesRaw = compound.getIntArray(Constants.Item.Fireworks.FADE_COLORS);
        for (final int fade : fadesRaw) {
            fades.add(Color.ofRgb(fade));
        }
        builder.fades(fades);
    }
    return builder.build();
}
Also used : SpongeFireworkEffectBuilder(org.spongepowered.common.item.SpongeFireworkEffectBuilder) Color(org.spongepowered.api.util.Color) FireworkShape(org.spongepowered.api.item.FireworkShape) FireworkEffect(org.spongepowered.api.item.FireworkEffect) MappedRegistry(net.minecraft.core.MappedRegistry) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with FireworkShape

use of org.spongepowered.api.item.FireworkShape in project SpongeCommon by SpongePowered.

the class SpongeFireworkEffectDataBuilder method buildContent.

@Override
protected Optional<FireworkEffect> buildContent(DataView container) throws InvalidDataException {
    if (container.contains(Constants.Item.Fireworks.FIREWORK_SHAPE, Constants.Item.Fireworks.FIREWORK_COLORS, Constants.Item.Fireworks.FIREWORK_FADE_COLORS, Constants.Item.Fireworks.FIREWORK_FLICKERS, Constants.Item.Fireworks.FIREWORK_TRAILS)) {
        final ResourceKey key = container.getResourceKey(Constants.Item.Fireworks.FIREWORK_SHAPE).get();
        final Optional<FireworkShape> shapeOptional = Sponge.game().registry(RegistryTypes.FIREWORK_SHAPE).findValue(key);
        if (!shapeOptional.isPresent()) {
            throw new InvalidDataException("Could not find the FireworkShape for the provided id: " + key);
        }
        final FireworkShape shape = shapeOptional.get();
        final boolean trails = container.getBoolean(Constants.Item.Fireworks.FIREWORK_TRAILS).get();
        final boolean flickers = container.getBoolean(Constants.Item.Fireworks.FIREWORK_FLICKERS).get();
        final List<Color> colors = container.getSerializableList(Constants.Item.Fireworks.FIREWORK_COLORS, Color.class).get();
        final List<Color> fadeColors = container.getSerializableList(Constants.Item.Fireworks.FIREWORK_FADE_COLORS, Color.class).get();
        return Optional.of(FireworkEffect.builder().colors(colors).flicker(flickers).fades(fadeColors).trail(trails).shape(shape).build());
    }
    return Optional.empty();
}
Also used : Color(org.spongepowered.api.util.Color) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) FireworkShape(org.spongepowered.api.item.FireworkShape) ResourceKey(org.spongepowered.api.ResourceKey)

Example 3 with FireworkShape

use of org.spongepowered.api.item.FireworkShape in project SpongeCommon by SpongePowered.

the class FireworkUtil method toCompound.

public static CompoundTag toCompound(final FireworkEffect effect) {
    final MappedRegistry<FireworkShape> registry = (MappedRegistry<FireworkShape>) (Object) Sponge.game().registry(RegistryTypes.FIREWORK_SHAPE);
    final CompoundTag tag = new CompoundTag();
    tag.putBoolean(Constants.Item.Fireworks.FLICKER, effect.flickers());
    tag.putBoolean(Constants.Item.Fireworks.TRAIL, effect.hasTrail());
    tag.putByte(Constants.Item.Fireworks.SHAPE_TYPE, (byte) registry.getId(effect.shape()));
    final int[] colorsArray = effect.colors().stream().mapToInt(Color::rgb).toArray();
    tag.putIntArray(Constants.Item.Fireworks.COLORS, colorsArray);
    final int[] fadeArray = effect.fadeColors().stream().mapToInt(Color::rgb).toArray();
    tag.putIntArray(Constants.Item.Fireworks.FADE_COLORS, fadeArray);
    return tag;
}
Also used : FireworkShape(org.spongepowered.api.item.FireworkShape) MappedRegistry(net.minecraft.core.MappedRegistry) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

FireworkShape (org.spongepowered.api.item.FireworkShape)3 MappedRegistry (net.minecraft.core.MappedRegistry)2 Color (org.spongepowered.api.util.Color)2 CompoundTag (net.minecraft.nbt.CompoundTag)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 ResourceKey (org.spongepowered.api.ResourceKey)1 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1 FireworkEffect (org.spongepowered.api.item.FireworkEffect)1 SpongeFireworkEffectBuilder (org.spongepowered.common.item.SpongeFireworkEffectBuilder)1