ServerboundChangeDifficultyPacket 0x04

ServerboundChangeDifficultyPacket: Minecraft 26.3 packet (0x04 Play serverbound). Wire name 'change_difficulty', fields: difficulty, handled

Minecraft 26.3PlayServerboundnet.minecraft.network.protocol.game

Protocol slots (1)

IDdecStateDirectionRegistry
0x044PLAYServerboundGamePacketTypes.SERVERBOUND_CHANGE_DIFFICULTY: "change_difficulty"

Packet ids are positional: the id is the index of the addPacket call in the protocol builder (see ProtocolInfoBuilder.listPackets). Ids change between game versions.

Wire fields (1)

FieldType
difficultyDifficulty

Fields are the components of the packet record.

Stream codec

static final StreamCodec<ByteBuf, ServerboundChangeDifficultyPacket> STREAM_CODEC = StreamCodec.composite( Difficulty.STREAM_CODEC, ServerboundChangeDifficultyPacket::difficulty, ServerboundChangeDiff

Modern packets serialize through StreamCodecs instead of read/write methods.

Handling

Resolved on the ServerGamePacketListener packet-listener interface by void handleChangeDifficulty(ServerboundChangeDifficultyPacket serverboundchangedifficultypacket). The implementation in the game is ServerGamePacketListenerImpl.

Full Source

ServerboundChangeDifficultyPacket.java23 lines · Minecraft 26.3 · verbatimSource page ↗
1
package net.minecraft.network.protocol.game;
2
3
import io.netty.buffer.ByteBuf;
4
import net.minecraft.network.codec.StreamCodec;
5
import net.minecraft.network.protocol.Packet;
6
import net.minecraft.network.protocol.PacketType;
7
import net.minecraft.world.Difficulty;
8
9
public record ServerboundChangeDifficultyPacket(Difficulty difficulty) implements Packet<ServerGamePacketListener> {
10
	public static final StreamCodec<ByteBuf, ServerboundChangeDifficultyPacket> STREAM_CODEC = StreamCodec.composite(
11
		Difficulty.STREAM_CODEC, ServerboundChangeDifficultyPacket::difficulty, ServerboundChangeDifficultyPacket::new
12
	);
13
14
	@Override
15
	public PacketType<ServerboundChangeDifficultyPacket> type() {
16
		return GamePacketTypes.SERVERBOUND_CHANGE_DIFFICULTY;
17
	}
18
19
	public void handle(final ServerGamePacketListener listener) {
20
		listener.handleChangeDifficulty(this);
21
	}
22
}
23