use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.
the class NetworkSystemImpl method processNewClient.
private void processNewClient(NetClient client) {
client.connected(entityManager, entitySerializer, eventSerializer, eventLibrary);
// log after connect so that the name has been set:
logger.info("New client connected: {}", client.getName());
client.send(NetData.NetMessage.newBuilder().setJoinComplete(NetData.JoinCompleteMessage.newBuilder().setClientId(client.getEntity().getComponent(NetworkComponent.class).getNetworkId())).build());
clientList.add(client);
netClientList.add(client);
clientPlayerLookup.put(client.getEntity(), client);
connectClient(client);
logger.info("New client entity: {}", client.getEntity());
for (EntityRef netEntity : entityManager.getEntitiesWith(NetworkComponent.class)) {
NetworkComponent netComp = netEntity.getComponent(NetworkComponent.class);
if (netComp.getNetworkId() != NULL_NET_ID) {
switch(netComp.replicateMode) {
case OWNER:
if (client.equals(getOwner(netEntity))) {
client.setNetInitial(netComp.getNetworkId());
}
break;
default:
// TODO: Relevance Check
client.setNetInitial(netComp.getNetworkId());
break;
}
}
}
}
use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.
the class NetworkSystemImpl method shutdown.
@Override
public void shutdown() {
allChannels.close().awaitUninterruptibly();
// Factory may be null if a local game session has happened, yet be initialized if networking has been used
if (factory != null) {
factory.releaseExternalResources();
}
processPendingDisconnects();
clientList.forEach(this::processRemovedClient);
server = null;
nextNetId = 1;
netIdToEntityId.clear();
if (mode != NetworkMode.CLIENT) {
if (this.entityManager != null) {
for (EntityRef entity : entityManager.getEntitiesWith(NetworkComponent.class)) {
NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
netComp.setNetworkId(0);
entity.saveComponent(netComp);
}
this.entityManager.unsubscribe(this);
}
}
mode = NetworkMode.NONE;
entityManager = null;
eventLibrary = null;
componentLibrary = null;
eventSerializer = null;
entitySerializer = null;
clientList.clear();
netClientList.clear();
blockManager = null;
biomeManager = null;
ownerLookup.clear();
ownedLookup.clear();
ownershipHelper = null;
storageManager = null;
logger.info("Network shutdown");
}
use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.
the class NetworkSystemImpl method unregisterNetworkEntity.
public void unregisterNetworkEntity(EntityRef entity) {
if (mode != NetworkMode.CLIENT) {
NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
if (netComponent != null) {
logger.debug("Unregistering network entity: {} with netId {}", entity, netComponent.getNetworkId());
netIdToEntityId.remove(netComponent.getNetworkId());
if (mode.isServer()) {
for (NetClient client : netClientList) {
client.setNetRemoved(netComponent.getNetworkId());
}
}
netComponent.setNetworkId(NULL_NET_ID);
entity.saveComponent(netComponent);
}
}
ownerLookup.remove(entity);
}
use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.
the class NetworkSystemImpl method onEntityComponentAdded.
@Override
public void onEntityComponentAdded(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 {} added to {}", component, entity);
client.setComponentAdded(netComp.getNetworkId(), component);
}
}
}
}
updatedOwnedEntities(entity, component, metadata);
}
use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.
the class NetworkSystemImpl method updateOwnership.
public void updateOwnership(EntityRef entity) {
NetworkComponent netComponent = entity.getComponent(NetworkComponent.class);
if (netComponent == null || netComponent.getNetworkId() == NULL_NET_ID) {
return;
}
EntityRef lastOwnerEntity = ownerLookup.get(entity);
if (lastOwnerEntity == null) {
lastOwnerEntity = EntityRef.NULL;
}
EntityRef newOwnerEntity = entity.getOwner();
if (!Objects.equal(lastOwnerEntity, newOwnerEntity)) {
NetClient lastOwner = getNetOwner(lastOwnerEntity);
NetClient newOwner = getNetOwner(newOwnerEntity);
if (!Objects.equal(lastOwner, newOwner)) {
recursiveUpdateOwnership(entity, lastOwner, newOwner);
if (newOwner != null) {
int id = netComponent.getNetworkId();
for (Component component : entity.iterateComponents()) {
if (componentLibrary.getMetadata(component.getClass()).isReplicated()) {
newOwner.setComponentDirty(id, component.getClass());
}
}
}
}
if (lastOwnerEntity.exists()) {
ownedLookup.remove(lastOwnerEntity, entity);
}
if (newOwnerEntity.exists()) {
ownerLookup.put(entity, newOwnerEntity);
ownedLookup.put(newOwnerEntity, entity);
} else {
ownerLookup.remove(entity);
}
}
}
Aggregations