ClientboundSetObjectivePacket 0x6C

ClientboundSetObjectivePacket: Minecraft 26.3 packet (0x6C Play clientbound). Wire name 'set_objective', fields: objective, method, handled

Minecraft 26.3PlayClientboundnet.minecraft.network.protocol.game

Protocol slots (1)

IDdecStateDirectionRegistry
0x6C108PLAYClientboundGamePacketTypes.CLIENTBOUND_SET_OBJECTIVE: "set_objective"

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 (2)

FieldType
objectiveObjective
methodint

Stream codec

static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSetObjectivePacket> STREAM_CODEC = Packet.codec( ClientboundSetObjectivePacket::write, ClientboundSetObjectivePacket::new );

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

Handling

Resolved on the ClientGamePacketListener packet-listener interface by void handleAddObjective(ClientboundSetObjectivePacket clientboundsetobjectivepacket). The implementation in the game is ClientPacketListener.

Full Source

ClientboundSetObjectivePacket.java90 lines · Minecraft 26.3 · verbatimSource page ↗
1
package net.minecraft.network.protocol.game;
2
3
import java.util.Optional;
4
import net.minecraft.network.RegistryFriendlyByteBuf;
5
import net.minecraft.network.chat.CommonComponents;
6
import net.minecraft.network.chat.Component;
7
import net.minecraft.network.chat.ComponentSerialization;
8
import net.minecraft.network.chat.numbers.NumberFormat;
9
import net.minecraft.network.chat.numbers.NumberFormatTypes;
10
import net.minecraft.network.codec.StreamCodec;
11
import net.minecraft.network.protocol.Packet;
12
import net.minecraft.network.protocol.PacketType;
13
import net.minecraft.world.scores.Objective;
14
import net.minecraft.world.scores.criteria.ObjectiveCriteria;
15
16
public class ClientboundSetObjectivePacket implements Packet<ClientGamePacketListener> {
17
	public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSetObjectivePacket> STREAM_CODEC = Packet.codec(
18
		ClientboundSetObjectivePacket::write, ClientboundSetObjectivePacket::new
19
	);
20
	public static final int METHOD_ADD = 0;
21
	public static final int METHOD_REMOVE = 1;
22
	public static final int METHOD_CHANGE = 2;
23
	private final String objectiveName;
24
	private final Component displayName;
25
	private final ObjectiveCriteria.RenderType renderType;
26
	private final Optional<NumberFormat> numberFormat;
27
	private final int method;
28
29
	public ClientboundSetObjectivePacket(final Objective objective, final int method) {
30
		this.objectiveName = objective.getName();
31
		this.displayName = objective.getDisplayName();
32
		this.renderType = objective.getRenderType();
33
		this.numberFormat = Optional.ofNullable(objective.numberFormat());
34
		this.method = method;
35
	}
36
37
	private ClientboundSetObjectivePacket(final RegistryFriendlyByteBuf input) {
38
		this.objectiveName = input.readUtf();
39
		this.method = input.readByte();
40
		if (this.method != 0 && this.method != 2) {
41
			this.displayName = CommonComponents.EMPTY;
42
			this.renderType = ObjectiveCriteria.RenderType.INTEGER;
43
			this.numberFormat = Optional.empty();
44
		} else {
45
			this.displayName = ComponentSerialization.TRUSTED_STREAM_CODEC.decode(input);
46
			this.renderType = input.readEnum(ObjectiveCriteria.RenderType.class);
47
			this.numberFormat = NumberFormatTypes.OPTIONAL_STREAM_CODEC.decode(input);
48
		}
49
	}
50
51
	private void write(final RegistryFriendlyByteBuf output) {
52
		output.writeUtf(this.objectiveName);
53
		output.writeByte(this.method);
54
		if (this.method == 0 || this.method == 2) {
55
			ComponentSerialization.TRUSTED_STREAM_CODEC.encode(output, this.displayName);
56
			output.writeEnum(this.renderType);
57
			NumberFormatTypes.OPTIONAL_STREAM_CODEC.encode(output, this.numberFormat);
58
		}
59
	}
60
61
	@Override
62
	public PacketType<ClientboundSetObjectivePacket> type() {
63
		return GamePacketTypes.CLIENTBOUND_SET_OBJECTIVE;
64
	}
65
66
	public void handle(final ClientGamePacketListener listener) {
67
		listener.handleAddObjective(this);
68
	}
69
70
	public String getObjectiveName() {
71
		return this.objectiveName;
72
	}
73
74
	public Component getDisplayName() {
75
		return this.displayName;
76
	}
77
78
	public int getMethod() {
79
		return this.method;
80
	}
81
82
	public ObjectiveCriteria.RenderType getRenderType() {
83
		return this.renderType;
84
	}
85
86
	public Optional<NumberFormat> getNumberFormat() {
87
		return this.numberFormat;
88
	}
89
}
90