Jump to content

[1.7.10]How to modify Player Speed


DanielDawn

Recommended Posts

So I'd like to make an item called running boots, where when you start sprinting it gives you a speed boost(Not the speed effect), and then through an event handler it removes the added speed, or sets it back to normal. I'm really confused... player.capabilities.setPlayerWalkSpeed(1); is what I'm currently trying to use.

EventHandler Class (It says Iron Boots because I haven't made the class yet.)

package danieldawn.smallenhancements;

import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;

public class SeEventHandler {
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onLivingUpdate(LivingUpdateEvent e) {

if (e.entityLiving instanceof EntityPlayer) {

EntityPlayer player = (EntityPlayer) e.entityLiving;

if (player.getCurrentArmor(0) != null && player.getCurrentArmor(0).getItem() == Items.iron_boots){
		if(player.isSprinting() == true){
			player.capabilities.setPlayerWalkSpeed(2);
		}
}
if (player.getEquipmentInSlot(0) == null){
		player.capabilities.setPlayerWalkSpeed(1);
}

}

}
			}

I'm hoping to do everything in the event handler as well? I'd also like to make more boots that do the exact same thing.

With the code above, no matter what, the player moves rapidly. (Yes this is my first Event Handler)

Link to comment
Share on other sites

The best way is to use AttributeModifiers, not modifying the player walk speed. Item#getItemAttributeModifiers can return a multimap containing any number of attribute modifiers that will be applied automatically when the item is equipped or held, and then removed when the opposite is true.

 

If you don't want the modifier to apply when the item is held, you have to do a lot of extra work: set up IExtendedEntityProperties to store the last equipped item in the boots armor slot and check each tick via PlayerTick whether the item has changed. Each time it changes, remove all modifiers for the previous item and apply any for the new item, if any, then store the new item.

Link to comment
Share on other sites

Thank you, I've read that in a previous post of yours actually involving your pegasus boots. However I would not like to have it give speed while running, or give speed when not sprinting... So I guess I'll just give up on this idea for now. Thanks for your reply though.

Link to comment
Share on other sites

It's still perfectly doable - subscribe to PlayerTickEvent and do something like the following:

public static final AttributeModifier SPEED_BONUS = new AttributeModifier(args);
if (player is wearing your boots && player.isSprinting()) {
   player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).applyModifier(SPEED_BONUS);
} else {
   player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).removeModifier(SPEED_BONUS);
}

You might want to throw in a check first to see if the modifier is already applied, or save a boolean flag like #isSprinting does so you don't add/remove it constantly.

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.