Solved Check if player is standing on block clicked

Discussion in 'Plugin Development' started by khave, Feb 1, 2014.

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

    khave

    So how would I do that?

    Here's a snippet of my code:
    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            final Player p = e.getPlayer();
     
            if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)){
                return;
            }
     
            if(!e.getPlayer().isSneaking()){
                return;
            }
            final Block clicked = e.getClickedBlock();
     
            if(p.getLocation() == clicked.getLocation().add(0, 1, 0)){ //I have tried also removing the .add
                p.setVelocity(p.getLocation().getDirection() //This is what doesn't work.
                        .multiply(1).setY(1));
                p.sendMessage("Location detected");
                }
            
    Also before anyone ask I have registered my evens. Everything works except the location finding. Thank you for the help ^^
     
  2. == in java checks for reference equality, not value equality. .equals() will check for value equality which is what you need here.
     
  3. Offline

    khave


    It still doesn't seem to work :c..

    Code:java
    1. if(p.getLocation().equals(clicked.getLocation().add(0, 1, 0))){
    2. p.setVelocity(p.getLocation().getDirection()
    3. .multiply(1).setY(1));
    4. p.sendMessage("Location detected");
    5. }
     
  4. Have you just tried printing out the locations and see why they're not equal? Then you can probably fix it yourself when you saw the difference ;)
     
  5. Offline

    khave


    I'll try that. Thanks!

    Thank you. I figured it out. The reason it wasn't working was cause the player location found a double and the clicked location found an int. Here's what I did if anyone else also needs it:
    Code:
    Location clickLoc = clicked.getLocation().add(0, 1, 0);
            Location playerLoc = new Location(p.getWorld(), p.getLocation().getBlockX(), p.getLocation().getBlockY(), p.getLocation().getBlockZ());
            if(playerLoc.equals(clickLoc)){
                p.setVelocity(p.getLocation().getDirection()
                        .multiply(1).setY(1));
                p.sendMessage("Location detected");
                }
     
  6. Oh thing, that issue again. You might as well just check the x, y and z coordinates yourself so you wouldn't need to create a new location.
     
  7. Offline

    khave


    Yea. I'll do that. What do you mean by that issue again? Has many people been tricked by that? xP
     
  8. khave No, it always tricks me, because I would expect that a location with exact coordinates would still match a location with only block coordinates, if they would describe the same block.
     
  9. Offline

    khave


    Yea it is a bit weird and quite annoying, but now I know, so I won't make the same mistake.
     
Thread Status:
Not open for further replies.

Share This Page