Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hey so im adding stats that players can have based on items they are wearing and a class that they choose, and one stat is Agility and I want it to increase movement speed.

 

Now looking at the villager mob I see they have a line called: getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.5d);

 

which I think makes them slower, as tests with the player show higher numbers mean moving faster, however putting this into my "setAgility" method just makes the screen seizure as the base value is being set every tick, and my attempts to counter act this fail. So how do I passively increase a players movement speed based on their agility stat, and do so properly?

 

here is my current code snippet in my extended player class:

public void setAgility(int value)
{
	this.agility = MathHelper.clamp_int(value + currentClass.getAgi(), -20, 20);
	if (player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getBaseValue() != 2)
	{
		player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(2);
		System.out.println("set speed");			
	}
	//System.out.println(player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue());
}

 

Now this method is called in the LivingUpdateEvent event so I'm hoping that this effects server and client side, as effects of my other events work when I alter this. Am I just changing the player stat in the wrong place? PS the move speed does work even if the screen is warping in and out I did a test MP and the players zoom around (of course once I fix this the insane test numbers will be handled in a mathHelper.clamp)

I think you need to only set it when it's first equipped and unset it when it's unequipped. If you are using a custom slot, look in the Slot class for clues.

Kain

  • Author

Ok so doing so makes it do it once, however the speed boost doesn't stay.

 

How can I make it set the players movespeed and be done with it untill the next stat change?

 

Nvm was setting the speed in the wrong place, aka client side.

 

EDIT 2:

ok so to have the speed effect applied one has to leave and reload the world, and changing the stat resets it back to default walk speed..

 

if (props.getAgility() != (agiMod - props.getClassModifers()[1]))
			{
				 props.setAgility(agiMod);
				 player.getEntityAttribute(SharedMonsterAttributes.movementSpeed)
				 .setBaseValue(MathHelper.clamp_double(SharedMonsterAttributes.movementSpeed.getDefaultValue() + props.getAgility() / 125, 0.45d, 0.7d));

				 System.out.println(player.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue());
			}

 

Do I have to use a command to refresh the player?

Walking speed is defined in net.minecraft.entity.player.PlayerCapabilities and it's pretty useful and is normally saved with the player data.

    public void writeCapabilitiesToNBT(NBTTagCompound p_75091_1_)
    {
        NBTTagCompound nbttagcompound1 = new NBTTagCompound();
        nbttagcompound1.setBoolean("invulnerable", this.disableDamage);
        nbttagcompound1.setBoolean("flying", this.isFlying);
        nbttagcompound1.setBoolean("mayfly", this.allowFlying);
        nbttagcompound1.setBoolean("instabuild", this.isCreativeMode);
        nbttagcompound1.setBoolean("mayBuild", this.allowEdit);
        nbttagcompound1.setFloat("flySpeed", this.flySpeed);
        nbttagcompound1.setFloat("walkSpeed", this.walkSpeed);
        p_75091_1_.setTag("abilities", nbttagcompound1);
    }

    public void readCapabilitiesFromNBT(NBTTagCompound p_75095_1_)
    {
        if (p_75095_1_.hasKey("abilities", 10))
        {
            NBTTagCompound nbttagcompound1 = p_75095_1_.getCompoundTag("abilities");
            this.disableDamage = nbttagcompound1.getBoolean("invulnerable");
            this.isFlying = nbttagcompound1.getBoolean("flying");
            this.allowFlying = nbttagcompound1.getBoolean("mayfly");
            this.isCreativeMode = nbttagcompound1.getBoolean("instabuild");

            if (nbttagcompound1.hasKey("flySpeed", 99))
            {
                this.flySpeed = nbttagcompound1.getFloat("flySpeed");
                this.walkSpeed = nbttagcompound1.getFloat("walkSpeed");
            }

            if (nbttagcompound1.hasKey("mayBuild", 1))
            {
                this.allowEdit = nbttagcompound1.getBoolean("mayBuild");
            }
        }
    }

You can use PlayerCapabilities#setPlayerWalkSpeed(float speed), but you have to make sure to do it on both sides (for agreement). OnLivingUpdate will set attributes accordingly. These are saved automagicly.

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.