Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.18.1] Implementing MobEffect with variable duration


Adil Yilan
 Share

Recommended Posts

Hi,

I have enchantment named "Stun" that can be applied to weapon and that has 3 levels:

  • Level 1 - 10% chance to stun mob for 1 sec
  • Level 2 - 20% chance to stun mob for 2 secs
  • Level 3 - 30% chance to stun mob for 3 secs

Plan is to apply MobEffect named StunnedMobEffect on doPostAttack in enchantment.

StunnedMobEffect should have variable duration based on the level of enchantment that was applied on weapon.

I am trying to find an example on how to do the following:

  • How can I set duration of effect dynamically?
  • How can I track whether duration has expired?

I have found these two methods that can be overriden:
 

public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {

public boolean isDurationEffectTick(int pDuration, int pAmplifier) {

But looking at the source code of MobEffect class, I can't figure out what pAmplifier is?

Any hints / samples on this would be appreciated. :)

 

 

 

 

 

Link to comment
Share on other sites

Amplifier is like the strength of the effect, for example movement speed boost with amplifier 0 will be Speed I, and 1 will be Speed II, 2 will be Speed III.

Effect is just an...effect type, EffectInstance contains all information for the actual effect being applied to the entity (duration, amplifier...etc).

For example there's only an effect type "Speed", but for an EffectInstance it could be:

"Level 2 Speed for 10 seconds"

"Level 4 Speed for 2 seconds"...

 

The first method applyEffectTick is called when a effect is impacting the entity, the first arg is the entity that has the effect/is affected by the effect, and amplifier is the strength of the effect.

The second method isDurationEFfectTick is to check whether the effect should impact/affect the entity this tick (the effects usually don't trigger every tick!), if true, then the first method is called.

 

Edited by poopoodice
Link to comment
Share on other sites

@poopoodice Thanks!

So, assuming that I got your comment correctly, would this be a proper way to apply an effect:
 

@Override
    public void doPostAttack(LivingEntity pAttacker, Entity pTarget, int pLevel) {
        
        // IF: Code is executing on the client.
        if (pAttacker.level.isClientSide()) {
            return;
        }
        
        // IF: Entity is not living entity.
        if (!(pTarget instanceof LivingEntity)) {
            return;
        }

        // Cast entity to living entity.
        LivingEntity mob = (LivingEntity)pTarget;
        
        // IF: Player already has effect.
        if (mob.hasEffect(PlayerMobEffects.STUNNED.get())){
            return;
        }

        // Calculate hit chance.
        int hitChance = pLevel * 10;
        
        // Create random generator.
        Random random = new Random();
        
        // Get number between 0 and 99.
        int roll = random.nextInt(100);
        
        // IF: Apply on hit chance was missed.
        if (roll >= hitChance) {
            return;
        }
        
        // Calculate duration of the effect
        int duration = 20 * pLevel;

        // Create instance of effect.
        MobEffectInstance effect = new MobEffectInstance(PlayerMobEffects.STUNNED.get(), duration, pLevel);

        // Apply effect to mob.
        mob.addEffect(effect);
    }

 

I have tested the code and it works, although I am not sure if there is smarter way to apply effect.

Still need to figure out how to make mob die after it gets stunned. :D

 

  • Like 1
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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Are you using Fabric? I'm not sure if this forum would be the right place then.
    • server failed to start and gives me "Failed to load data packs, can't proceed with server load. You can either fix your data packs or reset to vanilla with --safeMode" error when i look in my logs. its updated to the latest version of forge(40.1.0), and I don't have any data packs installed as far as I know. And I will upload the full logs if asked as i dont know how to attach a document to this post.  
    • Pioneer Craft [modded] {1.18.2} {SMP} {Community} {Light Roleplay} We are Pioneer Craft and we seek you !!! We are building a community of passionate players to build, trade, and share some laughs with. Are you tired of playing alone? Or are you here seeking a server with dedicated members; a server where you are not overlooked and can voice your opinions? If you are interested in these things too, then come join us in Pioneer Craft! Our server is player built and driven. We have a custom mod pack with mods chosen by our community to suit our needs. We have designed our server for player driven roleplay! There will be four communities which will each be self-sufficient. We encourage and seek builders who want to create a beautiful world to live in. This is a whitelist server and we are looking for dedicated applicants and experienced builders. Applications are located on our website which can be accessed through the discord. Other than the application questions, we are requesting photos of your prior builds which will be taken into consideration. Please show photos of your builds in the share-your-work section of the discord. The server uses CurseForge to run. This server is for mature members (16+) . Discord (must join): https://discord.gg/c4uJvb29CN
    • Pioneer Craft [modded] {1.18.2} {SMP} {Community} {Light Roleplay} We are Pioneer Craft and we seek you !!! We are building a community of passionate players to build, trade, and share some laughs with. Are you tired of playing alone? Or are you here seeking a server with dedicated members; a server where you are not overlooked and can voice your opinions? If you are interested in these things too, then come join us in Pioneer Craft! Our server is player built and driven. We have a custom mod pack with mods chosen by our community to suit our needs. We have designed our server for player driven roleplay! There will be four communities which will each be self-sufficient. We encourage and seek builders who want to create a beautiful world to live in. This is a whitelist server and we are looking for dedicated applicants and experienced builders. Applications are located on our website which can be accessed through the discord. Other than the application questions, we are requesting photos of your prior builds which will be taken into consideration. Please show photos of your builds in the share-your-work section of the discord. The server uses CurseForge to run. This server is for mature members (16+) . Discord (must join): https://discord.gg/c4uJvb29CN 
    • Update: I didn't need capabilities. In fact, I didn't need to override applyEffectTick at all. But it seems like I need reflections. The increased hitbox size now works for both entity and block collision--with a little drawback. This is what it looks like so far: // this event handler checks every tick if the living entity has the "big" effect @SubscribeEvent public static void entitySizeChange(LivingEvent.LivingUpdateEvent event) { LivingEntity livingEntity = event.getEntityLiving(); if (livingEntity.getActiveEffectsMap() != null && livingEntity.hasEffect(ModEffects.BIG.get())) { EntityDimensions newDims = livingEntity.getDimensions(livingEntity.getPose()).scale(8.0F, 2.0F); try { // using reflection Field field = Entity.class.getDeclaredField("dimensions"); field.setAccessible(true); field.set(livingEntity, newDims); // setting the living entity's EntityDimensions EntityDimensions newEntityDimensions = (EntityDimensions) field.get(livingEntity); livingEntity.setBoundingBox(newEntityDimensions.makeBoundingBox( // setting the living entity's AABB livingEntity.getX(), livingEntity.getY(), livingEntity.getZ() )); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } }   The aforementioned drawback is that, when the effect wears off, the living entity's still keeps its bigger hitbox until it changes pose (i.e. my player crouching). Given that the EntityEvent.Size event fires whenever an entity changes pose, should I manually fire it with EVENT_BUS::post when that entity's effect wears off? If so, where? I tried manually firing EntityEvent.Size on my overridden removeAttributeModifiers(), but to no avail. 
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.