private void cleanUpTemporaryEntity(EntityRef entity) {
    Prefab prefab = entity.getParentPrefab();

    for (Component comp : entity.iterateComponents()) {
      if (!COMMON_BLOCK_COMPONENTS.contains(comp.getClass())
          && (prefab == null || !prefab.hasComponent(comp.getClass()))) {
        entity.removeComponent(comp.getClass());
      }
    }
    entity.removeComponent(NetworkComponent.class);

    if (prefab != null) {
      for (Component comp : prefab.iterateComponents()) {
        Component currentComp = entity.getComponent(comp.getClass());
        if (currentComp == null) {
          entity.addComponent(entityManager.getComponentLibrary().copy(comp));
        } else {
          ComponentMetadata<?> metadata =
              entityManager.getComponentLibrary().getMetadata(comp.getClass());
          boolean changed = false;
          for (FieldMetadata field : metadata.getFields()) {
            Object expected = field.getValue(comp);
            if (!Objects.equal(expected, field.getValue(currentComp))) {
              field.setValue(currentComp, expected);
              changed = true;
            }
          }
          if (changed) {
            entity.saveComponent(currentComp);
          }
        }
      }
    }
    entityManager.destroyEntityWithoutEvents(entity);
  }
  private void serializeComponentFull(
      Component component,
      boolean ignoreIfNoFields,
      FieldSerializeCheck<Component> fieldCheck,
      EntityData.PackedEntity.Builder entityData,
      ByteString.Output entityFieldIds,
      ByteString.Output componentFieldCounts,
      boolean componentInitial) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(component.getClass());
    if (componentMetadata == null) {
      logger.error("Unregistered component type: {}", component.getClass());
      return;
    }

    Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
    byte fieldCount = 0;
    for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
      if (fieldCheck.shouldSerializeField(field, component, componentInitial)) {
        PersistedData fieldValue = serializer.serialize(field, component, serializationContext);
        if (!fieldValue.isNull()) {
          entityFieldIds.write(field.getId());

          entityData.addFieldValue(((ProtobufPersistedData) fieldValue).getValue());
          fieldCount++;
        }
      }
    }

    if (fieldCount != 0 || !ignoreIfNoFields) {
      entityData.addComponentId(idTable.get(component.getClass()));
      componentFieldCounts.write(fieldCount);
    }
  }
 private <T extends Component> void updateComponent(
     EntityRef blockEntity, ComponentMetadata<T> metadata, T targetComponent) {
   T currentComp = blockEntity.getComponent(metadata.getType());
   if (currentComp != null) {
     boolean changed = false;
     for (FieldMetadata<T, ?> field : metadata.getFields()) {
       Object newVal = field.getValue(targetComponent);
       if (!Objects.equal(field.getValue(currentComp), newVal)) {
         field.setValue(currentComp, newVal);
         changed = true;
       }
     }
     if (changed) {
       blockEntity.saveComponent(currentComp);
     }
   }
 }
  private void serializeComponentDelta(
      Component oldComponent,
      Component newComponent,
      FieldSerializeCheck<Component> fieldCheck,
      EntityData.PackedEntity.Builder entityData,
      ByteString.Output entityFieldIds,
      ByteString.Output componentFieldCounts,
      boolean componentInitial) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(oldComponent.getClass());
    if (componentMetadata == null) {
      logger.error("Unregistered component type: {}", oldComponent.getClass());
      return;
    }

    byte fieldCount = 0;
    Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
    for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
      if (fieldCheck.shouldSerializeField(field, newComponent, componentInitial)) {
        Object oldValue = field.getValue(oldComponent);
        Object newValue = field.getValue(newComponent);
        if (!Objects.equal(oldValue, newValue)) {
          PersistedData data = serializer.serializeValue(field, newValue, serializationContext);
          if (!data.isNull()) {
            entityFieldIds.write(field.getId());
            entityData.addFieldValue(((ProtobufPersistedData) data).getValue());
            fieldCount++;
          } else {
            logger.error(
                "Exception serializing component type: {}, field: {} - returned null",
                componentMetadata,
                field);
          }
        }
      }
    }

    if (fieldCount > 0) {
      entityData.addComponentId(idTable.get(newComponent.getClass()));
      componentFieldCounts.write(fieldCount);
    }
  }