use of org.terasology.network.NetworkComponent 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.network.NetworkComponent in project Terasology by MovingBlocks.
the class NetworkSystemImpl method onEntityComponentChange.
@Override
public void onEntityComponentChange(EntityRef entity, Class<? extends Component> component) {
NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
ComponentMetadata<? extends Component> metadata = componentLibrary.getMetadata(component);
if (netComp != null && netComp.getNetworkId() != NULL_NET_ID) {
switch(mode) {
case LISTEN_SERVER:
case DEDICATED_SERVER:
if (metadata.isReplicated()) {
for (NetClient client : netClientList) {
client.setComponentDirty(netComp.getNetworkId(), component);
}
}
break;
case CLIENT:
if (server != null && metadata.isReplicatedFromOwner() && getOwnerEntity(entity).equals(server.getClientEntity())) {
server.setComponentDirty(netComp.getNetworkId(), component);
}
break;
default:
break;
}
}
updatedOwnedEntities(entity, component, metadata);
}
use of org.terasology.network.NetworkComponent 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);
}
}
use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.
the class NetEntityRefTypeHandler method serializeCollection.
@Override
public PersistedData serializeCollection(Collection<EntityRef> value, SerializationContext context) {
List<PersistedData> items = Lists.newArrayList();
for (EntityRef ref : value) {
BlockComponent blockComponent = ref.getComponent(BlockComponent.class);
if (blockComponent != null) {
Vector3i blockPos = blockComponent.getPosition();
items.add(context.create(blockPos.x, blockPos.y, blockPos.z));
} else {
NetworkComponent netComponent = ref.getComponent(NetworkComponent.class);
if (netComponent != null) {
items.add(context.create(netComponent.getNetworkId()));
} else {
items.add(context.createNull());
}
}
}
return context.create(items);
}
use of org.terasology.network.NetworkComponent in project Terasology by MovingBlocks.
the class NetEntityRefTypeHandler method serialize.
@Override
public PersistedData serialize(EntityRef value, SerializationContext context) {
BlockComponent blockComponent = value.getComponent(BlockComponent.class);
if (blockComponent != null) {
Vector3i pos = blockComponent.getPosition();
return context.create(pos.x, pos.y, pos.z);
}
NetworkComponent netComponent = value.getComponent(NetworkComponent.class);
if (netComponent != null) {
return context.create(netComponent.getNetworkId());
}
return context.createNull();
}
Aggregations