2021-03-04 22:30:43 +01:00
|
|
|
package com.minelittlepony.unicopia.entity;
|
|
|
|
|
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
|
|
|
|
|
|
|
import com.minelittlepony.unicopia.util.NbtSerialisable;
|
|
|
|
|
|
|
|
import net.minecraft.entity.Entity;
|
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;
|
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;
|
|
|
|
|
|
|
|
public class EntityReference<T extends Entity> implements NbtSerialisable {
|
|
|
|
|
|
|
|
private UUID uuid;
|
|
|
|
private int clientId;
|
|
|
|
|
2021-03-05 19:52:49 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-03-04 22:30:43 +01:00
|
|
|
public void set(@Nullable T entity) {
|
|
|
|
if (entity != null) {
|
|
|
|
uuid = entity.getUuid();
|
2021-08-04 15:38:03 +02:00
|
|
|
clientId = entity.getId();
|
2021-03-05 19:52:49 +01:00
|
|
|
pos = Optional.of(entity.getPos());
|
2021-12-27 22:14:46 +01:00
|
|
|
} else {
|
|
|
|
uuid = null;
|
|
|
|
clientId = 0;
|
|
|
|
pos = Optional.empty();
|
2021-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 13:05:31 +01:00
|
|
|
/**
|
|
|
|
* Gets the position the last known position of the assigned entity.
|
|
|
|
*/
|
2021-03-05 19:52:49 +01:00
|
|
|
public Optional<Vec3d> getPosition() {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public Optional<T> getOrEmpty(World world) {
|
2021-03-04 22:30:43 +01:00
|
|
|
if (uuid != null && world instanceof ServerWorld) {
|
2021-12-27 13:05:31 +01:00
|
|
|
return Optional.ofNullable((T)((ServerWorld)world).getEntity(uuid)).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.isRemoved();
|
2021-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-08-04 15:38:03 +02:00
|
|
|
public void toNBT(NbtCompound tag) {
|
2021-03-04 22:30:43 +01:00
|
|
|
if (uuid != null) {
|
|
|
|
tag.putUuid("uuid", uuid);
|
|
|
|
}
|
2021-03-05 19:52:49 +01:00
|
|
|
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) {
|
2021-03-05 19:52:49 +01:00
|
|
|
uuid = tag.containsUuid("uuid") ? tag.getUuid("uuid") : null;
|
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");
|
|
|
|
}
|
|
|
|
}
|