Search in sources :

Example 1 with Vector3f

use of com.flowpowered.math.vector.Vector3f 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);
}
Also used : ItemType(org.spongepowered.api.item.ItemType) FireworkEffect(org.spongepowered.api.item.FireworkEffect) Direction(org.spongepowered.api.util.Direction) NotePitch(org.spongepowered.api.data.type.NotePitch) SpongeNotePitch(org.spongepowered.common.data.type.SpongeNotePitch) Item(net.minecraft.item.Item) SpongeNotePitch(org.spongepowered.common.data.type.SpongeNotePitch) IBlockState(net.minecraft.block.state.IBlockState) Optional(java.util.Optional) Potion(net.minecraft.potion.Potion) Color(org.spongepowered.api.util.Color) SPacketEntityMetadata(net.minecraft.network.play.server.SPacketEntityMetadata) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) Vector3d(com.flowpowered.math.vector.Vector3d) EnumParticleTypes(net.minecraft.util.EnumParticleTypes) Vector3f(com.flowpowered.math.vector.Vector3f) EntityDataManager(net.minecraft.network.datasync.EntityDataManager) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) SpongeItemStackSnapshot(org.spongepowered.common.item.inventory.SpongeItemStackSnapshot) PotionType(net.minecraft.potion.PotionType)

Example 2 with Vector3f

use of com.flowpowered.math.vector.Vector3f in project SpongeAPI by SpongePowered.

the class ColorTest method testOfVector3f.

@Test
public void testOfVector3f() {
    final Vector3f vecGray = new Vector3f(128.0, 128.459, 127.5);
    final Color colorGray = Color.of(vecGray);
    assertTrue(Color.GRAY.equals(colorGray));
}
Also used : Vector3f(com.flowpowered.math.vector.Vector3f) Test(org.junit.Test)

Example 3 with Vector3f

use of com.flowpowered.math.vector.Vector3f in project LanternServer by LanternPowered.

the class CommandParticle method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(new PatternMatchingCommandElement(Text.of("type")) {

        @Override
        protected Iterable<String> getChoices(CommandSource source) {
            return Sponge.getGame().getRegistry().getAllOf(ParticleType.class).stream().filter(type -> ((LanternParticleType) type).getInternalType().isPresent()).map(CatalogType::getId).collect(Collectors.toList());
        }

        @Override
        protected Object getValue(String choice) throws IllegalArgumentException {
            final Optional<ParticleType> ret = Sponge.getGame().getRegistry().getType(ParticleType.class, choice);
            if (!ret.isPresent() || !((LanternParticleType) ret.get()).getInternalType().isPresent()) {
                throw new IllegalArgumentException("Invalid input " + choice + " was found");
            }
            return ret.get();
        }
    }, GenericArguments2.targetedVector3d(Text.of("position")), // The default value should be 0 for x, y and z
    GenericArguments2.vector3d(Text.of("offset"), Vector3d.ZERO), GenericArguments2.doubleNum(Text.of("speed"), 1.0), GenericArguments.optional(GenericArguments2.integer(Text.of("count"), 1)), GenericArguments.optional(new CommandElement(Text.of("mode")) {

        @Nullable
        @Override
        protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
            return args.next();
        }

        @Override
        public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
            Optional<String> arg = args.nextIfPresent();
            if (arg.isPresent()) {
                return Stream.of("normal", "force").filter(new StartsWithPredicate(arg.get())).collect(Collectors.toList());
            }
            return Collections.emptyList();
        }
    }), GenericArguments.optional(GenericArguments.player(Text.of("player"))), GenericArguments.optional(new CommandElement(Text.of("params")) {

        @Nullable
        @Override
        protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
            List<Integer> params = new ArrayList<>();
            while (args.hasNext()) {
                String arg = args.next();
                try {
                    params.add(Integer.parseInt(arg));
                } catch (NumberFormatException e) {
                    throw args.createError(t("Expected an integer, but input '%s' was not", arg));
                }
            }
            return params.stream().mapToInt(i -> i).toArray();
        }

        @Override
        public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
            return Collections.emptyList();
        }
    })).executor((src, args) -> {
        final LanternParticleType particleType = args.<LanternParticleType>getOne("type").get();
        final int particleId = particleType.getInternalType().getAsInt();
        final Vector3f position = args.<Vector3d>getOne("position").get().toFloat();
        final Vector3f offset = args.<Vector3d>getOne("offset").get().toFloat();
        final float speed = args.<Double>getOne("speed").get().floatValue();
        final int count = args.<Integer>getOne("count").orElse(1);
        final boolean longDistance = args.<String>getOne("mode").map(mode -> mode.equalsIgnoreCase("force")).orElse(false);
        final int[] params = args.<int[]>getOne("params").orElse(new int[0]);
        final LanternWorld world = CommandHelper.getWorld(src, args);
        final int dataLength;
        if (particleType == ParticleTypes.BLOCK_CRACK || particleType == ParticleTypes.BLOCK_DUST || particleType == ParticleTypes.FALLING_DUST) {
            dataLength = 1;
        } else if (particleType == ParticleTypes.ITEM_CRACK) {
            dataLength = 2;
        } else {
            dataLength = 0;
        }
        if (params.length != dataLength) {
            throw new CommandException(t("Invalid parameters (%s), length mismatch (got %s, expected %s) for the particle type %s", Arrays.toString(params), params.length, dataLength, particleType.getId()));
        }
        final MessagePlayOutSpawnParticle message = new MessagePlayOutSpawnParticle(particleId, position, offset, speed, count, params, longDistance);
        if (args.hasAny("player")) {
            args.<LanternPlayer>getOne("player").get().getConnection().send(message);
        } else {
            for (LanternPlayer player : world.getRawPlayers()) {
                player.getConnection().send(message);
            }
        }
        src.sendMessage(t("commands.particle.success", particleType.getName(), count));
        return CommandResult.success();
    });
}
Also used : Arrays(java.util.Arrays) MessagePlayOutSpawnParticle(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSpawnParticle) Vector3d(com.flowpowered.math.vector.Vector3d) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) CommandArgs(org.spongepowered.api.command.args.CommandArgs) LanternParticleType(org.lanternpowered.server.effect.particle.LanternParticleType) GenericArguments(org.spongepowered.api.command.args.GenericArguments) ArrayList(java.util.ArrayList) ParticleType(org.spongepowered.api.effect.particle.ParticleType) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) ParticleTypes(org.spongepowered.api.effect.particle.ParticleTypes) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) GenericArguments2(org.lanternpowered.server.command.element.GenericArguments2) LanternWorld(org.lanternpowered.server.world.LanternWorld) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) CommandSource(org.spongepowered.api.command.CommandSource) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) Stream(java.util.stream.Stream) Vector3f(com.flowpowered.math.vector.Vector3f) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Optional(java.util.Optional) PatternMatchingCommandElement(org.spongepowered.api.command.args.PatternMatchingCommandElement) Collections(java.util.Collections) CommandContext(org.spongepowered.api.command.args.CommandContext) LanternParticleType(org.lanternpowered.server.effect.particle.LanternParticleType) ParticleType(org.spongepowered.api.effect.particle.ParticleType) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) LanternWorld(org.lanternpowered.server.world.LanternWorld) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) ArrayList(java.util.ArrayList) List(java.util.List) CommandArgs(org.spongepowered.api.command.args.CommandArgs) Optional(java.util.Optional) MessagePlayOutSpawnParticle(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutSpawnParticle) CommandException(org.spongepowered.api.command.CommandException) CommandSource(org.spongepowered.api.command.CommandSource) Vector3d(com.flowpowered.math.vector.Vector3d) PatternMatchingCommandElement(org.spongepowered.api.command.args.PatternMatchingCommandElement) Vector3f(com.flowpowered.math.vector.Vector3f) LanternParticleType(org.lanternpowered.server.effect.particle.LanternParticleType) CommandElement(org.spongepowered.api.command.args.CommandElement) PatternMatchingCommandElement(org.spongepowered.api.command.args.PatternMatchingCommandElement) Nullable(javax.annotation.Nullable) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 4 with Vector3f

use of com.flowpowered.math.vector.Vector3f in project LanternServer by LanternPowered.

the class ProcessorPlayOutParticleEffect method preProcess.

private ICachedMessage preProcess(ParticleEffect effect0) {
    final LanternParticleEffect effect = (LanternParticleEffect) effect0;
    final LanternParticleType type = effect.getType();
    final OptionalInt internalType = type.getInternalType();
    // Special cases
    if (!internalType.isPresent()) {
        if (type == ParticleTypes.FIREWORKS) {
            // Create the fireworks data item
            final LanternItemStack itemStack = new LanternItemStack(ItemTypes.FIREWORKS);
            itemStack.tryOffer(Keys.FIREWORK_EFFECTS, effect.getOptionOrDefault(ParticleOptions.FIREWORK_EFFECTS).get());
            // Write the item to a parameter list
            final ByteBufParameterList parameterList = new ByteBufParameterList(ByteBufferAllocator.unpooled());
            parameterList.add(EntityParameters.Fireworks.ITEM, itemStack);
            return new CachedFireworksMessage(new MessagePlayOutEntityMetadata(CachedFireworksMessage.ENTITY_ID, parameterList));
        } else if (type == ParticleTypes.FERTILIZER) {
            final int quantity = effect.getOptionOrDefault(ParticleOptions.QUANTITY).get();
            return new CachedEffectMessage(2005, quantity, false);
        } else if (type == ParticleTypes.SPLASH_POTION) {
            final int potionId = this.potionEffectTypeToId.getInt(effect.getOptionOrDefault(ParticleOptions.POTION_EFFECT_TYPE).get());
            return new CachedEffectMessage(2002, potionId, false);
        } else if (type == ParticleTypes.BREAK_BLOCK) {
            final int state = getBlockState(effect, type.getDefaultOption(ParticleOptions.BLOCK_STATE));
            if (state == 0) {
                return EmptyCachedMessage.INSTANCE;
            }
            return new CachedEffectMessage(2001, state, false);
        } else if (type == ParticleTypes.MOBSPAWNER_FLAMES) {
            return new CachedEffectMessage(2004, 0, false);
        } else if (type == ParticleTypes.ENDER_TELEPORT) {
            return new CachedEffectMessage(2003, 0, false);
        } else if (type == ParticleTypes.DRAGON_BREATH_ATTACK) {
            return new CachedEffectMessage(2006, 0, false);
        } else if (type == ParticleTypes.FIRE_SMOKE) {
            final Direction direction = effect.getOptionOrDefault(ParticleOptions.DIRECTION).get();
            return new CachedEffectMessage(2000, getDirectionData(direction), false);
        }
        return EmptyCachedMessage.INSTANCE;
    }
    final int internalId = internalType.getAsInt();
    final Vector3f offset = effect.getOption(ParticleOptions.OFFSET).map(Vector3d::toFloat).orElse(Vector3f.ZERO);
    final 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
    final Optional<BlockState> defaultBlockState;
    if (type != ParticleTypes.ITEM_CRACK && (defaultBlockState = type.getDefaultOption(ParticleOptions.BLOCK_STATE)).isPresent()) {
        final int state = getBlockState(effect, defaultBlockState);
        if (state == 0) {
            return EmptyCachedMessage.INSTANCE;
        }
        extra = new int[] { state };
    }
    final Optional<ItemStackSnapshot> defaultItemStackSnapshot;
    if (extra == null && (defaultItemStackSnapshot = type.getDefaultOption(ParticleOptions.ITEM_STACK_SNAPSHOT)).isPresent()) {
        final Optional<ItemStackSnapshot> optItemStackSnapshot = effect.getOption(ParticleOptions.ITEM_STACK_SNAPSHOT);
        if (optItemStackSnapshot.isPresent()) {
            extra = toExtraItemData(optItemStackSnapshot.get().createStack());
        } else {
            final Optional<BlockState> optBlockState = effect.getOption(ParticleOptions.BLOCK_STATE);
            if (optBlockState.isPresent()) {
                final BlockState blockState = optBlockState.get();
                final Optional<ItemType> optItemType = blockState.getType().getItem();
                if (optItemType.isPresent()) {
                    // TODO: Item damage value
                    extra = new int[] { ItemRegistryModule.get().getInternalId(optItemType.get()), 0 };
                } else {
                    return EmptyCachedMessage.INSTANCE;
                }
            } else {
                extra = toExtraItemData(defaultItemStackSnapshot.get().createStack());
            }
        }
    }
    if (extra == null) {
        extra = new int[0];
    }
    final Optional<Double> defaultScale = type.getDefaultOption(ParticleOptions.SCALE);
    final Optional<Color> defaultColor;
    final Optional<NotePitch> defaultNote;
    final Optional<Vector3d> defaultVelocity;
    if (defaultScale.isPresent()) {
        double scale = effect.getOption(ParticleOptions.SCALE).orElse(defaultScale.get());
        // Server formula: sizeServer = (-sizeClient * 2) + 2
        if (type == ParticleTypes.LARGE_EXPLOSION || type == ParticleTypes.SWEEP_ATTACK) {
            scale = (-scale * 2f) + 2f;
        }
        if (scale == 0f) {
            return new CachedParticleMessage(internalId, offset, quantity, extra);
        }
        f0 = scale;
    } else if ((defaultColor = type.getDefaultOption(ParticleOptions.COLOR)).isPresent()) {
        final boolean isSpell = type == ParticleTypes.MOB_SPELL || type == ParticleTypes.AMBIENT_MOB_SPELL;
        Color color = effect.getOption(ParticleOptions.COLOR).orElse(null);
        if (!isSpell && (color == null || color.equals(defaultColor.get()))) {
            return new CachedParticleMessage(internalId, 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 && type == ParticleTypes.REDSTONE_DUST) {
            f0 = 0.00001f;
        }
    } else if ((defaultNote = type.getDefaultOption(ParticleOptions.NOTE)).isPresent()) {
        final NotePitch notePitch = effect.getOption(ParticleOptions.NOTE).orElse(defaultNote.get());
        final float note = ((LanternNotePitch) notePitch).getInternalId();
        if (note == 0f) {
            return new CachedParticleMessage(internalId, offset, quantity, extra);
        }
        f0 = note / 24f;
    } else if ((defaultVelocity = type.getDefaultOption(ParticleOptions.VELOCITY)).isPresent()) {
        final Vector3d velocity = effect.getOption(ParticleOptions.VELOCITY).orElse(defaultVelocity.get());
        f0 = velocity.getX();
        f1 = velocity.getY();
        f2 = velocity.getZ();
        final 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 (type == ParticleTypes.WATER_SPLASH) {
            f1 = 0f;
        }
        if (f0 == 0f && f1 == 0f && f2 == 0f) {
            return new CachedParticleMessage(internalId, offset, quantity, extra);
        }
    }
    // Is this check necessary?
    if (f0 == 0f && f1 == 0f && f2 == 0f) {
        return new CachedParticleMessage(internalId, offset, quantity, extra);
    }
    return new CachedOffsetParticleMessage(internalId, new Vector3f(f0, f1, f2), offset, quantity, extra);
}
Also used : ItemType(org.spongepowered.api.item.ItemType) LanternParticleEffect(org.lanternpowered.server.effect.particle.LanternParticleEffect) Direction(org.spongepowered.api.util.Direction) NotePitch(org.spongepowered.api.data.type.NotePitch) LanternNotePitch(org.lanternpowered.server.data.type.LanternNotePitch) ByteBufParameterList(org.lanternpowered.server.network.entity.parameter.ByteBufParameterList) Optional(java.util.Optional) Color(org.spongepowered.api.util.Color) OptionalInt(java.util.OptionalInt) LanternItemStack(org.lanternpowered.server.inventory.LanternItemStack) LanternNotePitch(org.lanternpowered.server.data.type.LanternNotePitch) BlockState(org.spongepowered.api.block.BlockState) Vector3d(com.flowpowered.math.vector.Vector3d) Vector3f(com.flowpowered.math.vector.Vector3f) MessagePlayOutEntityMetadata(org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayOutEntityMetadata) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) LanternParticleType(org.lanternpowered.server.effect.particle.LanternParticleType)

Example 5 with Vector3f

use of com.flowpowered.math.vector.Vector3f in project LanternServer by LanternPowered.

the class CodecPlayOutSpawnParticle method encode.

@Override
public ByteBuffer encode(CodecContext context, MessagePlayOutSpawnParticle message) throws CodecException {
    Vector3f position = message.getPosition();
    Vector3f offset = message.getOffset();
    int[] extra = message.getExtra();
    ByteBuffer buf = context.byteBufAlloc().buffer(BASE_LENGTH);
    buf.writeInteger(message.getParticleId());
    buf.writeBoolean(message.isLongDistance());
    buf.writeFloat(position.getX());
    buf.writeFloat(position.getY());
    buf.writeFloat(position.getZ());
    buf.writeFloat(offset.getX());
    buf.writeFloat(offset.getY());
    buf.writeFloat(offset.getZ());
    buf.writeFloat(message.getData());
    buf.writeInteger(message.getCount());
    for (int value : extra) {
        buf.writeVarInt(value);
    }
    return buf;
}
Also used : Vector3f(com.flowpowered.math.vector.Vector3f) ByteBuffer(org.lanternpowered.server.network.buffer.ByteBuffer)

Aggregations

Vector3f (com.flowpowered.math.vector.Vector3f)5 Vector3d (com.flowpowered.math.vector.Vector3d)3 Optional (java.util.Optional)3 LanternParticleType (org.lanternpowered.server.effect.particle.LanternParticleType)2 BlockState (org.spongepowered.api.block.BlockState)2 NotePitch (org.spongepowered.api.data.type.NotePitch)2 ItemType (org.spongepowered.api.item.ItemType)2 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)2 Color (org.spongepowered.api.util.Color)2 Direction (org.spongepowered.api.util.Direction)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 OptionalInt (java.util.OptionalInt)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Nullable (javax.annotation.Nullable)1 IBlockState (net.minecraft.block.state.IBlockState)1 Item (net.minecraft.item.Item)1