Sign.getLine Error

Discussion in 'Spigot Plugin Development' started by KingDotCom, Jun 2, 2017.

  1. Code (Text):

    Block i = e.getClickedBlock();
                Sign sign = (Sign) i;
                if(sign.getLine(0).equals(
    In this code _____ .getLine is an error! Can someone help me please Thaaanks in advanced.
     
  2. Can you post more of the code?
     
  3. Usually, you explain the error present, but I think I know what is wrong. You need to get the block state of the block to cast it to a sign with Block#getState() I believe, then you can cast to a sign.
     
  4. Make sure you're using the correct "sign" (block or entity, I believe).
     
  5. Code (Java):
    Sign sign = (Sign) i;
    Should be
    Code (Java):
    Sign sign = (Sign) i.getState();
    This gets the actual Block State of the Block which contains the sign line data.

    For a reference to the JavaDoc:
    Block#getState()

    For more information on signs, visit here on the spigot wiki.
     
    #5 Aussie, Jun 2, 2017
    Last edited: Jun 2, 2017
    • Agree Agree x 1
  6. You might want to check that the block _is_ a sign before casting its state as well.
     
    • Agree Agree x 3
  7. Code (Text):

    Block i = e.getClickedBlock();
                Sign sign = (Sign) i.getState();
                if(sign.getLine(0).equals(ChatColor.DARK_GRAY + "---------"))
    sign.getLine(0) is still an error
     
  8. Make sure you are using "org.bukkit.block.Sign" and not "org.bukkit.material.Sign" in your imports at the top of your class.
     
  9. Sign doesn't import...? Do I HAVE to use 1.8?
     
  10. This is basically the code you want

    Code (Text):
    package me.test.signplugin;

    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class SignTest {
       
        public void readSign(PlayerInteractEvent e) {
            Block i = e.getClickedBlock();
            Sign sign = (Sign) i.getState();
           
            if (sign.getLine(0).equalsIgnoreCase("sometext")) {
                //Your code here!
            }
        }
       
    }