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());
}
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));
});
}
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);
}
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);
});
}
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;
}
}
Aggregations