Jump to content

Recommended Posts

Posted (edited)

I'm trying to make a set of armor that makes the player 50% faster for every armor piece they're wearing. But first I needed to be able to change the player's speed in general. So I made a simple event handler that speeds the player up every tick: 

@OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
        if (event.side == LogicalSide.CLIENT) {
            event.player.abilities.setWalkSpeed(1f);
        }
    }

This works and it gives the player an insane speed boost without any conditions. What's confusing me is that PlayerEntity#abilities#setWalkSpeed() doesn't work anymore as soon as I add any logic to the handler. It doesn't even need to influence the method call. here's an example that doesn't work anymore:

@OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event) {

        if (event.side == LogicalSide.CLIENT) {

            PlayerEntity player = (PlayerEntity) event.player;
            float walk_speed = player.abilities.getWalkSpeed();
            float speed_bonus = 0.1f;
            for (ItemStack stack : player.getArmorInventoryList()) {
                if (stack.getItem() instanceof DynamoCoreArmorItem) {
                    speed_bonus += 0.05f;
                }
            }
            System.out.println(speed_bonus);

            event.player.abilities.setWalkSpeed(1f);
        }
    }

As you see all I did was add some code before calling PlayerEntity#abilities#setWalkSpeed(). The call itsself is exactly the same as before. But for some reason the second example doesn't work.

There are no errors. 

Can anyone explain this to me?

Edited by FenrisFox86
  • FenrisFox86 changed the title to [1.16.4] Modifying player speed in an event
Posted
@OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public static void onPlayerTick(TickEvent.PlayerTickEvent event) {

        if (event.side == LogicalSide.CLIENT) {
            
            event.player.abilities.setWalkSpeed(1f);
            event.player.sendPlayerAbilities();
        }
    }

I already tried this. Just as in the code shown in my second example, the setWalkSpeed method call doesn't work when I add this line of code. 

Posted
  On 7/6/2021 at 5:16 PM, FenrisFox86 said:

I already tried this. Just as in the code shown in my second example, the setWalkSpeed method call doesn't work when I add this line of code.

Expand  

you also can try to use an AttributeModifier (Attributes#MOVEMENT_SPEED), use the ArmorItem as an example
or try to manipulate the player's motion, but that's tricky

  • Thanks 1
Posted

I now solved it using Attribute Modifiers as Luis_ST suggested. this is my implementation:

@SubscribeEvent
    public static void onLivingArmorEquip(LivingEquipmentChangeEvent event) {
        if (!event.getEntityLiving().getEntityWorld().isRemote()) {
            LivingEntity living = event.getEntityLiving();
            living.getAttributeManager().createInstanceIfAbsent(Attributes.MOVEMENT_SPEED);
            ModifiableAttributeInstance attribute = living.getAttribute(Attributes.MOVEMENT_SPEED);

            for (AttributeModifier modifier : attribute.getModifierListCopy()) {
                if (modifier.getName().equals("dynamo_speed")) {
                    attribute.removeModifier(modifier);
                }
            }

            for (ItemStack stack : living.getArmorInventoryList()) {
                if (stack.getItem() instanceof DynamoCoreArmorItem) {
                    attribute.applyNonPersistentModifier(
                            new AttributeModifier("dynamo_speed", 0.2f, AttributeModifier.Operation.MULTIPLY_TOTAL));
                }
            }
        }
    }

The PlayerEntity#abilities method could've also interferred with other mods so I'm glad that it worked this way. 

  • FenrisFox86 changed the title to (Solved) [1.16.4] Modifying player speed based on Armor

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • UPDATE: this seems to be an Arch-specific issue. Switching to Ubuntu server fixed EVERYTHING.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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