private void handlePressurePlateEvents() { Set<Vector3i> toRemoveSignal = Sets.newHashSet(activatedPressurePlates); Iterable<EntityRef> players = entityManager.getEntitiesWith(CharacterComponent.class, LocationComponent.class); for (EntityRef player : players) { Vector3f playerLocation = player.getComponent(LocationComponent.class).getWorldPosition(); Vector3i locationBeneathPlayer = new Vector3i(playerLocation.x + 0.5f, playerLocation.y - 0.5f, playerLocation.z + 0.5f); Block blockBeneathPlayer = worldProvider.getBlock(locationBeneathPlayer); if (blockBeneathPlayer == signalPressurePlate) { EntityRef entityBeneathPlayer = blockEntityRegistry.getBlockEntityAt(locationBeneathPlayer); SignalProducerComponent signalProducer = entityBeneathPlayer.getComponent(SignalProducerComponent.class); if (signalProducer != null) { if (signalProducer.signalStrength == 0) { startProducingSignal(entityBeneathPlayer, -1); activatedPressurePlates.add(locationBeneathPlayer); } else { toRemoveSignal.remove(locationBeneathPlayer); } } } } for (Vector3i pressurePlateLocation : toRemoveSignal) { EntityRef pressurePlate = blockEntityRegistry.getBlockEntityAt(pressurePlateLocation); SignalProducerComponent signalProducer = pressurePlate.getComponent(SignalProducerComponent.class); if (signalProducer != null) { stopProducingSignal(pressurePlate); activatedPressurePlates.remove(pressurePlateLocation); } } }
@ReceiveEvent(components = {FenceGateComponent.class}) public void onActivate(ActivateEvent event, EntityRef entity) { FenceGateComponent fenceGateComponent = entity.getComponent(FenceGateComponent.class); fenceGateComponent.isClosed = !fenceGateComponent.isClosed; entity.saveComponent(fenceGateComponent); BlockComponent blockComp = entity.getComponent(BlockComponent.class); if (blockComp == null) { event.cancel(); return; } Vector3i primePos = new Vector3i(blockComp.getPosition()); Block primeBlock = worldProvider.getBlock(primePos); Block newBlock = null; if (fenceGateComponent.isClosed) { newBlock = BlockManager.getInstance() .getBlockFamily("fences:FenceGateClosed") .getBlockForPlacing(worldProvider, primePos, primeBlock.getDirection(), Side.FRONT); } else { newBlock = BlockManager.getInstance() .getBlockFamily("fences:FenceGateOpen") .getBlockForPlacing(worldProvider, primePos, primeBlock.getDirection(), Side.FRONT); } if (newBlock != null) { blockEntityRegistry.setBlock(primePos, newBlock, primeBlock, entity); } }
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()); } }
// Generates all non-temporary block entities private void generateBlockEntities(Chunk chunk) { ChunkBlockIterator i = chunk.getBlockIterator(); while (i.next()) { if (i.getBlock().isKeepActive()) { registry.getBlockEntityAt(i.getBlockPos()); } } }
@Override public boolean isConnectingTo( Vector3i blockLocation, Side connectSide, WorldProvider worldProvider, BlockEntityRegistry blockEntityRegistry) { Vector3i neighborLocation = new Vector3i(blockLocation); neighborLocation.add(connectSide.getVector3i()); EntityRef neighborEntity = blockEntityRegistry.getBlockEntityAt(neighborLocation); return neighborEntity != null && connectsToNeighbor(neighborEntity); }
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); } } }
private void processEvent(NetData.EventMessage message) { try { Event event = eventSerializer.deserialize(message.getEvent()); EntityRef target = EntityRef.NULL; if (message.hasTargetBlockPos()) { target = blockEntityRegistry.getBlockEntityAt( NetMessageUtil.convert(message.getTargetBlockPos())); } else if (message.hasTargetId()) { target = networkSystem.getEntity(message.getTargetId()); } if (target.exists()) { target.send(event); } else { logger.info( "Dropping event {} for unavailable entity {}", event.getClass().getSimpleName(), target); } } catch (DeserializationException e) { logger.error("Failed to deserialize event", e); } }
@ReceiveEvent public void onActivate( ActivateEvent event, EntityRef entity, TunnelActionComponent tunnelActionComponent) { Vector3f dir = new Vector3f(event.getDirection()); dir.scale(4.0f); Vector3f origin = new Vector3f(event.getOrigin()); origin.add(dir); Vector3i blockPos = new Vector3i(); int particleEffects = 0; int blockCounter = tunnelActionComponent.maxDestroyedBlocks; for (int s = 0; s <= tunnelActionComponent.maxTunnelDepth; s++) { origin.add(dir); if (!worldProvider.isBlockRelevant(origin)) { break; } for (int i = 0; i < tunnelActionComponent.maxRaysCast; i++) { Vector3f direction = random.nextVector3f(); Vector3f impulse = new Vector3f(direction); impulse.scale(tunnelActionComponent.explosiveForce); for (int j = 0; j < 3; j++) { Vector3f target = new Vector3f(origin); target.x += direction.x * j; target.y += direction.y * j; target.z += direction.z * j; blockPos.set((int) target.x, (int) target.y, (int) target.z); Block currentBlock = worldProvider.getBlock(blockPos); if (currentBlock.isDestructible()) { if (particleEffects < tunnelActionComponent.maxParticalEffects) { EntityBuilder builder = entityManager.newBuilder("engine:smokeExplosion"); builder.getComponent(LocationComponent.class).setWorldPosition(target); builder.build(); particleEffects++; } if (random.nextFloat() < tunnelActionComponent.thoroughness) { EntityRef blockEntity = blockEntityRegistry.getEntityAt(blockPos); blockEntity.send( new DoDamageEvent( tunnelActionComponent.damageAmount, tunnelActionComponent.damageType)); } blockCounter--; } if (blockCounter <= 0) { return; } } } } // No blocks were destroyed, so cancel the event if (blockCounter == tunnelActionComponent.maxDestroyedBlocks) { event.consume(); } }
public void updateTarget() { if (!target.exists() && targetBlockPos != null && blockRegistry != null) { target = blockRegistry.getEntityAt(targetBlockPos); } }