Nearby entities

Discussion in 'Plugin Development' started by Irantwomiles, Mar 4, 2015.

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

    Irantwomiles

    how do I add a potion effect to a nearby entity? I've tried everything!
     
  2. Online

    timtower Administrator Administrator Moderator

    Moved to plugin development.
     
  3. @Irantwomiles
    You could start off by showing us what you've tried already, because if you've tried everything then you wouldn't be here asking this question.
     
  4. Offline

    DannyDog

    @Irantwomiles

    Entity.getNearbyEntities(double arg0, double arg1, double arg2)
    Loop through the list that function gives you and apply the PotionEffect to the individual entity.
     
  5. Offline

    Irantwomiles

    Ok I'll post when I get home
     
  6. Code:java
    1.  
    2. Player player = /*player*/;
    3. for(Entity entity : player.getNeartbyEntitys(10, 10, 10) { //10 blocks
    4. //add potion effecct
    5. }
    6.  
     
  7. Offline

    Irantwomiles

    @MaTaMoR_ would I add potion effect to the player or entity?
     
  8. Offline

    TehHypnoz

    Example:
    Code:
    entity.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20, 1);
    
    This would give the entity speed level 2 for 20 ticks, which is 1 second. The reason the level is 2 and not 1 is because the counting starts at 0 (so speed 1 would be 0, speed 3 would be 2 etc).
     
  9. If you only wanna add efects to the players check if the entity is a Player
     
  10. Offline

    Irantwomiles

    @TehHypnoz @MaTaMoR_ Ok ill try it again, maybe i did something wrong. Thanks for the replys

    Code:
    package me.iran.bukkit;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Bard extends JavaPlugin implements Listener {
       
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
       
        public void onJoin(PlayerJoinEvent event) {
            final Player player = event.getPlayer();
            Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() {
                public void run() {
                    if (player.getInventory().getHelmet().equals(Material.GOLD_HELMET) && player.getInventory().getChestplate().equals(Material.GOLD_CHESTPLATE) && player.getInventory().getLeggings().equals(Material.GOLD_LEGGINGS) && player.getInventory().getBoots().equals(Material.GOLD_BOOTS)) {
                        player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 100000, 1));
                        if (player.getItemInHand().equals(Material.SUGAR)) {
                            player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100000, 1));
                            for (Entity entity : player.getNearbyEntities(10, 10, 10)) {
                               
                            }
                        }
                    }
                }
            }, 0L, 1L);
        }
    
    
    }
    
    @TehHypnoz what you said doesnt work, any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  11. @Irantwomiles Well, for one thing your for loop is empty, you should probably relook at past posts. Also, you need to point out what isn't working. We can't read you mind. ;)
     
  12. Offline

    Irantwomiles

    oh sorry, well I forgot to say, I removed what I had in there before posting this :p well inside the for loop I tried to do what TehHy said by doing entity.addPotionEffect which didnt work and I was wondering how I could add a potion effect to players near me.
     
  13. Offline

    DannyDog

    Check if the nearby Entity is an instance of a Player, then what seems to work for me is to make a new potion effect then apply to that to the entity (player).
    Example:
    new PotionEffect(PotionEffectType, duration, amplifier).apply(livingentity)
     
  14. @Irantwomiles
    Try this:
    Code:
    for(Entity entity : player.getNearbyEntities(10, 10, 10) { //Get the nearby entities
      if(entity instanceof Player) { //Check if they're a player
        entity.addPotionEffect(new PotionEffect(type,  duration(in ticks, 20 ticks = 1 second), ampflier)); //Add the potion effect
      }
    }
     
  15. Offline

    Irantwomiles

    @megamichiel i thought I tried it already I ll do it again thanks
     
Thread Status:
Not open for further replies.

Share This Page