Search in sources :

Example 1 with LoadWorldEvent

use of org.spongepowered.api.event.world.LoadWorldEvent in project SpongeForge by SpongePowered.

the class SpongeForgeEventFactory method callWorldLoadEvent.

private static LoadWorldEvent callWorldLoadEvent(Event event) {
    LoadWorldEvent spongeEvent = (LoadWorldEvent) event;
    // Since Forge only uses a single save handler, we need to make sure to pass the overworld's handler.
    // This makes sure that mods dont attempt to save/read their data from the wrong location.
    final net.minecraft.world.World minecraftWorld = (net.minecraft.world.World) spongeEvent.getTargetWorld();
    ((IMixinWorld) spongeEvent.getTargetWorld()).setCallingWorldEvent(true);
    ((IMixinChunkProviderServer) minecraftWorld.getChunkProvider()).setForceChunkRequests(true);
    ((IMixinEventBus) MinecraftForge.EVENT_BUS).post(new WorldEvent.Load(minecraftWorld), true);
    ((IMixinChunkProviderServer) minecraftWorld.getChunkProvider()).setForceChunkRequests(false);
    ((IMixinWorld) minecraftWorld).setCallingWorldEvent(false);
    return spongeEvent;
}
Also used : IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) LoadWorldEvent(org.spongepowered.api.event.world.LoadWorldEvent) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) LoadWorldEvent(org.spongepowered.api.event.world.LoadWorldEvent) EntityJoinWorldEvent(net.minecraftforge.event.entity.EntityJoinWorldEvent) UnloadWorldEvent(org.spongepowered.api.event.world.UnloadWorldEvent) WorldEvent(net.minecraftforge.event.world.WorldEvent) SaveWorldEvent(org.spongepowered.api.event.world.SaveWorldEvent) TargetWorldEvent(org.spongepowered.api.event.world.TargetWorldEvent) World(org.spongepowered.api.world.World) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)

Example 2 with LoadWorldEvent

use of org.spongepowered.api.event.world.LoadWorldEvent 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)

Aggregations

LoadWorldEvent (org.spongepowered.api.event.world.LoadWorldEvent)2 IOException (java.io.IOException)1 EntityJoinWorldEvent (net.minecraftforge.event.entity.EntityJoinWorldEvent)1 WorldEvent (net.minecraftforge.event.world.WorldEvent)1 CauseStack (org.lanternpowered.server.event.CauseStack)1 LanternCauseStack (org.lanternpowered.server.event.LanternCauseStack)1 SaveWorldEvent (org.spongepowered.api.event.world.SaveWorldEvent)1 TargetWorldEvent (org.spongepowered.api.event.world.TargetWorldEvent)1 UnloadWorldEvent (org.spongepowered.api.event.world.UnloadWorldEvent)1 Scoreboard (org.spongepowered.api.scoreboard.Scoreboard)1 World (org.spongepowered.api.world.World)1 IMixinWorld (org.spongepowered.common.interfaces.world.IMixinWorld)1 IMixinChunkProviderServer (org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)1 IMixinEventBus (org.spongepowered.mod.interfaces.IMixinEventBus)1