| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.network.FriendlyByteBuf;
|
| 6 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.network.protocol.Packet;
|
| 9 | import net.minecraft.network.protocol.PacketType;
|
| 10 | import net.minecraft.world.damagesource.DamageSource;
|
| 11 | import net.minecraft.world.damagesource.DamageType;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.phys.Vec3;
|
| 15 |
|
| 16 | public record ClientboundDamageEventPacket(int entityId, Holder<DamageType> sourceType, int sourceCauseId, int sourceDirectId, Optional<Vec3> sourcePosition)
|
| 17 | implements Packet<ClientGamePacketListener> {
|
| 18 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundDamageEventPacket> STREAM_CODEC = Packet.codec(
|
| 19 | ClientboundDamageEventPacket::write, ClientboundDamageEventPacket::new
|
| 20 | );
|
| 21 |
|
| 22 | public ClientboundDamageEventPacket(final Entity entity, final DamageSource source) {
|
| 23 | this(
|
| 24 | entity.getId(),
|
| 25 | source.typeHolder(),
|
| 26 | source.getEntity() != null ? source.getEntity().getId() : -1,
|
| 27 | source.getDirectEntity() != null ? source.getDirectEntity().getId() : -1,
|
| 28 | Optional.ofNullable(source.sourcePositionRaw())
|
| 29 | );
|
| 30 | }
|
| 31 |
|
| 32 | private ClientboundDamageEventPacket(final RegistryFriendlyByteBuf input) {
|
| 33 | this(
|
| 34 | input.readVarInt(),
|
| 35 | DamageType.STREAM_CODEC.decode(input),
|
| 36 | readOptionalEntityId(input),
|
| 37 | readOptionalEntityId(input),
|
| 38 | input.readOptional(i -> new Vec3(i.readDouble(), i.readDouble(), i.readDouble()))
|
| 39 | );
|
| 40 | }
|
| 41 |
|
| 42 | private static void writeOptionalEntityId(final FriendlyByteBuf output, final int id) {
|
| 43 | output.writeVarInt(id + 1);
|
| 44 | }
|
| 45 |
|
| 46 | private static int readOptionalEntityId(final FriendlyByteBuf input) {
|
| 47 | return input.readVarInt() - 1;
|
| 48 | }
|
| 49 |
|
| 50 | private void write(final RegistryFriendlyByteBuf output) {
|
| 51 | output.writeVarInt(this.entityId);
|
| 52 | DamageType.STREAM_CODEC.encode(output, this.sourceType);
|
| 53 | writeOptionalEntityId(output, this.sourceCauseId);
|
| 54 | writeOptionalEntityId(output, this.sourceDirectId);
|
| 55 | output.writeOptional(this.sourcePosition, (o, pos) -> {
|
| 56 | o.writeDouble(pos.x());
|
| 57 | o.writeDouble(pos.y());
|
| 58 | o.writeDouble(pos.z());
|
| 59 | });
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public PacketType<ClientboundDamageEventPacket> type() {
|
| 64 | return GamePacketTypes.CLIENTBOUND_DAMAGE_EVENT;
|
| 65 | }
|
| 66 |
|
| 67 | public void handle(final ClientGamePacketListener listener) {
|
| 68 | listener.handleDamageEvent(this);
|
| 69 | }
|
| 70 |
|
| 71 | public DamageSource getSource(final Level level) {
|
| 72 | if (this.sourcePosition.isPresent()) {
|
| 73 | return new DamageSource(this.sourceType, this.sourcePosition.get());
|
| 74 | }
|
| 75 |
|
| 76 | Entity cause = level.getEntity(this.sourceCauseId);
|
| 77 | Entity direct = level.getEntity(this.sourceDirectId);
|
| 78 | return new DamageSource(this.sourceType, direct, cause);
|
| 79 | }
|
| 80 | }
|
| 81 |
|