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);
  }
  /**
   * Transforms a block entity with the change of block type. This is driven from the delta between
   * the old and new block type prefabs, but takes into account changes made to the block entity.
   *
   * @param blockEntity The entity to update
   * @param oldType The previous type of the block
   * @param type The new type of the block
   */
  private void updateBlockEntityComponents(
      EntityRef blockEntity,
      Block oldType,
      Block type,
      Set<Class<? extends Component>> retainComponents) {
    BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);

    Optional<Prefab> oldPrefab = oldType.getPrefab();
    EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
    oldEntityBuilder.addComponent(
        new BlockComponent(oldType, new Vector3i(blockComponent.getPosition())));
    BeforeEntityCreated oldEntityEvent =
        new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
    blockEntity.send(oldEntityEvent);
    for (Component comp : oldEntityEvent.getResultComponents()) {
      oldEntityBuilder.addComponent(comp);
    }

    Optional<Prefab> newPrefab = type.getPrefab();
    EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
    newEntityBuilder.addComponent(
        new BlockComponent(type, new Vector3i(blockComponent.getPosition())));
    BeforeEntityCreated newEntityEvent =
        new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
    blockEntity.send(newEntityEvent);
    for (Component comp : newEntityEvent.getResultComponents()) {
      newEntityBuilder.addComponent(comp);
    }

    for (Component component : blockEntity.iterateComponents()) {
      if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass())
          && !entityManager
              .getComponentLibrary()
              .getMetadata(component.getClass())
              .isRetainUnalteredOnBlockChange()
          && !newEntityBuilder.hasComponent(component.getClass())
          && !retainComponents.contains(component.getClass())) {
        blockEntity.removeComponent(component.getClass());
      }
    }

    blockComponent.setBlock(type);
    blockEntity.saveComponent(blockComponent);

    HealthComponent health = blockEntity.getComponent(HealthComponent.class);
    if (health == null && type.isDestructible()) {
      blockEntity.addComponent(
          new HealthComponent(type.getHardness(), type.getHardness() / BLOCK_REGEN_SECONDS, 1.0f));
    } else if (health != null && !type.isDestructible()) {
      blockEntity.removeComponent(HealthComponent.class);
    } else if (health != null && type.isDestructible()) {
      health.maxHealth = type.getHardness();
      health.currentHealth = Math.min(health.currentHealth, health.maxHealth);
      blockEntity.saveComponent(health);
    }

    for (Component comp : newEntityBuilder.iterateComponents()) {
      copyIntoPrefab(blockEntity, comp, retainComponents);
    }
  }
 private boolean processOutputForSetResetGate(EntityRef blockEntity) {
   SignalConsumerAdvancedStatusComponent consumerAdvancedStatusComponent =
       blockEntity.getComponent(SignalConsumerAdvancedStatusComponent.class);
   EnumSet<Side> signals = SideBitFlag.getSides(consumerAdvancedStatusComponent.sidesWithSignals);
   SignalProducerComponent producerComponent =
       blockEntity.getComponent(SignalProducerComponent.class);
   int resultSignal = producerComponent.signalStrength;
   if (signals.contains(Side.TOP) || signals.contains(Side.BOTTOM)) {
     resultSignal = 0;
   } else if (signals.size() > 0) {
     resultSignal = -1;
   }
   if (producerComponent.signalStrength != resultSignal) {
     producerComponent.signalStrength = resultSignal;
     blockEntity.saveComponent(producerComponent);
     if (resultSignal != 0) {
       if (!blockEntity.hasComponent(SignalProducerModifiedComponent.class)) {
         blockEntity.addComponent(new SignalProducerModifiedComponent());
       }
     } else if (blockEntity.hasComponent(SignalProducerModifiedComponent.class)) {
       blockEntity.removeComponent(SignalProducerModifiedComponent.class);
     }
     return true;
   }
   return false;
 }
 private boolean stopProducingSignal(EntityRef entity) {
   SignalProducerComponent producer = entity.getComponent(SignalProducerComponent.class);
   if (producer.signalStrength != 0) {
     producer.signalStrength = 0;
     entity.saveComponent(producer);
     entity.removeComponent(SignalProducerModifiedComponent.class);
     return true;
   }
   return false;
 }
 @Test
 public void testEntityBecomesTemporaryIfForceBlockActiveComponentRemoved() {
   EntityRef blockEntity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
   blockEntity.addComponent(new ForceBlockActiveComponent());
   worldProvider.update(1.0f);
   blockEntity.removeComponent(ForceBlockActiveComponent.class);
   worldProvider.update(1.0f);
   assertFalse(blockEntity.exists());
   assertFalse(blockEntity.isActive());
 }
  private void signalButtonActivated(EntityRef block, SignalProducerComponent producerComponent) {
    if (block.hasComponent(SignalDelayedActionComponent.class)) {
      block.removeComponent(SignalDelayedActionComponent.class);
    }

    SignalDelayedActionComponent actionComponent = new SignalDelayedActionComponent();
    actionComponent.executeTime = time.getGameTimeInMs() + BUTTON_PRESS_TIME;
    block.addComponent(actionComponent);

    startProducingSignal(block, -1);
  }
  @ReceiveEvent(components = {BlockComponent.class, SignalTimeDelayComponent.class})
  public void configureTimeDelay(SetSignalDelayEvent event, EntityRef entity) {
    SignalTimeDelayComponent timeDelayComponent =
        entity.getComponent(SignalTimeDelayComponent.class);
    timeDelayComponent.delaySetting = Math.min(500, event.getTime());

    entity.saveComponent(timeDelayComponent);
    if (timeDelayComponent.delaySetting == 1000
        && entity.hasComponent(SignalTimeDelayModifiedComponent.class)) {
      entity.removeComponent(SignalTimeDelayModifiedComponent.class);
    } else if (!entity.hasComponent(SignalTimeDelayModifiedComponent.class)) {
      entity.addComponent(new SignalTimeDelayModifiedComponent());
    }
  }
 private void signalChangedForDelayOnGate(
     EntityRef entity, SignalConsumerStatusComponent consumerStatusComponent) {
   SignalTimeDelayComponent delay = entity.getComponent(SignalTimeDelayComponent.class);
   if (consumerStatusComponent.hasSignal) {
     // Schedule for the gate to be looked at when the time passes
     SignalDelayedActionComponent delayedAction = new SignalDelayedActionComponent();
     delayedAction.executeTime = time.getGameTimeInMs() + delay.delaySetting;
     entity.addComponent(delayedAction);
   } else {
     // Remove any signal-delayed actions on the entity and turn off signal from it, if it has any
     if (entity.hasComponent(SignalDelayedActionComponent.class)) {
       entity.removeComponent(SignalDelayedActionComponent.class);
     }
     stopProducingSignal(entity);
   }
 }
  @Test
  public void testEntityMissingComponentsAddedBeforeCleanUp() {
    worldStub.setBlock(Vector3i.zero(), blockWithString);
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    entity.removeComponent(StringComponent.class);

    LifecycleEventChecker checker =
        new LifecycleEventChecker(entityManager.getEventSystem(), StringComponent.class);

    worldProvider.update(1.0f);
    assertEquals(
        Lists.newArrayList(
            new EventInfo(OnAddedComponent.newInstance(), entity),
            new EventInfo(OnActivatedComponent.newInstance(), entity)),
        checker.receivedEvents);
  }
  @Test
  public void networkComponentRemovedWhenTemporaryCleanedUp() {
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    entity.addComponent(new RetainedOnBlockChangeComponent(2));

    LifecycleEventChecker checker =
        new LifecycleEventChecker(entityManager.getEventSystem(), NetworkComponent.class);
    entity.removeComponent(RetainedOnBlockChangeComponent.class);

    worldProvider.update(1.0f);

    assertEquals(
        Lists.newArrayList(
            new EventInfo(BeforeDeactivateComponent.newInstance(), entity),
            new EventInfo(BeforeRemoveComponent.newInstance(), entity)),
        checker.receivedEvents);
  }
  private void handleDelayedActionsEvents() {
    long worldTime = time.getGameTimeInMs();
    BlockAtLocationDelayedAction action;
    while ((action = delayedActions.peek()) != null && action.executeTime <= worldTime) {
      action = delayedActions.poll();

      final Vector3i actionLocation = action.blockLocation.toVector3i();
      if (worldProvider.isBlockRelevant(actionLocation)) {
        final Block block = worldProvider.getBlock(actionLocation);
        final BlockFamily blockFamily = block.getBlockFamily();

        final EntityRef blockEntity = blockEntityRegistry.getBlockEntityAt(actionLocation);

        if (blockFamily == signalOnDelayGate) {
          startProducingSignal(blockEntity, -1);
        } else if (blockFamily == signalOffDelayGate) {
          stopProducingSignal(blockEntity);
        } else if (blockFamily == signalOrGate
            || blockFamily == signalAndGate
            || blockFamily == signalXorGate) {
          if (processOutputForNormalGate(blockEntity)) {
            gateLastSignalChangeTime.put(new ImmutableBlockLocation(actionLocation), worldTime);
          }
        } else if (blockFamily == signalNandGate) {
          if (processOutputForRevertedGate(blockEntity)) {
            gateLastSignalChangeTime.put(new ImmutableBlockLocation(actionLocation), worldTime);
          }
        } else if (blockFamily == signalSetResetGate) {
          if (processOutputForSetResetGate(blockEntity)) {
            gateLastSignalChangeTime.put(new ImmutableBlockLocation(actionLocation), worldTime);
          }
        } else if (block == signalButton) {
          stopProducingSignal(blockEntity);
        }

        blockEntity.removeComponent(SignalDelayedActionComponent.class);
      } else {
        // TODO Remove this workaround when BlockEntities will be stored with the chunk they belong
        // to
        action.executeTime += NOT_LOADED_BLOCK_RETRY_DELAY;
        delayedActions.add(action);
      }
    }
  }
  @ReceiveEvent
  public void expireResistDamageEffect(
      DelayedActionTriggeredEvent event, EntityRef entity, ResistDamageComponent component) {
    final String actionId = event.getActionId();
    if (actionId.startsWith(AlterationEffects.EXPIRE_TRIGGER_PREFIX)) {
      String effectName = actionId.substring(AlterationEffects.EXPIRE_TRIGGER_PREFIX.length());
      String[] split = actionId.split(":");

      if (split.length != 4) {
        return;
      }

      if (split[2].equalsIgnoreCase(AlterationEffects.RESIST_DAMAGE)) {
        component.rdes.remove(split[3]);

        if (component.rdes.size() == 0 && component != null) {
          entity.removeComponent(ResistDamageComponent.class);
        }
      }
    }
  }