Solved Cast Inventory to Chest

Discussion in 'Plugin Development' started by baenii, Oct 29, 2019.

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

    baenii

    Hej everyone,

    Im currently working on chests which will refill themselve when they are empty.
    For that I need to check if the chest is clear and if so, refill it. As Im working with a public static classes for setting the items into the chest, I need to cast the Inventory to a Chest but java gives me an error.
    Any idea how to solve my problem?

    Code:
    //Refill
        @EventHandler
        public void onCloseChest(InventoryCloseEvent e) {
         
            if(e.getPlayer() instanceof Player) {
             
                Player p = (Player) e.getPlayer();
             
                if(e.getInventory().getHolder() instanceof Chest) {
                 
                    Chest chest = (Chest) e.getInventory();
                 
                    if(onEmptyChest(e.getInventory())) {
    
                        InventoryChest.setItems(chest);
                     
                    }
                 
                }
             
            }
         
        }
    
    Error (open)

    [03:24:36 ERROR]: Could not pass event InventoryCloseEvent to System v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.handleInventoryCloseEvent(CraftEventFactory.java:838) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1368) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callInventoryOpenEvent(CraftEventFactory.java:705) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.EntityPlayer.openContainer(EntityPlayer.java:654) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.BlockChest.interact(SourceFile:339) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerInteractManager.interact(PlayerInteractManager.java:496) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:724) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:50) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:80) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.inventory.CraftInventory cannot be cast to org.bukkit.block.Chest
    at de.baeni.skypvpsystem.listeners.ManagerChest.onCloseChest(ManagerChest.java:60) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_231]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    ... 20 more
    [03:24:36 ERROR]: Could not pass event InventoryCloseEvent to System v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:305) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.handleInventoryCloseEvent(CraftEventFactory.java:838) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:1368) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:21) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketPlayInCloseWindow.a(PacketPlayInCloseWindow.java:12) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_231]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_231]
    at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R1.inventory.CraftInventory cannot be cast to org.bukkit.block.Chest
    at de.baeni.skypvpsystem.listeners.ManagerChest.onCloseChest(ManagerChest.java:60) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_231]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_231]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_231]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301) ~[spigot-1.8.jar:git-Spigot-c3c767f-33d5de3]
    ... 15 more
     
  2. Offline

    KarimAKL

    @baenii Get the inventory's holder using Inventory#getHolder(), and then check if it's an instanceof Chest, if so, cast the holder to Chest.
     
    baenii likes this.
  3. Offline

    baenii

    @KarimAKL That's what I did here, isn't it? :p


    //EDIT: One sec.. I've just casted the Inventory.

    //EDIT EDIT: Casting the Inventory Holder instead of the Inventory actually solved my problem. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page