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

My goal:

• A player entity shall be mounted on any Entity

 

My step:

• ray trace entity player is looking at

• I'm stuck here, mount player on the entity.

 

For testing purpose, the player will always be looking at a Minecart.

 

if (Minecraft.getInstace().objectMouseOver.getType() == RayTraceResult.Type.ENTITY) {
	Minecraft.getInstace().enqueue(() -> { // execute on the same thread
		context.getSource().asPlayer().startRiding(((EntityRayTraceResult)Minecraft.getInstace().objectMouseOver).getEntity(), true); // server side
		Minecraft.getInstace().player.startRiding(((EntityRayTraceResult)Minecraft.getInstace().objectMouseOver).getEntity(), true); // client side
		context.getSource().asPlayer().connection.sendPacket(new SSetPassengersPacket(((EntityRayTraceResult)mcInstance.objectMouseOver).getEntity())); // update client?
	}
}

I try swap ordering of the three lines that execute on main thread, have no success.

 

By no success, I mean that, either I need to re-login the game to see myself on the cart, or when I re-log in I'm not in the cart anymore.

(If server success, need to re-login to see myself on the cart)

(If client success, can't press shift to get out of the cart)

(not once that both side are success)

 

Is there anything that I'm missing?

 

 

SOLVED by change from

Minecraft.getInstace().enqueue(...)

to

context.getSource().asPlayer().getServer().enqueue(...)

 

Edited by Zen3515
Solved

  • Author
51 minutes ago, diesieben07 said:

You must do this on the server side and on the server side only. Minecraft will handle the rest.

Could you provide some code snippet? I intended to use this mod on a single player world.

If I only use 

context.getSource().asPlayer().startRiding(((EntityRayTraceResult)Minecraft.getInstace().objectMouseOver).getEntity(), true);

I'll need to re-login to the world to see the effect,

Or you're saying that I can't use Minecraft.getInstace().objectMouseOver and I must ray trace them myself?

  • Author
30 minutes ago, diesieben07 said:

That's irrelevant. Single player means you are running a server on the same computer and there is only one player connected.

The same rules apply, world manipulation (such as mounting an entity) must be done on the logical server.

 

Correct, Minecraft is the client.

Thanks, so I just enqueue it to the server, if only I knew it was this simple ?

if(mcInstance.objectMouseOver.getType() == RayTraceResult.Type.ENTITY) {
    final int mountingEntityId = ((EntityRayTraceResult)mcInstance.objectMouseOver).getEntity().getEntityId();
    this.player.getServer().enqueue(new TickDelayedTask(this.player.getServer().getTickCounter(), () -> {
        this.player.startRiding(this.player.world.getEntityByID(mountingEntityId), true); // server side
    }));
}

 

16 minutes ago, Zen3515 said:

 


if(mcInstance.objectMouseOver.getType() == RayTraceResult.Type.ENTITY) {
    final int mountingEntityId = ((EntityRayTraceResult)mcInstance.objectMouseOver).getEntity().getEntityId();
    this.player.getServer().enqueue(new TickDelayedTask(this.player.getServer().getTickCounter(), () -> {
        this.player.startRiding(this.player.world.getEntityByID(mountingEntityId), true); // server side
    }));
}

 

In order to let the client communicate with the server, you need to create your own packet.

Have a look at my mod:

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity/blob/master/src/main/java/simelectricity/essential/utils/network/MessageContainerSync.java

And register your custom packet during onCommonSetup(FMLCommonSetupEvent event) event:

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity/blob/master/src/main/java/simelectricity/essential/Essential.java

 

If a class contains calls to the Minecraft class, it will crash on the dedicated server. Client-only classes are not available on a dedicated server. In this case, use a DistExecutor (used to be called proxy):

public static CommonProxy proxy = DistExecutor.runForDist(()->()->new ClientProxy(), ()->()->new CommonProxy());

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity/blob/master/src/main/java/simelectricity/essential/ClientProxy.java

 

In your case, you should create a ClientProxy class and make a function to return the value of Minecraft.getInstance().objectMouseOver. 

One last thing, I think you should perform the raytrace on the server side instead of the client side.

My mod: SimElectricity - High Voltages, Electrical Power Transmission & Distribution

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity

  • Author
26 minutes ago, diesieben07 said:

No.... no no no. You can't do that.

The only way to communicate between client and server is packets.

Yes, I do aware of that, however, it is a single player mod, and it just work for this case

5 minutes ago, Rikka0_0 said:

In order to let the client communicate with the server, you need to create your own packet.

Have a look at my mod:

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity/blob/master/src/main/java/simelectricity/essential/utils/network/MessageContainerSync.java

And register your custom packet during onCommonSetup(FMLCommonSetupEvent event) event:

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity/blob/master/src/main/java/simelectricity/essential/Essential.java

 

If a class contains calls to the Minecraft class, it will crash on the dedicated server. Client-only classes are not available on a dedicated server. In this case, use a DistExecutor (used to be called proxy):

public static CommonProxy proxy = DistExecutor.runForDist(()->()->new ClientProxy(), ()->()->new CommonProxy());

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity/blob/master/src/main/java/simelectricity/essential/ClientProxy.java

 

In your case, you should create a ClientProxy class and make a function to return the value of Minecraft.getInstance().objectMouseOver. 

One last thing, I think you should perform the raytrace on the server side instead of the client side.

Thanks, that's really helpful :)

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.