Plugin works but I keep getting a error

Discussion in 'Plugin Development' started by JD_Guy17, Jan 25, 2014.

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

    JD_Guy17

    This code works but when I try to do any other interactions it comes up with a error in console. The first line says ' Could not pass PlayerInteractEvent to GunsV1.0

    Here is my code:
    Code:java
    1. package me.jdguy17.warplugin;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Arrow;
    5. import org.bukkit.entity.Fireball;
    6. import org.bukkit.entity.Snowball;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. import me.jdguy17.warplugin.main;
    14.  
    15. public class guns implements Listener {
    16. public guns(main plugin){
    17. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    18. }
    19.  
    20.  
    21. @EventHandler
    22. public void onPlayerInteract(final PlayerInteractEvent e) {
    23. if ((e.getAction() == Action.RIGHT_CLICK_AIR)) {
    24. if (e.getPlayer().getInventory().contains(Material.SNOW_BALL)) {
    25. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Machine Gun")) {
    26. e.getPlayer().launchProjectile(Snowball.class);
    27. ItemStack ammo = new ItemStack(Material.SNOW_BALL, 1);
    28. e.getPlayer().getInventory().removeItem(ammo);
    29. e.getPlayer().updateInventory();
    30. }
    31. }
    32. }
    33.  
    34. if (e.getPlayer().getInventory().contains(Material.BUCKET)) {
    35. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Pistol")) {
    36. e.getPlayer().launchProjectile(Arrow.class);
    37. ItemStack ammo1 = new ItemStack(Material.BUCKET, 1);
    38. e.getPlayer().getInventory().removeItem(ammo1);
    39. e.getPlayer().updateInventory();
    40. }
    41. }
    42.  
    43.  
    44. if (e.getPlayer().getInventory().contains(Material.SHEARS)) {
    45. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Rocket Launcher")) {
    46. e.getPlayer().launchProjectile(Fireball.class);
    47. ItemStack ammo2 = new ItemStack(Material.SHEARS, 1);
    48. e.getPlayer().getInventory().removeItem(ammo2);
    49. e.getPlayer().updateInventory();
    50. }
    51. } else {
    52. return;
    53. }
    54. }
    55. }
    56.  
    57.  
    58.  
     
  2. Offline

    teunie75

    More details?
     
  3. Offline

    JD_Guy17

    teunie75 I don't know what you need to know. Could you give me more details about what you need?
     
  4. Offline

    teunie75

    The error itself
     
  5. Offline

    JD_Guy17

  6. Offline

    RawCode

    image wont work, copypaste text from server's log.
     
  7. Offline

    teunie75

    There is a NullPointer on line 35 of guns.java
     
  8. Offline

    JD_Guy17

  9. Offline

    teunie75

    Check if variables are null before executing the code.
     
  10. Offline

    JD_Guy17

    teunie75 I can't find any issue's. Here's the code, could you take a look please:
    Code:java
    1. package me.jdguy17.warplugin;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.entity.Arrow;
    5. import org.bukkit.entity.Fireball;
    6. import org.bukkit.entity.Snowball;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. import me.jdguy17.warplugin.main;
    14.  
    15. public class guns implements Listener {
    16. public guns(main plugin){
    17. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    18. }
    19.  
    20.  
    21. @EventHandler
    22. public void onPlayerInteract(final PlayerInteractEvent e) {
    23. if ((e.getAction() == Action.RIGHT_CLICK_AIR)) {
    24. if (e.getPlayer().getInventory().contains(Material.SNOW_BALL)) {
    25. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Machine Gun")) {
    26. e.getPlayer().launchProjectile(Snowball.class);
    27. ItemStack ammo = new ItemStack(Material.SNOW_BALL, 1);
    28. e.getPlayer().getInventory().removeItem(ammo);
    29. e.getPlayer().updateInventory();
    30. }
    31. }
    32.  
    33.  
    34. if (e.getPlayer().getInventory().contains(Material.BUCKET)) {
    35. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Pistol")) {
    36. e.getPlayer().launchProjectile(Arrow.class);
    37. ItemStack ammo1 = new ItemStack(Material.BUCKET, 1);
    38. e.getPlayer().getInventory().removeItem(ammo1);
    39. e.getPlayer().updateInventory();
    40. }
    41. }
    42.  
    43.  
    44. if (e.getPlayer().getInventory().contains(Material.SHEARS)) {
    45. if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Rocket Launcher")) {
    46. e.getPlayer().launchProjectile(Fireball.class);
    47. ItemStack ammo2 = new ItemStack(Material.SHEARS, 1);
    48. e.getPlayer().getInventory().removeItem(ammo2);
    49. e.getPlayer().updateInventory();
    50. }
    51. } else {
    52. return;
    53. }
    54. }
    55. }
    56. }
    57.  
    58.  
    59.  
     
  11. Offline

    teunie75

    Try checking if the item in the hand of player has ItemMeta before getting ItemMeta stuff like displayname
    Code:
    if (e.getPlayer().getInventory().contains(Material.BUCKET)) {
    if (e.getPlayer().getInventory().getItemInHand().hasItemMeta()){ //Checking for ItemMeta
                    if (e.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals("Pistol")) { //Has ItemMeta, now you can do something with that
                            e.getPlayer().launchProjectile(Arrow.class);
                              ItemStack ammo1 = new ItemStack(Material.BUCKET, 1);
                              e.getPlayer().getInventory().removeItem(ammo1);
                              e.getPlayer().updateInventory();
                        }
                    }
    }
    
     
  12. Offline

    JD_Guy17

    teunie75 Still getting the same error
     
  13. Offline

    FunnyGopher

    Try checking if the player has something in their hand first. If the player has nothing in their hand, then getItemInHand() would return null.
    Code:
    if (e.getPlayer().getInventory().getItemInHand() != null) {
        //Check for ItemMeta...
        //etc...
    }
     
  14. Offline

    JD_Guy17

    FunnyGopher It happens even if they have a item in their hand
     
  15. Offline

    FunnyGopher

    Well, unless you're actually seeing a different error that is not on line 35, I can't help you. If you have the necessary checks in there, then nothing on that line should be null during runtime.
     
  16. Offline

    xTrollxDudex

    JD_Guy17
    PHP:
    if(!e.getPlayer().getItemInHand().hasItemMeta()) {
        return;
    }
     
Thread Status:
Not open for further replies.

Share This Page