| 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 |
|