Keeping Inventorys Open after it is closed

Discussion in 'Plugin Development' started by MrPeterss, Sep 30, 2020.

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

    MrPeterss

    Hey guys,

    Usually, when I have an issue I go to these forums. But I couldn't find one for this.

    I'm trying to keep an inventory open (so when the player tries to close it opens again). But, I read from the JavaDoc that you can't open an inventory inside an InventoryCloseEvent, so what people suggested is to schedule a task for like a couple of ticks later... I tried that but it still gives me an error pointing to the line in which the task is scheduled (look below for full console error and code).

    If you can tell me what I'm doing wrong I would appreciate it because I can't find this anywhere.

    Thanks,
    MrPeterss

    Code:
    public void closeInventory(InventoryCloseEvent e) {
                Inventory i = e.getInventory();
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
      
                        @Override
                        public void run() {
                           Player p=(Player) e.getPlayer();
                           p.openInventory(i);
      
                        }
                    }, 20);
                }
    Code:
    [12:24:27] [Server thread/ERROR]: Could not pass event InventoryCloseEvent to AmongUs v1.0
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:319) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at org.bukkit.craftbukkit.v1_16_R2.event.CraftEventFactory.handleInventoryCloseEvent(CraftEventFactory.java:1298) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.PlayerConnection.a(PlayerConnection.java:1965) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:18) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:1) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.TickTask.run(SourceFile:18) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.MinecraftServer.ba(MinecraftServer.java:941) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.MinecraftServer.executeNext(MinecraftServer.java:934) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeAll(SourceFile:103) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.MinecraftServer.sleepForTick(MinecraftServer.java:917) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:850) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
    Caused by: java.lang.ClassCastException: me.mrpetersss.Main4$reg cannot be cast to org.bukkit.plugin.Plugin
            at me.mrpetersss.Main4$reg.closeInventory(Main4.java:348) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:315) ~[spigot-1.16.2.jar:git-Spigot-379750e-43c7ff9]
            ... 19 more
     
  2. Online

    timtower Moderator Moderator

    @MrPeterss You got a subclass or something? Because what you are using as "this" is not the main class.
     
  3. Offline

    MrPeterss

    Oh yeah I’m kinda dumb. I’ll reroute that to the main class

    thanks for the help,
    MrPeterss
     
Thread Status:
Not open for further replies.

Share This Page