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

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);
			}
		}
	}
}

 

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

  • Author
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)

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);
		}
	}

}

 

  • Author
Quote

the other direction works but the speed is very slow

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.

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)

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.