Help with getting direction of sign

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

Thread Status:
Not open for further replies.
  1. Offline

    YJJcoolcool

    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:
              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:
    [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. This is a cast problem. There is no need for Entity classes or Block classes. Just these simple lines will be the solution. You need to get the state of the block first. Then cast it to org.bukkit.material.Sign after you've done a instanceof check first. And finally add the method getData(); and get the facing. You were close :)

    spigot 1.12 nms 1_12_R1
    Code:
    Block sign = player.getWorld().getBlockAt(x,y,z);
    BlockState state = sign.getState();
    org.bukkit.material.Sign sign_data = (org.bukkit.material.Sign)state.getData();
    BlockFace face = sign_data.getFacing();
     
  3. Offline

    YJJcoolcool

    Hi, I tried adding the code you put. The editor puts errors for the last two lines in the code you put, one saying that "The type Sign is depreciated" and the other one saying the method .getFacing from the type Sign is depreciated.
    Code:
                   Block sign = player.getWorld().getBlockAt(x,y,z);
                    BlockState state = sign.getState();
                    Sign signWrite = (Sign) state;
                    if (!(state instanceof Sign)) {
                        player.sendMessage(ChatColor.RED+"Block is not a sign!");
                        return; // block is not a sign
                    }
                    signWrite.setLine(1, "Amount:");
                    signWrite.setLine(2, "YC$"+amount);
                    signWrite.setLine(3, "Click to Cancel");
                    signWrite.update();
                    player.sendMessage(ChatColor.GREEN+"Updated Cashier sign.");
                    player.sendMessage(ChatColor.GREEN+"Updating Customer sign...");
                    player.sendMessage("Obtained Sign Data!");
                    org.bukkit.material.Sign sign_data = (org.bukkit.material.Sign)state.getData();
                    BlockFace face = sign_data.getFacing();
                    player.sendMessage("Sign is facing: "+face);
     
  4. What is the server fork you have? And the version?
     
  5. Offline

    YJJcoolcool

    Spigot 1.15.2
     
Thread Status:
Not open for further replies.

Share This Page