| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import net.minecraft.network.FriendlyByteBuf;
|
| 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.entity.player.Abilities;
|
| 8 |
|
| 9 | public class ClientboundPlayerAbilitiesPacket implements Packet<ClientGamePacketListener> {
|
| 10 | public static final StreamCodec<FriendlyByteBuf, ClientboundPlayerAbilitiesPacket> STREAM_CODEC = Packet.codec(
|
| 11 | ClientboundPlayerAbilitiesPacket::write, ClientboundPlayerAbilitiesPacket::new
|
| 12 | );
|
| 13 | private static final int FLAG_INVULNERABLE = 1;
|
| 14 | private static final int FLAG_FLYING = 2;
|
| 15 | private static final int FLAG_CAN_FLY = 4;
|
| 16 | private static final int FLAG_INSTABUILD = 8;
|
| 17 | private final boolean invulnerable;
|
| 18 | private final boolean isFlying;
|
| 19 | private final boolean canFly;
|
| 20 | private final boolean instabuild;
|
| 21 | private final float flyingSpeed;
|
| 22 | private final float walkingSpeed;
|
| 23 |
|
| 24 | public ClientboundPlayerAbilitiesPacket(final Abilities abilities) {
|
| 25 | this.invulnerable = abilities.invulnerable;
|
| 26 | this.isFlying = abilities.flying;
|
| 27 | this.canFly = abilities.mayfly;
|
| 28 | this.instabuild = abilities.instabuild;
|
| 29 | this.flyingSpeed = abilities.getFlyingSpeed();
|
| 30 | this.walkingSpeed = abilities.getWalkingSpeed();
|
| 31 | }
|
| 32 |
|
| 33 | private ClientboundPlayerAbilitiesPacket(final FriendlyByteBuf input) {
|
| 34 | byte bitfield = input.readByte();
|
| 35 | this.invulnerable = (bitfield & 1) != 0;
|
| 36 | this.isFlying = (bitfield & 2) != 0;
|
| 37 | this.canFly = (bitfield & 4) != 0;
|
| 38 | this.instabuild = (bitfield & 8) != 0;
|
| 39 | this.flyingSpeed = input.readFloat();
|
| 40 | this.walkingSpeed = input.readFloat();
|
| 41 | }
|
| 42 |
|
| 43 | private void write(final FriendlyByteBuf output) {
|
| 44 | byte bitfield = 0;
|
| 45 | if (this.invulnerable) {
|
| 46 | bitfield = (byte)(bitfield | 1);
|
| 47 | }
|
| 48 |
|
| 49 | if (this.isFlying) {
|
| 50 | bitfield = (byte)(bitfield | 2);
|
| 51 | }
|
| 52 |
|
| 53 | if (this.canFly) {
|
| 54 | bitfield = (byte)(bitfield | 4);
|
| 55 | }
|
| 56 |
|
| 57 | if (this.instabuild) {
|
| 58 | bitfield = (byte)(bitfield | 8);
|
| 59 | }
|
| 60 |
|
| 61 | output.writeByte(bitfield);
|
| 62 | output.writeFloat(this.flyingSpeed);
|
| 63 | output.writeFloat(this.walkingSpeed);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public PacketType<ClientboundPlayerAbilitiesPacket> type() {
|
| 68 | return GamePacketTypes.CLIENTBOUND_PLAYER_ABILITIES;
|
| 69 | }
|
| 70 |
|
| 71 | public void handle(final ClientGamePacketListener listener) {
|
| 72 | listener.handlePlayerAbilities(this);
|
| 73 | }
|
| 74 |
|
| 75 | public boolean isInvulnerable() {
|
| 76 | return this.invulnerable;
|
| 77 | }
|
| 78 |
|
| 79 | public boolean isFlying() {
|
| 80 | return this.isFlying;
|
| 81 | }
|
| 82 |
|
| 83 | public boolean canFly() {
|
| 84 | return this.canFly;
|
| 85 | }
|
| 86 |
|
| 87 | public boolean canInstabuild() {
|
| 88 | return this.instabuild;
|
| 89 | }
|
| 90 |
|
| 91 | public float getFlyingSpeed() {
|
| 92 | return this.flyingSpeed;
|
| 93 | }
|
| 94 |
|
| 95 | public float getWalkingSpeed() {
|
| 96 | return this.walkingSpeed;
|
| 97 | }
|
| 98 | }
|
| 99 |
|