Solved Error with custom item collision

Discussion in 'Plugin Development' started by RUDD33, Oct 20, 2014.

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

    RUDD33

    I'm having an issue with collisions, such as LEFT_CLICK_BLOCK, involving a custom item. Here is the code:

    Code:
        @EventHandler
        public void onPlayerInteract (PlayerInteractEvent event) {
            Player player = event.getPlayer();
           
            if (event.getAction() == Action.LEFT_CLICK_BLOCK){
                if (player.getItemInHand().getItemMeta().getDisplayName() == "Test") {
                            player.sendMessage("hi");
                        }
                        }
                }
            
    The code seems to work fine. When left-clicking blocks with the custom item, the player is sent the test message, but an error pops up in the command console as follows:

    Code:
    [23:25:40 ERROR]: Could not pass event PlayerInteractEvent to BeatsMaker v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at org.bukkit.craftbukkit.v1_7_R3.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:226) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-
    g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.PlayerInteractManager.dig(PlayerInteract
    Manager.java:103) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :536) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInBlockDig.a(SourceFile:53) [c
    raftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInBlockDig.handle(SourceFile:8
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
    Caused by: java.lang.NullPointerException
            at me.RUDD33.com.BeatsMaker.onPlayerInteract(BeatsMaker.java:31) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _67]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _67]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_67]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_67]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-11-g3fd9db2-b3098jnks]
            ... 15 more
    The error only pops up when left-clicking objects with no item in hand. It works fine if the object in hand is the custom object or any other object. Also, if the custom item code removed and only the action event click and the send message command are remaining, there is no error. I'd like to be able to get rid of this error message. Help with this would be very appreciated. Thank you!
     
  2. Offline

    TheCwispyOne

    RUDD33 You need to check if p.getItemInHand() != null
     
  3. Offline

    _Filip

    Why aren't you checking if the string contents are the same?
     
  4. Offline

    fireblast709

    RUDD33 check if the held ItemStack isn't null (like mentioned before), check if the ItemMeta isn't null and check if the display name isn't null (last two have built-in methods).

    Also, compare Strings with equals[IgnoreCase]() instead of ==. Strings are not primitives, and a new String reference is created for each String you use.
     
    RUDD33 likes this.
  5. Offline

    RUDD33

  6. Offline

    _Filip

    fireblast709 '==' doesn't compare references. It compares the values.
     
  7. Offline

    fireblast709

    _Filip it compares the memory address of objects iirc, not values of objects.
     
  8. Offline

    _Filip

    fireblast709 You seem ro be confusing reference with objects.
     
  9. Offline

    fireblast709

    _Filip please explain to me how I am confusing them (include your initial post - how == compares values - if possible)
     
  10. Offline

    Rocoty

    _Filip If they compared values it would need to dereference the references to get the value first, in which case var == null would be a no-no. == checks if two references are the point to the same memory address (which means it will return true if two references refer to the same object/value)
     
  11. Offline

    _Filip

    Rocoty I never claimed that the value of objects are compared using '==' and if I did, I apologise, as I have a hard time writing when on my phone. I said the value of the reference is compared (as said before) the memory address. I don't really know what to tell you guys about what I meant past that point. I believe I thoroughly explained what I meant.
     
  12. Offline

    fireblast709

    _Filip in the end we were talking about the same thing, misinterpreting the other.
     
  13. Offline

    _Filip

  14. Offline

    Rocoty

    _Filip Well, we seem to be agreeing then. This was just a misunderstanding
     
  15. Offline

    RUDD33

    @fireblast709
    At least I thought it was fixed...
    Recently, I figured out that any item that was interacted with (whether left or right click) logged the same error as the one in the initial post.

    Here is what I did with the code at first:
    Code:
      @EventHandler
        public void onPlayerInteract (PlayerInteractEvent event) {
            Player player = event.getPlayer();
       
            if (player.getItemInHand().getItemMeta() != null){
            if (player.getDisplayName() != null){
            if (player.getItemInHand() != null){
            if (event.getAction() == Action.LEFT_CLICK_BLOCK){
                if (player.getItemInHand().getItemMeta().getDisplayName() == "Test") {
                            player.sendMessage("hi");
                        }
                        }
                }

    That didn't work so I added this code above the ItemMeta null check:
    Code:
    if (!player.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("Test")) {
    return;}

    This fixed the initial error I was having, but when I looked again, any item that was not a "custom item" logged the same error. So if I left-clicked or right-clicked with grass, for example, the error popped up. I want to know what I did wrong with the new code which made it swap the custom item having the error to the any item that wasn't custom having an error. I did some tests and I think it comes from the piece of code above about checking if the player didn't have that item in hand, but I'm not sure how to prevent the custom item from logging the event without it. Thank you. :(
     
  16. Offline

    fireblast709

    RUDD33 you mixed up the order xD. First check if the ItemStack the player is holding isn't null (getItemInHand()), then check if the ItemMeta isn't null, then check if the displayname of the ItemMeta isn't null (you had player.getDisplayName(), this has to be player.getItemInHand().getItemMeta().getDisplayName())

    Even better, by the way, is checking hasItemMeta() and hasDisplayName() (they save you a != null)
     
    RUDD33 likes this.
  17. Offline

    RUDD33

    fireblast709 Thank you. Thank you! That did the trick and thanks for the tip to save some space. :D
     
Thread Status:
Not open for further replies.

Share This Page