Reading block under the block?

Discussion in 'Plugin Development' started by infopaste, Dec 31, 2013.

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

    infopaste

    This is what I would like to do,

    When a player falls,
    if the block, 2 block's below him is glass:2 then cancel fall damage,
    if not then don't cancel fall damage.

    How would you code that? How do you register two blocks below the player?
     
  2. Offline

    Seadragon91

    Here is the code:
    Code:
    @EventHandler
    public void onEntityDamage(EntityDamageEvent event) {
        if (event.getEntityType() == EntityType.PLAYER) {
            Location loc = event.getEntity().getLocation().subtract(0, 2, 0);
            if (loc.getBlock().getType() == Material.GLASS)
                event.setCancelled(true);
        }
    }
    Register the event and it should work
     
  3. Offline

    infopaste

    Thank you!
     
  4. Offline

    Bart

    I don't remember if player.getLocation() returns the block they are standing on or their leg block but I think it is the one they are standing on. In which case,

    Code:java
    1. Block blockBelow = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
     
  5. Offline

    infopaste

    Just realized when I was going though the code, I just want it to block fall damage nothing else. Do you mind rewriting it for just fall damage?
     
  6. Offline

    Bart

    I didn't write any code to stop fall damage.
     
  7. Offline

    infopaste

    I just want it to block fall damage nothing else, how would you do that?
     
  8. Offline

    Bart

    Add a check for the damage reason to be Fall damage, if it is then cancel the event.
     
  9. Offline

    Failplease

    infopaste ,
    Here's the code:
    Code:
        @EventHandler
        public void onEntityDamage(EntityDamageEvent event) {
            if (event.getCause().equals(DamageCause.FALL)) {
                event.setCancelled(true);
            }
        }
     
  10. Offline

    infopaste

    Thanks Failplease, but I want it so fall damage is only cancelled when you land on anything, only if 2 blocks below (that block you land on) is glass.

    I tried this put is sitll disables all damages, not just fall damage:

    Code:java
    1. public void onEntityDamage(EntityDamageEvent event) {
    2. if (event.getCause().equals(DamageCause.FALL)) {
    3. if (event.getEntityType() == EntityType.PLAYER) {
    4. Location loc = event.getEntity().getLocation().subtract(0, 2, 0);
    5. if (loc.getBlock().getType() == Material.GLASS)
    6. event.setCancelled(true);
    7. }
    8. }
    9. }
     
  11. Offline

    RossCoombs

    infopaste

    Code:java
    1. public void onEntityDamage(EntityDamageEvent event) {
    2. if (event.getEntityType() == EntityType.PLAYER) {
    3. Location loc = event.getEntity().getLocation().subtract(0, 2, 0);
    4. if (loc.getBlock().getType() == Material.GLASS) {
    5. if (event.getCause().equals(DamageCause.FALL)) {
    6. event.setCancelled(true);
    7. }
    8. }
    9. }
    10. }


    Had it the wrong way round, I'm pretty sure this should work.
     
Thread Status:
Not open for further replies.

Share This Page