Search in sources :

Example 1 with IdentifiableObjectStore

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;
}
Also used : DataView(org.spongepowered.api.data.DataView) ObjectStore(org.lanternpowered.server.data.io.store.ObjectStore) IdentifiableObjectStore(org.lanternpowered.server.data.io.store.IdentifiableObjectStore) IdentifiableObjectStore(org.lanternpowered.server.data.io.store.IdentifiableObjectStore) LanternEntity(org.lanternpowered.server.entity.LanternEntity)

Example 2 with IdentifiableObjectStore

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;
}
Also used : ObjectStore(org.lanternpowered.server.data.io.store.ObjectStore) IdentifiableObjectStore(org.lanternpowered.server.data.io.store.IdentifiableObjectStore) IdentifiableObjectStore(org.lanternpowered.server.data.io.store.IdentifiableObjectStore) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) LanternEntityType(org.lanternpowered.server.entity.LanternEntityType) UUID(java.util.UUID) LanternEntity(org.lanternpowered.server.entity.LanternEntity)

Aggregations

IdentifiableObjectStore (org.lanternpowered.server.data.io.store.IdentifiableObjectStore)2 ObjectStore (org.lanternpowered.server.data.io.store.ObjectStore)2 LanternEntity (org.lanternpowered.server.entity.LanternEntity)2 UUID (java.util.UUID)1 LanternEntityType (org.lanternpowered.server.entity.LanternEntityType)1 DataView (org.spongepowered.api.data.DataView)1 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1