PlayerInteractionEvent

Discussion in 'Plugin Development' started by Stackoverload, Feb 27, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Stackoverload

    Hello, I'm making a plugin which when the user right clicks a certain block something will happen. I used the following code but it did nothing. Yes, I did register the events and what not (All my other events work fine). :

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. if(event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    4. if(event.getClickedBlock().getType() == Material.STONE) {
    5. if(event.getPlayer().getItemInHand() == null) {
    6. event.getPlayer().sendMessage("You right clicked stone!");
    7. } else if(event.getMaterial() == Material.POTION && event.getPlayer().getItemInHand().getDurability() == 0) {
    8. event.getPlayer().sendMessage("You right clicked stone with a glass bottle!");
    9. }
    10. }
    11. }
    12. }
     
  2. Offline

    Vandrake

    I believe the getItemInHand would return ItemStack AIR
    but you really need to debug your code before posting it here...
     
  3. Offline

    Minesuchtiiii

    I would do:
    Code:java
    1. @EventHandler
    2. public void onInteract (PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. Block b = e.getClickedBlock().getType();
    6. if(b == Material.STONE() {
    7. if(p.getItemInHand() == null) {
    8. p.sendMessage("You right clicked a stone block!");
    9. }
    10. else if(p.getItemInHand().getType() == Material.POTION && b == Material.STONE) {
    11. p.sendMessage("You right clicked a stone block with a glass bottle!");
    12. }
    13. else if(!(p.getItemInHand().getType == Material.POTION)) {
    14. p.sendMessage("Right clicked a stone block with " + p.getItemInHand().getType() + "!");
    15. }
    16. }
     
  4. Offline

    Vandrake

    You don't get it.
    You need to debug each line and check where it fails.
    Also, those nested ifs are too restricted. Check them again
     
  5. Offline

    Minesuchtiiii

    Vandrake
    Why should he debug each line?!
    This doesn't make sence!
    Simply look in the console in which line the error is, get to the line, search the error, fix it, export, try again..!
    It's better to make more 'ifs' because if it's not the cause only one thing in an 'if' isn't so (no && ..) you can do better 'else' ..!
     
  6. Offline

    Vandrake

    What if there's no error?
    What if there's just silence?
    you do a command but nothing happens?No sound, no msg, nothing...
    That's the point of debugging each line. I'm just trying to help, do whatever the hell you want. Listen to me or don't. None of my business
     
  7. Offline

    Minesuchtiiii

    Everyone's got his method.. if you want tell us yours, okay.
    If this method helps him, okay.
    If not?
    I would start a new projekt and do everything again.. check everything.. think if you made everything right - after each line.
    If this doesn't work?
    If nothing works?
    Search help.
    What is he doing here?
    That.
     
    Stackoverload likes this.
  8. Offline

    Vandrake

    Yes. Because thats easier than doing a System.out.println("1"); after each method... Jesus...
     
  9. Offline

    Minesuchtiiii

  10. Offline

    Vandrake

    Do you know what Debug means?
     
  11. A question,
    1. Are you getting an error? if so on what error and on what line. If not then is the code just not working or is it doing something else?
     
  12. Offline

    Stackoverload

    Zero errors. I'm thinking Material.Stone is not the right 'getClickedBlock' type. Unsure.

    I have debugged... null is meant to be air. So they can only right click with air.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  13. Try this in your if statement
    Code:java
    1. event.getPlayer().getItemInHand() == null || event.getPlayer().getItemInHand() == Material.AIR
     
    Stackoverload likes this.
  14. Offline

    Stackoverload

    Doesn't work - getItemInHand() returns an ItemStack not Material.
     
  15. Offline

    AoH_Ruthless

    Stackoverload
    event.getPlayer().getItemInHand().getType() maybe?

    Minesuchtiiii
    Maybe it's not wise to bring up an old argument that has subsided, but I feel this should be addressed: Vandrake was just trying to help. It isn't fair to hammer on him that his idea of debugging is wrong just because you don't have a clue what he is talking about. Frankly, debugging is the logical decision that I would make: It's rather effortless in comparison to your approach, and doesn't obfuscate or break your code in any way.
     
    Stackoverload likes this.
  16. Offline

    Stackoverload

    How did I hammer him...
     
  17. Offline

    Niknea

    Minesuchtiiii Why should he create a whole new project over one bug?
     
  18. Offline

    xTigerRebornx

    Stackoverload Have you added debug to see if the event is even getting fired in the first place?
     
    Stackoverload likes this.
  19. Offline

    AoH_Ruthless

    Stackoverload likes this.
  20. Offline

    Minesuchtiiii

    What's ur problem?
    He should do whatever he wants..
     
  21. Offline

    Stackoverload

    No problem, I thought you were talking about him. Wasn't sure.

    Yes, the problem is the specific block. Which in this case would be Material.STONE.

    It seems that when I right click on a block with a Stone block in my hand it triggers.... I want it to detect when I right click with air (hand) and the block is stone... Odd.

    You were somewhat right! Except you needed 'event.getPlayer().getItemInHand().getType() =='. Thanks for all the help guys it's greatly appreciated! Happy developing!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page