Unicopia/src/main/java/com/minelittlepony/unicopia/entity/EntityReference.java

132 lines
3.8 KiB
Java
Raw Normal View History

2021-03-04 22:30:43 +01:00
package com.minelittlepony.unicopia.entity;
import java.util.Optional;
2021-03-04 22:30:43 +01:00
import java.util.UUID;
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
import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.minecraft.entity.Entity;
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;
import net.minecraft.util.math.Vec3d;
2021-03-04 22:30:43 +01:00
import net.minecraft.world.World;
/**
* 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.
*
* @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 {
/**
* Server UUID of the entity used to look up the entity instance on the server.
*/
private Optional<UUID> uuid = Optional.empty();
/**
* Corresponding client id used to look up the entity instance on the client.
*/
2021-03-04 22:30:43 +01:00
private int clientId;
/**
* The last-known position of the entity.
*/
private Optional<Vec3d> pos = Optional.empty();
2021-12-29 23:59:17 +01:00
public EntityReference() {}
public EntityReference(T entity) {
set(entity);
}
public EntityReference(NbtCompound nbt) {
fromNBT(nbt);
}
public void copyFrom(EntityReference<? extends T> other) {
uuid = other.uuid;
clientId = other.clientId;
pos = other.pos;
}
2021-03-04 22:30:43 +01:00
public void set(@Nullable T entity) {
if (entity != null) {
uuid = Optional.of(entity.getUuid());
2021-08-04 15:38:03 +02:00
clientId = entity.getId();
pos = Optional.of(entity.getPos());
2021-12-27 22:14:46 +01:00
} else {
uuid = Optional.empty();
2021-12-27 22:14:46 +01:00
clientId = 0;
pos = Optional.empty();
2021-03-04 22:30:43 +01:00
}
}
2022-09-14 16:48:34 +02:00
/**
* Gets the assigned entity's UUID
*/
public Optional<UUID> getId() {
return uuid;
}
2021-12-27 13:05:31 +01:00
/**
2022-09-14 16:48:34 +02:00
* Gets the last known position of the assigned entity.
2021-12-27 13:05:31 +01:00
*/
public Optional<Vec3d> getPosition() {
return pos;
}
public boolean referenceEquals(Entity entity) {
return entity != null && entity.getUuid().equals(uuid.orElse(null));
}
2021-03-04 22:30:43 +01:00
public boolean isPresent(World world) {
2021-12-27 13:05:31 +01:00
return getOrEmpty(world).isPresent();
2021-03-04 22:30:43 +01:00
}
public void ifPresent(World world, Consumer<T> consumer) {
2021-12-27 13:05:31 +01:00
getOrEmpty(world).ifPresent(consumer);
}
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);
}
@SuppressWarnings("unchecked")
public Optional<T> getOrEmpty(World world) {
if (uuid.isPresent() && world instanceof ServerWorld serverWorld) {
return uuid.map(serverWorld::getEntity).map(e -> (T)e).filter(this::checkReference);
2021-03-04 22:30:43 +01:00
}
if (clientId != 0) {
2021-12-27 13:05:31 +01:00
return Optional.ofNullable((T)world.getEntityById(clientId)).filter(this::checkReference);
2021-03-04 22:30:43 +01:00
}
2021-12-27 13:05:31 +01:00
return Optional.empty();
}
private boolean checkReference(Entity e) {
pos = Optional.of(e.getPos());
return e instanceof PlayerEntity || !e.isRemoved();
2021-03-04 22:30:43 +01:00
}
@Override
2021-08-04 15:38:03 +02:00
public void toNBT(NbtCompound tag) {
uuid.ifPresent(uuid -> tag.putUuid("uuid", uuid));
pos.ifPresent(p -> tag.put("pos", NbtSerialisable.writeVector(p)));
2021-03-04 22:30:43 +01:00
tag.putInt("clientId", clientId);
}
@Override
2021-08-04 15:38:03 +02:00
public void fromNBT(NbtCompound tag) {
uuid = tag.containsUuid("uuid") ? Optional.of(tag.getUuid("uuid")) : Optional.empty();
2021-12-27 13:05:31 +01:00
pos = tag.contains("pos") ? Optional.ofNullable(NbtSerialisable.readVector(tag.getList("pos", NbtElement.DOUBLE_TYPE))) : Optional.empty();
2021-03-04 22:30:43 +01:00
clientId = tag.getInt("clientId");
}
}