use of org.lanternpowered.server.data.io.store.IdentifiableObjectStore in project LanternServer by LanternPowered.
the class EntitySerializer method serialize.
@Override
public DataView serialize(LanternEntity object) {
final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
dataView.set(ID, object.getType().getId());
// noinspection unchecked
final ObjectStore<LanternEntity> store = (ObjectStore) ObjectStoreRegistry.get().get(object.getClass()).get();
store.serialize(object, dataView);
if (store instanceof IdentifiableObjectStore) {
((IdentifiableObjectStore) store).serializeUniqueId(dataView, object.getUniqueId());
}
return dataView;
}
use of org.lanternpowered.server.data.io.store.IdentifiableObjectStore in project LanternServer by LanternPowered.
the class EntitySerializer method deserialize.
@Override
public LanternEntity deserialize(DataView dataView) throws InvalidDataException {
String id0 = dataView.getString(ID).get();
final String id;
// Fast fail if the data isn't old
if (dataView.getInt(DATA_VERSION).orElse(0) < 704) {
id = fixEntityId(dataView, id0);
} else {
id = id0;
}
dataView.remove(ID);
final LanternEntityType entityType = (LanternEntityType) Sponge.getRegistry().getType(EntityType.class, id).orElseThrow(() -> new InvalidDataException("Unknown entity id: " + id));
// noinspection unchecked
final ObjectStore<LanternEntity> store = (ObjectStore) ObjectStoreRegistry.get().get(entityType.getEntityClass()).get();
final UUID uniqueId;
if (store instanceof IdentifiableObjectStore) {
uniqueId = ((IdentifiableObjectStore) store).deserializeUniqueId(dataView);
} else {
uniqueId = UUID.randomUUID();
}
// noinspection unchecked
final LanternEntity entity = (LanternEntity) entityType.getEntityConstructor().apply(uniqueId);
store.deserialize(entity, dataView);
return entity;
}
Aggregations