Jump to content

[Solved][1.19.2] Move entity up when key pressed and player is riding it.


Recommended Posts

Posted (edited)

I am trying to make an entity move up, but it only moves up a quarter of a block and then stops. I have one packet to move it up when a key is pressed, and another packet to move it back down if there is no player riding it.

This is my code to make it move up:

public static class ClientForgeEvents {
        @SubscribeEvent
        public static void onKeyInput(InputEvent.Key event) {
            if (KeyBinding.LAUNCH_KEY.consumeClick()) {
                assert Minecraft.getInstance().player != null;
                if (Minecraft.getInstance().player.getRootVehicle() instanceof RocketEntity) {
                    launch = true;
                    ModPackets.sendToServer(new MoveRocketC2SPacket());
                }
            }
        }

        @SubscribeEvent
        public static void ticks(TickEvent.PlayerTickEvent event) {
            if (Minecraft.getInstance().player != null) {
                if (Minecraft.getInstance().player.getRootVehicle() instanceof RocketEntity) {
                    if (launch) {
                        ModPackets.sendToServer(new MoveRocketC2SPacket());
                    }
                } else {
                    launch = false;
                    ModPackets.sendToServer(new MoveRocketDownC2SPacket());
                }
            }
        }
    }

And this is the handle method for the two packets

public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context ctx = supplier.get();
        ctx.enqueueWork(() -> {
            // Here we are on the server
            ServerPlayer player = ctx.getSender();
            ServerLevel level = player.getLevel();
                if (player.getRootVehicle().getY() <= 151) {
                    player.getRootVehicle().setDeltaMovement(player.getRootVehicle().getDeltaMovement().add(0.0D, 0.05D, 0.0D));
                    player.getRootVehicle().move(MoverType.SELF, player.getRootVehicle().getDeltaMovement());
                } else if (player.getRootVehicle().getY() >= 151) {
                    player.getRootVehicle().setDeltaMovement(Vec3.ZERO);
                    player.getRootVehicle().move(MoverType.SELF, player.getRootVehicle().getDeltaMovement());
                    ModPackets.sendToPlayer(new RequestDestPlanetS2CPacket(), player);
                }
        });
        return true;
    }
public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context ctx = supplier.get();
        ctx.enqueueWork(() -> {
            // Here we are on the server
            ServerPlayer player = ctx.getSender();
            ServerLevel level = player.getLevel();
            level.getAllEntities().forEach(x -> {
                if (x instanceof RocketEntity) {
                    if (!(x.getFirstPassenger() instanceof ServerPlayer)) {
                        x.setDeltaMovement(x.getDeltaMovement().normalize().add(0.0D, -0.14D, 0.0D));
                        x.move(MoverType.SELF, x.getDeltaMovement());
                    }
                }
            });
        });
        return true;
    }

I have found that the Delta movement changes correctly, but it does not make the rocket move up.

Edited by Master_J
solved
Posted

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted

If I run the two packets on the client side, then moving up works but moving down does not. If I run them on the server side moving up doesn't work but after a few minutes it sometimes works and moving down works. If I run the moving down packet on the server and the moving up packet on the client, nothing works.

 

My entity needs to move up when it has a passenger and move down when there is no passenger.

Posted

People that don't show their code, will usually just be ignored.

And so will just posting snippets out of context unless the problem is obvious from the little code you post.

 

Did you even read the link I posted about the important of the entity's controlling passenger and how it affects where to run your logic?

If you did, why do you not show your RocketEntity so we can confirm you did it correctly?

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted
17 hours ago, warjort said:

snippets out of context unless the problem is obvious from the little code you post.

I thought the problem was only related to those functions.

 

17 hours ago, warjort said:

Did you even read the link I posted about the important of the entity's controlling passenger and how it affects where to run your logic?

I did read that post, which was why I ran the code to move the rocket up on the client as the player is a passenger, and the code to move it down on the server because it would have no passengers.

 

17 hours ago, warjort said:

show your RocketEntity

Here is my Entity class: https://github.com/MasterJ-06/Neutron_Galaxy/blob/master/src/main/java/neutrongalaxy/masterj/neutrongalaxy/entities/RocketEntity.java

But I call the methods to control the movement here in the client events for a key press:

https://github.com/MasterJ-06/Neutron_Galaxy/blob/master/src/main/java/neutrongalaxy/masterj/neutrongalaxy/events/ClientEvents.java

https://github.com/MasterJ-06/Neutron_Galaxy/blob/master/src/main/java/neutrongalaxy/masterj/neutrongalaxy/networking/packet/MoveRocketDownC2SPacket.java

Posted
Quote

I did read that post, which was why I ran the code to move the rocket up on the client as the player is a passenger, and the code to move it down on the server because it would have no passengers.

You didn't it understand then.

From from what I can see you just extend the boat so it will behave with the same rules as a boat.

Except where you have tried to add some kind of up/down control in "random" places, like sending network packets to somewhere that is probably not the controlling side?

 

I would guess a rocket should be never be directly controlled by the player and instead always by the server.

With the only player input coming from rocket controls sent to the server using network packets.

i.e. you should override it so the rocket never has a controlling passenger and you probably don't want to subclass the boat class which is designed to be steered by the player.

 

Also, this is nonsense and will crash a dedicated server:

https://github.com/MasterJ-06/Neutron_Galaxy/blob/f78700fc2e8bca23d4cf98212095c12cdf3aa7a0/src/main/java/neutrongalaxy/masterj/neutrongalaxy/entities/RocketEntity.java#L93

 

And this will perform really badly.

https://github.com/MasterJ-06/Neutron_Galaxy/blob/f78700fc2e8bca23d4cf98212095c12cdf3aa7a0/src/main/java/neutrongalaxy/masterj/neutrongalaxy/networking/packet/MoveRocketDownC2SPacket.java#L36

I don't see why you have done this in response to a network packet anyway? Shouldn't this be in the Rocket's tick() method and wouldn't normal gravity do this?

The way you have it is all players not riding rockets spam down packets at the server 20 times a second.

https://github.com/MasterJ-06/Neutron_Galaxy/blob/f78700fc2e8bca23d4cf98212095c12cdf3aa7a0/src/main/java/neutrongalaxy/masterj/neutrongalaxy/events/ClientEvents.java#L58

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted

I have looked at some other mods, and discovered that they store booleans for movement as entitydata and reference it in the tick method. After implementing this and extending the entity class instead of the boat class, I no longer need any network packets to control the movement. Everything is now working, except the rocket keeps teleporting back to the ground midflight like this: https://www.youtube.com/watch?v=HFX9ZzRyFQQ .

 

https://github.com/MasterJ-06/Neutron_Galaxy/blob/master/src/main/java/neutrongalaxy/masterj/neutrongalaxy/events/ClientEvents.java

https://github.com/MasterJ-06/Neutron_Galaxy/blob/master/src/main/java/neutrongalaxy/masterj/neutrongalaxy/entities/RocketEntity.java

Posted (edited)

You know the tick happens on both the client and server?

You have code in your tick() that could only work client side. It will crash on a dedicated server.

e.g. unconditionally sending packets to the server, referencing LocalPlayer.

 

This code only sets the data in the client. The entity data only gets sent from the server to the client when it changes on the server.

https://github.com/MasterJ-06/Neutron_Galaxy/blob/479db614313108816c32690f68c8d1eff2a7614f/src/main/java/neutrongalaxy/masterj/neutrongalaxy/events/ClientEvents.java#L42

It's not going to send the data to the server, so the server won't know the rocket is launched. You need a network packet for client -> server.

 

I haven't tried to understand the logic of what you are doing, but I would guess what is actually happening is you are only moving the rocket on the client, while on the server it isn't moving at all (it never got told about the launch).

Since the server is the real data, eventualy it will correct the client and voila, you are back on the ground.

 

Read this:

https://forge.gemwire.uk/wiki/Sides

 

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Posted

https://github.com/MasterJ-06/Neutron_Galaxy/blob/479db614313108816c32690f68c8d1eff2a7614f/src/main/java/neutrongalaxy/masterj/neutrongalaxy/events/ClientEvents.java#L31

That is also probably wrong. That "launch" is in a static field, but is clearly dependent upon a specific game/save data.

If the user switches from one save to another you will leak that data across the different saves.

You also have no mechanism to save that state when the player logs out. Maybe that is not important in this case?

 

In general, don't use static fields that are not effectively immutable.

This is only a boolean, but if you do it with other data you will likely cause memory leaks or worse.

 

If you really must use a static field (try to avoid it), have some way to reset/refresh the data when the client logs in and out of a game save.

e.g. https://github.com/MinecraftForge/MinecraftForge/blob/01846c729a21c13cb86447c4e233b83a314b6856/src/main/java/net/minecraftforge/client/event/ClientPlayerNetworkEvent.java#L75

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

  • Master_J changed the title to [Solved][1.19.2] Move entity up when key pressed and player is riding it.

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.