# Solved How to get direction a player is walking in?

Discussion in 'Spigot Plugin Development' started by PartyMC, Mar 14, 2020.

1. ### PartyMC

I'm currently making a simple door where only people with certain permissions can go through, and I want players when they do not have the permission to be pushed backwards. I got pretty far, but for now, it pushes the player backwards from the direction they were looking from. This means that they can just turn around and push themselves into there, but I do not want that. So I want to get the direction a player is walking in. How do I get that?

2. ### Andross

Hello,

You can use Player#getLocation()#getDirection?

Else, instead of using the player direction, why not using the door direction? I mean, you get the "in front" direction of the door and you push the players toward this direction, so it does not depends anymore of the players direction.

3. ### PartyMC

I forgot to explain that it's not a real door they walk through, just a gap which can be detected to be one of these doors by a sign. The thing with Player#getLocation()#getDirection() is that players can just rotate and walk right in.

try getting the vector between the player and the block that represents the door, invert it and set the players velocity to that vector

5. ### PartyMC

Im fairly new to vectors, could you explain how to do this? Something like this or something?
Code (Java):
Vector pv = p.getVelocity();
I do not get how to get the velocity of a block though

a player's velocity is basically the speed at which he travels, which should not matter. instead you're looking for his positional vector, which you can get with
Vector playerVector = player.getLocation().toVector();.
Then you just subtract the block location's vector to get the "inverted" difference and call a normalize on it to make the length 1 and the push back consistently.
Vector difference = playerVector.subtract(block.getLocation().toVector()).normalize();
The last thing you do is setting the players velocity to that new vector
player.setVelocity(difference);

If you want to learn more about vectors, there is a very in-depth article here on spigot.

7. ### PartyMC

Thanks alot! This worked for me!

8. ### PartyMC

I actually have one more slight problem; I'm using a sign to create these doors, and at the "block.getLocation()" it automatically gets the direction too, which results in the player launching the way the sign is pointed. How could I fix this?

The block should not inherently know which way it's facing, for that you would need to cast it's data to Directional first, so your problem might originate from something else. If the player is standing in front of the sign, they will get pushed in the direction it's facing since it's the opposite direction from between the player and the block.

10. ### PartyMC

I tested it with placing my sign in different directions, and each time I was also pushed in other directions. I tried setting the direction to null, but that doesnt work. This is the code I have right now:
Code (Java):
@EventHandler
public void onMove(PlayerMoveEvent e) {
Player p = e.getPlayer();
Location l = p.getLocation();
l.setY(l.getY() - 2);
if(l.getBlock().getState().getType().equals(Material.OAK_SIGN)) {
Sign b = (Sign) l.getBlock().getState();
if(b.getLine(0).equalsIgnoreCase("[door]")) {
String perm = b.getLine(1);
if(!p.hasPermission(perm)) {

Vector pv = p.getLocation().toVector();
Vector d = pv.subtract(b.getLocation().toVector().normalize());

p.spawnParticle(Particle.BARRIER, l.getBlockX()+0.5, l.getBlockY()+3.5, l.getBlockZ()+0.5, 0, 0, 0, 0.1, 1, null);
p.setVelocity(d.multiply(0.003));
if(getConfig().getBoolean("message-enabled") == true) {
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(getConfig().getString("message").replace("&", "ยง")));
}
}
}
}
}
Edit: I did some more testing and now signs just shoot me randomly, so it must be something in the code like you said.