Solved Signs

Discussion in 'Plugin Development' started by InspectorFacepalm, Jun 3, 2013.

Thread Status:
Not open for further replies.
  1. I need some help here with sign events, so I don't know how to do this, but, how do you make it when you right click a sign with a specific word it will send you message, anyone know how to do this?
     
  2. Offline

    chasechocolate

    PlayerInteractEvent, check for right click block, check if event.getClickedBlock().getState() instanceof Sign, cast to Sign (org.bukkit.block), and then use sign.getLine(<0, 1, 2 or 3>) for getting lines.
     
  3. Hmm, for some reason that doesn't work, am I doing this right?
    Code:
     @EventHandler
            public void onPlayerInteract(PlayerInteractEvent ev)
            {
              if (ev.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block b = ev.getClickedBlock();
                if ((b.getType() == Material.SIGN_POST) || (b.getType() == Material.WALL_SIGN)) {
     
  4. Offline

    boboman13

    InspectorFacepalm I know that there is a third Material.SOMETHING for signs - at least I use 3. Does it work with sign posts and signs on walls?
     
  5. Hmm, leme check.
     
  6. Offline

    CluelessDev


    The event you're looking for would be:
    http://jd.bukkit.org/beta/apidocs/org/bukkit/event/player/PlayerInteractEvent.html

    Then check to see if the clicked block from the event is of type Sign:
    http://jd.bukkit.org/beta/apidocs/org/bukkit/block/Sign.html
    Code:
    Block clickedBlock = event.getClickBlocked();
     
    if (clickedBlock.getTypeID() == 63 | clickedBlock.getTypeID() == 68)
    {
    //DoStuff
    } 
    Next create a variable of type Sign by casting the clicked block to it if it passes your check:
    Code:
    Sign sign = (Sign)clickedBlock;
    Use the getLines() method from the Sign class to get the lines of text from the sign
    Code:
    String[] lines = sign.getLines();
    Then just iterate through the array, checking each line to see if it equals your word, if it equals it then send them the message.
    One method:
    Code:
    boolean checked = false;
    int increment = 0;
    while(checked == false)
    {
        if(lines[increment].equalsignorecase("<word>")
        {
            event.getPlayer().sendMessage("<message>");
            checked = true;
        }
        else if (increment >= test.length - 1)
        {
            checked = true;
        }
     
        increment++;
    }
    Another method:
    Code:
    boolean messaged = false;//stopping the loop from messaging the player multiple times
    For (String line : lines)
    {
        if(line.equalsignorecase("<WordToCheck>") & !messaged)
        {
            event.getPlayer().sendMessage(<Message>);
            messaged = true;
        }
    }

    I haven't actually tried compiling any of the code so it may or may not work exactly as described.
     
  7. Woah, wait
    do I do it like this
    Code:
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent ev)
            {
    
                boolean messaged = false;//stopping the loop from messaging the player multiple times
                For (String line : lines)
                {
                    if(line.equalsignorecase("lol") & !messaged)
                    {
                        ev.getPlayer().sendMessage("hi");
                        messaged = true;
                    }
                }
    
         
                      }
                    }
                  
         
            }
    }
     
  8. Offline

    CluelessDev

    The brackets in that block of code are a mess, but essentially yes. You'll need to go through and get the lines from the sign before trying to iterate through them though. Since right now you haven't defined what the variable "lines" is.
     
  9. Offline

    Omerrg

    If(e.getClickedBlock().getState() instanceof Sign){
    //than this is a sign block..
    }
     
  10. Offline

    Arcoz

    Here you go:
    Code:
    @EventHandler
     
    public void onSignCreate(SignChangeEvent sign){
    Player sender = sign.getPlayer();
    if(sign.getLine(0).equalsIgnoreCase("something")){
    sender.sendMessage("Arcoz helped me");
    }
    }
     
  11. ah kay
    @Omergg I'll check that now :)
    boboman13 Thanks, I tried that, doesn't work D:

    nope, doesn't work
    Here's my code if anyone needs it
    Code:
         @EventHandler
            public void onSignChange(SignChangeEvent e) {
              Player p = e.getPlayer();
              if (e.getLine(0).equalsIgnoreCase("[Test]"))
                  if (e.getLine(1).isEmpty()) {
                    p.sendMessage(ChatColor.RED + "Error: Enter your name on the second line!");
                    e.setCancelled(true);
                  } else {
                    e.setLine(0, "[ShipCraft]");
                    p.sendMessage(ChatColor.GOLD + "You have created your name ");
            
                  }
          
                }
    
         @EventHandler
         
         public void onSignCreate(SignChangeEvent sign){
         Player sender = sign.getPlayer();
         if(sign.getLine(0).equalsIgnoreCase("something")){
         sender.sendMessage("Arcoz helped me");
         }
         }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
  12. Offline

    Arcoz

    Code:
        @EventHandler
     
        public void onSignCreate(SignChangeEvent sign){
        Player sender = sign.getPlayer();
        if(sign.getLine(0).equalsIgnoreCase("[Test]")){
    if (sign.getLine(1).isEmpty()){
    sender.sendMessage(ChatColor.RED + "Error: Enter your name on the second line!");
    else{[/FONT]
    sign.setLine(0, "[ShipCraft]");
        sender.sendMessage("Arcoz helped me");
        }
        }
    That's odd, that should work.

    Oh wait, you wont the code for interacting with it?
     
  13. Offline

    pokuit

    InspectorFacepalm Check that your registering your events. Also I think onSignChange only fires when i sign is changed not placed so try the playerinteractevent again. Also try logging and checking if the signchangeevent is actually being run or the playerinteractevent with getServer().getLogger().info("");
     
  14. ergh, that doesn't work ;/

    I have already registered my events and everything :/

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

    pokuit

    InspectorFacepalm can you give us your whole main class.; *You can take out any importantthings you want to keep secret*
     
  16. Code:
           @EventHandler
           
            public void onSignCreate1(SignChangeEvent sign){
            Player sender = sign.getPlayer();
            if(sign.getLine(0).equalsIgnoreCase("[Test]")){
        if (sign.getLine(1).isEmpty()){
        sender.sendMessage(ChatColor.RED + "Error: Enter your name on the second line!");
        }else{
        sign.setLine(0, "[ShipCraft]");
            sender.sendMessage("Arcoz helped me");
            }
            }
            }
    
         @EventHandler
         
         public void onSignCreate(SignChangeEvent sign){
         Player sender = sign.getPlayer();
         if(sign.getLine(0).equalsIgnoreCase("something")){
         sender.sendMessage("Arcoz helped me");
         }
         }
    }
    
    there is more, it's just imports and the class name
     
  17. Offline

    Omerrg

    Signchangeevent is not what you need...... I did that before, use playerinteractevent and check if its a sign than cast it to a sign and check the line you want =] tell me if you need anymore help, I'm in a middle of class so it will take me sometime to response
     
  18. Offline

    Arcoz

    remove the last event handler.

    And get help with the interraction
     
  19. Thanks Omerrg and everyone!
    I'll try and figure it out now. :)
     
Thread Status:
Not open for further replies.

Share This Page