Solved Wrong cast ?

Discussion in 'Plugin Development' started by Sw_aG, Feb 8, 2020.

  1. Offline

    Sw_aG

    Hi guys, I'm trying to interact with chests and I keep getting weird errors stating it can't cast a craftblock to a chest.

    The code: (the event is player interact event)
    PHP:
    if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && e.getClickedBlock().getType().equals(Material.CHEST)) {
                    
      
    Chest chest = (Cheste.getClickedBlock(); // <--- THE ERROR
                    
    if(!lootedContainers.contains(chest.getInventory())) {
                        
    spawnParticles(chest);   
          }
    return;
    }
    The error:
    HTML:
    [ERROR] Could not pass event PlayerInteractEvent to IdoGeNSkywars v1
    
    org.bukkit.event.EventException
    
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:211) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:462) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:728) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_212]
    
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_212]
    
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    at java.lang.Thread.run(Thread.java:813) [?:1.8.0_212]
    
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.block.CraftBlock incompatible with org.bukkit.craftbukkit.v1_8_R3.block.CraftChest
    
    at SkywarsPackage.main.playerInteractEvent(main.java:1433) ~[?:?]
    
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_212]
    
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_212]
    
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
    
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
    
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[craftbukkit-1.8.8.jar:git-Bukkit-e1ebe52]
    
    ... 17 more
    Thanks for reading
     
    Last edited: Feb 8, 2020
  2. Offline

    bowlerguy66

    @Sw_aG When casting a block to a chest, you have to use the BlockState value of the block, like this:
    Code:
    Chest chest = (Chest) block.getState();
     
  3. Offline

    Sw_aG

    Forgot it !
    Thanks bro
     

Share This Page