Jump to content

What's the best way to change player speed, etc with armor?


pacguy

Recommended Posts

I want to make sets of armor that can raise or lower the players movement speed. The goal is for each armor piece to increase or decrease the players speed, and to allow the player to mix and match them to get different speeds. For a rough example, a helmet that slows you down by 1, a chest plate that slows you down by 3, and leggings that speed you up by 4 should equate to normal speed, while boots that slow or speed you up would affect your final speed. I'm hoping for more then that as well (hopefully flight and some other stuff), but I'm starting with speed.

Of course, speed and slowness potion effects are a thing, but I want to avoid them because they're a messy workaround that can screw up other things the player is trying to do, overriding the potions system. I'm making a mod here, not a datapack. And they don't offer particularly fine control anyways.

The next thing I stumbled on was player.abilities, which exposes a couple of player features that could prove useful! Thing is, I can't quite work with it properly. Having each armor piece modify the speed value instead of set it means that it needs to be reset every tick to avoid having the value simply grow or shrink forever, so I have an onPlayerTick event that runs

event.player.abilities.setWalkSpeed(0.1F);

to ensure that doesn't happen. I run this in the custom ArmorItems' onArmorTick function to modify the players speed:

		switch(armorSlot) {
		case HEAD:
			playerAbilities.setWalkSpeed(playerAbilities.getWalkSpeed() * 0.8F);
			break;
			
		case CHEST:
			playerAbilities.setWalkSpeed(playerAbilities.getWalkSpeed() * 0.5F);
			break;
			
		case LEGS:
			playerAbilities.setWalkSpeed(playerAbilities.getWalkSpeed() * 2F * 1.25F * 1.25F);
			break;
			
		case FEET:	
			playerAbilities.setWalkSpeed(playerAbilities.getWalkSpeed() * 0.8F);
			break;

Now what I have sort of works; if you open a world while wearing the armor, you start walking at the correct speed depending on what your wearing. But it never updates until you close and open the world again. The code is definitely running, logging confirms that onPlayerTick and onArmorTick are running and that player.abilties.getWalkSpeed() outputs what I'd expect it to. But it never seems to do anything in game. I tried running the suspiciously named "player.sendPlayerAbilities()" after all the changes in each function and that had no noticeable effect. I tried making all the code only run on the logical server, and that appeared to do nothing. I've toggled back and forth with having onPlayerTick run at the start or end of the tick phase. Haven't really discovered any difference.

While looking up stuff on how I should detect armor removal properly, the implementations I stumbled across used onPlayerTick, since there is no event for armor inventory changes, but they made the assumption that only one player exists, which I'd obviously want to avoid. Unfortunately, I couldn't think up a solution that worked for multiple players though... So I just had it set the speed every player tick. While I doubt it's the problem (the speed will often get stuck at a higher or lower speed value if you started the world while wearing armor, even if you take all the armor off and let onPlayerTick setWalkSpeed() to 0.1 for long periods of time), I was worried having the speed be set constantly multiple times per frame might be causing problems somehow.

I tried to look up some more stuff related to player.abilities, only to stumble on this library which says that mods that use the feature are infamous for having compatibility issues, which makes perfect sense, especially given how I'm trying to use it; I'm overwriting it every frame! And if something else decided to mess with player speed, it likely wouldn't play well with my multipliers at all...

So is there a better way to do this? Am I barking up the wrong tree? Or am I just missing something? If I've learned anything, minecraft and forge offer a lot of potential ways to do things, and the obvious one isn't always the right way of doing things...

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.