Jump to content

Recommended Posts

Posted (edited)

Hello,

I have successfully implemented a custom vehicle whose code is slightly based on the vanilla minecraft boat. Unfortunately, my vehicle also shares the same glitch as the boat: when the player dismounts, the vehicle under their feet sometimes gets teleported to another location, causing the player to get stuck and experience a camera "jitter" effect. 

Through some debugging, I have discovered that the server and client are not always synced regarding the position of the vehicle. Do I need to use a custom packet for this? I tried Entity:syncPacketPositionCodec but to no avail.

Edited by LeeCrafts
Posted (edited)

Found the solution. I had to discard the vehicle and respawn it whenever its passenger dismounts it.

 

Edit: MAJOR bug has been fixed. If logging out while riding the vehicle, the (player) passenger "dismounts" it, and calling Entity::discard does not actually work for some reason. The vehicle's replacement still gets added to the world, so if the player then rejoins the world, the vehicle gets duplicated. To prevent this bug, we must have an event listener that checks if the player is riding the vehicle while logging out. Therefore, a capability is needed for the vehicle.

// Events class

@SubscribeEvent
public static void playerLogOutEvent(PlayerEvent.PlayerLoggedOutEvent event) {
    Player player = event.getEntity();
    if (!player.level.isClientSide) {
        if (player.getVehicle() instanceof CustomVehicle customVehicle) {
            customVehicle.getCapability(ModCapabilities.CUSTOM_VEHICLE_CAPABILITY).ifPresent(iCustomVehicleCap -> {
                CustomVehicleCap customVehicleCap = (CustomVehicleCap) iCustomVehicleCap;
                customVehicleCap.playerPassengerHadLoggedOut = true;
            });
        }
    }
}

 

// Custom vehicle class

// This method gets called whenever a passenger dismounts the vehicle
@Override
public @NotNull Vec3 getDismountLocationForPassenger(@NotNull LivingEntity pPassenger) {
    Vec3 vec3 = super.getDismountLocationForPassenger(pPassenger);
    this.getCapability(ModCapabilities.CUSTOM_VEHICLE_CAPABILITY).ifPresent(iCustomVehicleCap -> {
        CustomVehicleCap customVehicleCap = (CustomVehicleCap) iCustomVehicleCap;
        // As said before, when the player "dismounts" the vehicle by logging off, calling discard() does not actually discard the vehicle for some reason.
        // Therefore, in this situation, another steed shouldn't replace it.
        if (!customVehicleCap.playerPassengerHadLoggedOut) {
            // this line may be slightly different depending on your vehicle entity's constructor
            CustomVehicle customVehicle = new CustomVehicle(this.getX(), this.getY(), this.getZ(), this.level);
            customVehicle.setVariant(this.getVariant());
            customVehicle.setYRot(this.getYRot());
            this.level.addFreshEntity(customVehicle);
            this.discard();
        }
        else {
            customVehicleCap.playerPassengerHadLoggedOut = false;
        }
        // this.discard();
    });
    return vec3;
}


Note: my vehicle only holds 1 passenger. If your custom vehicle can hold multiple passengers, you might need to check if there are any remaining passengers before discarding + replacing your vehicle.

Edited by LeeCrafts
  • LeeCrafts changed the title to [1.19.3, SOLVED] Custom vehicle dismount glitch

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.