EventHandler Sign Interaction

Discussion in 'Spigot Plugin Development' started by Megalodon67, Jan 5, 2016.

  1. I have been trying to write a simple plugin as a test to familiarize with the Eventhandler and have come up with this as a method of getting the player interacting and send them a test message. Unfortunately, this is not completed as on interaction (the event type) the message is not sent to the user (me). As I right click the sign nothing is occurring. This is likely a simple mistake... Please help!
    Code (Text):
    @EventHandler
        public void onRightClick(PlayerInteractEvent e) {
            Player player = (Player) e.getPlayer();
            if (e.getClickedBlock().equals("Sign")) {
                player.sendMessage("Test!");
            }
     
  2. try Player player = e.getPlayer();
     
  3. Try to check if the material of the item == Material.SIGN instead of ".equals()". "==" is usually used to compare objects while ".equals()" is usually used to compare strings.
     
  4. You're comparing a block to the string "Sign". Get the material using Block#getType and compare it with the sign material (Material.SIGN_POST)
     
    #5 PickNChew, Jan 5, 2016
    Last edited: Jan 5, 2016
    • Agree Agree x 2
  5. No success with the change, still getting nothing.
     
  6. No, his use of .equals() is better, but as @PickNChew pointed out, he's comparing the wrong things.
     
  7. There are three sign materials, try
    Code (Text):
    if(e.getClickedBlock().equals(Material.WALLSIGN) || e.getClickedBlock().equals(Material.SIGNPOST) {
    ...
    Edit: Made code actual code and not just idea of code, *grumble, grumble, @nfell2009
     
  8. Not with that capitalisation and missing underscores.
     
  9. And no, you're wrong. Material is an enum. When comparing enumerations, you should use the == operator. You may be confusing this with Strings, of which you should *almost* always compare with .equals() when comparing equality.
     
    • Agree Agree x 2
  10. Actually, .equals is the same as == for enums, unless its input is null.. but null doesn't seem pop out randomly in your code/sarcasm
     
  11. I tried this, but what do you mean by Block#getType?
    Code (Text):
    if (e.getClickedBlock().equals(Material.SIGN)) {
     
  12. if(e.getClickedBlock().getType() == Material.SIGN)
     
  13. Thanks. I will try it now!
     
  14. You sure about that?
     
    • Agree Agree x 1
    • Winner Winner x 1
  15. Sadly, the issue persists with the current code... Not even an error message!
    Code (Text):
    @EventHandler
        public void onRightClick(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            if (e.getClickedBlock().getType().equals(Material.SIGN)) {
                player.sendMessage("Test!");
            }
        }
     
  16. Block b = e.getClickedBlock();
    if(b.getType()==Material.WALL_SIGN || b.getType()==Material.SIGN || b.getType()==Material.SIGN_POST)

    You need to check if it is ANY of the 3 types of signs.
     
  17. Did you register your event?
     
    • Agree Agree x 1
  18. Still getting the same issue.
    Code (Text):
        @EventHandler
        public void onRightClick(PlayerInteractEvent e) {
            Player player = e.getPlayer();
            if (e.getClickedBlock().equals(Material.WALL_SIGN) || e.getClickedBlock().equals(Material.SIGN_POST)) {
                player.sendMessage("Test!");
            }
        }
     
  19. Make sure its registered, like Insou said earlier.