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
            at$1.execute( ~[spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.PlayerConnection.a( [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$ [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_121]
            at 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( [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.DedicatedServer.D( [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at net.minecraft.server.v1_10_R1.MinecraftServer.C( [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at [spigot-1.10.jar:git-Spigot-6016ac7-10c10b3]
            at 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 ~[?:?]
            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$1.execute( ~[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;
        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):
      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.