2021-03-04 22:30:43 +01:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
|
|
|
|
2023-08-06 19:26:17 +02:00
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.util.Objects;
|
2021-03-05 19:52:49 +01:00
|
|
|
import java.util.Optional;
|
2021-03-04 22:30:43 +01:00
|
|
|
import java.util.UUID;
|
2021-03-06 13:53:40 +01:00
|
|
|
import java.util.function.Consumer;
|
2021-03-04 22:30:43 +01:00
|
|
|
|
2021-08-04 15:38:03 +02:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-03-04 22:30:43 +01:00
|
|
|
|
2023-08-06 19:26:17 +02:00
|
|
|
import com.minelittlepony.unicopia.ability.magic.Caster;
|
|
|
|
import com.minelittlepony.unicopia.ability.magic.Levelled;
|
2021-03-04 22:30:43 +01:00
|
|
|
import com.minelittlepony.unicopia.util.NbtSerialisable;
|
|
|
|
|
|
|
|
import net.minecraft.entity.Entity;
|
2022-01-01 23:43:21 +01:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2021-08-04 15:38:03 +02:00
|
|
|
import net.minecraft.nbt.NbtCompound;
|
2021-12-27 13:05:31 +01:00
|
|
|
import net.minecraft.nbt.NbtElement;
|
2021-03-04 22:30:43 +01:00
|
|
|
import net.minecraft.server.world.ServerWorld;
|
2023-08-06 19:26:17 +02:00
|
|
|
import net.minecraft.util.Util;
|
2021-03-05 19:52:49 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2021-03-04 22:30:43 +01:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
|
2022-09-17 22:11:24 +02:00
|
|
|
/**
|
|
|
|
* An indirect reference to an entity by its unique id.
|
|
|
|
* Used to store the 'owner' reference for certain objects that allows them to\
|
|
|
|
* remember who they belong to even when the entity has been unloaded.
|
|
|
|
*
|
2023-08-06 19:26:17 +02:00
|
|
|
* Will also remember the position and certain attributes of the owner.
|
|
|
|
*
|
2022-09-17 22:11:24 +02:00
|
|
|
* @param <T> The type of the entity this reference points to.
|
|
|
|
*/
|
2021-03-04 22:30:43 +01:00
|
|
|
public class EntityReference<T extends Entity> implements NbtSerialisable {
|
2023-08-06 19:26:17 +02:00
|
|
|
@Nullable
|
|
|
|
private EntityValues<T> reference;
|
2021-03-04 22:30:43 +01:00
|
|
|
|
2023-08-06 19:26:17 +02:00
|
|
|
private WeakReference<T> directReference = new WeakReference<>(null);
|
2021-03-05 19:52:49 +01:00
|
|
|
|
2021-12-29 23:59:17 +01:00
|
|
|
public EntityReference() {}
|
|
|
|
|
|
|
|
public EntityReference(T entity) {
|
|
|
|
set(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public EntityReference(NbtCompound nbt) {
|
|
|
|
fromNBT(nbt);
|
|
|
|
}
|
|
|
|
|
2023-08-06 19:26:17 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2022-09-17 19:13:39 +02:00
|
|
|
public void copyFrom(EntityReference<? extends T> other) {
|
2023-08-06 19:26:17 +02:00
|
|
|
this.reference = ((EntityReference<T>)other).reference;
|
|
|
|
this.directReference = new WeakReference<>(other.directReference.get());
|
2021-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
|
2023-08-06 19:26:17 +02:00
|
|
|
public boolean set(@Nullable T entity) {
|
|
|
|
this.directReference = new WeakReference<>(entity);
|
|
|
|
this.reference = entity == null ? null : new EntityValues<>(entity);
|
|
|
|
return entity != null;
|
2022-01-01 21:08:50 +01:00
|
|
|
}
|
|
|
|
|
2023-08-06 19:26:17 +02:00
|
|
|
public Optional<EntityValues<T>> getTarget() {
|
|
|
|
T value = directReference.get();
|
|
|
|
if (value != null) {
|
|
|
|
set(value);
|
|
|
|
}
|
|
|
|
return Optional.ofNullable(reference);
|
2021-03-05 19:52:49 +01:00
|
|
|
}
|
|
|
|
|
2023-08-05 18:39:56 +02:00
|
|
|
public boolean isSet() {
|
2023-08-06 19:26:17 +02:00
|
|
|
return reference != null;
|
2023-08-05 18:39:56 +02:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:03:14 +01:00
|
|
|
public boolean referenceEquals(Entity entity) {
|
2023-08-06 19:26:17 +02:00
|
|
|
return entity != null && referenceEquals(entity.getUuid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean referenceEquals(UUID uuid) {
|
|
|
|
return (reference == null ? Util.NIL_UUID : reference.uuid()).equals(uuid);
|
2022-02-23 19:03:14 +01:00
|
|
|
}
|
|
|
|
|
2023-08-06 19:26:17 +02:00
|
|
|
public boolean referenceEquals(@Nullable EntityReference<?> other) {
|
|
|
|
final EntityValues<?> st = reference;
|
|
|
|
final EntityValues<?> ot = other == null ? null : other.reference;
|
|
|
|
return st == ot || (st != null && ot != null && Objects.equals(st.uuid(), ot.uuid()));
|
2021-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 13:53:40 +01:00
|
|
|
public void ifPresent(World world, Consumer<T> consumer) {
|
2021-12-27 13:05:31 +01:00
|
|
|
getOrEmpty(world).ifPresent(consumer);
|
2021-03-06 13:53:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-04 22:30:43 +01:00
|
|
|
@Nullable
|
|
|
|
public T get(World world) {
|
2021-12-27 13:05:31 +01:00
|
|
|
return getOrEmpty(world).orElse(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<T> getOrEmpty(World world) {
|
2023-08-06 19:26:17 +02:00
|
|
|
return Optional.ofNullable(directReference.get())
|
|
|
|
.or(() -> reference == null ? Optional.empty() : reference.resolve(world))
|
|
|
|
.filter(this::set);
|
2021-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
public void toNBT(NbtCompound tag) {
|
2023-08-06 19:26:17 +02:00
|
|
|
getTarget().ifPresent(ref -> ref.toNBT(tag));
|
2021-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
public void fromNBT(NbtCompound tag) {
|
2023-08-06 19:26:17 +02:00
|
|
|
this.reference = tag.contains("uuid") ? new EntityValues<>(tag) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return getTarget().map(EntityValues::uuid).orElse(Util.NIL_UUID).hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
public record EntityValues<T extends Entity>(
|
|
|
|
UUID uuid,
|
|
|
|
Vec3d pos,
|
|
|
|
int clientId,
|
|
|
|
boolean isPlayer,
|
|
|
|
boolean isDead,
|
|
|
|
Levelled.LevelStore level,
|
|
|
|
Levelled.LevelStore corruption) {
|
|
|
|
public EntityValues(Entity entity) {
|
|
|
|
this(
|
|
|
|
entity.getUuid(),
|
|
|
|
entity.getPos(),
|
|
|
|
entity.getId(), entity instanceof PlayerEntity,
|
|
|
|
!entity.isAlive(),
|
|
|
|
Caster.of(entity).map(Caster::getLevel).map(Levelled::copyOf).orElse(Levelled.EMPTY),
|
|
|
|
Caster.of(entity).map(Caster::getCorruption).map(Levelled::copyOf).orElse(Levelled.EMPTY)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public EntityValues(NbtCompound tag) {
|
|
|
|
this(
|
|
|
|
tag.getUuid("uuid"),
|
|
|
|
NbtSerialisable.readVector(tag.getList("pos", NbtElement.DOUBLE_TYPE)),
|
|
|
|
tag.getInt("clientId"),
|
|
|
|
tag.getBoolean("isPlayer"),
|
|
|
|
tag.getBoolean("isDead"),
|
|
|
|
Levelled.fromNbt(tag.getCompound("level")),
|
|
|
|
Levelled.fromNbt(tag.getCompound("corruption"))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public Optional<T> resolve(World world) {
|
|
|
|
if (world instanceof ServerWorld serverWorld) {
|
|
|
|
return Optional.ofNullable((T)serverWorld.getEntity(uuid));
|
|
|
|
}
|
|
|
|
return Optional.ofNullable((T)world.getEntityById(clientId()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void toNBT(NbtCompound tag) {
|
|
|
|
tag.putUuid("uuid", uuid);
|
|
|
|
tag.put("pos", NbtSerialisable.writeVector(pos));
|
|
|
|
tag.putInt("clientId", clientId);
|
|
|
|
tag.putBoolean("isPlayer", isPlayer);
|
|
|
|
tag.putBoolean("isDead", isDead);
|
|
|
|
tag.put("level", level.toNbt());
|
|
|
|
tag.put("corruption", corruption.toNbt());
|
|
|
|
}
|
2021-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
}
|