| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.shorts.ShortSet;
|
| 4 | import java.util.function.BiConsumer;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.SectionPos;
|
| 7 | import net.minecraft.network.FriendlyByteBuf;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 | import net.minecraft.network.protocol.Packet;
|
| 10 | import net.minecraft.network.protocol.PacketType;
|
| 11 | import net.minecraft.world.level.block.Block;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.chunk.LevelChunkSection;
|
| 14 |
|
| 15 | public class ClientboundSectionBlocksUpdatePacket implements Packet<ClientGamePacketListener> {
|
| 16 | public static final StreamCodec<FriendlyByteBuf, ClientboundSectionBlocksUpdatePacket> STREAM_CODEC = Packet.codec(
|
| 17 | ClientboundSectionBlocksUpdatePacket::write, ClientboundSectionBlocksUpdatePacket::new
|
| 18 | );
|
| 19 | private static final int POS_IN_SECTION_BITS = 12;
|
| 20 | private final SectionPos sectionPos;
|
| 21 | private final short[] positions;
|
| 22 | private final BlockState[] states;
|
| 23 |
|
| 24 | public ClientboundSectionBlocksUpdatePacket(final SectionPos sectionPos, final ShortSet changes, final LevelChunkSection section) {
|
| 25 | this.sectionPos = sectionPos;
|
| 26 | int count = changes.size();
|
| 27 | this.positions = new short[count];
|
| 28 | this.states = new BlockState[count];
|
| 29 | int i = 0;
|
| 30 |
|
| 31 | for (short packedPos : changes) {
|
| 32 | this.positions[i] = packedPos;
|
| 33 | this.states[i] = section.getBlockState(
|
| 34 | SectionPos.sectionRelativeX(packedPos), SectionPos.sectionRelativeY(packedPos), SectionPos.sectionRelativeZ(packedPos)
|
| 35 | );
|
| 36 | i++;
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | private ClientboundSectionBlocksUpdatePacket(final FriendlyByteBuf input) {
|
| 41 | this.sectionPos = SectionPos.STREAM_CODEC.decode(input);
|
| 42 | int count = input.readVarInt();
|
| 43 | this.positions = new short[count];
|
| 44 | this.states = new BlockState[count];
|
| 45 |
|
| 46 | for (int i = 0; i < count; i++) {
|
| 47 | long packedChange = input.readVarLong();
|
| 48 | this.positions[i] = (short)(packedChange & 4095L);
|
| 49 | this.states[i] = Block.BLOCK_STATE_REGISTRY.byId((int)(packedChange >>> 12));
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | private void write(final FriendlyByteBuf output) {
|
| 54 | SectionPos.STREAM_CODEC.encode(output, this.sectionPos);
|
| 55 | output.writeVarInt(this.positions.length);
|
| 56 |
|
| 57 | for (int i = 0; i < this.positions.length; i++) {
|
| 58 | output.writeVarLong((long)Block.getId(this.states[i]) << 12 | this.positions[i]);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public PacketType<ClientboundSectionBlocksUpdatePacket> type() {
|
| 64 | return GamePacketTypes.CLIENTBOUND_SECTION_BLOCKS_UPDATE;
|
| 65 | }
|
| 66 |
|
| 67 | public void handle(final ClientGamePacketListener listener) {
|
| 68 | listener.handleChunkBlocksUpdate(this);
|
| 69 | }
|
| 70 |
|
| 71 | public void runUpdates(final BiConsumer<BlockPos, BlockState> updateFunction) {
|
| 72 | BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos();
|
| 73 |
|
| 74 | for (int i = 0; i < this.positions.length; i++) {
|
| 75 | short packedPos = this.positions[i];
|
| 76 | cursor.set(this.sectionPos.relativeToBlockX(packedPos), this.sectionPos.relativeToBlockY(packedPos), this.sectionPos.relativeToBlockZ(packedPos));
|
| 77 | updateFunction.accept(cursor, this.states[i]);
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|