public bool SpawnEntity(IEntity entity) { var player = _client.Player; if (player.Id == entity.Id) { return(false); } var chunkManager = _world.ChunkManager; var chunkPosition = ChunkPosition.FromWorld(entity.Position); if (!chunkManager.IsLoaded(chunkPosition)) { Logger.Warn("Trying to spawn {0} in an unloaded chunk", entity); return(false); } if (!_loadedEntities.Add(entity)) { return(false); } Logger.Info("Spawning entity #{0} on {1}", entity.Id, _client); var connection = _client.Connection; connection.SpawnEntity(entity); return(true); }
public IBlockState GetBlock(Vector3Int position) { var chunkPos = ChunkPosition.FromWorld(position); if (!ChunkManager.IsLoaded(chunkPos)) { return(null); } var chunk = ChunkManager.GetChunk(chunkPos); var index = Chunk.SectionIndex(position.Y); if (!chunk.HasSection(index)) { return(null); } var section = chunk[index]; return(section.BlockStorage.GetBlock( position.X % Minecraft.Units.Chunk.Size, position.Y % Minecraft.Units.Chunk.SectionHeight, position.Z % Minecraft.Units.Chunk.Size )); }
private void OnEntitySpawned(object sender, EntityEventArgs e) { var entity = e.Entity; var position = ChunkPosition.FromWorld(entity.Position); var chunkManager = _world.ChunkManager; if (chunkManager.IsLoaded(position)) { SpawnEntity(entity); } }
private void OnWorldContextRegistered(object sender, WorldContextEventArgs e) { var chunkManager = _world.ChunkManager; var worldContext = e.WorldContext; var entityManager = worldContext.EntityManager; foreach (var entity in entityManager) { var chunkPosition = ChunkPosition.FromWorld(entity.Position); if (chunkManager.IsLoaded(chunkPosition)) { SpawnEntity(entity); } } entityManager.EntitySpawned += OnEntitySpawned; entityManager.EntityRemoved += OnEntityRemoved; }
public void SetBlock(Vector3Int position, IBlockState blockState) { var chunkPos = ChunkPosition.FromWorld(position); if (!ChunkManager.IsLoaded(chunkPos)) { return; } var chunk = ChunkManager.GetChunk(chunkPos); var index = Chunk.SectionIndex(position.Y); var section = chunk.CreateSection(index); section.BlockStorage.SetBlock( position.X % Minecraft.Units.Chunk.Size, position.Y % Minecraft.Units.Chunk.SectionHeight, position.Z % Minecraft.Units.Chunk.Size, blockState ); }
private void OnChunkUnloaded(object sender, ChunkEventArgs e) { var toRemove = new HashSet <IEntity>(); var unloadedChunk = e.Position; var worldContexts = _world.WorldContexts; foreach (var worldContext in worldContexts) { var entityManager = worldContext.EntityManager; foreach (var entity in entityManager) { var chunkPosition = ChunkPosition.FromWorld(entity.Position); if (unloadedChunk != chunkPosition) { continue; } toRemove.Add(entity); } } RemoveEntities(toRemove); }
public IEnumerable <IEntity> GetEntities(ChunkPosition position) { return(_entities.Where(e => ChunkPosition.FromWorld(e.Position) == position)); }