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
6 minutes ago, FenrisFox86 said:

Uhmm... Sorry, but I can't find any method like that. Am I just bein' stupid right now?

you still use MCP mappings, used PlayerEntity#sendPlayerAbilities

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
17 minutes ago, 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.

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



×
×
  • Create New...

Important Information

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