Search in sources :

Example 21 with CauseStack

use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.

the class LanternWorldManager method loadWorld.

/**
 * Loads a {@link World} for the world entry if possible.
 *
 * @param worldEntry the world entry
 * @return the world, if found
 */
private Optional<World> loadWorld(@Nullable WorldLookupEntry worldEntry) {
    if (worldEntry == null) {
        return Optional.empty();
    }
    if (worldEntry.world != null) {
        return Optional.of(worldEntry.world);
    }
    WorldConfigResult result;
    try {
        result = getOrCreateWorldConfig(worldEntry.properties.getWorldName());
    } catch (IOException e) {
        this.game.getLogger().error("Unable to read the world config, please fix this issue before loading the world.", e);
        return Optional.empty();
    }
    Scoreboard scoreboard;
    try {
        scoreboard = ScoreboardIO.read(worldEntry.folder);
    } catch (IOException e) {
        this.logger.error("Unable to read the scoreboard data.", e);
        scoreboard = Scoreboard.builder().build();
    }
    // Create the world instance
    final LanternWorld world = new LanternWorld(this.game, result.config, worldEntry.folder, scoreboard, worldEntry.properties);
    // Share the world instance
    worldEntry.world = world;
    worldEntry.properties.setWorld(world);
    // Initialize the world if not done before
    world.initialize();
    // Generate the spawn if needed
    if (worldEntry.properties.doesKeepSpawnLoaded()) {
        world.enableSpawnArea(true);
    }
    // Load the chunk loading tickets, they may load some chunks
    try {
        world.getChunkManager().loadTickets();
    } catch (IOException e) {
        this.logger.warn("An error occurred while loading the chunk loading tickets", e);
    }
    final CauseStack causeStack = CauseStack.currentOrEmpty();
    causeStack.pushCause(world);
    final LoadWorldEvent event = SpongeEventFactory.createLoadWorldEvent(causeStack.getCurrentCause(), world);
    causeStack.popCause();
    Sponge.getEventManager().post(event);
    if (event.isCancelled()) {
        return Optional.empty();
    }
    // The world is ready for ticks
    addWorldTask(world);
    return Optional.of(world);
}
Also used : LanternCauseStack(org.lanternpowered.server.event.LanternCauseStack) CauseStack(org.lanternpowered.server.event.CauseStack) LoadWorldEvent(org.spongepowered.api.event.world.LoadWorldEvent) Scoreboard(org.spongepowered.api.scoreboard.Scoreboard) IOException(java.io.IOException)

Example 22 with CauseStack

use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.

the class LanternWorldManager method createWorld.

/**
 * Creates the world properties for the specified settings and the dimension id.
 *
 * @param worldArchetype The world archetype
 * @param folderName The folder name
 * @param dimensionId The dimension id
 * @return The new or existing world properties, if creation was successful
 */
private LanternWorldProperties createWorld(WorldArchetype worldArchetype, String folderName, int dimensionId) throws IOException {
    final LanternWorldArchetype settings0 = (LanternWorldArchetype) checkNotNull(worldArchetype, "worldArchetype");
    final String worldName = worldArchetype.getName();
    WorldLookupEntry entry = this.worldByName.get(worldName);
    if (entry != null) {
        return entry.properties;
    }
    WorldConfigResult worldConfigResult;
    // Create a config
    try {
        worldConfigResult = getOrCreateWorldConfig(worldName);
    } catch (IOException e) {
        throw new IOException("Unable to read/write the world config, please fix this issue before" + " creating the world.", e);
    }
    // Create the world properties
    final LanternWorldProperties worldProperties = new LanternWorldProperties(settings0.getName(), worldConfigResult.config);
    if (worldConfigResult.newCreated) {
        worldProperties.update(settings0);
    }
    // Get the world folder
    final Path worldFolder = getWorldFolder(folderName, dimensionId);
    try {
        Files.createDirectories(worldFolder);
    } catch (IOException e) {
        throw new IOException("Unable to create the world folders for " + settings0.getName(), e);
    }
    // Store the new properties
    addWorldProperties(worldProperties, worldFolder, dimensionId);
    final CauseStack causeStack = CauseStack.currentOrEmpty();
    Sponge.getEventManager().post(SpongeEventFactory.createConstructWorldPropertiesEvent(causeStack.getCurrentCause(), worldArchetype, worldProperties));
    // Save the world properties to reserve the world folder
    saveWorldProperties(worldProperties);
    return worldProperties;
}
Also used : Path(java.nio.file.Path) LanternCauseStack(org.lanternpowered.server.event.LanternCauseStack) CauseStack(org.lanternpowered.server.event.CauseStack) IOException(java.io.IOException)

Example 23 with CauseStack

use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.

the class LanternWorld method enableSpawnArea.

/**
 * Enables whether the spawn volume should be generated and keeping it loaded.
 *
 * @param keepSpawnLoaded keep spawn loaded
 */
void enableSpawnArea(boolean keepSpawnLoaded) {
    if (keepSpawnLoaded) {
        final Vector3i spawnPoint = this.properties.getSpawnPosition();
        if (this.spawnLoadingTicket == null) {
            this.spawnLoadingTicket = (ChunkLoadingTicket) this.chunkManager.createTicket(this.game.getMinecraftPlugin()).get();
        } else {
            this.spawnLoadingTicket.unforceChunks();
        }
        final int chunkX = spawnPoint.getX() >> 4;
        final int chunkZ = spawnPoint.getZ() >> 4;
        this.logger.info("Generating spawn volume...");
        final CauseStack causeStack = CauseStack.current();
        for (int x = chunkX - SPAWN_SIZE; x < chunkX + SPAWN_SIZE; x++) {
            for (int z = chunkZ - SPAWN_SIZE; z < chunkZ + SPAWN_SIZE; z++) {
                this.chunkManager.getOrCreateChunk(x, z, causeStack, true);
                this.spawnLoadingTicket.forceChunk(new Vector2i(x, z));
            }
        }
        this.logger.info("Finished generating spawn volume.");
    } else if (this.spawnLoadingTicket != null) {
        this.spawnLoadingTicket.unforceChunks();
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) Vector3i(com.flowpowered.math.vector.Vector3i) Vector2i(com.flowpowered.math.vector.Vector2i)

Example 24 with CauseStack

use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.

the class LanternWorld method spawnEntity.

@Override
public boolean spawnEntity(Entity entity) {
    checkNotNull(entity, "entity");
    checkArgument(!entity.isRemoved(), "The entity may not be removed.");
    checkArgument(entity.getWorld() == this, "The entity is not be located in this world.");
    final CauseStack causeStack = CauseStack.current();
    final SpawnEntityEvent.Custom event = SpongeEventFactory.createSpawnEntityEventCustom(causeStack.getCurrentCause(), Lists.newArrayList(entity));
    Sponge.getEventManager().post(event);
    return !finishSpawnEntityEvent(event).isEmpty();
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent)

Example 25 with CauseStack

use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.

the class LanternWorld method pulse.

public void pulse() {
    final CauseStack causeStack = CauseStack.current();
    causeStack.pushCause(this);
    this.chunkManager.pulse(causeStack);
    this.timeUniverse.pulse();
    if (this.weatherUniverse != null) {
        this.weatherUniverse.pulse(causeStack);
    }
    // Pulse the entities
    pulseEntities();
    // Pulse the tile entities
    getLoadedChunks().forEach(chunk -> ((LanternChunk) chunk).pulse());
    causeStack.popCause();
    // TODO: Maybe async?
    this.observedChunkManager.pulse();
    this.entityProtocolManager.updateTrackers(this.players);
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack)

Aggregations

CauseStack (org.lanternpowered.server.event.CauseStack)54 ItemStack (org.spongepowered.api.item.inventory.ItemStack)18 Entity (org.spongepowered.api.entity.Entity)16 ArrayList (java.util.ArrayList)15 Nullable (javax.annotation.Nullable)15 World (org.spongepowered.api.world.World)15 Optional (java.util.Optional)14 Lantern (org.lanternpowered.server.game.Lantern)13 AbstractSlot (org.lanternpowered.server.inventory.AbstractSlot)13 LanternWorld (org.lanternpowered.server.world.LanternWorld)13 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)13 Vector3i (com.flowpowered.math.vector.Vector3i)12 List (java.util.List)12 LanternPlayer (org.lanternpowered.server.entity.living.player.LanternPlayer)12 Keys (org.spongepowered.api.data.key.Keys)12 EventContextKeys (org.spongepowered.api.event.cause.EventContextKeys)12 Sponge (org.spongepowered.api.Sponge)11 Transform (org.spongepowered.api.entity.Transform)11 Player (org.spongepowered.api.entity.living.player.Player)11 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)11