use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class PojoEntityManager method addComponent.
/**
* Adds (or replaces) a component to an entity
*
* @param entityId
* @param component
* @param <T>
* @return The added component
*/
@Override
public <T extends Component> T addComponent(long entityId, T component) {
Preconditions.checkNotNull(component);
Optional<Component> oldComponent = getPool(entityId).map(pool -> pool.getComponentStore().put(entityId, component));
// notify internal users first to get the unobstructed views on the entity as it is at this moment.
if (!oldComponent.isPresent()) {
notifyComponentAdded(getEntity(entityId), component.getClass());
} else {
logger.error("Adding a component ({}) over an existing component for entity {}", component.getClass(), entityId);
notifyComponentChanged(getEntity(entityId), component.getClass());
}
// Note: systems are free to remove the component that was just added, which might cause some trouble here...
if (eventSystem != null) {
EntityRef entityRef = getEntity(entityId);
if (!oldComponent.isPresent()) {
eventSystem.send(entityRef, OnAddedComponent.newInstance(), component);
eventSystem.send(entityRef, OnActivatedComponent.newInstance(), component);
} else {
eventSystem.send(entityRef, OnChangedComponent.newInstance(), component);
}
}
return component;
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class EventSystemImpl method send.
@Override
public void send(EntityRef entity, Event event, Component component) {
if (Thread.currentThread() != mainThread) {
pendingEvents.offer(new PendingEvent(entity, event, component));
} else {
SetMultimap<Class<? extends Component>, EventHandlerInfo> handlers = componentSpecificHandlers.get(event.getClass());
if (handlers != null) {
List<EventHandlerInfo> eventHandlers = Lists.newArrayList(handlers.get(component.getClass()));
eventHandlers.sort(priorityComparator);
for (EventHandlerInfo eventHandler : eventHandlers) {
if (eventHandler.isValidFor(entity)) {
eventHandler.invoke(entity, event);
}
}
}
}
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class Config method setModuleConfigs.
/**
* @param generatorUri the generator Uri
* @param configs the new config params for the world generator
*/
public void setModuleConfigs(SimpleUri generatorUri, Map<String, Component> configs) {
Gson gson = createGsonForModules();
Map<String, JsonElement> map = Maps.newHashMap();
for (Map.Entry<String, Component> entry : configs.entrySet()) {
JsonElement json = gson.toJsonTree(entry.getValue());
map.put(entry.getKey(), json);
}
config.getModuleConfigs().put(generatorUri, map);
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class CoreCommands method dumpEntities.
/**
* Writes out information on entities having specific components to a text file for debugging
* If no component names provided - writes out information on all entities
*
* @param componentNames string contains one or several component names, if more then one name
* provided - they must be braced with double quotes and all names separated
* by space
* @return String containing information about number of entities saved
* @throws IOException thrown when error with saving file occures
*/
@Command(shortDescription = "Writes out information on all entities to a JSON file for debugging", helpText = "Writes entity information out into a file named \"<timestamp>-entityDump.json\"." + " Supports list of component names, which will be used to only save entities that contains" + " one or more of those components. Names should be separated by spaces.")
public String dumpEntities(@CommandParam(value = "componentNames", required = false) String... componentNames) throws IOException {
int savedEntityCount;
EngineEntityManager engineEntityManager = (EngineEntityManager) entityManager;
PrefabSerializer prefabSerializer = new PrefabSerializer(engineEntityManager.getComponentLibrary(), engineEntityManager.getTypeSerializerLibrary());
WorldDumper worldDumper = new WorldDumper(engineEntityManager, prefabSerializer);
Path outFile = PathManager.getInstance().getHomePath().resolve(Instant.now() + "-entityDump.json");
if (componentNames.length == 0) {
savedEntityCount = worldDumper.save(outFile);
} else {
List<Class<? extends Component>> filterComponents = Arrays.stream(componentNames).map(// Trim off whitespace
String::trim).filter(// Remove empty strings
o -> !o.isEmpty()).map(// All component class names end with "component"
o -> o.toLowerCase().endsWith("component") ? o : o + "component").map(o -> Streams.stream(moduleManager.getEnvironment().getSubtypesOf(Component.class)).filter(e -> e.getSimpleName().equalsIgnoreCase(o)).findFirst()).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
if (!filterComponents.isEmpty()) {
savedEntityCount = worldDumper.save(outFile, filterComponents);
} else {
return "Could not find components matching given names";
}
}
return "Number of entities saved: " + savedEntityCount;
}
use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.
the class ThirdPersonRemoteClientSystem method linkHeldItemLocationForRemotePlayer.
/**
* Changes held item entity.
* <p>
* Detaches old held item and removes its components. Adds components to new held item and attaches it to the mount
* point entity.
*/
private void linkHeldItemLocationForRemotePlayer(EntityRef newItem, EntityRef player) {
if (relatesToLocalPlayer(player)) {
logger.debug("linkHeldItemLocationForRemotePlayer called with an entity that relates to the local player," + " ignoring{}", player);
return;
}
// Find out if there is a current held item that maps to this player
EntityRef currentHeldItem = EntityRef.NULL;
for (EntityRef heldItemCandidate : entityManager.getEntitiesWith(ItemIsRemotelyHeldComponent.class)) {
EntityRef remotePlayerCandidate = heldItemCandidate.getComponent(ItemIsRemotelyHeldComponent.class).remotePlayer;
logger.debug("For held item candidate {} got its player candidate as {}", heldItemCandidate, remotePlayerCandidate);
if (remotePlayerCandidate.equals(player)) {
logger.debug("Thinking we found a match with player {} so counting this held item as relevant for " + "processing", player);
currentHeldItem = heldItemCandidate;
// need to remove the old item
if (newItem.equals(EntityRef.NULL)) {
logger.debug("Found an existing held item but the new request was to no longer hold anything so " + "destroying {}", currentHeldItem);
currentHeldItem.destroy();
return;
}
break;
}
}
// In the case of an actual change of item other than an empty hand we need to hook up a new held item entity
if (newItem != null && !newItem.equals(EntityRef.NULL) && !newItem.equals(currentHeldItem)) {
RemotePersonHeldItemMountPointComponent mountPointComponent = player.getComponent(RemotePersonHeldItemMountPointComponent.class);
if (mountPointComponent != null) {
// currentHeldItem is at this point the old item
if (currentHeldItem != EntityRef.NULL) {
currentHeldItem.destroy();
}
currentHeldItem = entityManager.create();
logger.debug("linkHeldItemLocationForRemotePlayer is now creating a new held item {}", currentHeldItem);
// add the visually relevant components
for (Component component : newItem.iterateComponents()) {
if (component instanceof VisualComponent && !(component instanceof FirstPersonHeldItemTransformComponent)) {
currentHeldItem.addComponent(component);
}
}
// ensure world location is set
currentHeldItem.addComponent(new LocationComponent());
// Map this held item to the player it is held by
ItemIsRemotelyHeldComponent itemIsRemotelyHeldComponent = new ItemIsRemotelyHeldComponent();
itemIsRemotelyHeldComponent.remotePlayer = player;
currentHeldItem.addComponent(itemIsRemotelyHeldComponent);
RemotePersonHeldItemTransformComponent heldItemTransformComponent = currentHeldItem.getComponent(RemotePersonHeldItemTransformComponent.class);
if (heldItemTransformComponent == null) {
heldItemTransformComponent = new RemotePersonHeldItemTransformComponent();
currentHeldItem.addComponent(heldItemTransformComponent);
}
Location.attachChild(mountPointComponent.mountPointEntity, currentHeldItem, heldItemTransformComponent.translate, new Quaternionf().rotationYXZ(Math.toRadians(heldItemTransformComponent.rotateDegrees.y), Math.toRadians(heldItemTransformComponent.rotateDegrees.x), Math.toRadians(heldItemTransformComponent.rotateDegrees.z)), heldItemTransformComponent.scale);
}
} else {
logger.info("Somehow ended up in the else during linkHeldItemLocationForRemotePlayer - current item was " + "{} and new item {}", currentHeldItem, newItem);
}
}
Aggregations