private void updateEntity(NetData.UpdateEntityMessage updateEntity) { EntityRef currentEntity = networkSystem.getEntity(updateEntity.getNetId()); if (currentEntity.exists()) { NetworkComponent netComp = currentEntity.getComponent(NetworkComponent.class); if (netComp == null) { logger.error( "Updating entity with no network component: {}, expected netId {}", currentEntity, updateEntity.getNetId()); return; } if (netComp.getNetworkId() != updateEntity.getNetId()) { logger.error("Network ID wrong before update"); } boolean blockEntityBefore = currentEntity.hasComponent(BlockComponent.class); entitySerializer.deserializeOnto(currentEntity, updateEntity.getEntity()); BlockComponent blockComponent = currentEntity.getComponent(BlockComponent.class); if (blockComponent != null && !blockEntityBefore) { if (!blockEntityRegistry .getExistingBlockEntityAt(blockComponent.getPosition()) .equals(currentEntity)) { logger.error("Failed to associated new block entity"); } } if (netComp.getNetworkId() != updateEntity.getNetId()) { logger.error( "Network ID lost in update: {}, {} -> {}", currentEntity, updateEntity.getNetId(), netComp.getNetworkId()); } } else { logger.warn("Received update for non-existent entity {}", updateEntity.getNetId()); } }
@Override public void send(Event event, EntityRef target) { NetworkComponent netComp = target.getComponent(NetworkComponent.class); if (netComp != null) { try { queuedOutgoingEvents.add( NetData.EventMessage.newBuilder() .setEvent(eventSerializer.serialize(event)) .setTargetId(netComp.getNetworkId()) .build()); } catch (SerializationException e) { logger.error("Failed to serialize event", e); } } }
@Override public EntityRef copy() { if (!isActive()) { return NULL; } Map<Class<? extends Component>, Component> classComponentMap = entityManager.copyComponents(this); if (networkSystem.getMode().isAuthority()) { NetworkComponent netComp = (NetworkComponent) classComponentMap.get(NetworkComponent.class); if (netComp != null) { netComp.setNetworkId(0); } } else { classComponentMap.remove(NetworkComponent.class); } return entityManager.create(classComponentMap.values()); }
@Override public String toString() { AssetUri prefabUri = getPrefabURI(); StringBuilder builder = new StringBuilder(); builder.append("EntityRef{id = "); builder.append(getId()); NetworkComponent networkComponent = getComponent(NetworkComponent.class); if (networkComponent != null) { builder.append(", netId = "); builder.append(networkComponent.getNetworkId()); } if (prefabUri != null) { builder.append(", prefab = '"); builder.append(prefabUri.toSimpleString()); builder.append("'"); } builder.append("}"); return builder.toString(); }