Search in sources :

Example 6 with IMixinWorldInfo

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

the class WorldManager method renameWorld.

public static Optional<WorldProperties> renameWorld(WorldProperties worldProperties, String newName) {
    checkNotNull(worldProperties);
    checkNotNull(newName);
    checkState(!worldByDimensionId.containsKey(((IMixinWorldInfo) worldProperties).getDimensionId()), "World is still loaded!");
    final Path oldWorldFolder = getCurrentSavesDirectory().get().resolve(worldProperties.getWorldName());
    final Path newWorldFolder = oldWorldFolder.resolveSibling(newName);
    if (Files.exists(newWorldFolder)) {
        return Optional.empty();
    }
    try {
        Files.move(oldWorldFolder, newWorldFolder);
    } catch (IOException e) {
        return Optional.empty();
    }
    unregisterWorldProperties(worldProperties, false);
    final WorldInfo info = new WorldInfo((WorldInfo) worldProperties);
    info.setWorldName(newName);
    // As we are moving a world, we want to move the dimension ID and UUID with the world to ensure
    // plugins and Sponge do not break.
    ((IMixinWorldInfo) info).setUniqueId(worldProperties.getUniqueId());
    if (((IMixinWorldInfo) worldProperties).getDimensionId() != null) {
        ((IMixinWorldInfo) info).setDimensionId(((IMixinWorldInfo) worldProperties).getDimensionId());
    }
    ((IMixinWorldInfo) info).createWorldConfig();
    new AnvilSaveHandler(WorldManager.getCurrentSavesDirectory().get().toFile(), newName, true, SpongeImpl.getDataFixer()).saveWorldInfo(info);
    registerWorldProperties((WorldProperties) info);
    return Optional.of((WorldProperties) info);
}
Also used : Path(java.nio.file.Path) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) WorldInfo(net.minecraft.world.storage.WorldInfo) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IOException(java.io.IOException) AnvilSaveHandler(net.minecraft.world.chunk.storage.AnvilSaveHandler)

Example 7 with IMixinWorldInfo

use of org.spongepowered.common.interfaces.world.IMixinWorldInfo 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)

Example 8 with IMixinWorldInfo

use of org.spongepowered.common.interfaces.world.IMixinWorldInfo in project SpongeForge by SpongePowered.

the class MixinDimensionManager method initDimension.

/**
 * @author Zidane - June 2nd, 2016
 * @reason Forge's initDimension is very different from Sponge's multi-world. We basically rig it into our system so mods work.
 * @param dim The dimension to load
 */
@Overwrite
public static void initDimension(int dim) {
    // World is already loaded, bail
    if (WorldManager.getWorldByDimensionId(dim).isPresent()) {
        return;
    }
    if (dim == 0) {
        throw new RuntimeException("Attempt made to initialize overworld!");
    }
    WorldManager.getWorldByDimensionId(0).orElseThrow(() -> new RuntimeException("Attempt made to initialize " + "dimension before overworld is loaded!"));
    DimensionType dimensionType = WorldManager.getDimensionType(dim).orElse(null);
    if (dimensionType == null) {
        SpongeImpl.getLogger().warn("Attempt made to initialize dimension id {} which isn't registered!" + ", falling back to overworld.", dim);
        return;
    }
    final WorldProvider provider = dimensionType.createDimension();
    // make sure to set the dimension id to avoid getting a null save folder
    provider.setDimension(dim);
    String worldFolder = WorldManager.getWorldFolderByDimensionId(dim).orElse(provider.getSaveFolder());
    WorldProperties properties = WorldManager.getWorldProperties(worldFolder).orElse(null);
    final Path worldPath = WorldManager.getCurrentSavesDirectory().get().resolve(worldFolder);
    if (properties == null || !Files.isDirectory(worldPath)) {
        if (properties != null) {
            WorldManager.unregisterWorldProperties(properties, false);
        }
        String modId = StaticMixinForgeHelper.getModIdFromClass(provider.getClass());
        WorldArchetype archetype = Sponge.getRegistry().getType(WorldArchetype.class, modId + ":" + dimensionType.getName().toLowerCase()).orElse(null);
        if (archetype == null) {
            final WorldArchetype.Builder builder = WorldArchetype.builder().dimension((org.spongepowered.api.world.DimensionType) (Object) dimensionType).keepsSpawnLoaded(dimensionType.shouldLoadSpawn());
            archetype = builder.build(modId + ":" + dimensionType.getName().toLowerCase(), dimensionType.getName());
        }
        IMixinWorldSettings worldSettings = (IMixinWorldSettings) archetype;
        worldSettings.setDimensionType((org.spongepowered.api.world.DimensionType) (Object) dimensionType);
        worldSettings.setLoadOnStartup(false);
        properties = WorldManager.createWorldProperties(worldFolder, archetype, dim);
        ((IMixinWorldInfo) properties).setDimensionId(dim);
        ((IMixinWorldInfo) properties).setIsMod(true);
    }
    if (!properties.isEnabled()) {
        return;
    }
    Optional<WorldServer> optWorld = WorldManager.loadWorld(properties);
    if (!optWorld.isPresent()) {
        SpongeImpl.getLogger().error("Could not load world [{}]!", properties.getWorldName());
    }
}
Also used : Path(java.nio.file.Path) DimensionType(net.minecraft.world.DimensionType) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) WorldServer(net.minecraft.world.WorldServer) WorldArchetype(org.spongepowered.api.world.WorldArchetype) WorldProvider(net.minecraft.world.WorldProvider) IMixinWorldSettings(org.spongepowered.common.interfaces.world.IMixinWorldSettings) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 9 with IMixinWorldInfo

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

the class WorldManager method loadWorld.

private static Optional<WorldServer> loadWorld(String worldName, @Nullable ISaveHandler saveHandler, @Nullable WorldProperties properties) {
    checkNotNull(worldName);
    final Path currentSavesDir = WorldManager.getCurrentSavesDirectory().orElseThrow(() -> new IllegalStateException("Attempt " + "made to load world too early!"));
    final MinecraftServer server = SpongeImpl.getServer();
    final Optional<WorldServer> optExistingWorldServer = getWorld(worldName);
    if (optExistingWorldServer.isPresent()) {
        return optExistingWorldServer;
    }
    if (!server.getAllowNether()) {
        SpongeImpl.getLogger().error("Unable to load world [{}]. Multi-world is disabled via [allow-nether] in [server.properties].", worldName);
        return Optional.empty();
    }
    final Path worldFolder = currentSavesDir.resolve(worldName);
    if (!Files.isDirectory(worldFolder)) {
        SpongeImpl.getLogger().error("Unable to load world [{}]. We cannot find its folder under [{}].", worldFolder, currentSavesDir);
        return Optional.empty();
    }
    if (saveHandler == null) {
        saveHandler = new AnvilSaveHandler(currentSavesDir.toFile(), worldName, true, SpongeImpl.getDataFixer());
    }
    // We weren't given a properties, see if one is cached
    if (properties == null) {
        properties = (WorldProperties) saveHandler.loadWorldInfo();
        // We tried :'(
        if (properties == null) {
            SpongeImpl.getLogger().error("Unable to load world [{}]. No world properties was found!", worldName);
            return Optional.empty();
        }
    }
    if (((IMixinWorldInfo) properties).getDimensionId() == null || ((IMixinWorldInfo) properties).getDimensionId() == Integer.MIN_VALUE) {
        ((IMixinWorldInfo) properties).setDimensionId(getNextFreeDimensionId());
    }
    setUuidOnProperties(getCurrentSavesDirectory().get(), properties);
    registerWorldProperties(properties);
    final WorldInfo worldInfo = (WorldInfo) properties;
    ((IMixinWorldInfo) worldInfo).createWorldConfig();
    // check if enabled
    if (!((WorldProperties) worldInfo).isEnabled()) {
        SpongeImpl.getLogger().error("Unable to load world [{}]. It is disabled.", worldName);
        return Optional.empty();
    }
    final int dimensionId = ((IMixinWorldInfo) properties).getDimensionId();
    registerDimension(dimensionId, (DimensionType) (Object) properties.getDimensionType());
    registerDimensionPath(dimensionId, worldFolder);
    SpongeImpl.getLogger().info("Loading world [{}] ({})", properties.getWorldName(), getDimensionType(dimensionId).get().getName());
    final WorldServer worldServer = createWorldFromProperties(dimensionId, saveHandler, (WorldInfo) properties, new WorldSettings((WorldInfo) properties));
    // Set the worlds on the Minecraft server
    reorderWorldsVanillaFirst();
    return Optional.of(worldServer);
}
Also used : Path(java.nio.file.Path) 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) AnvilSaveHandler(net.minecraft.world.chunk.storage.AnvilSaveHandler) WorldSettings(net.minecraft.world.WorldSettings) IMixinWorldSettings(org.spongepowered.common.interfaces.world.IMixinWorldSettings) IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftServer(net.minecraft.server.MinecraftServer)

Example 10 with IMixinWorldInfo

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

the class WorldManager method createWorldInfoFromSettings.

public static WorldInfo createWorldInfoFromSettings(Path currentSaveRoot, org.spongepowered.api.world.DimensionType dimensionType, int dimensionId, String worldFolderName, WorldSettings worldSettings, String generatorOptions) {
    final MinecraftServer server = SpongeImpl.getServer();
    worldSettings.setGeneratorOptions(generatorOptions);
    ((IMixinWorldSettings) (Object) worldSettings).setDimensionType(dimensionType);
    ((IMixinWorldSettings) (Object) worldSettings).setGenerateSpawnOnLoad(((IMixinDimensionType) dimensionType).shouldGenerateSpawnOnLoad());
    final WorldInfo worldInfo = new WorldInfo(worldSettings, worldFolderName);
    setUuidOnProperties(dimensionId == 0 ? currentSaveRoot.getParent() : currentSaveRoot, (WorldProperties) worldInfo);
    ((IMixinWorldInfo) worldInfo).setDimensionId(dimensionId);
    SpongeImpl.postEvent(SpongeEventFactory.createConstructWorldPropertiesEvent(Sponge.getCauseStackManager().getCurrentCause(), (WorldArchetype) (Object) worldSettings, (WorldProperties) worldInfo));
    return worldInfo;
}
Also used : WorldArchetype(org.spongepowered.api.world.WorldArchetype) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) WorldInfo(net.minecraft.world.storage.WorldInfo) IMixinWorldInfo(org.spongepowered.common.interfaces.world.IMixinWorldInfo) IMixinWorldSettings(org.spongepowered.common.interfaces.world.IMixinWorldSettings) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) IMixinMinecraftServer(org.spongepowered.common.interfaces.IMixinMinecraftServer) MinecraftServer(net.minecraft.server.MinecraftServer)

Aggregations

IMixinWorldInfo (org.spongepowered.common.interfaces.world.IMixinWorldInfo)13 WorldServer (net.minecraft.world.WorldServer)6 WorldInfo (net.minecraft.world.storage.WorldInfo)6 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)6 Path (java.nio.file.Path)5 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)5 IMixinWorldSettings (org.spongepowered.common.interfaces.world.IMixinWorldSettings)5 AnvilSaveHandler (net.minecraft.world.chunk.storage.AnvilSaveHandler)4 IMixinMinecraftServer (org.spongepowered.common.interfaces.IMixinMinecraftServer)4 IOException (java.io.IOException)3 MinecraftServer (net.minecraft.server.MinecraftServer)3 WorldSettings (net.minecraft.world.WorldSettings)3 WorldArchetype (org.spongepowered.api.world.WorldArchetype)3 UUID (java.util.UUID)2 DimensionType (net.minecraft.world.DimensionType)2 ISaveHandler (net.minecraft.world.storage.ISaveHandler)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1