| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import io.netty.handler.codec.DecoderException;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.network.FriendlyByteBuf;
|
| 7 | import net.minecraft.world.entity.PositionPath;
|
| 8 | import net.minecraft.world.entity.PositionStep;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | public sealed interface VecDelta permits VecDelta.Linear, VecDelta.Stepped {
|
| 13 | VecDelta ZERO = new VecDelta.Linear((short)0, (short)0, (short)0);
|
| 14 |
|
| 15 | int stepCount();
|
| 16 |
|
| 17 | boolean hasDeltaX();
|
| 18 |
|
| 19 | boolean hasDeltaZ();
|
| 20 |
|
| 21 | PositionPath decode(VecDeltaCodec positionCodec);
|
| 22 |
|
| 23 | static VecDelta read(final FriendlyByteBuf input, final int stepCount) {
|
| 24 | if (stepCount <= 0) {
|
| 25 | short xa = input.readShort();
|
| 26 | short ya = input.readShort();
|
| 27 | short za = input.readShort();
|
| 28 | return new VecDelta.Linear(xa, ya, za);
|
| 29 | }
|
| 30 |
|
| 31 | int maxSteps = input.readableBytes() / 7;
|
| 32 | if (stepCount > maxSteps) {
|
| 33 | throw new DecoderException("VecDelta with size " + stepCount + " is bigger than allowed " + maxSteps);
|
| 34 | }
|
| 35 |
|
| 36 | List<VecDelta.Stepped.DeltaStep> steps = new ArrayList<>(stepCount);
|
| 37 |
|
| 38 | for (int i = 0; i < stepCount; i++) {
|
| 39 | int ticks = input.readVarInt();
|
| 40 | short xa = input.readShort();
|
| 41 | short ya = input.readShort();
|
| 42 | short za = input.readShort();
|
| 43 | steps.add(new VecDelta.Stepped.DeltaStep(xa, ya, za, ticks));
|
| 44 | }
|
| 45 |
|
| 46 | return new VecDelta.Stepped(steps);
|
| 47 | }
|
| 48 |
|
| 49 | static void write(final FriendlyByteBuf output, final VecDelta delta) {
|
| 50 | VecDelta var2 = delta;
|
| 51 |
|
| 52 | while (true) {
|
| 53 | switch (var2) {
|
| 54 | case VecDelta.Linear(short xa, short ya, short za):
|
| 55 | output.writeShort(xa);
|
| 56 | output.writeShort(ya);
|
| 57 | output.writeShort(za);
|
| 58 | break;
|
| 59 | case VecDelta.Stepped(List var20):
|
| 60 | List var10 = var20;
|
| 61 |
|
| 62 | for (VecDelta.Stepped.DeltaStep step : var10) {
|
| 63 | output.writeVarInt(step.ticks);
|
| 64 | output.writeShort(step.xa);
|
| 65 | output.writeShort(step.ya);
|
| 66 | output.writeShort(step.za);
|
| 67 | }
|
| 68 | break;
|
| 69 | default:
|
| 70 | throw new MatchException(null, null);
|
| 71 | }
|
| 72 |
|
| 73 | return;
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | record Linear(short xa, short ya, short za) implements VecDelta {
|
| 78 | @Override
|
| 79 | public int stepCount() {
|
| 80 | return 0;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public boolean hasDeltaX() {
|
| 85 | return this.xa != 0;
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public boolean hasDeltaZ() {
|
| 90 | return this.za != 0;
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public PositionPath decode(final VecDeltaCodec positionCodec) {
|
| 95 | Vec3 pos = positionCodec.decode(this.xa, this.ya, this.za);
|
| 96 | return PositionPath.of(pos);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | record Stepped(List<VecDelta.Stepped.DeltaStep> steps) implements VecDelta {
|
| 101 | private static final int MIN_BYTES_PER_STEP = 7;
|
| 102 |
|
| 103 | @Override
|
| 104 | public int stepCount() {
|
| 105 | return this.steps.size();
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public boolean hasDeltaX() {
|
| 110 | for (VecDelta.Stepped.DeltaStep step : this.steps) {
|
| 111 | if (step.xa != 0) {
|
| 112 | return true;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | return false;
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public boolean hasDeltaZ() {
|
| 121 | for (VecDelta.Stepped.DeltaStep step : this.steps) {
|
| 122 | if (step.za != 0) {
|
| 123 | return true;
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | return false;
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public PositionPath decode(final VecDeltaCodec positionCodec) {
|
| 132 | if (this.steps.isEmpty()) {
|
| 133 | return PositionPath.of(positionCodec.getBase());
|
| 134 | }
|
| 135 |
|
| 136 | List<PositionStep> output = new ArrayList<>(this.steps.size());
|
| 137 | Vec3 originalBase = positionCodec.getBase();
|
| 138 |
|
| 139 | for (VecDelta.Stepped.DeltaStep e : this.steps) {
|
| 140 | Vec3 pos = positionCodec.decode(e.xa, e.ya, e.za);
|
| 141 | output.add(new PositionStep(pos, e.ticks));
|
| 142 | positionCodec.setBase(pos);
|
| 143 | }
|
| 144 |
|
| 145 | positionCodec.setBase(originalBase);
|
| 146 | return PositionPath.stepped(output);
|
| 147 | }
|
| 148 |
|
| 149 | public static VecDelta.@Nullable Stepped tryEncode(final VecDeltaCodec positionCodec, final List<PositionStep> steps) {
|
| 150 | if (steps.isEmpty()) {
|
| 151 | return new VecDelta.Stepped(List.of());
|
| 152 | }
|
| 153 |
|
| 154 | List<VecDelta.Stepped.DeltaStep> output = new ArrayList<>(steps.size());
|
| 155 | Vec3 originalBase = positionCodec.getBase();
|
| 156 |
|
| 157 | for (PositionStep step : steps) {
|
| 158 | Vec3 pos = step.position();
|
| 159 | long xa = positionCodec.encodeX(pos);
|
| 160 | long ya = positionCodec.encodeY(pos);
|
| 161 | long za = positionCodec.encodeZ(pos);
|
| 162 | if (VecDeltaCodec.isDeltaTooBig(xa, ya, za)) {
|
| 163 | positionCodec.setBase(originalBase);
|
| 164 | return null;
|
| 165 | }
|
| 166 |
|
| 167 | output.add(new VecDelta.Stepped.DeltaStep((short)xa, (short)ya, (short)za, step.tickOffset()));
|
| 168 | positionCodec.setBase(pos);
|
| 169 | }
|
| 170 |
|
| 171 | positionCodec.setBase(originalBase);
|
| 172 | return new VecDelta.Stepped(output);
|
| 173 | }
|
| 174 |
|
| 175 | public record DeltaStep(short xa, short ya, short za, int ticks) {
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 |
|