Search in sources :

Example 1 with LanternEntityType

use of org.lanternpowered.server.entity.LanternEntityType in project LanternServer by LanternPowered.

the class LanternWorld method createEntity.

public Entity createEntity(EntityType type, Vector3d position, Consumer<Entity> entityConsumer) {
    checkNotNull(position, "position");
    final LanternEntityType entityType = (LanternEntityType) checkNotNull(type, "type");
    final LanternEntity entity = (LanternEntity) entityType.getEntityConstructor().apply(UUID.randomUUID());
    entity.setPositionAndWorld(this, position);
    entityConsumer.accept(entity);
    final CauseStack causeStack = CauseStack.current();
    // Only throw the post event, the pre event will
    // only be called in specific cases.
    final ConstructEntityEvent.Post event = SpongeEventFactory.createConstructEntityEventPost(causeStack.getCurrentCause(), entity, type, entity.getTransform());
    Sponge.getEventManager().post(event);
    return entity;
}
Also used : ConstructEntityEvent(org.spongepowered.api.event.entity.ConstructEntityEvent) CauseStack(org.lanternpowered.server.event.CauseStack) LanternEntityType(org.lanternpowered.server.entity.LanternEntityType) LanternEntity(org.lanternpowered.server.entity.LanternEntity)

Example 2 with LanternEntityType

use of org.lanternpowered.server.entity.LanternEntityType in project LanternServer by LanternPowered.

the class LanternChunk method createEntity.

@SuppressWarnings("unchecked")
@Override
public Entity createEntity(EntityType type, Vector3d position) {
    checkNotNull(position, "position");
    final LanternEntityType entityType = (LanternEntityType) checkNotNull(type, "type");
    checkVolumeBounds(position.getFloorX(), position.getFloorY(), position.getFloorZ());
    // noinspection unchecked
    final LanternEntity entity = (LanternEntity) entityType.getEntityConstructor().apply(UUID.randomUUID());
    entity.setPositionAndWorld(this.world, position);
    return entity;
}
Also used : LanternEntityType(org.lanternpowered.server.entity.LanternEntityType) LanternEntity(org.lanternpowered.server.entity.LanternEntity)

Example 3 with LanternEntityType

use of org.lanternpowered.server.entity.LanternEntityType 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

LanternEntity (org.lanternpowered.server.entity.LanternEntity)3 LanternEntityType (org.lanternpowered.server.entity.LanternEntityType)3 UUID (java.util.UUID)1 IdentifiableObjectStore (org.lanternpowered.server.data.io.store.IdentifiableObjectStore)1 ObjectStore (org.lanternpowered.server.data.io.store.ObjectStore)1 CauseStack (org.lanternpowered.server.event.CauseStack)1 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1 ConstructEntityEvent (org.spongepowered.api.event.entity.ConstructEntityEvent)1