| 1 | package net.minecraft.world.level.gameevent;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import io.netty.buffer.ByteBuf;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.UUID;
|
| 10 | import java.util.function.Function;
|
| 11 | import net.minecraft.core.UUIDUtil;
|
| 12 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 13 | import net.minecraft.network.codec.StreamCodec;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.world.entity.Entity;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.phys.Vec3;
|
| 18 |
|
| 19 | public class EntityPositionSource implements PositionSource {
|
| 20 | public static final MapCodec<EntityPositionSource> CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> i.group(
|
| 22 | UUIDUtil.CODEC.fieldOf("source_entity").forGetter(EntityPositionSource::getUuid), Codec.FLOAT.optionalFieldOf("y_offset", 0.0F).forGetter(o -> o.yOffset)
|
| 23 | )
|
| 24 | .apply(i, (uuid, offset) -> new EntityPositionSource(Either.right(Either.left(uuid)), offset))
|
| 25 | );
|
| 26 | public static final StreamCodec<ByteBuf, EntityPositionSource> STREAM_CODEC = StreamCodec.composite(
|
| 27 | ByteBufCodecs.VAR_INT,
|
| 28 | EntityPositionSource::getId,
|
| 29 | ByteBufCodecs.FLOAT,
|
| 30 | o -> o.yOffset,
|
| 31 | (id, offset) -> new EntityPositionSource(Either.right(Either.right(id)), offset)
|
| 32 | );
|
| 33 | private Either<Entity, Either<UUID, Integer>> entityOrUuidOrId;
|
| 34 | private final float yOffset;
|
| 35 |
|
| 36 | public EntityPositionSource(final Entity entity, final float yOffset) {
|
| 37 | this(Either.left(entity), yOffset);
|
| 38 | }
|
| 39 |
|
| 40 | private EntityPositionSource(final Either<Entity, Either<UUID, Integer>> entityOrUuidOrId, final float yOffset) {
|
| 41 | this.entityOrUuidOrId = entityOrUuidOrId;
|
| 42 | this.yOffset = yOffset;
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public Optional<Vec3> getPosition(final Level level) {
|
| 47 | if (this.entityOrUuidOrId.left().isEmpty()) {
|
| 48 | this.resolveEntity(level);
|
| 49 | }
|
| 50 |
|
| 51 | return this.entityOrUuidOrId.left().map(entity -> entity.position().add(0.0, this.yOffset, 0.0));
|
| 52 | }
|
| 53 |
|
| 54 | private void resolveEntity(final Level level) {
|
| 55 | this.entityOrUuidOrId
|
| 56 | .map(
|
| 57 | Optional::of,
|
| 58 | uuidOrId -> Optional.ofNullable(uuidOrId.map(uuid -> level instanceof ServerLevel serverLevel ? serverLevel.getEntity(uuid) : null, level::getEntity))
|
| 59 | )
|
| 60 | .ifPresent(entity -> this.entityOrUuidOrId = Either.left(entity));
|
| 61 | }
|
| 62 |
|
| 63 | public UUID getUuid() {
|
| 64 | return this.entityOrUuidOrId.map(Entity::getUUID, uuidOrId -> uuidOrId.map(Function.identity(), id -> {
|
| 65 | throw new RuntimeException("Unable to get entityId from uuid");
|
| 66 | }));
|
| 67 | }
|
| 68 |
|
| 69 | private int getId() {
|
| 70 | return this.entityOrUuidOrId.map(Entity::getId, uuidOrId -> uuidOrId.map(uuid -> {
|
| 71 | throw new IllegalStateException("Unable to get entityId from uuid");
|
| 72 | }, Function.identity()));
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public PositionSourceType<EntityPositionSource> getType() {
|
| 77 | return PositionSourceType.ENTITY;
|
| 78 | }
|
| 79 |
|
| 80 | public static class Type implements PositionSourceType<EntityPositionSource> {
|
| 81 | @Override
|
| 82 | public MapCodec<EntityPositionSource> codec() {
|
| 83 | return EntityPositionSource.CODEC;
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public StreamCodec<ByteBuf, EntityPositionSource> streamCodec() {
|
| 88 | return EntityPositionSource.STREAM_CODEC;
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|