use of org.spongepowered.api.item.FireworkEffect in project SpongeCommon by SpongePowered.
the class SpongeParticleHelper method toCachedPacket.
private static ICachedParticleEffect toCachedPacket(SpongeParticleEffect effect) {
SpongeParticleType type = effect.getType();
EnumParticleTypes internal = type.getInternalType();
// Special cases
if (internal == null) {
if (type == ParticleTypes.FIREWORKS) {
final List<FireworkEffect> effects = type.getDefaultOption(ParticleOptions.FIREWORK_EFFECTS).get();
if (effects.isEmpty()) {
return EmptyCachedPacket.INSTANCE;
}
final net.minecraft.item.ItemStack itemStack = new net.minecraft.item.ItemStack(Items.FIREWORKS);
FireworkUtils.setFireworkEffects(itemStack, effects);
final SPacketEntityMetadata packetEntityMetadata = new SPacketEntityMetadata();
packetEntityMetadata.entityId = CachedFireworkPacket.FIREWORK_ROCKET_ID;
packetEntityMetadata.dataManagerEntries = new ArrayList<>();
packetEntityMetadata.dataManagerEntries.add(new EntityDataManager.DataEntry<>(EntityFireworkRocket.FIREWORK_ITEM, itemStack));
return new CachedFireworkPacket(packetEntityMetadata);
}
if (type == ParticleTypes.FERTILIZER) {
int quantity = effect.getOptionOrDefault(ParticleOptions.QUANTITY).get();
return new CachedEffectPacket(2005, quantity, false);
} else if (type == ParticleTypes.SPLASH_POTION) {
Potion potion = (Potion) effect.getOptionOrDefault(ParticleOptions.POTION_EFFECT_TYPE).get();
for (PotionType potionType : PotionType.REGISTRY) {
for (net.minecraft.potion.PotionEffect potionEffect : potionType.getEffects()) {
if (potionEffect.getPotion() == potion) {
return new CachedEffectPacket(2002, PotionType.REGISTRY.getIDForObject(potionType), false);
}
}
}
return EmptyCachedPacket.INSTANCE;
} else if (type == ParticleTypes.BREAK_BLOCK) {
int state = getBlockState(effect, type.getDefaultOption(ParticleOptions.BLOCK_STATE));
if (state == 0) {
return EmptyCachedPacket.INSTANCE;
}
return new CachedEffectPacket(2001, state, false);
} else if (type == ParticleTypes.MOBSPAWNER_FLAMES) {
return new CachedEffectPacket(2004, 0, false);
} else if (type == ParticleTypes.ENDER_TELEPORT) {
return new CachedEffectPacket(2003, 0, false);
} else if (type == ParticleTypes.DRAGON_BREATH_ATTACK) {
return new CachedEffectPacket(2006, 0, false);
} else if (type == ParticleTypes.FIRE_SMOKE) {
final Direction direction = effect.getOptionOrDefault(ParticleOptions.DIRECTION).get();
return new CachedEffectPacket(2000, getDirectionData(direction), false);
}
return EmptyCachedPacket.INSTANCE;
}
Vector3f offset = effect.getOption(ParticleOptions.OFFSET).map(Vector3d::toFloat).orElse(Vector3f.ZERO);
int quantity = effect.getOption(ParticleOptions.QUANTITY).orElse(1);
int[] extra = null;
// The extra values, normal behavior offsetX, offsetY, offsetZ
double f0 = 0f;
double f1 = 0f;
double f2 = 0f;
// Depends on behavior
// Note: If the count > 0 -> speed = 0f else if count = 0 -> speed = 1f
Optional<BlockState> defaultBlockState;
if (internal != EnumParticleTypes.ITEM_CRACK && (defaultBlockState = type.getDefaultOption(ParticleOptions.BLOCK_STATE)).isPresent()) {
int state = getBlockState(effect, defaultBlockState);
if (state == 0) {
return EmptyCachedPacket.INSTANCE;
}
extra = new int[] { state };
}
Optional<ItemStackSnapshot> defaultSnapshot;
if (extra == null && (defaultSnapshot = type.getDefaultOption(ParticleOptions.ITEM_STACK_SNAPSHOT)).isPresent()) {
Optional<ItemStackSnapshot> optSnapshot = effect.getOption(ParticleOptions.ITEM_STACK_SNAPSHOT);
if (optSnapshot.isPresent()) {
ItemStackSnapshot snapshot = optSnapshot.get();
extra = new int[] { Item.getIdFromItem((Item) snapshot.getType()), ((SpongeItemStackSnapshot) snapshot).getDamageValue() };
} else {
Optional<BlockState> optBlockState = effect.getOption(ParticleOptions.BLOCK_STATE);
if (optBlockState.isPresent()) {
BlockState blockState = optBlockState.get();
Optional<ItemType> optItemType = blockState.getType().getItem();
if (optItemType.isPresent()) {
extra = new int[] { Item.getIdFromItem((Item) optItemType.get()), ((Block) blockState.getType()).getMetaFromState((IBlockState) blockState) };
} else {
return EmptyCachedPacket.INSTANCE;
}
} else {
ItemStackSnapshot snapshot = defaultSnapshot.get();
extra = new int[] { Item.getIdFromItem((Item) snapshot.getType()), ((SpongeItemStackSnapshot) snapshot).getDamageValue() };
}
}
}
if (extra == null) {
extra = new int[0];
}
Optional<Double> defaultScale = type.getDefaultOption(ParticleOptions.SCALE);
Optional<Color> defaultColor;
Optional<NotePitch> defaultNote;
Optional<Vector3d> defaultVelocity;
if (defaultScale.isPresent()) {
double scale = effect.getOption(ParticleOptions.SCALE).orElse(defaultScale.get());
// Server formula: sizeServer = (-sizeClient * 2) + 2
if (internal == EnumParticleTypes.EXPLOSION_LARGE || internal == EnumParticleTypes.SWEEP_ATTACK) {
scale = (-scale * 2f) + 2f;
}
if (scale == 0f) {
return new CachedParticlePacket(internal, offset, quantity, extra);
}
f0 = scale;
} else if ((defaultColor = type.getDefaultOption(ParticleOptions.COLOR)).isPresent()) {
Color color = effect.getOption(ParticleOptions.COLOR).orElse(null);
boolean isSpell = internal == EnumParticleTypes.SPELL_MOB || internal == EnumParticleTypes.SPELL_MOB_AMBIENT;
if (!isSpell && (color == null || color.equals(defaultColor.get()))) {
return new CachedParticlePacket(internal, offset, quantity, extra);
} else if (isSpell && color == null) {
color = defaultColor.get();
}
f0 = color.getRed() / 255f;
f1 = color.getGreen() / 255f;
f2 = color.getBlue() / 255f;
// but we already chose for the color, can't have both
if (isSpell) {
f0 = Math.max(f0, 0.001f);
f2 = Math.max(f0, 0.001f);
}
// If the f0 value 0 is, the redstone will set it automatically to red 255
if (f0 == 0f && internal == EnumParticleTypes.REDSTONE) {
f0 = 0.00001f;
}
} else if ((defaultNote = type.getDefaultOption(ParticleOptions.NOTE)).isPresent()) {
NotePitch notePitch = effect.getOption(ParticleOptions.NOTE).orElse(defaultNote.get());
float note = ((SpongeNotePitch) notePitch).getByteId();
if (note == 0f) {
return new CachedParticlePacket(internal, offset, quantity, extra);
}
f0 = note / 24f;
} else if ((defaultVelocity = type.getDefaultOption(ParticleOptions.VELOCITY)).isPresent()) {
Vector3d velocity = effect.getOption(ParticleOptions.VELOCITY).orElse(defaultVelocity.get());
f0 = velocity.getX();
f1 = velocity.getY();
f2 = velocity.getZ();
Optional<Boolean> slowHorizontalVelocity = type.getDefaultOption(ParticleOptions.SLOW_HORIZONTAL_VELOCITY);
if (slowHorizontalVelocity.isPresent() && effect.getOption(ParticleOptions.SLOW_HORIZONTAL_VELOCITY).orElse(slowHorizontalVelocity.get())) {
f0 = 0f;
f2 = 0f;
}
// The y value won't work for this effect, if the value isn't 0 the velocity won't work
if (internal == EnumParticleTypes.WATER_SPLASH) {
f1 = 0f;
}
if (f0 == 0f && f1 == 0f && f2 == 0f) {
return new CachedParticlePacket(internal, offset, quantity, extra);
}
}
// Is this check necessary?
if (f0 == 0f && f1 == 0f && f2 == 0f) {
return new CachedParticlePacket(internal, offset, quantity, extra);
}
return new CachedOffsetParticlePacket(internal, new Vector3f(f0, f1, f2), offset, quantity, extra);
}
use of org.spongepowered.api.item.FireworkEffect in project Skree by Skelril.
the class JungleRaidEffectListener method handleLoss.
private void handleLoss(JungleRaidInstance inst, Player player) {
FlagEffectData data = inst.getFlagData();
boolean isTitanEnabled = inst.isFlagEnabled(JungleRaidFlag.TITAN_MODE);
boolean isTitan = player.getUniqueId().equals(data.titan);
// Normal Jungle Raid fireworks and stuff
Color killerColor = Color.WHITE;
Color teamColor = inst.getTeamColor(player).orElse(Color.WHITE);
Optional<Player> optKiller = inst.getLastAttacker(player);
if (optKiller.isPresent()) {
Player killer = optKiller.get();
killerColor = inst.getTeamColor(killer).orElse(Color.WHITE);
if (isTitanEnabled) {
if (isTitan) {
data.titan = killer.getUniqueId();
ItemStack teamHood = newItemStack(ItemTypes.LEATHER_HELMET);
teamHood.offer(Keys.DISPLAY_NAME, Text.of(TextColors.WHITE, "Titan Hood"));
teamHood.offer(Keys.COLOR, Color.BLACK);
// playerEquipment.set(EquipmentTypes.HEADWEAR, teamHood);
tf(killer).inventory.armorInventory.set(3, tf(teamHood));
} else if (killer.getUniqueId().equals(data.titan)) {
killerColor = Color.BLACK;
}
}
}
if (isTitan && data.titan.equals(player.getUniqueId())) {
data.titan = null;
}
if (!inst.isFlagEnabled(JungleRaidFlag.DEATH_ROCKETS)) {
return;
}
Location<World> playerLoc = player.getLocation();
Color finalKillerColor = killerColor;
for (int i = 0; i < 12; i++) {
Task.builder().delayTicks(i * 4).execute(() -> {
Firework firework = (Firework) inst.getRegion().getExtent().createEntity(EntityTypes.FIREWORK, playerLoc.getPosition());
FireworkEffect fireworkEffect = FireworkEffect.builder().flicker(Probability.getChance(2)).trail(Probability.getChance(2)).color(teamColor).fade(finalKillerColor).shape(FireworkShapes.CREEPER).build();
firework.offer(Keys.FIREWORK_EFFECTS, Lists.newArrayList(fireworkEffect));
firework.offer(Keys.FIREWORK_FLIGHT_MODIFIER, Probability.getRangedRandom(2, 5));
inst.getRegion().getExtent().spawnEntity(firework, Cause.source(SpawnCause.builder().type(SpawnTypes.PLUGIN).build()).build());
}).submit(SkreePlugin.inst());
}
}
use of org.spongepowered.api.item.FireworkEffect in project SpongeCommon by SpongePowered.
the class FireworkUtils method getFireworkEffects.
public static Optional<List<FireworkEffect>> getFireworkEffects(Object object) {
ItemStack item = ItemStack.EMPTY;
if (object instanceof ItemStack) {
item = (ItemStack) object;
}
if (object instanceof EntityFireworkRocket) {
item = FireworkUtils.getItem((EntityFireworkRocket) object);
}
if (item.isEmpty())
return Optional.empty();
List<FireworkEffect> effects;
if (item.getItem() == Items.FIREWORKS) {
NBTTagCompound fireworks = item.getSubCompound("Fireworks");
if (fireworks == null || !fireworks.hasKey("Explosions"))
return Optional.empty();
NBTTagList effectsNbt = fireworks.getTagList("Explosions", NbtDataUtil.TAG_COMPOUND);
effects = Lists.newArrayList();
for (int i = 0; i < effectsNbt.tagCount(); i++) {
NBTTagCompound effectNbt = effectsNbt.getCompoundTagAt(i);
effects.add(fromNbt(effectNbt));
}
} else {
FireworkEffect effect = FireworkUtils.getChargeEffect(item);
if (effect == null)
return Optional.empty();
effects = ImmutableList.of(effect);
}
return Optional.of(effects);
}
Aggregations