Error when trying to get Sign block.

Discussion in 'Spigot Plugin Development' started by EVILCAT6, Apr 20, 2017.

  1. Hello. I'm having an issue where when a player places a sign and puts certain text, it listens out for that and replaces the text, it causes a giant error.

    Code (Text):
    [14:39:19 ERROR]: Could not pass event SignChangeEvent to CommandableSigns v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:2087) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.PacketPlayInUpdateSign.a(SourceFile:44) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.PacketPlayInUpdateSign.a(SourceFile:10) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
            at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571) [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_10_R1.block.CraftBlock cannot be cast to org.bukkit.block.Sign
            at co.uk.thelarval.events.SignCreation.onSignCreate(SignCreation.java:24) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            ... 15 more
    This is what the code looks like:

    Code (Text):
    public class SignCreation implements Listener {

        Core core;
     
        String display;
        String command;
        String arguments;
     
        public SignCreation(Core core) {
            this.core = core;
        }
     
        @EventHandler
        public void onSignCreate(SignChangeEvent event) {
            Sign sign = (Sign) event.getBlock();
            if(sign.getLine(0).equalsIgnoreCase("[Command]")) {
                event.getPlayer().sendMessage("This bit works.");
                String commandName = sign.getLine(1);
                for(String keys : core.getConfig().getConfigurationSection("signs").getKeys(false)) {
                    if(keys.equalsIgnoreCase(commandName)) {
                        display = core.getConfig().getString("signs." + keys + ".display");
                        command = core.getConfig().getString("sign." + keys + ".command");
                        arguments = core.getConfig().getString("sign." + keys + ".arguments");
                        sign.setLine(1, display);
                        sign.setLine(2, "/" + command);
                        sign.setLine(3, arguments);
                     
                        event.getPlayer().sendMessage("§7[CommandableSigns] §aSign has been created!");
                    }
                }
            }
        }
     
    }
     
    Line 24 is Sign sign = (Sign) event.getBlock();

    I have tried changing event.getBlock(); to event.getBlock().getState(); but nothing happens beyond that point (no errors, nothing happens)

    Help is appreciated, thanks.
     
  2. If what I'm understanding is true, you don't need to actually cast a sign to the block (which would be the sign). Just use event.getLine() and so on

    So for example,
    Code (Java):
    if(event.getLine(0).equalsIgnoreCase("[Command]")){
      event.getPlayer().sendMessage("This bit works.");
      }
     
  3. Well then I can't get a variable (sign) which is critical for me.
     
  4. Whys that?

    After looking through everything, you should be able to replace sign with event everywhere and be fine. Maybe not
     
  5. If I don't make a variable (sign) I have to use event.[method here] for everything, which I certainly don't want to do for everything that I'm doing in that class.
     
  6. I guess you could always just rename the event so
    Code (Text):
    public void onSignCreate(SignChangeEvent sign) {
     
  7. That's not what I want. Using the Sign interface gives me a lot more options to control the block with. Changing the event name accomplishes nothing but using 'sign' instead of 'event'...
     
  8. Senmori

    Senmori Retired Resource Staff
    Retired Patron

    Code (Java):
    Sign sign = (Sign)event.getBlock().getState()
     
  9. As I said in the original post, doing that then prevents anything from happening, no errors or anything.