| 1 | package net.minecraft.world.level.gameevent.vibrations;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.UUID;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.UUIDUtil;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.projectile.Projectile;
|
| 12 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 13 | import net.minecraft.world.phys.Vec3;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public record VibrationInfo(
|
| 17 | Holder<GameEvent> gameEvent, float distance, Vec3 pos, @Nullable UUID uuid, @Nullable UUID projectileOwnerUuid, @Nullable Entity entity
|
| 18 | ) {
|
| 19 | public static final Codec<VibrationInfo> CODEC = RecordCodecBuilder.create(
|
| 20 | i -> i.group(
|
| 21 | GameEvent.CODEC.fieldOf("game_event").forGetter(VibrationInfo::gameEvent),
|
| 22 | Codec.floatRange(0.0F, Float.MAX_VALUE).fieldOf("distance").forGetter(VibrationInfo::distance),
|
| 23 | Vec3.CODEC.fieldOf("pos").forGetter(VibrationInfo::pos),
|
| 24 | UUIDUtil.CODEC.lenientOptionalFieldOf("source").forGetter(o -> Optional.ofNullable(o.uuid())),
|
| 25 | UUIDUtil.CODEC.lenientOptionalFieldOf("projectile_owner").forGetter(o -> Optional.ofNullable(o.projectileOwnerUuid()))
|
| 26 | )
|
| 27 | .apply(i, (event, distance, pos, source, projectileOwner) -> new VibrationInfo(event, distance, pos, source.orElse(null), projectileOwner.orElse(null)))
|
| 28 | );
|
| 29 |
|
| 30 | public VibrationInfo(
|
| 31 | final Holder<GameEvent> gameEvent, final float distance, final Vec3 pos, final @Nullable UUID uuid, final @Nullable UUID projectileOwnerUuid
|
| 32 | ) {
|
| 33 | this(gameEvent, distance, pos, uuid, projectileOwnerUuid, null);
|
| 34 | }
|
| 35 |
|
| 36 | public VibrationInfo(final Holder<GameEvent> gameEvent, final float distance, final Vec3 pos, final @Nullable Entity entity) {
|
| 37 | this(gameEvent, distance, pos, entity == null ? null : entity.getUUID(), getProjectileOwner(entity), entity);
|
| 38 | }
|
| 39 |
|
| 40 | private static @Nullable UUID getProjectileOwner(final @Nullable Entity entity) {
|
| 41 | return entity instanceof Projectile projectile && projectile.getOwner() != null ? projectile.getOwner().getUUID() : null;
|
| 42 | }
|
| 43 |
|
| 44 | public Optional<Entity> getEntity(final ServerLevel level) {
|
| 45 | return Optional.ofNullable(this.entity).or(() -> Optional.ofNullable(this.uuid).map(level::getEntity));
|
| 46 | }
|
| 47 |
|
| 48 | public Optional<Entity> getProjectileOwner(final ServerLevel level) {
|
| 49 | return this.getEntity(level)
|
| 50 | .filter(e -> e instanceof Projectile)
|
| 51 | .map(e -> (Projectile)e)
|
| 52 | .map(Projectile::getOwner)
|
| 53 | .or(() -> Optional.ofNullable(this.projectileOwnerUuid).map(level::getEntity));
|
| 54 | }
|
| 55 | }
|
| 56 |
|