| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.List;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 10 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 11 | import net.minecraft.network.codec.StreamCodec;
|
| 12 | import net.minecraft.network.protocol.Packet;
|
| 13 | import net.minecraft.network.protocol.PacketType;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.world.entity.ai.attributes.Attribute;
|
| 16 | import net.minecraft.world.entity.ai.attributes.AttributeInstance;
|
| 17 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 18 |
|
| 19 | public class ClientboundUpdateAttributesPacket implements Packet<ClientGamePacketListener> {
|
| 20 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundUpdateAttributesPacket> STREAM_CODEC = StreamCodec.composite(
|
| 21 | ByteBufCodecs.VAR_INT,
|
| 22 | ClientboundUpdateAttributesPacket::getEntityId,
|
| 23 | ClientboundUpdateAttributesPacket.AttributeSnapshot.STREAM_CODEC.apply(ByteBufCodecs.list(128)),
|
| 24 | ClientboundUpdateAttributesPacket::getValues,
|
| 25 | ClientboundUpdateAttributesPacket::new
|
| 26 | );
|
| 27 | private final int entityId;
|
| 28 | private final List<ClientboundUpdateAttributesPacket.AttributeSnapshot> attributes;
|
| 29 |
|
| 30 | public ClientboundUpdateAttributesPacket(final int entityId, final Collection<AttributeInstance> values) {
|
| 31 | this.entityId = entityId;
|
| 32 | this.attributes = Lists.newArrayList();
|
| 33 |
|
| 34 | for (AttributeInstance value : values) {
|
| 35 | this.attributes.add(new ClientboundUpdateAttributesPacket.AttributeSnapshot(value.getAttribute(), value.getBaseValue(), value.getModifiers()));
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | private ClientboundUpdateAttributesPacket(final int entityId, final List<ClientboundUpdateAttributesPacket.AttributeSnapshot> attributes) {
|
| 40 | this.entityId = entityId;
|
| 41 | this.attributes = attributes;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public PacketType<ClientboundUpdateAttributesPacket> type() {
|
| 46 | return GamePacketTypes.CLIENTBOUND_UPDATE_ATTRIBUTES;
|
| 47 | }
|
| 48 |
|
| 49 | public void handle(final ClientGamePacketListener listener) {
|
| 50 | listener.handleUpdateAttributes(this);
|
| 51 | }
|
| 52 |
|
| 53 | public int getEntityId() {
|
| 54 | return this.entityId;
|
| 55 | }
|
| 56 |
|
| 57 | public List<ClientboundUpdateAttributesPacket.AttributeSnapshot> getValues() {
|
| 58 | return this.attributes;
|
| 59 | }
|
| 60 |
|
| 61 | public record AttributeSnapshot(Holder<Attribute> attribute, double base, Collection<AttributeModifier> modifiers) {
|
| 62 | public static final StreamCodec<ByteBuf, AttributeModifier> MODIFIER_STREAM_CODEC = StreamCodec.composite(
|
| 63 | Identifier.STREAM_CODEC,
|
| 64 | AttributeModifier::id,
|
| 65 | ByteBufCodecs.DOUBLE,
|
| 66 | AttributeModifier::amount,
|
| 67 | AttributeModifier.Operation.STREAM_CODEC,
|
| 68 | AttributeModifier::operation,
|
| 69 | AttributeModifier::new
|
| 70 | );
|
| 71 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundUpdateAttributesPacket.AttributeSnapshot> STREAM_CODEC = StreamCodec.composite(
|
| 72 | Attribute.STREAM_CODEC,
|
| 73 | ClientboundUpdateAttributesPacket.AttributeSnapshot::attribute,
|
| 74 | ByteBufCodecs.DOUBLE,
|
| 75 | ClientboundUpdateAttributesPacket.AttributeSnapshot::base,
|
| 76 | MODIFIER_STREAM_CODEC.apply(ByteBufCodecs.collection(ArrayList::new)),
|
| 77 | ClientboundUpdateAttributesPacket.AttributeSnapshot::modifiers,
|
| 78 | ClientboundUpdateAttributesPacket.AttributeSnapshot::new
|
| 79 | );
|
| 80 | }
|
| 81 | }
|
| 82 |
|