[Solved] player.getBedSpawnLocation() troubles.

Discussion in 'Plugin Development' started by Slamakans, Nov 28, 2011.

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

    Slamakans

    Okay, so I get this error message whenever I left-click.
    Error:
    Code:java
    1. Could not pass event PLAYER_INTERACT to Useful Compass
    2. at org.bukkit.craftbukkit.entity.CraftPlayer.getBedSpawnLocation(CraftPl
    3. ayer.java:538)
    4. at me.slamakans.usefulcompass.UCPlayerListener.onPlayerInteract(UCPlayer
    5. Listener.java:43)
    6. at org.bukkit.plugin.java.JavaPluginLoader$11.execute(JavaPluginLoader.j
    7. ava:330)
    8. at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    9. a:58)
    10. at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    11. ava:339)
    12. at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEven
    13. t(CraftEventFactory.java:171)
    14. at org.bukkit.craftbukkit.event.CraftEventFactory.callPlayerInteractEven
    15. t(CraftEventFactory.java:142)
    16. at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:824)
    17. at net.minecraft.server.Packet18ArmAnimation.a(SourceFile:38)
    18. at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
    19. at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
    20. at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
    21. at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
    22. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
    23. at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)


    And this is my code:
    Code:java
    1. package me.slamakans.usefulcompass;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.World;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.block.Action;
    7. import org.bukkit.event.player.PlayerInteractEvent;
    8. import org.bukkit.event.player.PlayerListener;
    9.  
    10. public class UCPlayerListener extends PlayerListener{
    11. public static UCMain plugin;
    12.  
    13. public UCPlayerListener(UCMain instance){
    14.  
    15. plugin = instance;
    16.  
    17. }
    18. public void onPlayerInteract(PlayerInteractEvent event){
    19.  
    20. Player player = event.getPlayer();
    21.  
    22. World world = player.getLocation().getWorld();
    23.  
    24. boolean rightClick = event.getAction().equals(Action.RIGHT_CLICK_BLOCK);
    25. boolean leftClick = event.getAction().equals(Action.LEFT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_AIR);
    26.  
    27. int itemId = player.getItemInHand().getTypeId();
    28.  
    29. if(itemId == 345 && rightClick){
    30.  
    31. Location clickedBlockLocation = event.getClickedBlock().getLocation();
    32.  
    33. plugin.lastLocation.remove(player);
    34. plugin.lastLocation.put(player, player.getCompassTarget());
    35.  
    36. player.setCompassTarget(clickedBlockLocation);
    37. UCMain.greenMessage(player, "Changed your compass target to target block.");
    38.  
    39. }else if(itemId == 345 && leftClick){
    40.  
    41. Location tempLoc = player.getBedSpawnLocation();
    42.  
    43. if(tempLoc == null) tempLoc = world.getSpawnLocation();
    44.  
    45. plugin.lastLocation.remove(player);
    46. plugin.lastLocation.put(player, player.getCompassTarget());
    47.  
    48. UCMain.greenMessage(player, "Changed your compass target to your spawn.");
    49.  
    50. player.setCompassTarget(tempLoc);
    51.  
    52. }
    53.  
    54. }


    Help is greatly appreciated and placing credits in the plugin thread and tester on devBukkit(If you tell me your name there).
     
  2. Offline

    Daniel Heppner

    Location tempLoc = player.getBedSpawnLocation();
    Player is null. It hasn't been initialized. Not sure why. (maybe because Player doesn't have a bed? Try checking if it's null before running that code.
    Also, I suggest not having a method for each color, it's a waste of code.
     
  3. Offline

    coldandtired

    Player.getBedSpawnLocation() will return null if the player hasn't slept in a bed or if the bed location isn't valid any more.
     
  4. Offline

    bleachisback

    No, I don't think that's the problem, considering that the NPE is generating inside the getBedSpawnLocation() itself. Furthermore, line 538 is
    Code:JAVA
    1.  
    2. return new Location(world, getHandle().getBed().x, getHandle().getBed().y, getHandle().getBed().z);

    Which makes me think that either getHandle() or getBed() is null, which is also weird since it's surrounded by this:
    Code:JAVA
    1.  
    2.  
    3. public Location getBedSpawnLocation() {
    4. World world = getServer().getWorld(getHandle().spawnWorld);
    5. if ((world != null) && (getHandle().getBed() != null)) {
    6. return new Location(world, getHandle().getBed().x, getHandle().getBed().y, getHandle().getBed().z);
    7. } else {
    8. return null;
    9. }
    10. }
     
  5. Offline

    Slamakans

    I couldn't figure out why the call was sending an error message, but I got around it by creating a hashmap where I put players in when they left bed, and then checking to see if player was in the hashmap.
    Code:java
    1. public class UCPlayerListener extends PlayerListener{
    2. public static UCMain plugin;
    3.  
    4. public UCPlayerListener(UCMain instance){
    5.  
    6. plugin = instance;
    7.  
    8. }
    9.  
    10. public final HashMap<Player, Player> hasBedSpawn = new HashMap<Player, Player>();
    11.  
    12. public void onPlayerInteract(PlayerInteractEvent event){
    13.  
    14. Player player = event.getPlayer();
    15.  
    16. World world = player.getLocation().getWorld();
    17.  
    18. boolean rightClick = event.getAction().equals(Action.RIGHT_CLICK_BLOCK);
    19. boolean leftClick = event.getAction().equals(Action.LEFT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_AIR);
    20.  
    21. int itemId = player.getItemInHand().getTypeId();
    22.  
    23. if(itemId == 345 && rightClick){
    24.  
    25.  
    26.  
    27. Location clickedBlockLocation = event.getClickedBlock().getLocation();
    28.  
    29. plugin.resetLL(player);
    30.  
    31. player.setCompassTarget(clickedBlockLocation);
    32. UCMain.greenMessage(player, "Changed your compass target to target block.");
    33.  
    34. }else if(itemId == 345 && leftClick){
    35.  
    36. plugin.resetLL(player);
    37.  
    38. if(hasBedSpawn.containsKey(player)){
    39.  
    40. player.setCompassTarget(player.getBedSpawnLocation());
    41.  
    42. }else player.setCompassTarget(world.getSpawnLocation());
    43.  
    44. UCMain.greenMessage(player, "Changed your compass target to your spawn.");
    45.  
    46. }
    47.  
    48. }
    49.  
    50. public void onPlayerBedLeave(PlayerBedLeaveEvent event){
    51.  
    52. Player player = event.getPlayer();
    53.  
    54. hasBedSpawn.remove(player);
    55. hasBedSpawn.put(player, player);
    56.  
    57. }
    58.  
    59. }


    Indeed, I figured that there was something odd with the thingumajig in the bed method, but I forgot to mention it, sorry ^^

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

Share This Page