Error with PlayerInteract event

Discussion in 'Plugin Development' started by rufdawgdacoder, Jun 16, 2014.

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

    rufdawgdacoder

    I have this error when i dont use my special sword:
    Please help me, here is my code:
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Block block = player.getTargetBlock(null, 100);
            Location bl = block.getLocation();
            World world = player.getWorld();
     
            if (player.getItemInHand().getType() == Material.DIAMOND_SWORD
                    && player.getItemInHand().getItemMeta().getDisplayName()
                            .contains("Special Sword")
                    && (event.getAction() == Action.RIGHT_CLICK_AIR)) {
                world.strikeLightning(bl);
     
  2. Offline

    JaguarJo

    Moved to Plugin Development.
     
  3. Offline

    Gater12

    rufdawgdacoder
    Could be they have nothing in their hand. So you have to handle that.
     
  4. Gater12 I think he also have to check if the item have display name (?)
     
  5. Offline

    rufdawgdacoder

    Someone_Like_You Gater12 I only get the error when i right click with a diamond sword, I need it so i could add this to my server and still have peoke be able to block, but people with a special sword strike lightning where they're looking when they right click/

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Block block = player.getTargetBlock(null, 100);
            Location bl = block.getLocation();
            World world = player.getWorld();
            Material sword = Material.DIAMOND_SWORD;
     
            if (player.getItemInHand().getType() != sword) {
                return;
            } else if (player.getItemInHand().getType() == sword
                    && (player.getItemInHand().getItemMeta().getDisplayName()
                            .contains("Special Sword") && (event.getAction() == Action.RIGHT_CLICK_AIR))) {
                world.strikeLightning(bl);
    so I've updated to this code, but it does not work, please help!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  6. Offline

    nateracecar5

    Add a parenthesis after .contains("Special Sword") and move one after Action.RIGHT_CLICK_AIR.

    That's all I see, but there might be something else.
     
  7. Offline

    rufdawgdacoder


    This doesn't seem to work, but thanks for the try
     
  8. rufdawgdacoder didnt read much, check if the sword itemmeta.hasDisplayName() and if player.getItemInHand() != null
     
  9. Offline

    AoH_Ruthless

    Gater12 nateracecar5 rufdawgdacoder

    You don't have to check if the item in hand is null first. Like Someone_Like_You said, the only thing that could be null is the display name. ItemMeta also can't be null, so first you have to pass a null check regarding the display name.

    Also, with your updated code, you return if a player doesn't have a diamond sword. Why in your elseif statement are you checking if they have a diamond sword there, then? That's bad practice :)
     
    Someone_Like_You likes this.
  10. AoH_Ruthless really? you dont have to null check item in hand?
     
  11. Offline

    AoH_Ruthless

    Someone_Like_You likes this.
  12. Offline

    rufdawgdacoder

    AoH_Ruthless Someone_Like_You I've ran into this problem where it doesn't throw a NullPpinterException but it doesn't strike lightning?
     
  13. Offline

    AoH_Ruthless

    rufdawgdacoder
    If the block is null (material is air), there is no location to strike lightning on. Therefore, you could get an NPE when trying to get a location of a null block.
     
  14. Offline

    fireblast709

    AoH_Ruthless I think I already showed you that ItemMeta could be null, didn't I? Also if the block is null there would be a NPE while getting the location.

    rufdawgdacoder debug your code to see if it even runs the world.strikeLightning(local)
     
  15. Offline

    AoH_Ruthless

    fireblast709
    Did you really? If you did, I'm sorry because I must have forgotten. I tacitly implied that you could get an NPE while getting the location, but you are right, I should have explicitly stated it.
     
  16. Offline

    Garris0n

    The only time getItemMeta() can ever be null is if the material is air. Also, hasItemMeta() has nothing to do with whether it will return null.
    https://github.com/Bukkit/CraftBukk...raftbukkit/inventory/CraftItemStack.java#L321
    http://jd.bukkit.org/dev/doxygen/de...Entity.html#a4c23d9eeb686e0ea0a5969a3cc289a3e
     
  17. Offline

    rufdawgdacoder

  18. Offline

    fireblast709

    rufdawgdacoder sending messages to players, logging messages to the console
     
  19. Offline

    rufdawgdacoder

    fireblast709 I've fixed you may leave now, thx for the help :D
     
Thread Status:
Not open for further replies.

Share This Page