Solved EventHandler

Discussion in 'Spigot Plugin Development' started by ApplePie, Jun 29, 2016.

  1. Hello.
    I'm trying to create an event that executes a command when a sign contains the line of [Scam].
    This does not work, as I don't ever get past my first check (Checking if the block clicked is a sign.)
    This is my code.
    Code (Text):
    @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            System.out.println("1");
            if (e.getClickedBlock() instanceof Sign) {
                System.out.println("2");
                if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    System.out.println("3");
                Sign sign = (Sign) e.getClickedBlock();
                if (sign.getLine(0) != null) {
                    System.out.println("4");
                    if (sign.getLine(0) == "[Scam]") {
                        System.out.println("5");
                        e.getPlayer().openInventory(new GUIInterface().guiInventory());
                    }
                }
            }
        }
        }
    It only prints out "1", and never gets to the fifth or even the second.
    Any help would be appreciated :)
     
  2. Use #equals() to compare Strings
     
  3. Try This

    Code (Text):
     
                if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                     if (e.getClickedBlock() instanceof Sign) {
                Sign sign = (Sign) e.getClickedBlock();
                if (sign.getLine(0) != null) {
                    if (sign.getLine(0).equalsIgnoreCase("[Scam]")) {
                        System.out.println("Done");
                        e.getPlayer().openInventory(new GUIInterface().guiInventory());
                    }
     
  4. Nope. Does to work either. Is there another way to check if the block is a sign?
    I really think I've tried most of them.
    Thanks for the help though.
     
  5. Figured out that a sign has a lot of properties.
    There is wall_sign, sign and sign_post... Although I can only cast the value of sign, making me unable to check a line...
    Anyone who knows how I can do this?
     
  6. You could check if the material type of the clicked block is either SIGN or WALL_SIGN, and get a sign object by calling e.getClickedBlock().getState() and casting that to Sign
     
    • Winner Winner x 1
  7. Choco

    Moderator

    The block clicked in an event cannot be an instance of a Sign. "Sign" is a block state. You must check if the state of the block is a Sign.
    Code (Java):
    if (event.getClickedBlock().getState() instanceof Sign)
    Which also means you must cast the state of the block to a Sign. You will get a ClassCastException later on in your code sequence as you are casing a Block to a BlockState.
    Code (Java):
    Sign sign = (Sign) event.getClickedBlock().getState();
    Lastly, the lines of a sign will never be null. If they are empty, it will simply return "".

    Also, as @Redrield stated above, use .equals() and .equalsIgnoreCase() to compare Strings as oppose to ==
     
    • Winner Winner x 1
  8. I already changed that. Forgot to update it.
    What you said worked. I can't thank you enough!
     
    • Friendly Friendly x 1