Posted May 17, 20205 yr 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 May 17, 20205 yr by Zen3515 Solved
May 17, 20205 yr 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?
May 17, 20205 yr 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 })); }
May 17, 20205 yr 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
May 17, 20205 yr 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.