package net.minecraft.world.entity; import com.mojang.datafixers.util.Either; import com.mojang.serialization.Codec; import io.netty.buffer.ByteBuf; import java.util.Optional; import java.util.UUID; import javax.annotation.Nullable; import net.minecraft.core.UUIDUtil; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.codec.StreamCodec; import net.minecraft.server.players.OldUsersConverter; import net.minecraft.world.level.Level; import net.minecraft.world.level.entity.UUIDLookup; import net.minecraft.world.level.entity.UniquelyIdentifyable; public class EntityReference { private static final Codec> CODEC = UUIDUtil.CODEC.xmap(EntityReference::new, EntityReference::getUUID); private static final StreamCodec> STREAM_CODEC = UUIDUtil.STREAM_CODEC .map(EntityReference::new, EntityReference::getUUID); private Either entity; public static Codec> codec() { return (Codec>)CODEC; } public static StreamCodec> streamCodec() { return (StreamCodec>)STREAM_CODEC; } public EntityReference(StoredEntityType p_394409_) { this.entity = Either.right(p_394409_); } public EntityReference(UUID p_396798_) { this.entity = Either.left(p_396798_); } public UUID getUUID() { return this.entity.map(p_392547_ -> (UUID)p_392547_, UniquelyIdentifyable::getUUID); } @Nullable public StoredEntityType getEntity(UUIDLookup p_395137_, Class p_396644_) { Optional optional = this.entity.right(); if (optional.isPresent()) { StoredEntityType storedentitytype = optional.get(); if (!storedentitytype.isRemoved()) { return storedentitytype; } this.entity = Either.left(storedentitytype.getUUID()); } Optional optional1 = this.entity.left(); if (optional1.isPresent()) { StoredEntityType storedentitytype1 = this.resolve(p_395137_.getEntity(optional1.get()), p_396644_); if (storedentitytype1 != null && !storedentitytype1.isRemoved()) { this.entity = Either.right(storedentitytype1); return storedentitytype1; } } return null; } @Nullable private StoredEntityType resolve(@Nullable UniquelyIdentifyable p_396749_, Class p_391245_) { return p_396749_ != null && p_391245_.isAssignableFrom(p_396749_.getClass()) ? p_391245_.cast(p_396749_) : null; } public boolean matches(StoredEntityType p_396754_) { return this.getUUID().equals(p_396754_.getUUID()); } public void store(CompoundTag p_391781_, String p_396728_) { p_391781_.store(p_396728_, UUIDUtil.CODEC, this.getUUID()); } @Nullable public static StoredEntityType get( @Nullable EntityReference p_392004_, UUIDLookup p_397681_, Class p_397380_ ) { return p_392004_ != null ? p_392004_.getEntity(p_397681_, p_397380_) : null; } @Nullable public static EntityReference read(CompoundTag p_394255_, String p_393635_) { return (EntityReference)p_394255_.read(p_393635_, codec()).orElse(null); } @Nullable public static EntityReference readWithOldOwnerConversion( CompoundTag p_396979_, String p_395953_, Level p_395721_ ) { Optional optional = p_396979_.read(p_395953_, UUIDUtil.CODEC); return optional.isPresent() ? new EntityReference<>(optional.get()) : p_396979_.getString(p_395953_) .map(p_395444_ -> OldUsersConverter.convertMobOwnerIfNecessary(p_395721_.getServer(), p_395444_)) .map(EntityReference::new) .orElse(null); } }