Search in sources :

Example 1 with ObjectStore

use of org.lanternpowered.server.data.io.store.ObjectStore 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 ObjectStore

use of org.lanternpowered.server.data.io.store.ObjectStore in project LanternServer by LanternPowered.

the class TileEntitySerializer method serialize.

@Override
public DataView serialize(LanternTileEntity object) {
    final DataView dataView = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED);
    dataView.set(ID, object.getType().getId());
    // noinspection unchecked
    final ObjectStore<LanternTileEntity> store = (ObjectStore) ObjectStoreRegistry.get().get(object.getClass()).get();
    store.serialize(object, dataView);
    return dataView;
}
Also used : DataView(org.spongepowered.api.data.DataView) ObjectStore(org.lanternpowered.server.data.io.store.ObjectStore) LanternTileEntity(org.lanternpowered.server.block.tile.LanternTileEntity)

Example 3 with ObjectStore

use of org.lanternpowered.server.data.io.store.ObjectStore in project LanternServer by LanternPowered.

the class TileEntitySerializer method deserialize.

@Override
public LanternTileEntity deserialize(DataView dataView) throws InvalidDataException {
    final String id = fixTileId(dataView, dataView.getString(ID).get());
    dataView.remove(ID);
    final LanternTileEntityType tileEntityType = (LanternTileEntityType) Sponge.getRegistry().getType(TileEntityType.class, id).orElseThrow(() -> new InvalidDataException("Unknown tile entity id: " + id));
    // noinspection unchecked
    final ObjectStore<LanternTileEntity> store = (ObjectStore) ObjectStoreRegistry.get().get(tileEntityType.getTileEntityType()).get();
    // noinspection unchecked
    final LanternTileEntity entity = (LanternTileEntity) tileEntityType.getTileEntityConstructor().get();
    store.deserialize(entity, dataView);
    return entity;
}
Also used : ObjectStore(org.lanternpowered.server.data.io.store.ObjectStore) LanternTileEntityType(org.lanternpowered.server.block.tile.LanternTileEntityType) InvalidDataException(org.spongepowered.api.data.persistence.InvalidDataException) LanternTileEntity(org.lanternpowered.server.block.tile.LanternTileEntity)

Example 4 with ObjectStore

use of org.lanternpowered.server.data.io.store.ObjectStore 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

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