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!"); }
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.
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)
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
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.
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
I tried this, but what do you mean by Block#getType? Code (Text): if (e.getClickedBlock().equals(Material.SIGN)) {
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!"); } }
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.
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!"); } }