# Bukkit shoot tnt & help with vectors

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]);
//}
sender.sendMessage(name+"BOOM!");
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);
tnt.setVelocity(v);
//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);

tnt.setVelocity(direction);

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:

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,

player.getEyeLocation().getDirection()


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
sender.sendMessage(ChatColor.DARK_AQUA+"BOOM!");

// 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
tnt.setVelocity(v);