1.15.2 Help with getting direction of sign

Discussion in 'Spigot Plugin Development' started by YJJcoolcool, Apr 5, 2020.

  1. Hi, I'm making a plugin and a part of it requires to get the coordinates and facing direction of the sign to figure out the coordinates of the sign behind it (both of them are at opposite sides on a wall) and edit the sign. I tried using some code from previously asked questions about this but the code didn't work.

    Here is the snippet of the part of the code which does it:
    (The line causing the error is "org.bukkit.block.Sign sign3 = (org.bukkit.block.Sign) sign.getData();")
    Code (Java):
                else if (item.getItemMeta().getDisplayName().equals(ChatColor.GREEN+"OK")) {
                    player.sendMessage(ChatColor.GREEN+"You hast presseded le OK buton");
                    player.sendMessage(ChatColor.GREEN+"Updating Cashier sign...");
                    Block block = player.getWorld().getBlockAt(x,y,z);
                    BlockState state = block.getState();
                    Sign sign = (Sign) state;
                    if (!(state instanceof Sign)) {
                        player.sendMessage(ChatColor.RED+"Block is not a sign!");
                        return; // block is not a sign
                    }
                    sign.setLine(1, "Amount:");
                    sign.setLine(2, "YC$"+amount);
                    sign.setLine(3, "Click to Cancel");
                    sign.update();
                    player.sendMessage(ChatColor.GREEN+"Updated Cashier sign.");
                    player.sendMessage(ChatColor.GREEN+"Updating Customer sign...");
                    org.bukkit.block.Sign sign3 = (org.bukkit.block.Sign) sign.getData();
                    player.sendMessage("Obtained Sign Data!");
                    BlockFace directionFacing = ((Entity) sign3).getFacing();
                    player.sendMessage("Sign is facing: "+directionFacing);
                    int x2=0;
                    int z2=0;
                    if (directionFacing == BlockFace.EAST) {
                        x2=x-2;
                        z2=z;
                    } else if (directionFacing == BlockFace.WEST) {
                        x2=x+2;
                        z2=z;
                    } else if (directionFacing == BlockFace.NORTH) {
                        x2=x;
                        z2=z-2;
                    } else if (directionFacing == BlockFace.SOUTH) {
                        x2=x;
                        z2=z+2;
                    }
                    player.sendMessage(ChatColor.GREEN+"Customer sign is at: "+x2+" "+y+" "+z2);
                    Block block2 = player.getWorld().getBlockAt(x2,y,z2);
                    BlockState state2 = block2.getState();
                    if (!(state2 instanceof Sign)) {
                        player.sendMessage(ChatColor.RED+"Block is not a sign!");
                        return; // block is not a sign
                    }
                    Sign sign2 = (Sign) state2;
                    sign2.setLine(1, "Amount:");
                    sign2.setLine(2, "YC$"+amount);
                    sign2.setLine(3, "Click to Pay");
                    sign2.update();
                    player.sendMessage(ChatColor.GREEN+"Updated Customer sign.");
                    player.closeInventory();
                }

    And here is the console error message:
    Code (Text):
    [11:09:02 ERROR]: Could not pass event InventoryClickEvent to YJJCityPlugins-Cashiers v1.0
    java.lang.ClassCastException: org.bukkit.material.Sign cannot be cast to org.bukkit.block.Sign
            at yjjcity.plugins.Cashiers.Events.EventsClass.InvenClick(EventsClass.java:164) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor340.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.15.2.jar:git-Paper-161]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.15.2.jar:git-Paper-161]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.15.2.jar:git-Paper-161]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:607) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:2333) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.PacketPlayInWindowClick.a(SourceFile:32) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:1038) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:1031) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:1015) ~[patched_1.15.2.jar:git-Paper-161]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:938) ~[patched_1.15.2.jar:git-Paper-161]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
     
  2. You maybe imported the wrong sign object. As the error says
    Code (Text):
    java.lang.ClassCastException: org.bukkit.material.Sign cannot be cast to org.bukkit.block.Sign
    Also you should check if the block is a sign before getting it's data, because if you use it on grass for example, you'll try to cast (Sign) object on grass block data, which will send an error
     
  3. So, what can I do to fix that? I tried using org.bukkit.block.Sign but the getFacing becomes an error. (I'm using Spigot 1.15.2 btw)
     
  4. And what's the new error?
     
  5. The method getFacing() is undefined for the type Sign
     
  6. Ok so try putting the first method you did, but in plugin.yml add: api-version: 1.13
     
  7. I have already done that
     
  8. Code (Java):
    BlockFace blockFace = ((Directional) block.getBlockData()).getFacing();
     
  9. Now that line has an error saying
    Directional cannot be resolved to a type
     
  10. Import Directional manually. I have a weird bug too where I cannot control+spacebar import the directional interface