Solved Check for Text in first line of a sign

Discussion in 'Plugin Development' started by Alias_Me, Jan 14, 2019 at 4:59 AM.

  1. Offline

    Alias_Me

    So when a Sign is placed (on a specific construction) I want a message to be sent to the player only if there is Text in the first line of the Sign (Doesn`t matter if there is text in the others).

    I tried checking like these options:
    Code:
    if(textsign.getLine(0) != null) {
    //
    if(textsign.getLine(0) != "") {
    //
    if(!(textsign.getLine(0).equals(null))) {
    //
    if(!(textsign.getLine(0).equals(""))) {
    
    but none worked.

    The rest of the class works perfectly without the if statement checking for text:
    Code:
    public class PlaceSigns implements Listener {
       @EventHandlerpublic void signMarked(SignChangeEvent event) {
        Block sign = (Block) event.getBlock();
        Sign textsign = (Sign) event.getBlock().getState();
        String text = (String) textsign.getLine(0);
        //Check for Blocks/If Sign belongs to Guidepost 
        //Get Location of Sign for reference
        Location signloc = sign.getLocation();
        int signlocX = sign.getX();
        int signlocY = sign.getY();
        int signlocZ = sign.getZ();
        World signworld = (World) sign.getWorld();
        Block checkwall = (Block) signworld.getBlockAt(signlocX, signlocY - 1, signlocZ);
        //Cobblestone Wall right under
        if(signworld.getBlockAt(signlocX, signlocY - 1, signlocZ).getType() == COBBLESTONE_WALL){
            if(signworld.getBlockAt(signlocX, signlocY - 2, signlocZ).getType() == GLOWSTONE) {
                if(textsign.getLine(0) == null) {
                    event.getPlayer().sendMessage(ChatColor.YELLOW + "Guidepost created and available for fast Travel!");}
                }
          }
    }
    }
     
  2. Offline

    timtower Ninja on the waves Moderator

    @Alias_Me Try printing the values of textsign.getLines()
     
  3. Offline

    Alias_Me

    @timtower So now the if-check looks like this:
    Code:
    if(signworld.getBlockAt(signlocX, signlocY - 2, signlocZ).getType() == GLOWSTONE) {
        event.getPlayer().sendMessage(ChatColor.YELLOW + "Guidepost created and available for fast Travel!");
        System.out.println(textsign.getLine(0));
        System.out.println(textsign.getLine(1));
        System.out.println(textsign.getLine(2));
        System.out.println(textsign.getLine(3));
    }
    
    The ingame message shows, but the console doesn`t print anything...
     
  4. Offline

    timtower Ninja on the waves Moderator

    It doesn't clear a couple lines either?
    What if you send them to the player?
     
  5. Offline

    Alias_Me

    @timtower
    Nope, doesn`t even show that something should have printed. No error message, no nothing, and sending to the player also does nothing.
     
  6. Offline

    DerDonut

    Are you sure there are no checked exceptions in your code? In my opinion there have to be some :oops:

    Edit: Okay and to solve your problem you have to take the getLine() function from the event, not from the sign. The sign will be updated after you handle the event so when using sign.getLine(0); you get the line that was on the sign before you edited it.
     
    Last edited: Jan 14, 2019 at 6:57 AM
  7. Offline

    Alias_Me

    @DerDonut Alright that works, thank you!
     

Share This Page