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()) {
  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())) {
  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:

    if(e.getInventory() != null && e.getRawSlot() >= e.getInventory().getSize()) {
  13. Still didn't work :/
  14. What line does your stacktrace say the NPE is on?
  15. PHP:
    if (!e.getInventory().equals(p.getInventory())) {
    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
        at$1.execute( ~[spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.PlayerConnection.a( [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$ [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at java.util.concurrent.Executors$ Source) [?:1.8.0_45]
        at 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( [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.DedicatedServer.A( [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at net.minecraft.server.v1_8_R2.MinecraftServer.z( [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at [spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        at Source) [?:1.8.0_45]
    Caused by: java.lang.NullPointerException
        at com.norrd.KickPlugin.InventoryListener.onInventoryClick( ~[?:?]
        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$1.execute( ~[spigot-1.8.3.jar:git-Spigot-26dfd01-ad2f806]
        ... 15 more
  17. Just do this.

    if (e.getInventory() != null && e.getInventor().getType() == InventoryType.PLAYER){
    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!