Adding a potion affect to a player when they are in the water

Discussion in 'Plugin Development' started by MetaSurvival, Sep 22, 2013.

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

    MetaSurvival

    Code:
    Code:java
    1. public static List<String> poseidon = new ArrayList<String>();
    2. @EventHandler
    3. public void onMove(PlayerMoveEvent event){
    4. Player player = event.getPlayer();
    5. if(event.getPlayer().getLocation().getWorld().getBlockAt(0, 0, 0).equals(Material.WATER)){
    6. if(poseidon.contains(player.getName())){
    7. player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 200, 1));
    8. }else{
    9. }
    10. }
    11.  


    There are no errors; It just doesn't do what it's meant to do
     
  2. Offline

    Chinwe

    You're checking the location 0,0,0, which is underneath bedrock in the void - ain't gonna be water :oops:
    event.getPlayer().getLocation() will get their leg location, player.getEyeLocation() will get their eye location, and player.getLocation().getBlock().getRelative(BlockFace.ANYBLOCKFACE).getLocation() will get the location relative to their current location :)
     
    MetaSurvival likes this.
  3. Offline

    MetaSurvival

    Chinwe
    I changed the code to:
    Code:
    if(event.getPlayer().getLocation().getBlock().getType().equals(Material.WATER)){
    if(poseidon.contains(player.getName())){
    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 200, 1));
    }else{
    }
    }
    }
    
    And it is still not working
     
  4. Offline

    Chinwe

    Try checking if it is Material.WATER or Material.STATIONARY_WATER, as WATER is only moving water I believe
     
    MetaSurvival likes this.
  5. Offline

    MetaSurvival

    Chinwe
    -_- how would I do that lol, the brackets have gone all wrong and i can't seem to do it -__-
    if(event.getPlayer().getLocation().getBlock().getType().equals(Material.WATER) || (Material.STATIONARY_WATER)){
     
  6. Offline

    Chinwe

    MetaSurvival
    Unfortunately you can't do it like that :(
    Code:
    // Shortens the if statement a bit
    Material mat = event.getPlayer().getLocation().getBlock().getType();
     
    if (mat == Material.WATER || mat == Material.STATIONARY_WATER)
    {
        // continue
    }
     
    
     
    MetaSurvival likes this.
  7. Offline

    MetaSurvival

    Chinwe
    Thanks!
    How would I remove the effect from the player when they get out of the water?
    Like this?
    Code:
    Material mat = event.getPlayer().getLocation().getBlock().getType();
    if(mat == Material.WATER || mat == Material.STATIONARY_WATER){
    if(poseidon.contains(player.getName())){
    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 200, 1));
    }
    if(mat != Material.WATER || mat != Material.STATIONARY_WATER){
    player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    }
     
    }
    }
    
     
  8. Offline

    BR3TON

    do an else to remove the potion effect.
     
    MetaSurvival likes this.
  9. Offline

    MetaSurvival

  10. Offline

    Chinwe

    MetaSurvival
    Put it in the else statement :)
    Code:
    if (mat == Material.WATER || mat == Material.STATIONARY_WATER)
    {
      // add potion effect
    }
    else
    {
      player.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    }
    I'd recommend putting this line before everything in PlayerMoveEvent, as without it, your code will be run several times a second for each player: this will make it only run if they move into another block -

    Code:
    @EventHandler
    public void onMove(PlayerMoveEvent event)
    {
        if (event.getFrom().getBlockX() == event.getTo().getBlockX() && event.getFrom().getBlockZ() == event.getTo().getBlockZ() && event.getFrom().getBlockY() == event.getTo().getBlockY())
            return;
     
        // continue
     
    }
    EDIT: Ninja'd :>
     
    MetaSurvival likes this.
  11. Offline

    MetaSurvival

Thread Status:
Not open for further replies.

Share This Page