setPassenger() not working

Discussion in 'Plugin Development' started by milesokeefe, Mar 24, 2012.

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

    milesokeefe

    My test code:
    Code:
    @EventHandler
            public void onVehicleCreate(VehicleCreateEvent event){
                Player player = Bukkit.getServer().getPlayer("milesokeefe");
                event.getVehicle().setPassenger(player);
    }
    I'm using the above method of finding the player temporarily for simplicity's sake.
    What happens when the event is called:
    Boats:
    • The player cannot control the boat, but can push it around by bumping into it.
    • The boat only moves horizontally, not up and down even when there is air below it, as if in water.
    • The boat moves through blocks.
    Minecarts:
    • The player is not physically put into the cart, or moved at all.
    • The player cannot control the cart at all, nor can the player bump into it, the cart has no collision detection at all.

    Both:
    • The player is not physically put into the vehicle or moved at all.
    • The player collides with blocks normally, but otherwise acts like it is in a vehicle, for example: after setPassenger() is called, the player cannot pick up floating items until he/she exits.
    • When the vehicle is right clicked as if to exit, the player is moved to the location of the vehicle and onVehicleExit() is properly called.
    • After setPassenger() is called, the player's avatar is set to the sitting position, despite not being in the vehicle.
    Anyone have any idea why this is happening? Is it just a bug in the latest update?

    Picture for proof/clarity:​
    [​IMG]
     
  2. Offline

    nicholasntp

    Probably. Try doing:
    Code:java
    1.  
    2. @EventHandler
    3. public void onVehicleCreate(VehicleCreateEvent event){
    4. Player player = event.getPlayer();
    5. event.getVehicle().setPassenger(player);
    6. }
    7.  


    Instead of:

    Code:java
    1.  
    2. @EventHandler
    3. public void onVehicleCreate(VehicleCreateEvent event){
    4. Player player = Bukkit.getServer().getPlayer("milesokeefe");
    5. event.getVehicle().setPassenger(player);
    6. }
    7.  


    Ah. Does it work like that?

    I think thats a good idea too.

    Idk. That confuses me too.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 19, 2020
  3. Offline

    milesokeefe

    There is no getPlayer() function.
    In fact, other than the method I posted, I've had to scan the area around the vehicle for the player who created it.
    It's a pretty awful hack of a job I'll admit, but I've searched the Java Docs and I don't really see another way.
    Here's how by the way:
    Code:
    List<Entity> possibly_player = event.getVehicle().getNearbyEntities(10.0, 10.0, 10.0);
                        for(int i =0; i< possibly_player.size();i++){
                            if(possibly_player.get(i).getType()  == EntityType.PLAYER){
                                event.getVehicle().setPassenger((Entity)possibly_player.get(i));
                                break;
                            }
                        }
    It does work, but for testing I prefer to just hard code my name.
    I'm thinking of adding a test for line of sight, so the code would
    1. scan the area in which it is possible to spawn
    2. find the players in the area
    3. use the first player that is looking directly at the vehicle
    It could be buggy if two people are looking exactly where is a boat is spawned and are a few blocks away from each other, but it's a small price to pay for the mod to work at all.

    Honestly I don't understand why event.getPlayer() isn't in the event, I don't see how else it could be created.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 10, 2020
  4. Offline

    Brettflan

    EDIT: I had the same thing happening, but in my case it was because I had teleported the player and the vehicle and then tried to use setPassenger() at that point. Even delaying it a tick didn't work, but 2 ticks (at least locally on a VM) is working for me. The problem for me was apparently that the client needed time to process the new entity locations before using setPassenger() or something along those lines.

    Of course, I'm left with the dilemma of trying to figure out the shortest number of ticks I can delay while still making sure it works for a potentially loaded server on the internet with players who potentially have bad connections... bah.
     
Thread Status:
Not open for further replies.

Share This Page