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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Please see https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/ for information on how to post your log correctly.
    • Hello!  The detailed description of how you got to where you are is certainly valuable.  But, at the end of the day (well, any time of the day actually), it is going to be the actual logs that going to provide the necessary details to hopefully solve your startup issue. Part of me wonders if you have installed a client-only mod on a dedicated server.  But I may very well be wrong, and it will be the logs that will tell that story.
    • Hello there! I didn't quite know where to go regarding this, but ended up deciding to post it here. I have been running a forge server with around 200 mods for me and some friends to play on casually, but have recently started to get an issue when booting the server. This all started after I decided to add some new mods to the server. Like usual, I add a mod, test run the server for any issues, and if all is well, I'll add a next one and so on until I have added all that I wanted to. After doing so, in all test runs, it all seemed to work just fine. However, the next day, after trying to boot the server, I kept getting an error regarding java.lang.NullPointerException, towards one of the mods I had recently added. So far so good, I removed the mod that was causing the issue, started up the server again, and here in when things took a turn for the worse. I received another java.lang.NullPointerException null error that wouldn't allow me to boot the server, but this time with a mod that wasn't part of the new ones I had recently added. I found this weird, but nonetheless, I removed it thinking it might be causing some conflicts with some of the new ones. Afterwards, booting the server again proved to be impossible, as it gave me another java.lang.NullPointerException null error with the 3rd mod I had ever installed on the server! This mod was there since the start, it added some biomes and had been just fine so far. This turn of events made me remove all the newer mods I had recently added in hopes to fix this whole ordeal, but alas, to no avail. Same error, with that same biome mod that had been there since day one. Reluctantly, I removed the biome mod, booted the server, and voila! The server was running, although without a major mod that had always been there to begin with. As I do not wish to part ways with this mod, specially since it had been working so far without any issues, I tried to bring everything back to how it was before I added those new mods, but kept on getting the same java.lang.NullPointerException null error for the biome mod. Even adding the newer mods won't cause me this error, with exception of the one that started it all, which I find quite odd since the mods I had been using without any issues are now giving me the same error the newer one that started it all gave me. Now, I have checked that everything is up to date regarding the mods, forge (forge-1.20.1-47.3.12) and java. The modpack runs perfectly fine when I start Minecraft itself, and play singleplayer, or even when I open a LAN world, everything works. Everything aside from the server. From what I could gather, this java.lang.NullPointerException null error would point to a missing value of sorts, for an item perhaps, within the mod that is causing the error, but aside from removing the whole mod, I lack the knowledge on how to fix this. With this in mind, if anyone would be so kind as to shine some light into this situation, with a way to fix all this blunder, I would be most grateful!
    • If you want a mana GUI, call Minecraft.getInstance().setScreen(new ManaScreen()); somewhere display your GUI. I would recommend creating a keybind and listening to key events, and if your key bind is pressed down, running that code.
    • You are creating an entire new screen, or GUI. What you probably want is an Overlay.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.