Can not pass InventoryClickEvent

Discussion in 'Plugin Development' started by Tofo, Jan 7, 2024.

  1. Offline

    Tofo

    hi I try to solve this error in my console.

    Error:
    Code:
    [14:22:59] [Server thread/ERROR]: Could not pass event InventoryClickEvent to KenshinsHideAndSeek v1.7.6
    java.lang.NullPointerException: Cannot invoke "org.bukkit.inventory.meta.ItemMeta.getDisplayName()" because "meta" is null
        at dev.tylerm.khs.game.listener.InventoryHandler.checkForSpectatorTeleportMenu(InventoryHandler.java:62) ~[KenshinsHideAndSeek-1.7.6.jar:?]
        at dev.tylerm.khs.game.listener.InventoryHandler.onInventoryClick(InventoryHandler.java:45) ~[KenshinsHideAndSeek-1.7.6.jar:?]
        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor32.execute(Unknown Source) ~[?:?]
        at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:77) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
        at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:81) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:git-Paper-365]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
        at io.papermc.paper.plugin.manager.PaperEventManager.callEvent(PaperEventManager.java:54) ~[paper-1.20.4.jar:git-Paper-365]
        at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.callEvent(PaperPluginManagerImpl.java:126) ~[paper-1.20.4.jar:git-Paper-365]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:615) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
        at net.minecraft.server.network.ServerGamePacketListenerImpl.handleContainerClick(ServerGamePacketListenerImpl.java:3117) ~[?:?]
        at net.minecraft.network.protocol.game.ServerboundContainerClickPacket.handle(ServerboundContainerClickPacket.java:58) ~[?:?]
        at net.minecraft.network.protocol.game.ServerboundContainerClickPacket.handle(ServerboundContainerClickPacket.java:23) ~[?:?]
        at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:54) ~[?:?]
        at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.20.4.jar:git-Paper-365]
        at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:153) ~[?:?]
        at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24) ~[?:?]
        at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1458) ~[paper-1.20.4.jar:git-Paper-365]
        at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:194) ~[paper-1.20.4.jar:git-Paper-365]
        at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:126) ~[?:?]
        at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1435) ~[paper-1.20.4.jar:git-Paper-365]
        at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1358) ~[paper-1.20.4.jar:git-Paper-365]
        at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:136) ~[?:?]
        at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1509) ~[paper-1.20.4.jar:git-Paper-365]
        at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1214) ~[paper-1.20.4.jar:git-Paper-365]
        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:321) ~[paper-1.20.4.jar:git-Paper-365]
        at java.lang.Thread.run(Thread.java:840) ~[?:?]
    Sourcecode:
    Code:
    public class InventoryHandler implements Listener {
    
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onInventoryClick(InventoryClickEvent event) {
            if (event.getCurrentItem() == null) return;
            if (!(event.getWhoClicked() instanceof Player)) return;
            checkForInventoryMove(event);
            checkForSpectatorTeleportMenu(event);
            checkForDebugMenu(event);
            checkForBlockHuntMenu(event);
        }
    
        private void checkForInventoryMove(InventoryClickEvent event){
            if (Main.getInstance().getBoard().contains((Player) event.getWhoClicked()) && Main.getInstance().getGame().getStatus() == Status.STANDBY) {
                event.setCancelled(true);
            }
        }
    
        private void checkForSpectatorTeleportMenu(InventoryClickEvent event){
            Player player = (Player) event.getWhoClicked();
               
            ItemStack item = event.getCurrentItem();
    
            ItemMeta meta = item.getItemMeta();
            String name = meta.getDisplayName();
    
            if (Main.getInstance().getBoard().isSpectator(player)) {
                if (XMaterial.PLAYER_HEAD.isSimilar(item)) {
                    event.setCancelled(true);
                    player.closeInventory();
                    Player clicked = Main.getInstance().getServer().getPlayer(name);
                    if (clicked == null) return;
                    player.teleport(clicked);
                } else if (XMaterial.ENCHANTED_BOOK.isSimilar(item)) {
                    event.setCancelled(true);
                    player.closeInventory();
                    if (!name.startsWith("Page ")) return;
                    String number_str = name.substring(5);
                    try {
                        int page = Integer.parseInt(number_str);
                        InteractHandler.createSpectatorTeleportPage(player, page - 1);
                    } catch(Exception ignored) {
    
                    }
                }
            }
        }
    
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Tofo Did you read the error?
    There is no item meta
     
  3. Offline

    Tofo

    i dont know what that is can you show me how to fix it i dont understand much about coding i just want to fix this plugin because its no longer maintained
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Tofo
    ItemMeta meta = item.getItemMeta();
    meta = null
    Do a null check on meta.
    Then: hasDisplayName on meta
     
  5. Offline

    Tofo

    how exact
     
  6. Offline

    timtower Administrator Administrator Moderator

Share This Page