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-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-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-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
T entity = get(world);
|
2021-08-04 15:38:03 +02:00
|
|
|
return entity != null && !entity.isRemoved();
|
2021-03-04 22:30:43 +01:00
|
|
|
}
|
|
|
|
|
2021-03-06 13:53:40 +01:00
|
|
|
public void ifPresent(World world, Consumer<T> consumer) {
|
|
|
|
if (isPresent(world)) {
|
|
|
|
consumer.accept(get(world));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 22:30:43 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Nullable
|
|
|
|
public T get(World world) {
|
|
|
|
if (uuid != null && world instanceof ServerWorld) {
|
|
|
|
return (T)((ServerWorld)world).getEntity(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clientId != 0) {
|
|
|
|
return (T)world.getEntityById(clientId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
pos = tag.contains("pos") ? Optional.ofNullable(NbtSerialisable.readVector(tag.getList("pos", 6))) : Optional.empty();
|
2021-03-04 22:30:43 +01:00
|
|
|
clientId = tag.getInt("clientId");
|
|
|
|
}
|
|
|
|
}
|