/// <summary> /// Saves a component to an entity /// </summary> /// <param name="entityId"> </param> /// <param name="component"> </param> public override void saveComponent(int entityId, Component component) { Component oldComponent = store.put(entityId, component); if (oldComponent == null) { logger.error("Saving a component ({}) that doesn't belong to this entity {}", component.GetType(), entityId); } if (eventSystem != null) { EntityRef entityRef = createEntityRef(entityId); if (oldComponent == null) { eventSystem.send(entityRef, OnAddedComponent.newInstance(), component); eventSystem.send(entityRef, OnActivatedComponent.newInstance(), component); } else { eventSystem.send(entityRef, OnChangedComponent.newInstance(), component); } } if (oldComponent == null) { notifyComponentAdded(getEntity(entityId), component.GetType()); } else { notifyComponentChanged(getEntity(entityId), component.GetType()); } }
/// <summary> /// Adds (or replaces) a component to an entity /// </summary> /// <param name="entityId"> </param> /// <param name="component"> </param> /// @param <T> </param> /// <returns> The added component </returns> public override T addComponent <T>(int entityId, T component) where T : org.terasology.entitySystem.Component { Preconditions.checkNotNull(component); Component oldComponent = store.put(entityId, component); if (oldComponent != null) { logger.error("Adding a component ({}) over an existing component for entity {}", component.GetType(), entityId); } if (eventSystem != null) { EntityRef entityRef = createEntityRef(entityId); if (oldComponent == null) { eventSystem.send(entityRef, OnAddedComponent.newInstance(), component); eventSystem.send(entityRef, OnActivatedComponent.newInstance(), component); } else { eventSystem.send(entityRef, OnChangedComponent.newInstance(), component); } } if (oldComponent == null) { notifyComponentAdded(getEntity(entityId), component.GetType()); } else { notifyComponentChanged(getEntity(entityId), component.GetType()); } return(component); }