PlayerCollision with Block event?

Discussion in 'Plugin Development' started by Bigrat123, Jan 31, 2014.

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

    Bigrat123

    Is there any easy way to check if a player has touched a block, either with head, face, feet, or back?

    I've tried...
    if(move.getTo().getBlock().getType() != Material.AIR)

    ...but that only detects the feet when the block is something like cobweb.
     
  2. Offline

    sgavster

    Bigrat123
    Code:java
    1. //PlayerMoveEvent
    2. for(BlockFace b : BlockFace.values) {
    3. if(player.getLocation().getBlock().getRelitive(b) != Material.AIR) {
    4. //stuff
    5. }
    6. }
     
  3. Offline

    cakenggt

    Try checking to see if the player's x or z coordinates are basically integers. What I mean by this is, if they are touching a block that they can't pass through, then their x or z should be a whole number. A quick check might look something like this:
    Code:java
    1. double toX = move.getTo().getX();
    2. double toZ = move.getTo().getZ();
    3. if (toX%1 < 0.001 || toZ%1 < 0.001 || move.getTo().getBlock().getType() != Material.AIR){
    4. //The player has touched a block with some part of their body
    5. }


    Wouldn't this also be true if they were in the very center of their block (0.5 meters away from all other block surfaces to the four sides)?
     
  4. Offline

    Bigrat123

    Hey, I appreciate the help, but rather than just giving me the code, I would rather have it explained to me..
    a)... so I can do it in the future,
    b)...because what you gave me put out errors.

    [​IMG]

    Well, found a solution to this
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent move){
    3. Player p = move.getPlayer();
    4. int x = (int) p.getLocation().getX();
    5. int y = (int) p.getLocation().getY()-1;
    6. int z = (int) p.getLocation().getZ();
    7. Location under = new Location(p.getWorld(), x,y,z);
    8.  
    9. if(under.getBlock().getType().equals(Material.AIR))
    10. {
    11.  
    12. }
    13. else{
    14. p.damage(20);
    15.  
    16. }
    17. }
    18.  
    19.  


    Will do the same for each side.
     
Thread Status:
Not open for further replies.

Share This Page