Jump to content

[1.15.2] Had a problem applying potion effects to the player


mdcby

Recommended Posts

I have an eventlistener which applies random potion effects to the player, but it works properly until player is dead. After player respawns function applies only specific and non-instant effects, so i have to restart the game to have the function work properly again.

Here's the code: 

EffectInstance effect;
                switch(*random n*)
                {case *random n*: effect=Potions.*Potion_Name*.getEffects().get(0);}

LivingEntity entity=(LivingEntity) src.getTrueSource();

entity.addPotionEffect(effect) //or entity.addPotionEffect(effect.getEffectInstance()); it doesn't matter.

 

p.s.

-src.getTrueSource() is always instance of PlayerEntity

-default defined too

-i did Event listener according to forge docs, tried static and everything works till the player is dead.

-Then it works, but improperly (applies some effects with very short duration, about few ticks, maybe 1, so they don't have enough time to trigger).

 

Don't know why the problem takes place and what else i can do to make this work :(

 

Link to comment
Share on other sites

1 hour ago, diesieben07 said:

It seems like you are reusing EffectInstance instances. You cannot do that.

1) What do you mean reusing? Every time event happens i create new variables pick the player and apply the effect.

2) I also tried to call addPotionEffect directly in switch cases without any unnecessary variables and got the same issue.

Link to comment
Share on other sites

40 minutes ago, diesieben07 said:

Post your actual code instead of random snippets.

public class myEvents {
    
    @SubscribeEvent
    public void DeathPotion(LivingDeathEvent event){
        
        DamageSource src=null;
        if(event.getSource()!=null)
            src=event.getSource();


        if(src!=null)
        {
            if(src.getTrueSource() instanceof PlayerEntity)
            {

                LivingEntity entity=(LivingEntity) src.getTrueSource();
                int n=13;
                int random=(int) (Math.random()*n);
                EffectInstance killer;
                switch(random)
                {
                case 0:
                    killer=Potions.HARMING.getEffects().get(0);
                    break;
                case 1:
                    killer=Potions.SLOW_FALLING.getEffects().get(0);
                    break;
                case 2:
                    killer=Potions.POISON.getEffects().get(0);
                    break;
                case 3:
                    killer=Potions.HEALING.getEffects().get(0);
                    break;
                case 4:
                    killer=Potions.LUCK.getEffects().get(0);
                    break;
                case 5:
                    killer=Potions.LEAPING.getEffects().get(0);
                    break;
                case 6:
                    killer=Potions.SLOWNESS.getEffects().get(0);
                    break;
                case 7:
                    killer=Potions.SWIFTNESS.getEffects().get(0);
                    break;
                case 8:
                    killer=Potions.WEAKNESS.getEffects().get(0);
                    break;
                case 9:
                    killer=Potions.REGENERATION.getEffects().get(0);
                    break;
                case 10:
                    killer=Potions.STRENGTH.getEffects().get(0);
                    break;
                case 11:
                    killer=Potions.STRONG_HARMING.getEffects().get(0);
                    break;
                case 12:
                    killer=Potions.STRONG_TURTLE_MASTER.getEffects().get(0);
                    break;
                default:
                    killer=Potions.NIGHT_VISION.getEffects().get(0);
                    break;
                }
                entity.addPotionEffect(killer);
            }
        }
    }
}

 

This is registration:

myEvents m=new myEvents();
MinecraftForge.EVENT_BUS.register(m);
          

Edited by diesieben07
code formatting
Link to comment
Share on other sites

29 minutes ago, diesieben07 said:

This is the same EffectInstance every time. You need to copy it first using the respective constructor of the EffectInstance class. You can see this being done in PotionItem#onItemUseFinish.

Seems like everything is working now, thanks ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.