Skip to content

Commit

Permalink
Fix SpongeWorldManager errors not always reported via CompletableFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Jun 19, 2022
1 parent 00cc9f3 commit bb6c901
Showing 1 changed file with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,9 @@ public CompletableFuture<org.spongepowered.api.world.server.ServerWorld> loadWor
final ResourceKey key = Objects.requireNonNull(template, "template").key();
final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(key);
if (Level.OVERWORLD.equals(registryKey)) {
FutureUtil.completedWithException(new IllegalArgumentException("The default world cannot be told to load!"));
return FutureUtil.completedWithException(new IllegalArgumentException("The default world cannot be told to load!"));
}

final ServerLevel serverWorld = this.worlds.get(registryKey);
if (serverWorld != null) {
return CompletableFuture.completedFuture((org.spongepowered.api.world.server.ServerWorld) serverWorld);
Expand All @@ -329,9 +330,8 @@ public CompletableFuture<org.spongepowered.api.world.server.ServerWorld> loadWor
@Override
public CompletableFuture<org.spongepowered.api.world.server.ServerWorld> loadWorld(final ResourceKey key) {
final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(key, "key"));

if (Level.OVERWORLD.equals(registryKey)) {
FutureUtil.completedWithException(new IllegalArgumentException("The default world cannot be told to load!"));
return FutureUtil.completedWithException(new IllegalArgumentException("The default world cannot be told to load!"));
}

final ServerLevel world = this.worlds.get(registryKey);
Expand Down Expand Up @@ -376,7 +376,6 @@ private CompletableFuture<org.spongepowered.api.world.server.ServerWorld> loadWo
try {
storageSource = this.createStorageSource(worldKey);
} catch (final IOException e) {
e.printStackTrace();
return FutureUtil.completedWithException(new RuntimeException(String.format("Failed to create level data for world '%s'!", worldKey), e));
}

Expand Down Expand Up @@ -450,14 +449,12 @@ public CompletableFuture<Boolean> unloadWorld(final org.spongepowered.api.world.
return CompletableFuture.completedFuture(false);
}

return CompletableFuture.supplyAsync(() -> {
try {
this.unloadWorld0((ServerLevel) world);
} catch (final IOException e) {
return false;
}
return true;
}, SpongeCommon.server());
try {
this.unloadWorld0((ServerLevel) world);
return CompletableFuture.completedFuture(true);
} catch (final IOException e) {
return FutureUtil.completedWithException(e);
}
}

@Override
Expand All @@ -473,7 +470,7 @@ public CompletableFuture<Optional<WorldTemplate>> loadTemplate(final ResourceKey
final LevelStem template = SpongeWorldManager.stemFromJson(key, new JsonParser().parse(reader));
return CompletableFuture.completedFuture(Optional.of(((LevelStemBridge) (Object) template).bridge$asTemplate()));
} catch (final IOException e) {
e.printStackTrace();
return FutureUtil.completedWithException(e);
}
}

Expand All @@ -487,7 +484,7 @@ public CompletableFuture<Boolean> saveTemplate(final WorldTemplate template) {
final JsonElement element = SpongeWorldManager.stemToJson(scratch);
this.writeTemplate(element, template.key());
} catch (final Exception ex) {
FutureUtil.completedWithException(ex);
return FutureUtil.completedWithException(ex);
}
return CompletableFuture.completedFuture(true);
}
Expand Down Expand Up @@ -659,7 +656,7 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
}

final JsonElement template;
if (isVanillaWorld(key)) {
if (this.isVanillaWorld(key)) {
final LevelStem stem = this.server.getWorldData().worldGenSettings().dimensions().get(SpongeWorldManager.createStemKey(key));
template = SpongeWorldManager.stemToJson(stem);
} else {
Expand All @@ -676,7 +673,7 @@ public FileVisitResult visitFile(final Path file, final BasicFileAttributes attr
try {
this.writeTemplate(template, copyKey);
} catch (final IOException e) {
FutureUtil.completedWithException(e);
return FutureUtil.completedWithException(e);
}

return CompletableFuture.completedFuture(true);
Expand Down Expand Up @@ -727,7 +724,7 @@ public CompletableFuture<Boolean> moveWorld(final ResourceKey key, final Resourc
return FutureUtil.completedWithException(e);
}

if (isVanillaWorld(key)) {
if (this.isVanillaWorld(key)) {
final LevelStem stem = this.server.getWorldData().worldGenSettings().dimensions().get(SpongeWorldManager.createStemKey(key));
JsonElement template = SpongeWorldManager.stemToJson(stem);

Expand Down Expand Up @@ -795,7 +792,7 @@ public CompletableFuture<Boolean> deleteWorld(final ResourceKey key) {
try {
Files.deleteIfExists(dimensionTemplate);
} catch (final IOException e) {
FutureUtil.completedWithException(e);
return FutureUtil.completedWithException(e);
}

return CompletableFuture.completedFuture(true);
Expand Down

0 comments on commit bb6c901

Please sign in to comment.