Solved setPassenger is null

Discussion in 'Plugin Development' started by Squid_Boss, Jun 24, 2014.

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

    Squid_Boss

    Sooo. I have a runnable class and in the runnable I set an entity to passenger the player. Then in the run method I remove the player's passenger, set their passenger to the same entity. But when the run method loops back I get an NPE on player.getPassenger().remove() even though the last thing to run before the loop was player.setPassenger(entity);

    I'm really confused as to why the passenger is becoming null after setting the passenger. Here is my code:
    Code:java
    1. public class Game_CaptureFlag extends BukkitRunnable {
    2.  
    3. private Player p;
    4.  
    5. private int timer = 0;
    6.  
    7. @SuppressWarnings("deprecation")
    8. public Game_CaptureFlag(Player p) {
    9. this.p = p;
    10.  
    11. p.setPassenger(p.getWorld().spawnFallingBlock(new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY() + 3, p.getLocation().getZ()), Material.WOOL, (byte) 14));
    12. }
    13.  
    14. @SuppressWarnings("deprecation")
    15. public void run() {
    16. timer++;
    17.  
    18. if (p.isSneaking()) {
    19. this.cancel();
    20. p.getPassenger().remove();
    21. }
    22.  
    23. if (timer == 5) {
    24. timer = 0;
    25. Bukkit.broadcastMessage("Passenger before remove: " + p.getPassenger());
    26. p.getPassenger().remove();
    27. Bukkit.broadcastMessage("Passenger after remove: " + p.getPassenger());
    28.  
    29. p.setPassenger(p.getWorld().spawnFallingBlock(new Location(p.getWorld(), p.getLocation().getX(), p.getLocation().getY() + 3, p.getLocation().getZ()), Material.WOOL, (byte) 14));
    30.  
    31. Bukkit.broadcastMessage("New Passenger: " + p.getPassenger());
    32. }
    33. }
    34.  
    35. }
     
  2. Offline

    AmShaegar

  3. Offline

    fireblast709

    Squid_Boss why are you removing it in the first place? Wouldn't it be better to check whether it is null (maybe it died. Yes that could even happen with falling blocks I suppose) and set it again whenever it is.
     
  4. Offline

    Squid_Boss

    AmShaegar It returns CraftFallingSand.

    fireblast709 yea I noticed when setting the falling block as a passenger to the player it would die (break) in a few seconds. So I guess I can try that.
     
  5. Offline

    AmShaegar

  6. Offline

    Squid_Boss

    AmShaegar I mean the entity is a CraftFallingSand. Considering it shows a block floating above my head, I'd say it returns true. It's just that after awhile *without the runnable* the entity will die will passengered to the player, which is what the runnable work around was for.

    fireblast709 Yeah, I just checked if the passenger was null and if it was reset the passenger to the falling block. Thanks.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page