use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class StorageManagerTest method setup.
@BeforeEach
public void setup(@TempDir Path tempHome) throws Exception {
super.setup();
PathManager.getInstance().useOverrideHomePath(tempHome);
savePath = PathManager.getInstance().getSavePath("testSave");
assert !Files.isRegularFile(tempHome.resolve("global.dat"));
entityManager = context.get(EngineEntityManager.class);
moduleEnvironment = mock(ModuleEnvironment.class);
blockManager = context.get(BlockManager.class);
extraDataManager = context.get(ExtraBlockDataManager.class);
ModuleManager moduleManager = mock(ModuleManager.class);
when(moduleManager.getEnvironment()).thenReturn(moduleEnvironment);
RecordedEventStore recordedEventStore = new RecordedEventStore();
recordAndReplayUtils = new RecordAndReplayUtils();
CharacterStateEventPositionMap characterStateEventPositionMap = new CharacterStateEventPositionMap();
DirectionAndOriginPosRecorderList directionAndOriginPosRecorderList = new DirectionAndOriginPosRecorderList();
recordAndReplaySerializer = new RecordAndReplaySerializer(entityManager, recordedEventStore, recordAndReplayUtils, characterStateEventPositionMap, directionAndOriginPosRecorderList, moduleManager, mock(TypeRegistry.class));
recordAndReplayCurrentStatus = context.get(RecordAndReplayCurrentStatus.class);
esm = new ReadWriteStorageManager(savePath, moduleEnvironment, entityManager, blockManager, extraDataManager, false, recordAndReplaySerializer, recordAndReplayUtils, recordAndReplayCurrentStatus);
context.put(StorageManager.class, esm);
this.character = entityManager.create();
Client client = createClientMock(PLAYER_ID, character);
NetworkSystem networkSystem = mock(NetworkSystem.class);
when(networkSystem.getMode()).thenReturn(NetworkMode.NONE);
when(networkSystem.getPlayers()).thenReturn(Arrays.asList(client));
context.put(NetworkSystem.class, networkSystem);
AssetManager assetManager = context.get(AssetManager.class);
BlockFamilyDefinitionData data = new BlockFamilyDefinitionData();
data.setBlockFamily(SymmetricFamily.class);
assetManager.loadAsset(new ResourceUrn("test:testblock"), data, BlockFamilyDefinition.class);
assetManager.loadAsset(new ResourceUrn("test:testblock2"), data, BlockFamilyDefinition.class);
testBlock = context.get(BlockManager.class).getBlock("test:testblock");
testBlock2 = context.get(BlockManager.class).getBlock("test:testblock2");
context.put(ChunkProvider.class, mock(ChunkProvider.class));
WorldProvider worldProvider = mock(WorldProvider.class);
when(worldProvider.getWorldInfo()).thenReturn(new WorldInfo());
context.put(WorldProvider.class, worldProvider);
}
use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class StorageManagerTest method createClientMock.
private Client createClientMock(String clientId, EntityRef charac) {
EntityRef clientEntity = createClientEntity(charac);
Client client = mock(Client.class);
when(client.getEntity()).thenReturn(clientEntity);
when(client.getId()).thenReturn(clientId);
return client;
}
use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class PlayerSystem method onConnect.
@ReceiveEvent(components = ClientComponent.class)
public void onConnect(ConnectedEvent connected, EntityRef entity) {
LocationComponent loc = entity.getComponent(LocationComponent.class);
// for new clients, the player store will return default values
PlayerStore playerStore = connected.getPlayerStore();
Client owner = networkSystem.getOwner(entity);
Vector3ic minViewDist = ViewDistance.LEGALLY_BLIND.getChunkDistance();
if (playerStore.hasCharacter()) {
Vector3fc storedLocation = playerStore.getRelevanceLocation();
loc.setWorldPosition(storedLocation);
entity.saveComponent(loc);
if (worldProvider.isBlockRelevant(storedLocation)) {
// chunk for spawning location is ready, so spawn right now
playerStore.restoreEntities();
EntityRef character = playerStore.getCharacter();
Vector3ic viewDist = owner.getViewDistance().getChunkDistance();
addRelevanceEntity(entity, viewDist, owner);
restoreCharacter(entity, character);
} else {
// otherwise wait until chunk is ready
addRelevanceEntity(entity, minViewDist, owner);
clientsPreparingToSpawn.add(new SpawningClientInfo(entity, storedLocation, playerStore));
}
} else {
Vector3fc spawnPosition = worldGenerator.getSpawnPosition(entity);
loc.setWorldPosition(spawnPosition);
entity.saveComponent(loc);
addRelevanceEntity(entity, minViewDist, owner);
if (worldProvider.isBlockRelevant(spawnPosition)) {
spawnPlayer(entity);
} else {
clientsPreparingToSpawn.add(new SpawningClientInfo(entity, spawnPosition));
}
}
}
use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class NetworkSystemImpl method update.
@Override
public void update() {
if (mode != NetworkMode.NONE) {
if (entityManager != null) {
processPendingConnections();
processPendingDisconnects();
long currentTimer = time.getRealTimeInMs();
boolean netTick = false;
if (currentTimer > nextNetworkTick) {
nextNetworkTick += NET_TICK_RATE;
netTick = true;
}
PerformanceMonitor.startActivity("Client update");
for (Client client : clientList) {
client.update(netTick);
}
PerformanceMonitor.endActivity();
if (server != null) {
server.update(netTick);
}
}
}
}
use of org.terasology.engine.network.Client in project Terasology by MovingBlocks.
the class EventSystemReplayImpl method broadcastEvent.
private void broadcastEvent(EntityRef entity, Event event, EventMetadata metadata) {
if (networkSystem.getMode().isServer()) {
NetworkComponent netComp = entity.getComponent(NetworkComponent.class);
BlockComponent blockComp = entity.getComponent(BlockComponent.class);
if (netComp != null || blockComp != null) {
Client instigatorClient = null;
if (metadata.isSkipInstigator() && event instanceof NetworkEvent) {
instigatorClient = networkSystem.getOwner(((NetworkEvent) event).getInstigator());
}
for (Client client : networkSystem.getPlayers()) {
if (!client.equals(instigatorClient)) {
client.send(event, entity);
}
}
}
}
}
Aggregations