I want to shoot a piece of tnt from the players face and then let it explode when it hits the target block.

        if (cmd.getName().equalsIgnoreCase("cannon")) {
        if(!perrmissionCheck(sender, "Needs.cannon", "cannon")){
            return true;
        if (!(sender instanceof Player)){
            sender.sendMessage(nameC+"You must be a player to preform this command.");
            return true;
        Block block = ((Player) sender).getTargetBlock(null, 50);
        Location location = ((Player) sender).getLocation();
        //float power = 4;
        //if (args.length == 1){
            //power = Float.parseFloat(args[0]);
        double x = block.getX();
        double y = block.getY();
        double z = block.getZ();
        TNTPrimed tnt = (TNTPrimed) ((Player)sender).getWorld().spawn(location, TNTPrimed.class);
        Vector v = new Vector(x, y, z);
        //block.getWorld().createExplosion(location, power);
        //block.getWorld().spawn(tntLoc, TNTPrimed.class);
        return true;

This kinda just ramdomly throws the tnt and then exploads i want to get where the player is looking and then shoot it that way if someone could also describe how the args work for new Vector(arg0, arg1, arg2) i would appreciate it...

Thanks any help is great

EDIT I relize this is really old but i wanted to share how I did it,

    int v = 2;

    if (args.length > 1){
        v = Integer.parseInt(args[0]);

    final Vector direction = user.getEyeLocation().getDirection().multiply(v);

    TNTPrimed tnt = user.getWorld().spawn(user.getEyeLocation().add(direction.getX(), direction.getY(), direction.getZ()), TNTPrimed.class);


    user.sendMessage(name + "Boom!");
    plugin.inGameMsg.msgAdmins(plugin.chatDark_AQUA + user.getName() + plugin.chatGray + " used cannon command!");
    return true;

this sends the tnt right where the player is looking


The three arguments taken by Vector are its (x, y, z) components. Here's a visual representation of a Vector in 3D space:

enter image description here

A vector can be used to store either a displacement (e.g. movement) or a position. In this case since you're passing the vector to setVelocity() it's a displacement - it specifies in which direction the TNT will be moving.

But it's probably not working because your Vector is currently holding the position of the target block in the world, and what you want is the direction the player is facing.

Try to see in the Player class if there's already a way to get a Vector representing the way it's facing. Otherwise you'd need to calculate one from the player's rotation values.


I know this is an old question, but here is the answer for someone else stumbling over this problem. :)

To get the Vector for the direction of the head you have to use,


Here is a working example of the code, you may have to play around with the multiplier to get the desired result.

I also rewrote some of the chatcolors to make it work for me.

// Get the player and the location of the eyes (head)
Player player = (Player) sender;
Location location = player.getEyeLocation();

// Send the player a message

// Prime a tnt and place it at the location of players head         
TNTPrimed tnt = (TNTPrimed) player.getWorld().spawn(location, TNTPrimed.class);
// mulitiply the direction from the location by 2 and store it as vector
Vector v = location.getDirection().multiply(2);
// Apply the vector to the primed tnt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.