Jump to content

Trying to remove player movement in one direction


Chixen

Recommended Posts

I'm trying to force the player onto one specific line along their Minecraft world by setting their velocity to always follow that line, but the player can still walk off of it with very reduced speed, but still enough to get anywhere. My code is below but it has some complicated math to set the line, so I'll also leave a simplified version of the code with the same problem below it.

public class EventHandler {
	
	@SubscribeEvent
	public static void onMove(final LivingUpdateEvent event) {
		if(event.getEntity() instanceof PlayerEntity) {
			final double d = 1;
			final double cd = Math.cos(d);
			final double sd = Math.sin(d);
			final double td = event.getEntity().getDeltaMovement().x * cd + event.getEntity().getDeltaMovement().z * sd;
			if(event.getEntity().getDeltaMovement().z != event.getEntity().getDeltaMovement().x * Math.tan(d)) {
				event.getEntity().setDeltaMovement(td * cd, event.getEntity().getDeltaMovement().y, td * sd);
			}
		}
	}
}
public class EventHandler {
	
	@SubscribeEvent
	public static void onMove(final LivingUpdateEvent event) {
		if(event.getEntity() instanceof PlayerEntity) {
			if(event.getEntity().getDeltaMovement().x != 0) {
				event.getEntity().setDeltaMovement(0, event.getEntity().getDeltaMovement().y, event.getEntity().getDeltaMovement().z);
			}
		}
	}
}

 

Link to comment
Share on other sites

12 hours ago, Chixen said:

I'm trying to force the player onto one specific line along their Minecraft world by setting their velocity to always follow that line, but the player can still walk off of it with very reduced speed, but still enough to get anywhere. My code is below but it has some complicated math to set the line, so I'll also leave a simplified version of the code with the same problem below it.

Your error is in the first line of your event, PlayerEntity extends the LivingEntity class, but a Player is not updated with LivingUpdateEvent, use the TickEvent.Player instead

Link to comment
Share on other sites

4 hours ago, Luis_ST said:

Your error is in the first line of your event, PlayerEntity extends the LivingEntity class, but a Player is not updated with LivingUpdateEvent, use the TickEvent.Player instead

I'm not sure I understand what you mean, What exactly do i change? Do I set the event to PlayerTickEvent? Is the issue with the if statement? I tried changing the event to PlayerTickEvent but nothing changed. I also tried some tests to see if the game isn't recognizing the velocity changes, but the player velocity never exceeds over 0 for over a tick even though the player is clearly moving. (Im using a version that sets all horizontal velocity to 0 for bugtesting purposes)

Link to comment
Share on other sites

9 hours ago, Chixen said:

Do I set the event to PlayerTickEvent?

yes

9 hours ago, Chixen said:

Is the issue with the if statement?

no

I tested your code in TickEvent.PlayerTickEvent and it works, -> the player can only move from northwest to southeast, the other direction works but the speed is very slow
that would be the code you should be using:
 

@EventBusSubscriber
public class OnPlayerTickEvent {

	@SubscribeEvent
	public static void playerTick(TickEvent.PlayerTickEvent event) {
		PlayerEntity player = event.player;
		double d = 1;
		double cd = Math.cos(d);
		double sd = Math.sin(d);
		double td = player.getDeltaMovement().x * cd + player.getDeltaMovement().z * sd;
		if (player.getDeltaMovement().z != player.getDeltaMovement().x * Math.tan(d)) {
			player.setDeltaMovement(td * cd, player.getDeltaMovement().y, td * sd);
		}
	}

}

 

Link to comment
Share on other sites

34 minutes ago, Chixen said:

That is the exact problem that I am facing. I want the speed in that direction to be 0. Watch the video I sent for a visual.

okay i have tested something and found out that as you suspected it is not possible to prevent the player from moving,
the movement of the player is calculated between TickEvent.Phase#START and TickEvent.Phase#END,
therefore it is not possible to prevent the movement of the player

after thinking about your problem, I have two options that you could try:

  1. used the key input event, and cancel this if the player tries to move in the direction he shouldn't move
  2. core modding, this would work, but is the last option you should use, because with core modding you can do anything,
    really anything (also core modding is not supported here in the forum, which is why you should use the forge discord chanel "non-api-modding" for this)
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.