use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class AbstractClient method findNamesOfOtherPlayers.
private Set<String> findNamesOfOtherPlayers(EntityManager entityManager, EntityRef player) {
Set<String> otherNames = new HashSet<>();
for (EntityRef clientInfo : entityManager.getEntitiesWith(ClientInfoComponent.class)) {
if (!clientInfo.equals(player)) {
DisplayNameComponent displayInfo = clientInfo.getComponent(DisplayNameComponent.class);
String usedName = displayInfo.name;
otherNames.add(usedName);
}
}
return otherNames;
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetClient method sendDirtyEntities.
private void sendDirtyEntities(NetData.NetMessage.Builder message) {
TIntIterator dirtyIterator = netDirty.iterator();
while (dirtyIterator.hasNext()) {
int netId = dirtyIterator.next();
EntityRef entity = networkSystem.getEntity(netId);
if (!entity.exists()) {
logger.error("Sending non-existent entity update for netId {}", netId);
}
boolean isOwner = networkSystem.getOwner(entity) == this;
EntityData.PackedEntity entityData = entitySerializer.serialize(entity, addedComponents.get(netId), dirtyComponents.get(netId), removedComponents.get(netId), new ServerComponentFieldCheck(isOwner, false));
if (entityData != null) {
message.addUpdateEntity(NetData.UpdateEntityMessage.newBuilder().setEntity(entityData).setNetId(netId));
}
}
netDirty.clear();
addedComponents.clear();
removedComponents.clear();
dirtyComponents.clear();
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetClient method processEvents.
private void processEvents(NetData.NetMessage message) {
boolean lagCompensated = false;
PredictionSystem predictionSystem = CoreRegistry.get(PredictionSystem.class);
for (NetData.EventMessage eventMessage : message.getEventList()) {
try {
Event event = eventSerializer.deserialize(eventMessage.getEvent());
EventMetadata<?> metadata = eventLibrary.getMetadata(event.getClass());
if (metadata.getNetworkEventType() != NetworkEventType.SERVER) {
logger.warn("Received non-server event '{}' from client '{}'", metadata, getName());
continue;
}
if (!lagCompensated && metadata.isLagCompensated()) {
if (predictionSystem != null) {
predictionSystem.lagCompensate(getEntity(), lastReceivedTime);
}
lagCompensated = true;
}
EntityRef target = EntityRef.NULL;
if (eventMessage.hasTargetId()) {
target = networkSystem.getEntity(eventMessage.getTargetId());
}
if (target.exists()) {
if (Objects.equal(networkSystem.getOwner(target), this)) {
target.send(event);
} else {
logger.warn("Received event {} for non-owned entity {} from {}", event, target, this);
}
}
} catch (DeserializationException e) {
logger.error("Failed to deserialize event", e);
} catch (RuntimeException e) {
logger.error("Error processing event", e);
}
}
if (lagCompensated && predictionSystem != null) {
predictionSystem.restoreToPresent();
}
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetworkSystemImpl method registerNetworkEntity.
public void registerNetworkEntity(EntityRef entity) {
if (mode == NetworkMode.NONE) {
return;
}
if (mode.isServer()) {
NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
netComponent.setNetworkId(nextNetId++);
entity.saveComponent(netComponent);
netIdToEntityId.put(netComponent.getNetworkId(), entity.getId());
switch(netComponent.replicateMode) {
case OWNER:
NetClient clientPlayer = getNetOwner(entity);
if (clientPlayer != null) {
clientPlayer.setNetInitial(netComponent.getNetworkId());
}
break;
default:
for (NetClient client : netClientList) {
// TODO: Relevance Check
client.setNetInitial(netComponent.getNetworkId());
}
break;
}
EntityRef owner = entity.getOwner();
if (owner.exists()) {
ownerLookup.put(entity, owner);
ownedLookup.put(owner, entity);
}
}
}
use of org.terasology.entitySystem.entity.EntityRef in project Terasology by MovingBlocks.
the class NetworkSystemImpl method onEntityComponentRemoved.
@Override
public void onEntityComponentRemoved(EntityRef entity, Class<? extends Component> component) {
ComponentMetadata<? extends Component> metadata = componentLibrary.getMetadata(component);
NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
if (netComp != null && netComp.getNetworkId() != NULL_NET_ID) {
if (mode.isServer()) {
if (metadata.isReplicated()) {
for (NetClient client : netClientList) {
logger.info("Component {} removed from {}", component, entity);
client.setComponentRemoved(netComp.getNetworkId(), component);
}
}
}
}
if (mode.isAuthority() && metadata.isReferenceOwner()) {
ownershipHelper.listOwnedEntities(entity.getComponent(component)).forEach(EntityRef::destroy);
}
}
Aggregations