Solved Error with signs

Discussion in 'Plugin Development' started by 567legodude, Jul 26, 2014.

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

    567legodude

    Im trying to add signs to my plugin.
    I want the code to execute when I right click a sign with "[something]" on it.
    Here is my code:
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent event) {
    3. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    4. BlockState state = event.getClickedBlock().getState();
    5. if (state instanceof Sign) {
    6. Sign sign = (Sign) event.getClickedBlock();
    7. if (sign.getLine(0).equalsIgnoreCase("[something]")) {
    8. //code
    9. }
    10. }
    11. }
    12. }

    Whenever I click the sign, the console gives an error "Could not pass PlayerInteractEvent to (PluginName)"
    Later in the error it says "...block.CraftBlock cannot be cast to org.bukkit.block.Sign"
    Ive tried so many tutorials for clicking a sign, then checking what is on the sign, but they all dont work, so can someone tell me the right way to check the text of a clicked sign then execute some code if it has some text on it.

    Yes, I also try this, and it doesnt work either:
    (Just want to say that yes, I did place a wall sign)
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent event) {
    3. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    4. if (event.getClickedBlock().getType() == Material.WALL_SIGN) {
    5. Sign sign = (Sign) event.getClickedBlock();
    6. if (sign.getLine(0).equalsIgnoreCase("[something]")) {
    7. //this happens if it works
    8. Player player = (Player) event.getPlayer();
    9. player.sendMessage("It worked.");
    10. }
    11. }
    12. }
    13. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    Wizehh

    Ah, sorry, I got it all wrong. What you need to do is cast Sign to the block's state, not the block itself.
    For example,
    Code:java
    1. Block block = event.getClickedBlock();
    2. if (block.getState() instanceof Sign) {
    3. Sign sign = (Sign) block.getState();
    4. }
     
    Niknea likes this.
  3. Offline

    567legodude

    Wizehh Thank you so much, it worked.
    For anyone that wants to see, this is the final code that worked:
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent event) {
    3. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    4. BlockState state = event.getClickedBlock().getState();
    5. if (state instanceof Sign) {
    6. Sign sign = (Sign) state;
    7. if (sign.getLine(0).equalsIgnoreCase("[something]")) {
    8. //this happens if it works
    9. Player player = (Player) event.getPlayer();
    10. player.sendMessage("It worked.");
    11. }
    12. }
    13. }
    14. }
     
Thread Status:
Not open for further replies.

Share This Page