Jump to content

Recommended Posts

Posted

I tried the usual:
 

player.capabilities.setPlayerWalkSpeed(speed);


No method for setPlayerWalkSpeed, and it's correct it doesn't have because is a EntityPlayerMP.
There is any way to change speed while being on server side? The topic is kinda scarce.

I tried:

PlayerCapabilities cap = ObfuscationReflectionHelper.getPrivateValue(EntityPlayer.class, player, "capabilities", "field_71075_bZ");
player.capabilities.setPlayerWalkSpeed(speed);

ObfuscationReflectionHelper.setPrivateValue(PlayerCapabilities.class, cap, speed,"walkSpeed", "field_73357_f");

 

It works as intended but the private field name changes from version to version and i would like something more maintanable(someone with forge 1.10 and i had 1.12 and the field name was the only problem).

What are my choices?

Posted
6 minutes ago, Curly_dev said:

I tried the usual:
 


player.capabilities.setPlayerWalkSpeed(speed);


No method for setPlayerWalkSpeed, and it's correct it doesn't have because is a EntityPlayerMP.
There is any way to change speed while being on server side? The topic is kinda scarce.

I tried:


PlayerCapabilities cap = ObfuscationReflectionHelper.getPrivateValue(EntityPlayer.class, player, "capabilities", "field_71075_bZ");
player.capabilities.setPlayerWalkSpeed(speed);

ObfuscationReflectionHelper.setPrivateValue(PlayerCapabilities.class, cap, speed,"walkSpeed", "field_73357_f");

 

It works as intended but the private field name changes from version to version and i would like something more maintanable(someone with forge 1.10 and i had 1.12 and the field name was the only problem).

What are my choices?

Take a look at that. im not sure if it still works in the higher versions tho.

 

https://github.com/Dragonisser/CobaltMod/blob/c2193858b867552420a0bd8b31dbdda05c867d3c/src/main/java/cobaltmod/handler/event/SpeedBootsHandler.java

  • Thanks 1
Posted (edited)

For whoever is struggling with this like i did:

 

private static final UUID setSpeedUUID = UUID.fromString("015ad548-47e6-11e8-842f-0ed5f89f718b");

double speed = 0.1D;

int operations = 0; (explained below in quote)

String name = 'anything here';
IAttributeInstance movement = player.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED); //Player is EntityPlayerMP or EntityPlayer

AttributeModifier setSpeedBonus = new AttributeModifier(setSpeedUUID, name, Math.abs(speed), operations);     
if(movement.getModifier(setSpeedUUID) != null) {					
  movement.removeAllModifiers();
}
movement.applyModifier(setSpeedBonus);

 
 

Quote

 

A modifier's operation dictates how it modifies an attribute's base value. Three operations exist:

Operation 0: Additive. Adds all of the modifiers' amounts to the current value of the attribute. For example, modifying an attribute with {Amount:2,Operation:0} and {Amount:4,Operation:0} with a Base of 3 results in 9 (3 + 2 + 4 = 9).

Operation 1: Multiplicative. Multiplies the current value of the attribute by (1 + x), where x is the sum of the modifiers' amounts. For example, modifying an attribute with {Amount:2,Operation:1} and {Amount:4,Operation:1} with a Base of 3 results in 21 (3 * (1 + 2 + 4) = 21).

Operation 2: Multiplicative. For every modifier, multiplies the current value of the attribute by (1 + x), where x is the amount of the particular modifier. Functions the same as Operation 1 if there is only a single modifier with operation 1 or 2. However, for multiple modifiers it will multiply the modifiers rather than adding them. For example, modifying an attribute with {Amount:2,Operation:2} and {Amount:4,Operation:2} with a Base of 3 results in 45 (3 * (1 + 2) * (1 + 4) = 45).[3]

The mathematical behavior is as follows: Operation 0: Increment X by Amount, Operation 1: Increment Y by X * Amount, Operation 2: Y = Y * (1 + Amount) (equivalent to Increment Y by Y * Amount). The game first sets X = Base, then executes all Operation 0 modifiers, then sets Y = X, then executes all Operation 1 modifiers, and finally executes all Operation 2 modifiers.

 

       

Edited by Curly_dev
Forgot extra code
  • Like 1

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.