How to Set and Unset Passengers of Entities

Discussion in 'Resources' started by XHawk87, Jan 11, 2012.

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

    XHawk87

    I personally struggled with this problem for a while, finding no help in the javadocs or in these forums. Eventually I investigated the Craftbukkit source code and found the solution, so I decided to share my findings with others who are likewise stuck.

    Setting one Entity as the passenger of another is fairly simple and works nicely. E.g.

    Code:
    vehicle.setPassenger(passenger);
    However, problems occur when trying to dismount. Lacking an unsetPassenger method, the next intuitive solution would be to do:

    Code:
    vehicle.setPassenger(null);
    Which has no effect. Why? Because the setPassenger method in Bukkit ties to the setPassengerOf method of the Minecraft server which sets the passenger Entity as the passenger of the vehicle Entity parameter. So in order to set the passenger of a vehicle, there must be a passenger from which to set it.

    The setPassenger method is in fact a toggle. Do it while dismounted and it mounts, do it while mounted and it dismounts. So if you happen to know the passenger, you could use this:

    Code:
    vehicle.setPassenger(passenger);
    Or if you just want to eject any passenger that it may have, you can do this:

    Code:
    vehicle.setPassenger(vehicle.getPassenger());
    Also note that if you set one entity as a passenger of a vehicle, and then a different entity as a passenger of that vehicle again, the first passenger will be displaced by the second. E.g.

    Code:
    vehicle.setPassenger(passengerA);
    vehicle.setPassenger(passengerB);
    passengerB will then be in the vehicle, and passengerA will not be.
     
    ProMCKingz and Caiden2000 like this.
Thread Status:
Not open for further replies.

Share This Page