Inventory Click Event [Answered]

Discussion in 'Spigot Plugin Development' started by Norrd, Jun 5, 2015.

  1. How would I stop a player from clicking in their own inventory and moving items in it while they have another inventory open?
     
  2. you can cancel InventoryClickEvent if event.getRawSlot() >= event.getInventory().getSize()
     
    • Like Like x 1
  3. I like your avatar xD its so creepily cute
     
    • Funny Funny x 2
  4. That comes up with a null point exception!
    Code (Text):
            }else if(e.getRawSlot() >= e.getInventory().getSize()) {
                e.setCancelled(true);
            }
     
  5. I dont know if this will do anything but switch the >= to <=
     
  6. I did :/
     
  7. do a null check on e.getInventory()
     
  8. If your intention is to cancel clicks in the player's inventory then it needs to be >=

    e.getRawSlot() returns the slot number as shown on the screen, e.g. top left of the chest would be 0, last slot in a double chest would be 53, first slot in players inventory is 54, etc.

    e.getInventory().getSize() returns the size of the main inventory that is open. e.g. for a double chest that will return 54.

    So, if rawSlot is larger or equal to the the inventory size then the click was in the players inventory
     
  9. =O So i was right! XD.
     
  10. You said <=
     
  11. When you say
    do you mean like
    Code (Text):
            }else if(!(e.getRawSlot() >= e.getInventory().getSize())) {
                e.setCancelled(true);
            }
     
  12. no, you wont want the ! to be there unless you want to cancel clicks in the chest instead

    i meant, if you are getting a NPE that specifies that line then add a check to make sure e.getInventory() isnt null:

    PHP:
    if(e.getInventory() != null && e.getRawSlot() >= e.getInventory().getSize()) {
        e.setCancelled(true);
    }
     
  13. Still didn't work :/
     
  14. What line does your stacktrace say the NPE is on?
     
  15. PHP:
    if (!e.getInventory().equals(p.getInventory())) {
        e.setCancelled(true);
    }
    Just typing this without a Java IDE, the code might not work :p
     
  16. This is the error message
    Code (Text):
    [08:18:55] [Server thread/ERROR]: Could not pass event InventoryClickEvent to KickGUI v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:1605) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.PacketPlayInWindowClick.a(SourceFile:31) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.PacketPlayInWindowClick.a(SourceFile:9) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_45]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
        at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:710) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at com.norrd.KickPlugin.InventoryListener.onInventoryClick(InventoryListener.java:26) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor74.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        ... 15 more
     
  17. Just do this.

    PHP:
    if (e.getInventory() != null && e.getInventor().getType() == InventoryType.PLAYER){
    e.setCancelled(true);
    }
    You will get a npe if you click outside the inventory, use this before the code explained above:

    if (e.getCurrentItem != null){
    //code above
    }

    Your welcome m8.
     
    • Agree Agree x 1
  18. Thank you everyone for all the help there was a few errors with my code and I have fixed them now and learnt from my mistakes! Thank you for helping and I hope you have a good day!