mojira.dev
MC-235035

Sleeping in a custom dimension with "natural" set to false causes crash

The bug

When creating a custom dimension in a datapack, setting "natural" to false and "bed_works" to true causes a fatal error to console when a player tries to sleep. Despite it being logged as a fatal error, the game doesn't crash, but players are unable to sleep.

Attached is a world that demonstrates the issue, and a datapack that adds a dimension affected by it.

Code analysis

Code analysis by @unknown can be found in this comment.

Linked issues

Attachments

Comments 2

The problem appears as a client-side fatal error, but the problem is in the server. (All names here are Yarn name) TL;DR: SleepFailureReason does not define the Text for the error and trySleep does not check if the failure reason has null text.

 net.minecraft.entity.player.PlayerEntity.SleepFailureReason defines several reasons for sleep failures. Most of them have associated text, but NOT_POSSIBLE_HERE and OTHER_PROBLEM does not, so it is an instance of the enum class with the internal value text set to null. The method for sleeping, net.minecraft.server.network.ServerPlayerEntity#trySleep, returns a Pair whose left side indicates the failure reason to be sent to the client. If the player is already sleeping or is dead, OTHER_PROBLEM is returned, and NOT_POSSIBLE_HERE if the dimension is not natural. When net.minecraft.block.BedBlock#onUse calls trySleep, there is a check that makes sure the pair's left item (failure reason) is not null, then calls ServerPlayerEntity#sendMessage which sends GameMessageS2CPacket to the client, with SleepFailureReason#toText as the message. *However, the check does not consider cases where toText returns null*, which is when the failure reason is NOT_POSSIBLE_HERE or OTHER_PROBLEM.

On client, net.minecraft.client.network.ClientPlayNetworkHandler#onGameMessage calls net.minecraft.client.gui.hud.InGameHud#addChatMessage. If the "hide matching names" options is true, it then calls InGameHud#extractSender, which then calls net.minecraft.client.font.TextVisitFactory#removeFormattingCodes and TextVisitFactory#visitFormatted where text.visit call throws NullPointerException.

I've attached the datapack from MC-249534 that changes the relevant settings in the End, which should be up-to-date to the latest dimension format as of 1.18.2. As of at least 22w12a, this does crash the singleplayer client in addition to logging a fatal error.

PrismaticVoid

gegy

Confirmed

Important

Crash, Custom Worlds

custom-worldgen, data-pack

1.17.1, 1.18.2, 22w12a

22w15a

Retrieved