Cannot cast block to sign

Discussion in 'Spigot Plugin Development' started by MadMaxCookie, May 29, 2016.

  1. Hello I have this stack trace when I click the sign

    Code (Java):
    [15:42:07 ERROR]: Could not pass event PlayerInteractEvent to CustomRecipe v1.6
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.craftbukkit.v1_9_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:231) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c
    01]
            at net.minecraft.server.v1_9_R1.PlayerInteractManager.a(PlayerInteractMa
    nager.java:482) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java
    :888) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInUseItem.a(SourceFile:55) [sp
    igot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInUseItem.a(SourceFile:11) [sp
    igot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9
    .2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:7
    21) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:4
    00) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:6
    60) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java
    :559) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R1.block.Cr
    aftBlock cannot be cast to org.bukkit.block.Sign
            at org.madmaxcookie.SignUpdate.onClick(SignUpdate.java:32) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _91]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _91]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_91]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            ... 17 more

    and I have this code :

    Code (Java):
         @EventHandler
        public void onClick(PlayerInteractEvent e) {
            if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK))
                return;
            if(e.getClickedBlock().getState() instanceof Sign) {
                Sign s = (Sign) e.getClickedBlock().getState();
                if(s.getLine(0).equalsIgnoreCase(colorize("&8[&bCustomRecipe&8]")) && (s.getLine(1).equalsIgnoreCase("create"))) {
                    plugin.currentFile.put(e.getPlayer(), s.getLine(2));
                    e.getPlayer().openInventory(plugin.createGUI(RecipeType.SHAPED));
                    return;
                }
                if(s.getLine(0).equalsIgnoreCase(colorize("&8[&bCustomRecipe&8]")) && (s.getLine(1).equalsIgnoreCase("edit"))) {
                    plugin.currentFile.put(e.getPlayer(), s.getLine(2));
                    String name = plugin.currentFile.get(e.getPlayer());
                    ConfigAccessor ca = ConfigAccessor.getInstance();
                    if(ca.getConfig().getKeys(false) != null && (!ca.getConfig().contains(name))) {
                        e.getPlayer().sendMessage(colorize("&8[&bCustomRecipe&8] &e>> &7Recipe &6" + s.getLine(2) + " &7is not existed"));
                        return;
                    }
                    String type = ca.getConfig().getString(name + ".Type");
                    if(type.equals("Shaped")) {
                        e.getPlayer().openInventory(plugin.createGUI(RecipeType.EDIT_SHAPED));
                    }else if(type.equals("Shapeless")) {
                        e.getPlayer().openInventory(plugin.createGUI(RecipeType.EDIT_SHAPELESS));
                    }else if(type.equals("Furnace")) {
                        e.getPlayer().openInventory(plugin.createGUI(RecipeType.EDIT_FURNACE));
                    }
                    return;
                }
             
                if(s.getLine(0).equalsIgnoreCase(colorize("&8[&bCustomRecipe&8]")) && (s.getLine(1).equalsIgnoreCase("view"))) {
                    plugin.currentFile.put(e.getPlayer(), s.getLine(2));
                    String name = plugin.currentFile.get(e.getPlayer());
                    ConfigAccessor ca = ConfigAccessor.getInstance();
                    if(ca.getConfig().getKeys(false) != null && (!ca.getConfig().contains(name))) {
                        e.getPlayer().sendMessage(colorize("&8[&bCustomRecipe&8] &e>> &7Recipe &6" + s.getLine(2) + " &7is not existed"));
                        return;
                    }
                    String type = ca.getConfig().getString(name + ".Type");
                    if(type.equals("Shaped")) {
                        e.getPlayer().openInventory(plugin.createGUI(RecipeType.VIEW_SHAPED));
                    }else if(type.equals("Shapeless")) {
                        e.getPlayer().openInventory(plugin.createGUI(RecipeType.VIEW_SHAPELESS));
                    }else if(type.equals("Furnace")) {
                        e.getPlayer().openInventory(plugin.createGUI(RecipeType.VIEW_FURNACE));
                    }
                    return;
                }
                return;
            }
            return;
        }
     
    #1 MadMaxCookie, May 29, 2016
    Last edited: May 31, 2016
  2. Change this:
    Code (Text):
    Sign s = (Sign) e.getClickedBlock();
    to:
    Code (Text):
    Sign s = (Sign) e.getClickedBlock().getState();
     
    • Agree Agree x 1
  3. oh yeah lemme try that.
     
  4. @elsiff nothing works and no stack traces.
     
  5. if I right click the sign nothing happens and no stack traces.
     
  6. Somehow doing instanceof doesn't work

    Try this:

    Code (Text):
    if(e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN){
                    Sign sign = (Sign) e.getClickedBlock().getState();
     
  7. trying.
     
  8. @TheBlackTeddy now there's a stack trace
    Code (Java):
    [16:35:48 ERROR]: Could not pass event PlayerInteractEvent to CustomRecipe v1.6
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:310) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at org.bukkit.craftbukkit.v1_9_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:231) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c
    01]
            at net.minecraft.server.v1_9_R1.PlayerInteractManager.a(PlayerInteractMa
    nager.java:482) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java
    :888) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInUseItem.a(SourceFile:55) [sp
    igot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PacketPlayInUseItem.a(SourceFile:11) [sp
    igot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:1
    3) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
            at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9
    .2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:7
    21) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:4
    00) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:6
    60) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java
    :559) [spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R1.block.Cr
    aftBlock cannot be cast to org.bukkit.block.Sign
            at org.madmaxcookie.SignUpdate.onClick(SignUpdate.java:33) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _91]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _91]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_91]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_91]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:306) ~[spigot-1.9.2.jar:git-Spigot-e6f93f4-ed60c01]
            ... 17 more
     
  9. Line 33 of SignUpdate class? And your imports.
     
  10. Line 33
    Code (Java):
                Sign s = (Sign) e.getClickedBlock();
    // imports
    Code (Java):
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.madmaxcookie.CustomRecipe.RecipeType;
     
  11.  
  12. I know that but if I did that there's no stack trace and nothing happens.
     
  13. |Debug your code than.

    EDIT: I saw your coee and you are hardcoding the ColorCodes, that will not work as they changed on the Sign.
     
  14. I used

    Code (Java):
    String colorize(String arg0) {
     return ChatColor.translateAlternat....('&', arg0);
    }
     
  15. Bumping its not fixed yet.
     
  16. Can you show your full code pls?
     
  17. check above please ?
     
  18. Well for starts use this :
    Code (Text):
    Sign s = (Sign) e.getClickedBlock().getState();
    to cast a sign (dont use the other ways). And you said that it isnt printing out any errors right? What i would do is check to make sure you dont have an extra bracket anywhere (this happens to me all the time) Also as mentioned above, include debug messages so that you can figure out where you messed up.
     
  19. haven't done that since then could you help me how to debug ?