Solved Need help adding potion effect to a player when hit by a zombie

Discussion in 'Plugin Development' started by Calebizzthaman, Jan 24, 2016.

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

    Calebizzthaman

    Hi! I'm trying to code a plugin and part of it is where a player is hit by a zombie it'll give the player poison for a certain amount of time. I can't seem to get it to work. I've also registered my event. I have no clue to why it isn't working! Can someone help?

    Code:
        public void onZombieHitPlayer(EntityDamageByEntityEvent e)
        {
            Entity entity = e.getEntity();
            Entity damager = e.getDamager();
            int poisontime = this.getConfig().getInt("poisontime");
           
            if(entity instanceof Player && damager instanceof Zombie )
            {
                 ((LivingEntity) e.getEntity()).addPotionEffect(new PotionEffect(PotionEffectType.POISON ,poisontime, 1));
            }
       
        }
     
  2. Offline

    ShowbizLocket61

    @Calebizzthaman
    Try adding @EventHandler in front. Like this:
    Code:
    @EventHandler
    public void onZombieHitPlayer(EntityDamageByEntityEvent e)
    {
     
  3. Offline

    Calebizzthaman

    I added that. Still didn't work. Then I changed it around a bit. To make the variable like this:
    Code:
    @EventHandler
    public void onZombieHitPlayer(EntityDamageByEntityEvent e)
    {
    LivingEntity entity = (LivingEntity) e.getEntity();
    
    }
    Still not working.
     
  4. Offline

    mythbusterma

  5. Offline

    ShowbizLocket61

    @Calebizzthaman
    1. Do you have an integer assigned in config to "poisontime"?
    2. Do you see any errors in console when you get hit by zombies?
    3. Can we see the line when you register your events?
     
  6. Offline

    Calebizzthaman

    I even added a time in the code instead of the config variable "poisontime". Still doesn't work. And no errors in console.

    Code:
    public void onEnable()
        {
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
  7. Offline

    WolfMage1

    A question which I am surprised why nobody has asked yet,

    Is it Enabled?
     
    Areoace likes this.
  8. Offline

    JoaoBM

    @Calebizzthaman Shouldn't you check if the Entity is a LivingEntity before casting?
     
  9. Offline

    Areoace

    I always thought it goes if (x instance of y) X x = (y) x; instead of X x = (y) x; if (x instanceof y)

    ?
     
  10. Offline

    WolfMage1

    You trying to be confusing? xD
     
  11. Offline

    Areoace

    I'm trying to guide OP :)
     
  12. Offline

    WolfMage1

    He'd probably be more guided if he could understand what you were saying xD

    Your code worked for me, instead of getting the poison duration from the config, try hard coding it first.

    Also, the duration I think is in ticks so if I did 20 it wouldn't be 20 seconds it would be 1, 20 ticks = 1 second

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Jan 25, 2016
  13. Offline

    Calebizzthaman


    I understand about the ticks and seconds. Will try hard coding it when I get home.
     
  14. Offline

    WolfMage1

    @Areoace
    It says instance of instead of instanceof, pls fix it it's bugging me xD
     
Thread Status:
Not open for further replies.

Share This Page