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

109 lines
3 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;
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;
public class EntityReference<T extends Entity> implements NbtSerialisable {
private Optional<UUID> uuid = Optional.empty();
2021-03-04 22:30:43 +01:00
private int clientId;
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<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
}
}
public Optional<UUID> getId() {
return uuid;
}
2021-12-27 13:05:31 +01:00
/**
* Gets the position the last known position of the assigned entity.
*/
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
}
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) {
return uuid.map(((ServerWorld)world)::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.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");
}
}