Testing if player is in water

Discussion in 'Plugin Development' started by MistahCheese, Jan 31, 2011.

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

    MistahCheese

    So, my code watches a player, and tries to find out if they're in water. To do this, I did:
    Code:
    Location l = event.getTo();
        Block block = l.getWorld().getBlockAt( l.getBlockX(), l.getBlockY(), l.getBlockZ());
            if ((block.getType() == Material.STATIONARY_WATER)
                    || (block.getType() == Material.WATER))
    For some reason, it never gets past that bit. Sorry, I'm sure this is noob-ish... it's my first plugin. Will someone please help?
     
  2. Offline

    Raphfrk

    You probably want to use .equals instead of ==. I am not 100% sure if that it required here though.

    Also, you could add

    System.out.println( "Block material: " + block.getType() );

    after the

    Block block =

    line.
     
  3. Offline

    eisental

    It's not needed to use .equals for enums. == is perfectly fine.
    Maybe you need to check the surroundings for water and not the actual block the player is in.
     
  4. Offline

    Schirf

    int x = l.getBlockX();
    int y = l.getBlockY();
    int z = l.getBlockZ();

    int typeaccordingtoworld = world.getBlockTypeIdAt(x, y, z);

    test against this value and you don't even need to fetch the block.

    I believe your call to block.getType is incorrect, in that it returns a Material. If you don't want to use the above code, change it to block.getTypeId
     
  5. Offline

    NathanWolf

    == should be fine for materials- they're enum constants.

    That all looks right to me, but I've never used event.To- are you certain it works?

    I would try event.getPlayer.getLocation instead.

    I have this exact code (almost) in an isUnderwater function, so I'm certain that it works :)
    --- merged: Feb 1, 2011 10:33 PM ---
    Oh, also- I check the block above the one the player is standing in as well. This is more "submerged", just checking the target block will fire if the player is just standing in (waist-deep) water.

    So, depends on what you want.
     
Thread Status:
Not open for further replies.

Share This Page