Jump to content

[1.19.2] [SOLVED] Cannot get passengers of an entity when there are multiple instances of the entity in the world.


Master_J

Recommended Posts

I have this if statement to check if the boolean launch = true, and a player is riding my entity every tick, and if both requirements are met then it moves the entity upwards. This works flawlessly if there is only one instance of the entity in the world. If I summon another instance of my entity, then

this.getPassengers().get(0)

returns an Array Index Out Of Bounds Exception for both entities.

 

if (ClientEvents.launch) {
    try {
        // This stops code from running until a passenger is detected
        Entity getPassenger = this.getPassengers().get(0);

        if (this.getY() < 91) {
            this.setDeltaMovement(this.getDeltaMovement().add(0.0D, 0.001D, 0.0D));
        } else {
            System.out.println("above 90 blocks");
            ClientEvents.launch = false;
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        ClientEvents.launch = false;
        System.out.println("No passenger so not moving up");
    }
}

I have tried a few other ways to get the passenger, but they all have the same issue. Most of the code for my entity is based on parts of the Boat class if that helps.

Edited by Master_J
Mark as solved
Link to comment
Share on other sites

Entity.tick() is run on both the client and server.

You can't access client side state from the server.

Only in singleplayer mode will they be in the same memory space.

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

 

You also can't store entity state in a static field.

It must be in an instance (per entity) field.

Otherwise multiple entities will write over the same shared data.

 

It's also considered bad practice to use Exceptions for non-exceptional program flow.

There are hasControllingPassenger() or getControllingPassenger() available for your use case.

Or you could check getPassengers().isEmpty() if it is not a controlling passenger.

 

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.

Link to comment
Share on other sites

12 hours ago, ChampionAsh5357 said:

Why are you doing this only on the client? This seems like logic that would best be applied on the server.

After applying the logic to the server instead of the client via network packets, everything works except I cannot seem to create a loop to continually check if the entity is above 91 blocks using a while loop, because it freezes the game. At the moment it only runs once every time the key is pressed. Any help would be appreciated.

 

This is my packet handler, and the packets are sent every time a key is pressed:

public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context ctx = supplier.get();
        ctx.enqueueWork(() -> {
            // This is the server
            ServerPlayer player = ctx.getSender();

            if (player.getRootVehicle().getY() < 91) {
                player.getRootVehicle().setDeltaMovement(player.getRootVehicle().getDeltaMovement().add(0.0D, 1.0D, 0.0D));
            }
        });
        return true;
    }

 

Link to comment
Share on other sites

You don't create a loop. Minecraft is "event driven".

You set some state inside your entity then override Entity.tick() or one of its called methods to do the check.

You should look at how LivingEntity handles Levitation in travel() - called by tick().

Levitation is a MobEffect rather than a simple flag inside the entity.

 

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.

Link to comment
Share on other sites

I am now sending a packet every tick, and updated the handle to make sure that the player is a passenger of my entity, but even when both of my if statements are true it seems to take a random amount of time from 1 to 30+ seconds to start moving my entity.

 

The updated handle:

public boolean handle(Supplier<NetworkEvent.Context> supplier) {
        NetworkEvent.Context ctx = supplier.get();
        ctx.enqueueWork(() -> {
            // Here we are on the server
            ServerPlayer player = ctx.getSender();

            if (player.getRootVehicle() instanceof RocketEntity) {
                if (player.getRootVehicle().getY() <= 91) {
                    player.getRootVehicle().setDeltaMovement(player.getRootVehicle().getDeltaMovement().add(0.0D, 0.05D, 0.0D));
                }
            }
        });
        return true;
    }

 

Link to comment
Share on other sites

If you want to modify the deltaMovement on the server for a player,

see https://forums.minecraftforge.net/topic/119307-any-idea-on-how-to-make-the-screen-shake/?do=findComment&comment=523410

 

That isn't the full story though.

 

When it comes to entities and passengers, you need to look at Entity.isControlledByLocalInstance() and LivingEntity.isEffectiveAi() and how different types of entities override these methods.

Good luck understanding how this fully works, because I don't. 🙂 

basic examples;

* entities are normally controlled by the server

* but players are normally controlled by the client

* but if an entity has the player as a passenger then it is controlled by the client/player

* unless the player is not the controlling passenger then they are both controlled by the server (e.g. riding a pig without a carrot on a stick)

Confused yet? 🙂 

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.

Link to comment
Share on other sites

40 minutes ago, warjort said:

* but if an entity has the player as a passenger then it is controlled by the client/player

* unless the player is not the controlling passenger then they are both controlled by the server (e.g. riding a pig without a carrot on a stick)

Thanks for explaining how players and entities are controlled, after looking at the Pig class and how it determined if the player was controlling it, I modified the getControllingPassenger method to make my entity unable to be controlled be the player so I can control it from the server, which fixed the issue.

 

These are the modified methods from the Pig class that I used:

@Nullable
public Entity getControllingPassenger() {
    Entity entity = this.getFirstPassenger();
    return entity != null && this.canBeControlledBy(entity) ? entity : null;
}

private boolean canBeControlledBy(Entity p_218248_) {
    return false;
}

 

Link to comment
Share on other sites

  • Master_J changed the title to [1.19.2] [SOLVED] Cannot get passengers of an entity when there are multiple instances of the entity in the world.

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.