FenrisFox86 Posted July 6, 2021 Share Posted July 6, 2021 (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 July 6, 2021 by FenrisFox86 Quote Link to comment Share on other sites More sharing options...
Luis_ST Posted July 6, 2021 Share Posted July 6, 2021 try to call PlayerEntity#onUpdateAbilities after change the walkSpeed Quote Link to comment Share on other sites More sharing options...
FenrisFox86 Posted July 6, 2021 Author Share Posted July 6, 2021 Uhmm... Sorry, but I can't find any method like that. Am I just bein' stupid right now? Quote Link to comment Share on other sites More sharing options...
Luis_ST Posted July 6, 2021 Share Posted July 6, 2021 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 Quote Link to comment Share on other sites More sharing options...
FenrisFox86 Posted July 6, 2021 Author Share Posted July 6, 2021 @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. Quote Link to comment Share on other sites More sharing options...
Luis_ST Posted July 6, 2021 Share Posted July 6, 2021 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 1 Quote Link to comment Share on other sites More sharing options...
FenrisFox86 Posted July 6, 2021 Author Share Posted July 6, 2021 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.