| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.IntFunction;
|
| 6 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.util.ByIdMap;
|
| 9 | import net.minecraft.world.phys.Vec3;
|
| 10 |
|
| 11 | public sealed interface PositionPath permits PositionPath.Linear, PositionPath.Stepped {
|
| 12 | StreamCodec<ByteBuf, PositionPath> STREAM_CODEC = PositionPath.Type.STREAM_CODEC.dispatch(PositionPath::type, PositionPath.Type::streamCodec);
|
| 13 |
|
| 14 | Vec3 endPosition();
|
| 15 |
|
| 16 | PositionPath.Type type();
|
| 17 |
|
| 18 | static PositionPath of(final Vec3 position) {
|
| 19 | return new PositionPath.Linear(position);
|
| 20 | }
|
| 21 |
|
| 22 | static PositionPath stepped(final List<PositionStep> steps) {
|
| 23 | return new PositionPath.Stepped(steps);
|
| 24 | }
|
| 25 |
|
| 26 | record Linear(Vec3 endPosition) implements PositionPath {
|
| 27 | public static final StreamCodec<ByteBuf, PositionPath.Linear> STREAM_CODEC = Vec3.STREAM_CODEC
|
| 28 | .map(PositionPath.Linear::new, PositionPath.Linear::endPosition);
|
| 29 |
|
| 30 | @Override
|
| 31 | public PositionPath.Type type() {
|
| 32 | return PositionPath.Type.LINEAR;
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | record Stepped(Vec3 endPosition, List<PositionStep> steps) implements PositionPath {
|
| 37 | public static final StreamCodec<ByteBuf, PositionPath.Stepped> STREAM_CODEC = PositionStep.STREAM_CODEC
|
| 38 | .apply(ByteBufCodecs.list())
|
| 39 | .map(PositionPath.Stepped::new, PositionPath.Stepped::steps);
|
| 40 |
|
| 41 | public Stepped(final List<PositionStep> steps) {
|
| 42 | this(steps.getLast().position(), steps);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public PositionPath.Type type() {
|
| 47 | return PositionPath.Type.STEPPED;
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | enum Type {
|
| 52 | LINEAR(PositionPath.Linear.STREAM_CODEC),
|
| 53 | STEPPED(PositionPath.Stepped.STREAM_CODEC);
|
| 54 |
|
| 55 | public static final IntFunction<PositionPath.Type> BY_ID = ByIdMap.continuous(Enum::ordinal, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 56 | public static final StreamCodec<ByteBuf, PositionPath.Type> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Enum::ordinal);
|
| 57 | private final StreamCodec<ByteBuf, ? extends PositionPath> streamCodec;
|
| 58 |
|
| 59 | Type(final StreamCodec<ByteBuf, ? extends PositionPath> streamCodec) {
|
| 60 | this.streamCodec = streamCodec;
|
| 61 | }
|
| 62 |
|
| 63 | public StreamCodec<ByteBuf, ? extends PositionPath> streamCodec() {
|
| 64 | return this.streamCodec;
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 |
|