use of org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge in project SpongeCommon by SpongePowered.
the class PrimaryLevelDataMixin method bridge$populateFromDimension.
@Override
public void bridge$populateFromDimension(final LevelStem dimension) {
final LevelStemBridge levelStemBridge = (LevelStemBridge) (Object) dimension;
this.impl$key = ((ResourceKeyBridge) (Object) dimension).bridge$getKey();
this.impl$dimensionType = dimension.type();
this.impl$displayName = levelStemBridge.bridge$displayName().orElse(null);
levelStemBridge.bridge$difficulty().ifPresent(v -> {
((LevelSettingsAccessor) (Object) this.settings).accessor$difficulty(RegistryTypes.DIFFICULTY.get().value((ResourceKey) (Object) v));
this.impl$customDifficulty = true;
});
levelStemBridge.bridge$gameMode().ifPresent(v -> {
((LevelSettingsAccessor) (Object) this.settings).accessor$gameType(RegistryTypes.GAME_MODE.get().value((ResourceKey) (Object) v));
this.impl$customGameType = true;
});
levelStemBridge.bridge$spawnPosition().ifPresent(v -> {
this.setSpawn(VecHelper.toBlockPos(v), this.spawnAngle);
this.impl$customSpawnPosition = true;
});
levelStemBridge.bridge$hardcore().ifPresent(v -> ((LevelSettingsAccessor) (Object) this.settings).accessor$hardcode(v));
this.impl$serializationBehavior = levelStemBridge.bridge$serializationBehavior().orElse(null);
this.impl$pvp = levelStemBridge.bridge$pvp().orElse(null);
this.impl$loadOnStartup = levelStemBridge.bridge$loadOnStartup();
this.impl$performsSpawnLogic = levelStemBridge.bridge$performsSpawnLogic();
this.impl$viewDistance = levelStemBridge.bridge$viewDistance().orElse(null);
}
use of org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge 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 org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge 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 org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge 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;
}
}
use of org.spongepowered.common.bridge.world.level.dimension.LevelStemBridge in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method loadWorld0.
private CompletableFuture<org.spongepowered.api.world.server.ServerWorld> loadWorld0(final net.minecraft.resources.ResourceKey<Level> registryKey, final LevelStem template, final WorldGenSettings generatorSettings) {
final PrimaryLevelData defaultLevelData = (PrimaryLevelData) this.server.getWorldData();
final LevelSettings defaultLevelSettings = ((PrimaryLevelDataAccessor) defaultLevelData).accessor$settings();
final LevelStemBridge templateBridge = (LevelStemBridge) (Object) template;
final ResourceKey worldKey = ((ResourceKeyBridge) templateBridge).bridge$getKey();
final WorldType worldType = (WorldType) template.type();
final ResourceKey worldTypeKey = RegistryTypes.WORLD_TYPE.get().valueKey((WorldType) template.type());
MinecraftServerAccessor.accessor$LOGGER().info("Loading world '{}' ({})", worldKey, worldTypeKey);
final String directoryName = this.getDirectoryName(worldKey);
final boolean isVanillaSubLevel = this.isVanillaSubWorld(directoryName);
final LevelStorageSource.LevelStorageAccess storageSource;
try {
if (isVanillaSubLevel) {
storageSource = LevelStorageSource.createDefault(this.defaultWorldDirectory).createAccess(directoryName);
} else {
storageSource = LevelStorageSource.createDefault(this.customWorldsDirectory).createAccess(worldKey.namespace() + File.separator + worldKey.value());
}
} catch (final IOException e) {
e.printStackTrace();
return FutureUtil.completedWithException(new RuntimeException(String.format("Failed to create level data for world '%s'!", worldKey), e));
}
PrimaryLevelData levelData;
levelData = (PrimaryLevelData) storageSource.getDataTag((DynamicOps<Tag>) BootstrapProperties.worldSettingsAdapter, defaultLevelSettings.getDataPackConfig());
if (levelData == null) {
final LevelSettings levelSettings;
final WorldGenSettings generationSettings;
if (this.server.isDemo()) {
levelSettings = MinecraftServer.DEMO_SETTINGS;
generationSettings = WorldGenSettings.demoSettings(BootstrapProperties.registries);
} else {
levelSettings = new LevelSettings(directoryName, (GameType) (Object) BootstrapProperties.gameMode.get(Sponge.game()), templateBridge.bridge$hardcore().orElse(BootstrapProperties.hardcore), (Difficulty) (Object) BootstrapProperties.difficulty.get(Sponge.game()), templateBridge.bridge$commands().orElse(BootstrapProperties.commands), new GameRules(), defaultLevelData.getDataPackConfig());
generationSettings = generatorSettings;
}
levelData = new PrimaryLevelData(levelSettings, generationSettings, Lifecycle.stable());
}
((PrimaryLevelDataBridge) levelData).bridge$populateFromDimension(template);
final InheritableConfigHandle<WorldConfig> configAdapter = SpongeGameConfigs.createWorld(worldTypeKey, worldKey);
((PrimaryLevelDataBridge) levelData).bridge$configAdapter(configAdapter);
levelData.setModdedInfo(this.server.getServerModName(), this.server.getModdedStatus().isPresent());
final boolean isDebugGeneration = levelData.worldGenSettings().isDebug();
final long seed = BiomeManager.obfuscateSeed(levelData.worldGenSettings().seed());
final ChunkProgressListener chunkStatusListener = ((MinecraftServerAccessor) this.server).accessor$progressListenerFactory().create(11);
final ServerLevel world = new ServerLevel(this.server, ((MinecraftServerAccessor) this.server).accessor$executor(), storageSource, levelData, registryKey, (DimensionType) worldType, chunkStatusListener, template.generator(), isDebugGeneration, seed, ImmutableList.of(), true);
this.worlds.put(registryKey, world);
return SpongeCommon.asyncScheduler().submit(() -> this.prepareWorld(world, isDebugGeneration)).thenApply(w -> {
((MinecraftServerAccessor) this.server).invoker$forceDifficulty();
return w;
}).thenCompose(w -> this.postWorldLoad(w, false)).thenApply(w -> (org.spongepowered.api.world.server.ServerWorld) w);
}
Aggregations