Search in sources :

Example 1 with LevelStem

use of net.minecraft.world.level.dimension.LevelStem in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method loadTemplate.

@Override
public CompletableFuture<Optional<WorldTemplate>> loadTemplate(final ResourceKey key) {
    final Path dataPackFile = this.getDataPackFile(Objects.requireNonNull(key, "key"));
    if (Files.exists(dataPackFile)) {
        try {
            final LevelStem template = this.loadTemplate0(SpongeWorldManager.createRegistryKey(key), dataPackFile);
            ((ResourceKeyBridge) (Object) template).bridge$setKey(key);
            return CompletableFuture.completedFuture(Optional.of(((LevelStemBridge) (Object) template).bridge$asTemplate()));
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return CompletableFuture.completedFuture(Optional.empty());
}
Also used : Path(java.nio.file.Path) LevelStem(net.minecraft.world.level.dimension.LevelStem) LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) IOException(java.io.IOException) ResourceKeyBridge(org.spongepowered.common.bridge.ResourceKeyBridge)

Example 2 with LevelStem

use of net.minecraft.world.level.dimension.LevelStem in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method loadProperties.

@Override
public CompletableFuture<Optional<ServerWorldProperties>> loadProperties(final ResourceKey key) {
    final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(key, "key"));
    if (this.worlds.get(registryKey) != null) {
        return CompletableFuture.completedFuture(Optional.empty());
    }
    if (!this.worldExists(key)) {
        return CompletableFuture.completedFuture(Optional.empty());
    }
    final boolean isVanillaWorld = this.isVanillaWorld(key);
    final String directoryName = this.getDirectoryName(key);
    final LevelStorageSource.LevelStorageAccess storageSource;
    try {
        if (isVanillaWorld) {
            storageSource = LevelStorageSource.createDefault(this.defaultWorldDirectory).createAccess(directoryName);
        } else {
            storageSource = LevelStorageSource.createDefault(this.customWorldsDirectory).createAccess(key.namespace() + File.separator + key.value());
        }
    } catch (final IOException e) {
        return FutureUtil.completedWithException(e);
    }
    final WorldData levelData;
    try {
        final PrimaryLevelData defaultLevelData = (PrimaryLevelData) this.server.getWorldData();
        final LevelSettings defaultLevelSettings = ((PrimaryLevelDataAccessor) defaultLevelData).accessor$settings();
        try {
            levelData = storageSource.getDataTag((DynamicOps<Tag>) BootstrapProperties.worldSettingsAdapter, defaultLevelSettings.getDataPackConfig());
        } catch (final Exception ex) {
            return FutureUtil.completedWithException(ex);
        }
    } finally {
        try {
            storageSource.close();
        } catch (final IOException ex) {
            return FutureUtil.completedWithException(ex);
        }
    }
    return this.loadTemplate(key).thenCompose(r -> {
        r.ifPresent(template -> {
            final LevelStem scratch = ((SpongeWorldTemplate) template).asDimension();
            ((PrimaryLevelDataBridge) levelData).bridge$populateFromDimension(scratch);
        });
        return CompletableFuture.completedFuture(Optional.of((ServerWorldProperties) levelData));
    });
}
Also used : ServerWorldProperties(org.spongepowered.api.world.server.storage.ServerWorldProperties) LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) IOException(java.io.IOException) WorldData(net.minecraft.world.level.storage.WorldData) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ReportedException(net.minecraft.ReportedException) PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) LevelStem(net.minecraft.world.level.dimension.LevelStem) Level(net.minecraft.world.level.Level) ServerLevel(net.minecraft.server.level.ServerLevel) LevelSettings(net.minecraft.world.level.LevelSettings) PrimaryLevelDataAccessor(org.spongepowered.common.accessor.world.level.storage.PrimaryLevelDataAccessor) DynamicOps(com.mojang.serialization.DynamicOps)

Example 3 with LevelStem

use of net.minecraft.world.level.dimension.LevelStem in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method saveTemplate.

@Override
public CompletableFuture<Boolean> saveTemplate(final WorldTemplate template) {
    final LevelStem scratch = ((SpongeWorldTemplate) Objects.requireNonNull(template, "template")).asDimension();
    try {
        final JsonElement element = SpongeWorldTemplate.DIRECT_CODEC.encodeStart(RegistryWriteOps.create(JsonOps.INSTANCE, BootstrapProperties.registries), scratch).getOrThrow(true, s -> {
        });
        final Path dataPackFile = this.getDataPackFile(template.key());
        Files.createDirectories(dataPackFile.getParent());
        DataPackSerializer.writeFile(dataPackFile, element);
        DataPackSerializer.writePackMetadata("World", this.dimensionsDataPackDirectory.getParent());
    } catch (final Exception ex) {
        FutureUtil.completedWithException(ex);
    }
    return CompletableFuture.completedFuture(true);
}
Also used : Path(java.nio.file.Path) LevelStem(net.minecraft.world.level.dimension.LevelStem) JsonElement(com.google.gson.JsonElement) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ReportedException(net.minecraft.ReportedException)

Example 4 with LevelStem

use of net.minecraft.world.level.dimension.LevelStem in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method saveProperties.

@Override
public CompletableFuture<Boolean> saveProperties(final ServerWorldProperties properties) {
    final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(properties, "properties").key());
    if (this.worlds.get(registryKey) != null) {
        return CompletableFuture.completedFuture(false);
    }
    final ResourceKey key = properties.key();
    final boolean isVanillaWorld = this.isVanillaWorld(key);
    final String directoryName = this.getDirectoryName(key);
    final LevelStorageSource.LevelStorageAccess storageSource;
    try {
        if (isVanillaWorld) {
            storageSource = LevelStorageSource.createDefault(this.defaultWorldDirectory).createAccess(directoryName);
        } else {
            storageSource = LevelStorageSource.createDefault(this.customWorldsDirectory).createAccess(key.namespace() + File.separator + key.value());
        }
    } catch (final IOException e) {
        return FutureUtil.completedWithException(e);
    }
    try {
        try {
            storageSource.saveDataTag(BootstrapProperties.registries, (WorldData) properties, null);
        } catch (final Exception ex) {
            return FutureUtil.completedWithException(ex);
        }
    } finally {
        try {
            storageSource.close();
        } catch (final IOException ex) {
            return FutureUtil.completedWithException(ex);
        }
    }
    // Properties doesn't have everything we need...namely the generator, load the template and set values we actually got
    return this.loadTemplate(key).thenCompose(r -> {
        final WorldTemplate template = r.orElse(null);
        if (template != null) {
            final LevelStem scratch = ((SpongeWorldTemplate) template).asDimension();
            ((LevelStemBridge) (Object) scratch).bridge$populateFromLevelData((PrimaryLevelData) properties);
            return this.saveTemplate(((LevelStemBridge) (Object) scratch).bridge$asTemplate());
        }
        return CompletableFuture.completedFuture(true);
    });
}
Also used : LevelStorageSource(net.minecraft.world.level.storage.LevelStorageSource) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ReportedException(net.minecraft.ReportedException) ResourceKey(org.spongepowered.api.ResourceKey) LevelStem(net.minecraft.world.level.dimension.LevelStem) LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) Level(net.minecraft.world.level.Level) ServerLevel(net.minecraft.server.level.ServerLevel) JsonObject(com.google.gson.JsonObject) WorldTemplate(org.spongepowered.api.world.server.WorldTemplate)

Example 5 with LevelStem

use of net.minecraft.world.level.dimension.LevelStem in project SpongeCommon by SpongePowered.

the class SpongeWorldManager method loadTemplate0.

private LevelStem loadTemplate0(final net.minecraft.resources.ResourceKey<Level> registryKey, final Path file) throws IOException {
    try (final InputStream stream = Files.newInputStream(file);
        final InputStreamReader reader = new InputStreamReader(stream)) {
        final JsonParser parser = new JsonParser();
        final JsonElement element = parser.parse(reader);
        final SingleTemplateAccess singleTemplateAccess = new SingleTemplateAccess(registryKey, element);
        final RegistryReadOps<JsonElement> settingsAdapter = RegistryReadOps.create(JsonOps.INSTANCE, singleTemplateAccess, (RegistryAccess.RegistryHolder) BootstrapProperties.registries);
        final MappedRegistry<LevelStem> registry = new MappedRegistry<>(net.minecraft.core.Registry.LEVEL_STEM_REGISTRY, Lifecycle.stable());
        settingsAdapter.decodeElements(registry, net.minecraft.core.Registry.LEVEL_STEM_REGISTRY, LevelStem.CODEC);
        final LevelStem template = registry.stream().findAny().orElse(null);
        if (template != null) {
            ((LevelStemBridge) (Object) template).bridge$setFromSettings(false);
        }
        return template;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LevelStem(net.minecraft.world.level.dimension.LevelStem) LevelStemBridge(org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge) InputStream(java.io.InputStream) JsonElement(com.google.gson.JsonElement) RegistryAccess(net.minecraft.core.RegistryAccess) MappedRegistry(net.minecraft.core.MappedRegistry) JsonParser(com.google.gson.JsonParser)

Aggregations

LevelStem (net.minecraft.world.level.dimension.LevelStem)13 IOException (java.io.IOException)7 LevelStemBridge (org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge)6 ExecutionException (java.util.concurrent.ExecutionException)5 ServerLevel (net.minecraft.server.level.ServerLevel)5 Level (net.minecraft.world.level.Level)5 WorldGenSettings (net.minecraft.world.level.levelgen.WorldGenSettings)4 LevelStorageSource (net.minecraft.world.level.storage.LevelStorageSource)4 ResourceKeyBridge (org.spongepowered.common.bridge.ResourceKeyBridge)4 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 ReportedException (net.minecraft.ReportedException)3 MappedRegistry (net.minecraft.core.MappedRegistry)3 ResourceLocation (net.minecraft.resources.ResourceLocation)3 LevelSettings (net.minecraft.world.level.LevelSettings)3 PrimaryLevelData (net.minecraft.world.level.storage.PrimaryLevelData)3 ResourceKey (org.spongepowered.api.ResourceKey)3 WorldTemplate (org.spongepowered.api.world.server.WorldTemplate)3 DimensionGeneratorSettingsAccessor (org.spongepowered.common.accessor.world.gen.DimensionGeneratorSettingsAccessor)3 PrimaryLevelDataAccessor (org.spongepowered.common.accessor.world.level.storage.PrimaryLevelDataAccessor)3