Vehicle Passenger

Discussion in 'Plugin Development' started by darkmage0252, Dec 17, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    darkmage0252

    How do i get a player thats in a vehicle?

    i tried

    Player p = (Player)event.getVehicle().getPassenger();
    but stuff like p.getName() wont work?
     
  2. Offline

    XHawk87

    Casting has a high priority in the Java parser. You are trying to cast an event object to a Player class, which will understandably fail. You should check the javadocs for the particular event you are listening for to find the correct method to call, and before attempting to cast the resulting passenger entity to a Player, make sure that the passenger is actually a Player. E.g.

    Code:
    Entity passenger = event.getEntered().getPassenger();
    if (passenger instanceof Player) {
        Player p = (Player) passenger;
        // do something
    }
     
  3. Errm.. no. The way he did it was fine. If he explicitly wanted to cast SomeEvent to Player, then he would have done
    Code:java
    1. Player p = ((Player)event).getVehicle().getPassenger();

    He would also get a ClassCastException by doing so.

    But agreed, he would have to check whether the pessenger is a Player first though.
     
  4. @darkmage0252 Apart from checking if passager is a player (it can be a mob you know :p), you should also check if passager is not null as well (because it can be empty).
     
Thread Status:
Not open for further replies.

Share This Page