Search in sources :

Example 1 with IMixinMinecraftServer

use of org.spongepowered.common.interfaces.IMixinMinecraftServer in project SpongeCommon by SpongePowered.

the class WorldManager method createWorldFromProperties.

private static WorldServer createWorldFromProperties(int dimensionId, ISaveHandler saveHandler, WorldInfo worldInfo, @Nullable WorldSettings worldSettings) {
    final MinecraftServer server = SpongeImpl.getServer();
    final WorldServer worldServer = new WorldServer(server, saveHandler, worldInfo, dimensionId, server.profiler);
    worldServer.init();
    // WorldSettings is only non-null here if this is a newly generated WorldInfo and therefore we need to initialize to calculate spawn.
    if (worldSettings != null) {
        worldServer.initialize(worldSettings);
    }
    worldServer.addEventListener(new ServerWorldEventHandler(server, worldServer));
    // This code changes from Mojang's to account for per-world API-set GameModes.
    if (!server.isSinglePlayer() && worldServer.getWorldInfo().getGameType().equals(GameType.NOT_SET)) {
        worldServer.getWorldInfo().setGameType(server.getGameType());
    }
    worldByDimensionId.put(dimensionId, worldServer);
    weakWorldByWorld.put(worldServer, worldServer);
    ((IMixinMinecraftServer) SpongeImpl.getServer()).putWorldTickTimes(dimensionId, new long[100]);
    ((IMixinChunkProviderServer) worldServer.getChunkProvider()).setForceChunkRequests(true);
    WorldManager.reorderWorldsVanillaFirst();
    SpongeImpl.postEvent(SpongeEventFactory.createLoadWorldEvent(Sponge.getCauseStackManager().getCurrentCause(), (org.spongepowered.api.world.World) worldServer));
    ((IMixinMinecraftServer) server).prepareSpawnArea(worldServer);
    ((IMixinChunkProviderServer) worldServer.getChunkProvider()).setForceChunkRequests(false);
    return worldServer;
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) ServerWorldEventHandler(net.minecraft.world.ServerWorldEventHandler) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) World(net.minecraft.world.World) IMixinChunkProviderServer(org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer) IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 2 with IMixinMinecraftServer

use of org.spongepowered.common.interfaces.IMixinMinecraftServer in project SpongeCommon by SpongePowered.

the class WorldManager method unloadWorld.

// TODO Result
public static boolean unloadWorld(WorldServer worldServer, boolean checkConfig) {
    checkNotNull(worldServer);
    final MinecraftServer server = SpongeImpl.getServer();
    // Likely leaked, don't want to drop leaked world data
    if (!worldByDimensionId.containsValue(worldServer)) {
        return false;
    }
    // Vanilla sometimes doesn't remove player entities from world first
    if (server.isServerRunning()) {
        if (!worldServer.playerEntities.isEmpty()) {
            return false;
        }
        // We only check config if base game wants to unload world. If mods/plugins say unload, we unload
        if (checkConfig) {
            if (((WorldProperties) worldServer.getWorldInfo()).doesKeepSpawnLoaded()) {
                return false;
            }
        }
    }
    try (PhaseContext<?> context = GeneralPhase.State.WORLD_UNLOAD.createPhaseContext().source(worldServer).buildAndSwitch()) {
        if (SpongeImpl.postEvent(SpongeEventFactory.createUnloadWorldEvent(Sponge.getCauseStackManager().getCurrentCause(), (org.spongepowered.api.world.World) worldServer))) {
            return false;
        }
        final IMixinWorldServer mixinWorldServer = (IMixinWorldServer) worldServer;
        final int dimensionId = mixinWorldServer.getDimensionId();
        try {
            // Don't save if server is stopping to avoid duplicate saving.
            if (server.isServerRunning()) {
                saveWorld(worldServer, true);
            }
            mixinWorldServer.getActiveConfig().save();
        } catch (MinecraftException e) {
            e.printStackTrace();
        } finally {
            worldByDimensionId.remove(dimensionId);
            weakWorldByWorld.remove(worldServer);
            ((IMixinMinecraftServer) server).removeWorldTickTimes(dimensionId);
            SpongeImpl.getLogger().info("Unloading world [{}] (DIM{})", worldServer.getWorldInfo().getWorldName(), dimensionId);
            reorderWorldsVanillaFirst();
        }
        if (!server.isServerRunning()) {
            unregisterDimension(dimensionId);
        }
    }
    return true;
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftException(net.minecraft.world.MinecraftException) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) IMixinWorld(org.spongepowered.common.interfaces.world.IMixinWorld) World(net.minecraft.world.World) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 3 with IMixinMinecraftServer

use of org.spongepowered.common.interfaces.IMixinMinecraftServer in project SpongeCommon by SpongePowered.

the class WorldManager method copyWorld.

public static CompletableFuture<Optional<WorldProperties>> copyWorld(WorldProperties worldProperties, String copyName) {
    checkArgument(worldPropertiesByFolderName.containsKey(worldProperties.getWorldName()), "World properties not registered!");
    checkArgument(!worldPropertiesByFolderName.containsKey(copyName), "Destination world name already is registered!");
    final WorldInfo info = (WorldInfo) worldProperties;
    final WorldServer worldServer = worldByDimensionId.get(((IMixinWorldInfo) info).getDimensionId().intValue());
    if (worldServer != null) {
        try {
            saveWorld(worldServer, true);
        } catch (MinecraftException e) {
            throw new RuntimeException(e);
        }
        ((IMixinMinecraftServer) SpongeImpl.getServer()).setSaveEnabled(false);
    }
    final CompletableFuture<Optional<WorldProperties>> future = SpongeImpl.getScheduler().submitAsyncTask(new CopyWorldTask(info, copyName));
    if (worldServer != null) {
        // World was loaded
        future.thenRun(() -> ((IMixinMinecraftServer) SpongeImpl.getServer()).setSaveEnabled(true));
    }
    return future;
}
Also used : IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftException(net.minecraft.world.MinecraftException) Optional(java.util.Optional) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) WorldInfo(net.minecraft.world.storage.WorldInfo) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer)

Aggregations

IMixinMinecraftServer (org.spongepowered.common.interfaces.IMixinMinecraftServer)3 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)3 MinecraftServer (net.minecraft.server.MinecraftServer)2 MinecraftException (net.minecraft.world.MinecraftException)2 World (net.minecraft.world.World)2 WorldServer (net.minecraft.world.WorldServer)2 IMixinWorld (org.spongepowered.common.interfaces.world.IMixinWorld)2 Optional (java.util.Optional)1 ServerWorldEventHandler (net.minecraft.world.ServerWorldEventHandler)1 WorldInfo (net.minecraft.world.storage.WorldInfo)1 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)1 IMixinWorldInfo (org.spongepowered.common.interfaces.world.IMixinWorldInfo)1 IMixinChunkProviderServer (org.spongepowered.common.interfaces.world.gen.IMixinChunkProviderServer)1