Jump to content

[1.15.2][SOLVED] How to properly mount entity on another entity


Recommended Posts

Posted (edited)

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
Posted
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?

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

 

Posted
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.

  • Thanks 1

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

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity

Posted
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...

×   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.