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();
}
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();
}
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;
}
Aggregations