Search in sources :

Example 1 with EventManager

use of org.spongepowered.api.event.EventManager in project SpongeAPI by SpongePowered.

the class SimpleServiceManagerTest method mockEventManager.

@Before
public void mockEventManager() throws Exception {
    this.manager = mock(PluginManager.class);
    this.testPluginContainer = mock(PluginContainer.class);
    when(this.testPluginContainer.getId()).thenReturn("TestPlugin");
    when(this.manager.fromInstance(this.testPlugin)).thenReturn(Optional.of(this.testPluginContainer));
    Game game = mock(Game.class);
    TestHooks.setInstance("eventManager", mock(EventManager.class));
    when(game.getEventManager()).thenReturn(mock(EventManager.class));
    CauseStackManager csm = mock(CauseStackManager.class);
    when(game.getCauseStackManager()).thenReturn(csm);
    when(csm.pushCause(null)).thenReturn(csm);
    when(csm.popCause()).thenReturn(null);
    when(csm.getCurrentCause()).thenReturn(Cause.of(EventContext.empty(), this));
    CauseStackManager.StackFrame sf = mock(CauseStackManager.StackFrame.class);
    when(sf.pushCause(null)).thenReturn(sf);
    when(sf.addContext(null, null)).thenReturn(sf);
    when(sf.popCause()).thenReturn(null);
    when(sf.getCurrentCause()).thenReturn(Cause.of(EventContext.empty(), this));
    when(csm.pushCauseFrame()).thenReturn(sf);
    TestHooks.setGame(game);
    TestHooks.setInstance("causeStackManager", csm);
}
Also used : PluginManager(org.spongepowered.api.plugin.PluginManager) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Game(org.spongepowered.api.Game) EventManager(org.spongepowered.api.event.EventManager) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Before(org.junit.Before)

Example 2 with EventManager

use of org.spongepowered.api.event.EventManager in project LanternServer by LanternPowered.

the class LanternChunkManager method populateChunk.

private void populateChunk(LanternChunk chunk, Cause cause, Random random) {
    chunk.populating = true;
    // Populate
    int chunkX = chunk.getX() * 16;
    int chunkZ = chunk.getZ() * 16;
    long worldSeed = this.world.getProperties().getSeed();
    random.setSeed(worldSeed);
    long xSeed = random.nextLong() / 2 * 2 + 1;
    long zSeed = random.nextLong() / 2 * 2 + 1;
    long chunkSeed = xSeed * chunkX + zSeed * chunkZ ^ worldSeed;
    random.setSeed(chunkSeed);
    // noinspection ConstantConditions
    final ChunkBiomeBuffer biomeBuffer = this.genBuffers.get().chunkBiomeBuffer;
    biomeBuffer.reuse(new Vector3i(chunkX + 8, 0, chunkZ + 8));
    // We ave to regenerate the biomes so that any
    // virtual biomes can be passed to the populator.
    final BiomeGenerator biomeGenerator = this.worldGenerator.getBiomeGenerator();
    biomeGenerator.generateBiomes(biomeBuffer);
    // Initialize the biomes into the chunk
    final ImmutableBiomeVolume immutableBiomeVolume = biomeBuffer.getImmutableBiomeCopy();
    chunk.initializeBiomes(biomeBuffer.detach().clone());
    // Using the biome at an arbitrary point within the chunk
    // ({16, 0, 16} in the vanilla game)
    final BiomeType biomeType = immutableBiomeVolume.getBiome(chunkX + 16, 0, chunkZ + 16);
    // Get the generation settings
    final BiomeGenerationSettings biomeGenSettings = this.worldGenerator.getBiomeSettings(biomeType);
    final List<Populator> populators = new LinkedList<>(biomeGenSettings.getPopulators());
    populators.addAll(this.worldGenerator.getPopulators());
    final EventManager eventManager = Sponge.getEventManager();
    final Vector3i min = new Vector3i(chunkX + 8, 0, chunkZ + 8);
    final Extent volume = new SoftBufferExtentViewDownsize(chunk.getWorld(), min, min.add(15, 0, 15), min.sub(8, 0, 8), min.add(23, 0, 23));
    // Call the pre populate event, this allows
    // modifications to the populators list
    // Called before a chunk begins populating. (javadoc)
    eventManager.post(SpongeEventFactory.createPopulateChunkEventPre(cause, populators, chunk));
    // First populate the chunk with the biome populators
    for (Populator populator : populators) {
        // Called when a populator is about to run against a chunk. (javadoc)
        eventManager.post(SpongeEventFactory.createPopulateChunkEventPopulate(cause, populator, chunk));
        populator.populate(this.world, volume, random);
    }
    // Called when a chunk finishes populating. (javadoc)
    eventManager.post(SpongeEventFactory.createPopulateChunkEventPost(cause, ImmutableList.copyOf(populators), chunk));
    this.world.getEventListener().onPopulateChunk(chunk);
    // We are done
    chunk.populated = true;
    chunk.populating = false;
}
Also used : ImmutableBiomeVolume(org.spongepowered.api.world.extent.ImmutableBiomeVolume) EventManager(org.spongepowered.api.event.EventManager) Extent(org.spongepowered.api.world.extent.Extent) BiomeGenerator(org.spongepowered.api.world.gen.BiomeGenerator) LinkedList(java.util.LinkedList) VirtualBiomeType(org.spongepowered.api.world.biome.VirtualBiomeType) BiomeType(org.spongepowered.api.world.biome.BiomeType) SoftBufferExtentViewDownsize(org.lanternpowered.server.world.extent.SoftBufferExtentViewDownsize) Vector3i(com.flowpowered.math.vector.Vector3i) BiomeGenerationSettings(org.spongepowered.api.world.biome.BiomeGenerationSettings) GenerationPopulator(org.spongepowered.api.world.gen.GenerationPopulator) Populator(org.spongepowered.api.world.gen.Populator)

Example 3 with EventManager

use of org.spongepowered.api.event.EventManager in project LanternServer by LanternPowered.

the class LanternChunkManager method generate.

/**
 * Attempts to generate the chunk.
 *
 * @param chunk The chunk
 * @param cause The cause
 */
private void generate(LanternChunk chunk, Cause cause) {
    final EventManager eventManager = Sponge.getEventManager();
    eventManager.post(SpongeEventFactory.createGenerateChunkEventPre(cause, chunk));
    final GenerationBuffers buffers = this.genBuffers.get();
    // noinspection ConstantConditions
    final ChunkBiomeBuffer biomeBuffer = buffers.chunkBiomeBuffer;
    biomeBuffer.reuse(new Vector3i(chunk.getX() << 4, 0, chunk.getZ() << 4));
    // Generate the biomes
    final BiomeGenerator biomeGenerator = this.worldGenerator.getBiomeGenerator();
    biomeGenerator.generateBiomes(biomeBuffer);
    // Initialize the biomes into the chunk
    final ImmutableBiomeVolume immutableBiomeVolume = biomeBuffer.getImmutableBiomeCopy();
    chunk.initializeBiomes(biomeBuffer.detach().clone());
    final ChunkBlockBuffer blockBuffer = buffers.chunkBlockBuffer;
    blockBuffer.reuse(new Vector3i(chunk.getX() << 4, 0, chunk.getZ() << 4));
    // Apply the main world generator
    final GenerationPopulator baseGenerator = this.worldGenerator.getBaseGenerationPopulator();
    baseGenerator.populate(this.world, blockBuffer, immutableBiomeVolume);
    // Get all the used biome types
    final Set<BiomeType> biomeTypes = ImmutableSet.copyOf(biomeBuffer.biomeTypes);
    for (BiomeType biomeType : biomeTypes) {
        final BiomeGenerationSettings settings = this.worldGenerator.getBiomeSettings(biomeType);
        for (GenerationPopulator generator : settings.getGenerationPopulators()) {
            generator.populate(this.world, blockBuffer, immutableBiomeVolume);
        }
    }
    // Apply the generator populators to complete the block buffer
    for (GenerationPopulator generator : this.worldGenerator.getGenerationPopulators()) {
        generator.populate(this.world, blockBuffer, immutableBiomeVolume);
    }
    // Create the chunk sections
    final ChunkSection[] sections = new ChunkSection[CHUNK_SECTIONS];
    for (int sy = 0; sy < CHUNK_SECTIONS; sy++) {
        final int nonAirCount = blockBuffer.nonAirCount[sy];
        if (nonAirCount > 0) {
            sections[sy] = new ChunkSection(blockBuffer.types[sy]);
        }
    }
    // Initialize the chunk
    chunk.initializeSections(sections);
    chunk.initializeHeightMap(null);
    chunk.initializeLight();
    eventManager.post(SpongeEventFactory.createGenerateChunkEventPost(cause, chunk));
}
Also used : ImmutableBiomeVolume(org.spongepowered.api.world.extent.ImmutableBiomeVolume) EventManager(org.spongepowered.api.event.EventManager) BiomeGenerator(org.spongepowered.api.world.gen.BiomeGenerator) VirtualBiomeType(org.spongepowered.api.world.biome.VirtualBiomeType) BiomeType(org.spongepowered.api.world.biome.BiomeType) GenerationPopulator(org.spongepowered.api.world.gen.GenerationPopulator) Vector3i(com.flowpowered.math.vector.Vector3i) BiomeGenerationSettings(org.spongepowered.api.world.biome.BiomeGenerationSettings) ChunkSection(org.lanternpowered.server.world.chunk.LanternChunk.ChunkSection)

Aggregations

EventManager (org.spongepowered.api.event.EventManager)3 Vector3i (com.flowpowered.math.vector.Vector3i)2 BiomeGenerationSettings (org.spongepowered.api.world.biome.BiomeGenerationSettings)2 BiomeType (org.spongepowered.api.world.biome.BiomeType)2 VirtualBiomeType (org.spongepowered.api.world.biome.VirtualBiomeType)2 ImmutableBiomeVolume (org.spongepowered.api.world.extent.ImmutableBiomeVolume)2 BiomeGenerator (org.spongepowered.api.world.gen.BiomeGenerator)2 GenerationPopulator (org.spongepowered.api.world.gen.GenerationPopulator)2 LinkedList (java.util.LinkedList)1 Before (org.junit.Before)1 ChunkSection (org.lanternpowered.server.world.chunk.LanternChunk.ChunkSection)1 SoftBufferExtentViewDownsize (org.lanternpowered.server.world.extent.SoftBufferExtentViewDownsize)1 Game (org.spongepowered.api.Game)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 PluginContainer (org.spongepowered.api.plugin.PluginContainer)1 PluginManager (org.spongepowered.api.plugin.PluginManager)1 Extent (org.spongepowered.api.world.extent.Extent)1 Populator (org.spongepowered.api.world.gen.Populator)1