Jump to content

[1.8] How to stop the player from sprinting?


HappyKiller1O1

Recommended Posts

Alright, so I need to stop the player from accessing the sprint option while certain things are true. I can modify the players walk speed quite fine. But, when attempting to stop the player from sprinting, it works for double tapping the forward key, and pressing the sprint key. But, holding the sprint key down allows them to sprint. Any way to prevent this?

 

Here's where I do the changing of movement speed:

@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event) {
	if(event.entity instanceof EntityPlayer) {
		EntityPlayer player = (EntityPlayer)event.entity;

		ExtendedPlayer props = ExtendedPlayer.get(player);

		if(props.get(player) != null) {
			if(props.isOverEncumbered()) {
				System.out.println("Player is Overencumbered! Changing walk speed...");

				player.capabilities.setPlayerWalkSpeed(0.05F);

				player.setSprinting(false);
			}else {
				if(player.capabilities.getWalkSpeed() != 0.1F) {
					player.capabilities.setPlayerWalkSpeed(0.1F);
				}
			}
		}
	}
}

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

You should consider adding AttributeModifiers to the player's speed instead of trying to manage it directly - it will play much better with vanilla / other mods and is also pretty easy. Take a look at the vanilla Sprinting modifier (in one of the Entity classes, maybe EntityLivingBase) and change the bonus to a penalty.

Link to comment
Share on other sites

Ah yes, I read through some older threads while waiting for your reply. So, you can simply use any randomly generated UUID to store your AttributeModifier?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

I got that to work pretty flawlessly. But, I hate how the FOV changes when I lower the speed. :/ I know there is an FOVUpdateEvent, but how will it know when not to change the FOV?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Hm, so I figured that if I check my ExtendedPlayer for the method "isOverEncumbered", I can basically tell the event when I am changing the speed. Should I just make the new FOV, the FOV setting the player may have set? (Just in case they changed their default FOV)

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Sorry, looks like 1.8 moved the #getFovModifier to AbstractClientPlayer class. Take a look at it and all should become clear. See how they get the entire speed attribute value (including ALL modifiers)? Do the same, but then subtract your custom modifier value (if present) from it before doing the rest of the FOV calculation.

Link to comment
Share on other sites

Hm, I decided, considering I will not be changing how slow you are when you're overencumbered; I would just use the double that is set for the speed after the modifier. Here is what I have:

 

@SubscribeEvent
public void onFOVChanged(FOVUpdateEvent event) {
	if(event.entity != null) {
		EntityPlayer player = event.entity;

		ExtendedPlayer props = ExtendedPlayer.get(player);

		if(props.isOverEncumbered() && player.getActivePotionEffects().isEmpty()) {
			IAttributeInstance iai = player.getEntityAttribute(SharedMonsterAttributes.movementSpeed);

			double speed = iai.getAttributeValue();

			//System.out.println("Speed: " + speed);
			//System.out.println("Compared Speed: " + 0.06000000089406967);

			if(speed == 0.06000000089406967) {
				System.out.println("SPEED IS KEY");

				event.newfov = 1.0F;
			}
		}
	}
}

 

Although, this does help A LOT with my code and such. This does not help me stop the player from sprinting entirely. xD

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

You should most definitely NOT compare floating point values like that using equality... not to mention that there could be other attribute modifiers affecting the players speed besides yours.

 

If you really want to stop the player from sprinting, the only reliable way to do so is to setSprinting(false) every tick and on the client side unset the sprint key:

KeyBinding.setKeyBindState(mc.gameSettings.keyBindSprint.getKeyCode(), false);

Link to comment
Share on other sites

Hm, I might honestly just keep the FOV change as it is not too much of a bother. And, should I do that in my LivingUpdateEvent, or in a client side tick handler?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Gotcha. ;) Although, couldn't I just disable the key once, and re-enable it when the player is not over-encumbered? Or does it need to be disabled every tick?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

Hm, I will be using the tick handler, but would there be any way of disabling the key input for a time? I feel like if you could, it might save some processing power from constantly setting the state of the key. I may be wrong though, so thanks. :P

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

Link to comment
Share on other sites

You both seem to forget the sprint keybind is not the only way to sprint. I still use the the old method of double tapping forward. So the tick handler is the definitely the way to go, I wouldn't be overly concerned about disabling the sprint key considering you could still start sprinting via double tapping forward.

Link to comment
Share on other sites

You disable sprint by doing:

 

 player.setSprinting(false) 

 

within a tick handler. I have to set the state of the KeyBind, in order to stop the player for pressing or holding down CTRL (or whatever they have set it to) to sprint. :P

 

@coolAlias So, I wasn't able to set the key state via ClientTickEvent (ExtendedPlayer couldn't get the props right). So I just check if it is a client world in #onLivingUpdate, and if so, set the KeyBind as false when OverEncumbered.

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello, I was trying to play a MOD in my preferred language, but I see that only some items are translated, and I go to debug and I get this information (the only thing that is translated is the bestiary):   [14sep.2024 17:14:36.415] [Render thread/WARN] [net.minecraft.client.resources.language.ClientLanguage/]: Skipped language file: mowziesmobs:lang/es_es.json (com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 394 column 2 path $.config.mowziesmobs.ice_crystal_attack_multiplier) [14sep.2024 17:14:36.421] [Render thread/WARN] [net.minecraft.client.resources.language.ClientLanguage/]: Skipped language file: iceandfire:lang/es_es.json (com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1349 column 4 path $.iceandfire.sound.subtitle.dragonflute)   Is that the reason why everything is not translated? , and is there any way to fix it? Thanks
    • I got my model to render from the models renderToBuffer method. But still not quite what I want. I want to render the model from my renderer's render method. I feel that having access to the renderer and its methods will open some doors for me later down the line. //EntityRendererProvider.Context pContext = ; I want this //ToaPlayerRenderer render = new ToaPlayerRenderer(pContext, false); // if I can get the above line to work, having the methods from the renderer class would be incredibly helpful down the line RenderType rendertype = model.renderType(p.getSkinTextureLocation()); // this should be something like render.getTextureLocation() VertexConsumer vertexconsumer = buffer.getBuffer(rendertype); model.renderToBuffer(stack, vertexconsumer, paLights, 1, 1, 1, 1, 1); // I don't want the render to happen here since it doesn't use the renderer //model.render(p, 1f, pTicks, stack, buffer, paLights); I want to render the model using this It is certainly getting closer though. Probably. I am still worried that even if pContext is initialized this new instance of the renderer class will still hit me with the classic and all too familiar "can't use static method in non-static context"
    • Hello, I am learning how to create Multipart Entities and I tried creating a PartEntity based on the EnderDragonPart code. However, when I tested summoning the entity in the game, the PartEntity appeared at position x 0, y 0, z 0 within the game. I tried to make it follow the main entity, and after testing again, the part entity followed the main entity but seemed to teleport back to x 0, y 0, z 0 every tick (I'm just guessing). I don't know how to fix this can someone help me? My github https://github.com/SteveKK666/Forge-NewWorld-1.20.1/tree/master/src/main/java/net/kk/newworldmod/entity/custom Illustration  https://drive.google.com/file/d/157SPvyQCE8GcsRXyQQkD4Dyhalz6LjBn/view?usp=drive_link Sorry for my English; I’m not very good at it. 
    • its still crashing with the same message
  • Topics

×
×
  • Create New...

Important Information

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