Shoot snowball?

Hello, i want to shoot an snowball in front of player, but i don’t know how can i do this?
do you have some idea?

(and a fireball on the future plz)

1 Like

Both Snowballs and Fireballs are Projectiles in the API.
Player is a ProjectileSource and has the method launchProjectile

It would look something like this:

Optional<Snowball> optSnowball = player.launchProjectile(Snowball.class);

i have an error, i think it’s not implemented yet…
[spoiler=error]
[15:52:59] [Server thread/ERROR] [Sponge]: Could not pass InteractBlockEvent$Sec
ondary$Impl to SpongePlugin:MyPluin{1.0}
java.lang.AbstractMethodError: Method net/minecraft/entity/player/EntityPlayerMP
.launchProjectile(Ljava/lang/Class;)Ljava/util/Optional; is abstract
at net.minecraft.entity.player.EntityPlayerMP.launchProjectile(EntityPla
yerMP.java) ~[qw.class:?]
at be.thesebakl.multiserve.MyPlugin.Main.oninteract(Main.java:536)
~[Main.class:?]
at org.spongepowered.common.event.listener.InteractEventListener_Main_on
interact8.handle(Unknown Source) ~[?:?]
at org.spongepowered.common.event.RegisteredListener.handle(RegisteredLi
stener.java:92) ~[RegisteredListener.class:1.8-1568-2.1-DEV-833]
at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEvent
Manager.java:225) [SpongeModEventManager.class:1.8-1568-2.1-DEV-833]
at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEvent
Manager.java:186) [SpongeModEventManager.class:1.8-1568-2.1-DEV-833]
at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEvent
Manager.java:265) [SpongeModEventManager.class:1.8-1568-2.1-DEV-833]
at org.spongepowered.mod.event.SpongeModEventManager.post(SpongeModEvent
Manager.java:237) [SpongeModEventManager.class:1.8-1568-2.1-DEV-833]
at org.spongepowered.common.Sponge.postEvent(Sponge.java:104) [Sponge.cl
ass:1.8-1568-2.1-DEV-833]
at net.minecraftforge.event.ForgeEventFactory.onPlayerInteract(ForgeEven
tFactory.java:66) [ForgeEventFactory.class:?]
at net.minecraft.network.NetHandlerPlayServer.func_147346_a(NetHandlerPl
ayServer.java:575) [rj.class:?]
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.func_
180769_a(SourceFile:59) [mx.class:?]
at net.minecraft.network.play.client.C08PacketPlayerBlockPlacement.func_
148833_a(SourceFile:10) [mx.class:?]
at net.minecraft.network.PacketThreadUtil$1.onProcessPacket(SourceFile:6
3) [ih.class:?]
at net.minecraft.network.PacketThreadUtil$1.run(SourceFile:13) [ih.class
:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
?:1.8.0_60]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_60]
at net.minecraftforge.fml.common.FMLCommonHandler.callFuture(FMLCommonHa
ndler.java:714) [FMLCommonHandler.class:?]
at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.jav
a:656) [MinecraftServer.class:?]
at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(Dedicated
Server.java:364) [po.class:?]
at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.jav
a:598) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:478) [M
inecraftServer.class:?]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_60]
[/spoiler]

You are correct, I had implemented this but it’s not in the master branch yet

Depending on your setup it may be possible to checkout to the branch feature/projectilesource to get the implementation if you need it.

oki, but how can i do??? i explain you
i want do an event who launch the projectile with shooter is the player,
and if the snowball touch an another player (another event), i want to get the shooter of the snowball, and the victim, and damage him (a gun)

how can i do it?

Projectile launching is now implemented so you can test it.

For your question, there’s a few things going on there.
To make a player shoot a snowball, it’s simply:

Player thePlayer = getThePlayer(); // Whatever
Optional<Snowball> snowball = thePlayer.launchProjectile(Snowball.class);

To make the snowball damage the player, listen to CollideEntityEvent and inspect the entities that are colliding.

1 Like

thx, but can i choose the speed? i want the snowball go fast
and if i can choose an another vector?

Yes, the launchProjectile takes a second argument called velocity

oki thx, but can you help me for the event? how can i get if there is an collision with a player and a snowball?
i have to do event.getCause.first(snowball.class) pour la classe et event.getCause.firste(player.class) pour le joeuur.
i’m no sure…

Something like this:

@Listener
public void onEntityCollide(CollideEntityEvent event) {
    Optional<Snowball> optSnowball = event.getCause().first(Snowball.class);
    if (!optSnowball.isPresent()) {
        return;
    }
    Snowball snowball = optSnowball.get();
    for (Entity entity : event.getEntities()) {
        if (entity instanceof Player) {
            Player player = (Player) entity;
            System.out.println(player + " hit by " + snowball);
        }
    }
}

Yes, chute door.