[Denied]How do you set poison effect on mob/player?

Discussion in 'Plugin Development' started by mike0631, Jul 11, 2012.

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

    mike0631

    I want the victim to get poison damage.
    Code:
    Entity victim = event.getEntity();
    
    Event using:
    Code:
    EntityDamageByEntityEvent
    
    What piece of code do i use?
     
  2. Offline

    hyperslam

    Just add the poiton effect to the player/mob.
    Code:
    victim.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 100, 1));
     
  3. Offline

    Father Of Time

  4. Offline

    mike0631

    The method addPotionEffect(PotionEffect) is undefined for the type Entity
     
  5. Offline

    Father Of Time

    Did you import the classes? I believe the first two are the result of not importing the PotionEffect or PotionEffectType into the class; and the third error is the result of the first two errors.

    if you are using Eclipse, hover your mouse over the red underlined variable name and import the appropriate classes.
     
  6. Offline

    mike0631

    Code:java
    1.  
    2. @EventHandler
    3. public void HitForPosion(EntityDamageByEntityEvent event){
    4. Entity damager = event.getDamager();
    5. if (damager instanceof Player){
    6. Player player = (Player) event.getDamager();
    7. if(player.getItemInHand().getTypeId() == plugin.getConfig().getInt("pweapon")){
    8. if (player.hasPermission("Mvip.poison")){
    9. if(!poisonList.contains(player.getName())){
    10. Entity victim = event.getEntity();
    11. Location l = victim.getLocation();
    12. Location l2 = l.add(0, 3, 0);
    13.  
    14. victim.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 100, 1));
    15. poisonList.add(player.getName());
    16. timer(player.getName());
    17. }else{
    18. player.sendMessage(ChatColor.RED + "Cooldown..");
    19. }
    20. }
    21. }
    22. }
    23. }
    24.  

    When I hover over it I get:
    Add cast to 'victim'

    someone help?

    The method addPotionEffect(PotionEffect) is undefined for the type Entity

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  7. Offline

    bartboy8

    Code:
    player.getWorld().spawnCreature(player.getLocation(), EntityType.CREEPER).addPotionEffect(new PotionEffect(PotionEffectType.POISON, 5, 200));
     
  8. Offline

    Father Of Time

    It's because you are using Entity, The super class "Entity" does not have the addPotionEffect method, that is a product of the LivingEntityClass. You need to cast the victim variable to something that derives from the living entity class.

    Remember, the Entity class is a very very broad class... it covers everything from mobs, players, items, itemstacks, etc... Each of these child classes has its own collection of methods specific to it's purpose. Seeing as only living things can be damaged by poison it would make sense that the addPotionEffect is only in the LivingEntity class and everything that inherits from it; because what good would applying a poison effect do to an Item class floating on the ground?

    Hope this helps. :D
     
  9. Offline

    mike0631

    But how can i edit it in my code, can you maybe help me?
    I tried.

    Cmon..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  10. Offline

    xGhOsTkiLLeRx

    Code:
    	@EventHandler
    	public void onPlayerDamage (EntityDamageByEntityEvent e) {
    		LivingEntity living = (LivingEntity) e.getEntity();
    		living.addPotionEffect(new PotionEffect(type, duration, amplifier));
    	}
    Duration in ticks - 1 sec = 20 ticks
    amplifier = how fast the effect is applied.
    Types: http://jd.bukkit.org/apidocs/org/bukkit/potion/PotionEffectType.html
     
    mike0631 likes this.
  11. Offline

    ProFatal

    He had another post http://forums.bukkit.org/threads/giving-a-mob-player-potion-effect.85895/ and I had already answered it as well. But yea nice.
     
  12. Offline

    xGhOsTkiLLeRx

  13. Offline

    ProFatal

    Thats what I thought as well.
     
Thread Status:
Not open for further replies.

Share This Page