3

I am using Spigot 1.11.2.

I have this simple code:

@EventHandler
public void onPlayerInteractEntity (PlayerInteractEntityEvent event) {
    if (entity instanceof AbstractHorse) {
        event.setCancelled(true);

        AbstractHorse horse = (AbstractHorse) event.getRightClicked();
        Player player = event.getPlayer();
        player.sendMessage(horse.getName());
    }
}

What it's supposed to do is if I right-click a horse, I should get its name but I shouldn't ride it, because of the event.setCancelled(true). It works as expected, but the problem is that when I right-click a horse, the game turns my view around (my player's yaw) to some number that is not consistent. Yet I don't want to turn around; I still want my player to be looking at where I was looking at (at the horse in this case) after I right-click the horse.

I tried some methods, like saving my pitch and yaw then setting it again after cancelling the event:

@EventHandler
public void onPlayerInteractEntity (PlayerInteractEntityEvent event) {
    Player player = event.getPlayer();
    float pitch = player.getLocation().getPitch();
    float yaw = player.getLocation().getYaw();

    if (entity instanceof AbstractHorse) {
        event.setCancelled(true);
        player.getLocation().setPitch(pitch);
        player.getLocation().setYaw(yaw);

        AbstractHorse horse = (AbstractHorse) event.getRightClicked();
        player.sendMessage(horse.getName());
    }
}

and also similarly tried saving the player's direction (via player.getLocation().getDirection()) then setting it again after cancelling the event, to no avail. Am I doing something wrong here? Or is this a bug?

Mark Zedler
  • 125
  • 1
  • 7

4 Answers4

2

Well, I found that you can't actually directly edit a player's pitch and yaw. You have to teleport the player to a custom Location with the custom pitch and yaw values, like this:

@EventHandler
public void onPlayerInteractEntity (PlayerInteractEntityEvent event) {
    Entity entity = event.getRightClicked();
    Player player = event.getPlayer();

    float pitch = player.getLocation().getPitch();
    float yaw = player.getLocation().getYaw();

    if (entity instanceof AbstractHorse) {
        event.setCancelled(true);

        Location loc = player.getLocation();
        loc.setPitch(pitch);
        loc.setYaw(yaw);
        player.teleport(loc);

        AbstractHorse horse = (AbstractHorse) event.getRightClicked();
        player.sendMessage(horse.getName());
    }
}

So now the player would still be looking to where they were looking at before right-clicking a horse, although it looks glitchy. There's no other way to do it so far, I believe.

Mark Zedler
  • 125
  • 1
  • 7
0

You are using entity in your if-condition, but you never declare it. Use this code:

@EventHandler
public void onPlayerInteractEntity (PlayerInteractEntityEvent event) {
    Entity entity = event.getRightClicked();

    if (entity instanceof AbstractHorse) {
        event.setCancelled(true);

        AbstractHorse horse = (AbstractHorse) entity;
        Player player = event.getPlayer();
        player.sendMessage(horse.getName());
    }
}
MrDarkLynx
  • 686
  • 1
  • 9
  • 15
0

You're looking for a VehicleEnterEvent. It's the event that's thrown when an Entity starts riding another Entity (it includes horses, minecarts, or any other entity under the right circumstance).

It's defined in the spigot javadocs


Quick example on how to implement it:

@EventHandler
public void onHorseMount(VehicleEnterEvent e) {
    Entity entered = e.getEntered();
    Vehicle enteredOn = e.getVehicle();
    if (enteredOn instanceof AbstractHorse) {

        //Entity entered a Horse

        if (!(entered instanceof Player))return; //Returns if this is not a player

        Player mountingHorse = (Player) entered;
        AbstractHorse horseEntered = (AbstractHorse) enteredOn;
        mountingHorse.sendMessage(horseEntered.getName());   //Sending the message
        e.setCancelled(true);
    }
}
Community
  • 1
  • 1
LeoColman
  • 6,950
  • 7
  • 34
  • 63
-2

You should use player.getEyeLocation() to retrieve pitch and yaw values

MartinStone
  • 184
  • 1
  • 2
  • 14