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;
 }
  @Test
  public void allComponentsNotMarkedAsRetainedRemovedOnBlockChange() {
    worldStub.setBlock(Vector3i.zero(), blockWithString);
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    entity.addComponent(new ForceBlockActiveComponent());
    entity.addComponent(new RetainedOnBlockChangeComponent(2));

    worldProvider.setBlock(Vector3i.zero(), BlockManager.getAir());

    assertTrue(entity.hasComponent(RetainedOnBlockChangeComponent.class));
    assertFalse(entity.hasComponent(ForceBlockActiveComponent.class));
  }
  @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());
    }
  }
  @Override
  public boolean validateBeforeStart(
      EntityRef instigator, EntityRef workstation, EntityRef processEntity) {
    if (!workstation.hasComponent(WorkstationInventoryComponent.class)) {
      return false;
    }

    // Defer the heat calculation until it is actually needed
    Float heat = null;

    for (int slot : WorkstationInventoryUtils.getAssignedSlots(workstation, "INPUT")) {
      HeatProcessedComponent processed =
          InventoryUtils.getItemAt(workstation, slot).getComponent(HeatProcessedComponent.class);
      if (processed != null) {
        float heatRequired = processed.heatRequired;
        if (heat == null) {
          heat =
              HeatUtils.calculateHeatForEntity(
                  workstation, CoreRegistry.get(BlockEntityRegistry.class));
        }
        if (heatRequired <= heat) {
          final String result =
              processed.blockResult != null ? processed.blockResult : processed.itemResult;
          if (canOutputResult(workstation, result)) {
            processEntity.addComponent(new SpecificInputSlotComponent(slot));
            processEntity.addComponent(new OutputTypeComponent(result));
            return true;
          }
        }
      }
    }

    return false;
  }
 @Override
 public boolean isValid(EntityRef workstation, int slotNo, EntityRef instigator, EntityRef item) {
   if (isInputSlot(workstation, slotNo)) {
     return item.hasComponent(HeatProcessedComponent.class);
   }
   return workstation == instigator;
 }
 @ReceiveEvent(components = {SignalConsumerStatusComponent.class})
 public void consumerModified(OnChangedComponent event, EntityRef entity) {
   if (entity.hasComponent(BlockComponent.class)) {
     SignalConsumerStatusComponent consumerStatusComponent =
         entity.getComponent(SignalConsumerStatusComponent.class);
     Vector3i blockLocation =
         new Vector3i(entity.getComponent(BlockComponent.class).getPosition());
     Block block = worldProvider.getBlock(blockLocation);
     BlockFamily blockFamily = block.getBlockFamily();
     if (block == lampTurnedOff && consumerStatusComponent.hasSignal) {
       logger.debug("Lamp turning on");
       worldProvider.setBlock(blockLocation, lampTurnedOn);
     } else if (block == lampTurnedOn && !consumerStatusComponent.hasSignal) {
       logger.debug("Lamp turning off");
       worldProvider.setBlock(blockLocation, lampTurnedOff);
     } else if (blockFamily == signalOrGate
         || blockFamily == signalAndGate
         || blockFamily == signalXorGate) {
       logger.debug("Signal changed for gate");
       signalChangedForNormalGate(entity, consumerStatusComponent);
     } else if (blockFamily == signalNandGate) {
       signalChangedForNotGate(entity, consumerStatusComponent);
     } else if (blockFamily == signalOnDelayGate) {
       signalChangedForDelayOnGate(entity, consumerStatusComponent);
     } else if (blockFamily == signalOffDelayGate) {
       signalChangedForDelayOffGate(entity, consumerStatusComponent);
     }
   }
 }
Example #7
0
 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 onEntityComponentAdded(EntityRef entity, Class<? extends Component> component) {
   if (temporaryBlockEntities.contains(entity)
       && entityManager.getComponentLibrary().getMetadata(component).isForceBlockActive()) {
     temporaryBlockEntities.remove(entity);
     if (!entity.hasComponent(NetworkComponent.class)) {
       entity.addComponent(new NetworkComponent());
     }
   }
 }
  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);
  }
 @SuppressWarnings("unchecked")
 private <T extends Component> void copyIntoPrefab(
     EntityRef blockEntity, T comp, Set<Class<? extends Component>> retainComponents) {
   ComponentMetadata<T> metadata =
       entityManager.getComponentLibrary().getMetadata((Class<T>) comp.getClass());
   if (!blockEntity.hasComponent(comp.getClass())) {
     blockEntity.addComponent(metadata.copyRaw(comp));
   } else if (!metadata.isRetainUnalteredOnBlockChange()
       && !retainComponents.contains(metadata.getType())) {
     updateComponent(blockEntity, metadata, comp);
   }
 }
 @ReceiveEvent(components = {SignalConsumerAdvancedStatusComponent.class})
 public void advancedConsumerModified(OnChangedComponent event, EntityRef entity) {
   if (entity.hasComponent(BlockComponent.class)) {
     Vector3i blockLocation =
         new Vector3i(entity.getComponent(BlockComponent.class).getPosition());
     Block block = worldProvider.getBlock(blockLocation);
     BlockFamily blockFamily = block.getBlockFamily();
     if (blockFamily == signalSetResetGate) {
       delayGateSignalChangeIfNeeded(entity);
     }
   }
 }
  @Test
  public void networkComponentAddedWhenChangedToNonTemporary() {
    LifecycleEventChecker checker =
        new LifecycleEventChecker(entityManager.getEventSystem(), NetworkComponent.class);
    EntityRef entity = worldProvider.getBlockEntityAt(new Vector3i(0, 0, 0));
    entity.addComponent(new RetainedOnBlockChangeComponent(2));

    assertEquals(
        Lists.newArrayList(
            new EventInfo(OnAddedComponent.newInstance(), entity),
            new EventInfo(OnActivatedComponent.newInstance(), entity)),
        checker.receivedEvents);
    assertTrue(entity.hasComponent(NetworkComponent.class));
  }
 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);
   }
 }
 private void delayGateSignalChangeIfNeeded(EntityRef entity) {
   if (!entity.hasComponent(SignalDelayedActionComponent.class)) {
     // Schedule for the gate to be looked either immediately (during "update" method) or at least
     // GATE_MINIMUM_SIGNAL_CHANGE_INTERVAL from the time it has last changed, whichever is later
     SignalDelayedActionComponent delayedAction = new SignalDelayedActionComponent();
     long whenToLookAt;
     final ImmutableBlockLocation location =
         new ImmutableBlockLocation(entity.getComponent(BlockComponent.class).getPosition());
     if (gateLastSignalChangeTime.containsKey(location)) {
       whenToLookAt = gateLastSignalChangeTime.get(location) + GATE_MINIMUM_SIGNAL_CHANGE_INTERVAL;
     } else {
       whenToLookAt = time.getGameTimeInMs();
     }
     delayedAction.executeTime = whenToLookAt;
     entity.addComponent(delayedAction);
   }
 }