InventoryClickevent error

Discussion in 'Plugin Development' started by vondalenocon4x, Apr 2, 2021.

  1. Offline

    vondalenocon4x

    How can i fix this
    Code:
    [13:42:34 ERROR]: Could not pass event InventoryClickEvent to UndroppableItem v0.3
    java.lang.NullPointerException: null
            at en.dev.undroppableitem.listener.PlayerInteractEntityListener.onInventoryClick(PlayerInteractEntityListener.java:20) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor4.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.16.5.jar:git-Paper-575]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.16.5.jar:git-Paper-575]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.16.5.jar:git-Paper-575]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:607) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:2687) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:32) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$ensureMainThread$1(PlayerConnectionUtils.java:35) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:1252) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:1245) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:1206) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1120) ~[patched_1.16.5.jar:git-Paper-575]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:289) ~[patched_1.16.5.jar:git-Paper-575]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_281]
    >

    Code:
    import en.dev.undroppableitem.Main;
    import org.bukkit.inventory.*;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryDragEvent;
    import org.bukkit.inventory.Inventory;
    
    public class PlayerInteractEntityListener implements Listener {
      @EventHandler
      public void onInventoryClick(InventoryClickEvent event) {
          if (event.getClick().isShiftClick()) {
              Inventory clicked = event.getClickedInventory();
              if (clicked == event.getWhoClicked().getInventory()) {
                  // The item is being shift clicked from the bottom to the top
                  ItemStack clickedOn = event.getCurrentItem();
                  if (clickedOn != null && event.getWhoClicked().getInventory().getItemInMainHand().getItemMeta().hasEnchant((Enchantment)Main.undroppable)) {
                      event.setCancelled(true);
                  }
              }
          }
      }
      public void onInventoryClick2(InventoryClickEvent event) {
          Inventory clicked = event.getClickedInventory();
          if (clicked != event.getWhoClicked().getInventory()) { // Note: !=
              // The cursor item is going into the top inventory
              org.bukkit.inventory.ItemStack onCursor = event.getCursor();
    
              if (onCursor != null && event.getWhoClicked().getInventory().getItemInMainHand().getItemMeta().hasEnchant((Enchantment)Main.undroppable)) {
                  event.setCancelled(true);
              }
          }
      }
      @EventHandler
      public void onInventoryDrag(InventoryDragEvent event) {
          @SuppressWarnings("unused")
        org.bukkit.inventory.ItemStack dragged = event.getOldCursor(); // This is the item that is being dragged
    
          if (event.getWhoClicked().getInventory().getItemInMainHand().getItemMeta().hasEnchant((Enchantment)Main.undroppable)) {
              int inventorySize = event.getInventory().getSize(); // The size of the inventory, for reference
    
              // Now we go through all of the slots and check if the slot is inside our inventory (using the inventory size as reference)
              for (int i : event.getRawSlots()) {
                  if (i < inventorySize) {
                      event.setCancelled(true);
                      break;
                  }
              }
          }
      }
    }
    
     
    Last edited: Apr 2, 2021
  2. Offline

    timtower Administrator Administrator Moderator

    @vondalenocon4x
    event.getWhoClicked().getInventory().getItemInMainHand().getItemMeta()
    Might not be an item, might not be meta
     
  3. Offline

    vondalenocon4x

    its working now but i have one and only problem right now my InventoryDragEvent is not working

    Code:
          public void onInventoryDrag(InventoryDragEvent event) {
              @SuppressWarnings("unused")
            org.bukkit.inventory.ItemStack dragged = event.getOldCursor(); // This is the item that is being dragged
    
              if (dragged.getEnchantments() != null && event.getWhoClicked().getInventory().getItemInMainHand().getItemMeta().hasEnchant((Enchantment)Main.undroppable)) {
                  int inventorySize = event.getInventory().getSize(); // The size of the inventory, for reference
    
                  // Now we go through all of the slots and check if the slot is inside our inventory (using the inventory size as reference)
                  for (int i : event.getRawSlots()) {
                      if (i < inventorySize) {
                          event.setCancelled(true);
                          break;
    }
                  }
              }
          }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 3, 2021
  4. Offline

    KarimAKL

  5. Offline

    vondalenocon4x

  6. Offline

    KarimAKL

    @vondalenocon4x You can start by telling us what you want to do and what is not working. We can begin helping you from there.
     
  7. Offline

    vondalenocon4x


    the error was fixed but the problem is when i tried to drag the item with custom enchant it's not working.

    i think there is a problem in my code here

    Code:
          public void onInventoryDrag(InventoryDragEvent event) {
              @SuppressWarnings("unused")
            org.bukkit.inventory.ItemStack dragged = event.getOldCursor(); // This is the item that is being dragged
    
              if (dragged.getEnchantments() != null && event.getWhoClicked().getInventory().getItemInMainHand().getItemMeta().hasEnchant((Enchantment)Main.undroppable)) {
                  int inventorySize = event.getInventory().getSize(); // The size of the inventory, for reference
    
                  // Now we go through all of the slots and check if the slot is inside our inventory (using the inventory size as reference)
                  for (int i : event.getRawSlots()) {
                      if (i < inventorySize) {
                          event.setCancelled(true);
                          break;
    }
                  }
              }
          }
    the thing is i want the item with custom enchant prevent to put in chest and to other containers too.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 3, 2021
  8. Offline

    blobic123

    @vondalenocon4x
    You need to provide more information that just that, you should include what your code is supposed to do, and probably also include what it's doing instead (add a breakpoint and step through the function).

    I assume you're trying to make a custom enchantment so that an item can't be removed from your inventory. If that's the case then I think this line is wrong:

    Code:
    dragged.getEnchantments() != null && event.getWhoClicked().getInventory().getItemInMainHand().getItemMeta().hasEnchant((Enchantment)Main.undroppable)
    getOldCursor is the item before it was being dragged, but getItemInMainHand is the item in the players hand (the equipped item in the hotbar), not the item being dragged.
     
    KarimAKL likes this.
  9. Offline

    vondalenocon4x


    yes your right im trying to make custom enchant to prevent moving it to players inventory. What should i change in this line

    Code:
    dragged.getEnchantments() != null && event.getWhoClicked().getInventory().getItemInMainHand().getItemMeta().hasEnchant((Enchantment)Main.undroppable)
    
     

Share This Page