diff --git a/src/main/java/nl/openminetopia/modules/places/PlacesModule.java b/src/main/java/nl/openminetopia/modules/places/PlacesModule.java index e8bae3ef..9cd7d3c4 100644 --- a/src/main/java/nl/openminetopia/modules/places/PlacesModule.java +++ b/src/main/java/nl/openminetopia/modules/places/PlacesModule.java @@ -156,21 +156,15 @@ public CompletableFuture> getWorlds() { } public CompletableFuture createWorld(String worldName, String title, String color, double temperature, String loadingName) { - CompletableFuture completableFuture = new CompletableFuture<>(); - - StormDatabase.getExecutorService().submit(() -> { - WorldModel worldModel = new WorldModel(); - worldModel.setName(worldName); - worldModel.setTitle(title); - worldModel.setTemperature(temperature); - worldModel.setColor(color); - worldModel.setLoadingName(loadingName); - - StormDatabase.getInstance().saveStormModel(worldModel); - completableFuture.complete(worldModel); - }); - - return completableFuture; + WorldModel worldModel = new WorldModel(); + worldModel.setName(worldName); + worldModel.setTitle(title); + worldModel.setTemperature(temperature); + worldModel.setColor(color); + worldModel.setLoadingName(loadingName); + + // Completes only once the row is written, and fails when the save fails. + return StormDatabase.getInstance().saveStormModel(worldModel).thenApply(id -> worldModel); } public CompletableFuture deleteWorld(String worldName) { @@ -180,21 +174,15 @@ public CompletableFuture deleteWorld(String worldName) { } public CompletableFuture createCity(String cityName, String title, String color, double temperature, String loadingName) { - CompletableFuture completableFuture = new CompletableFuture<>(); - - StormDatabase.getExecutorService().submit(() -> { - CityModel cityModel = new CityModel(); - cityModel.setName(cityName); - cityModel.setTitle(title); - cityModel.setTemperature(temperature); - cityModel.setColor(color); - cityModel.setLoadingName(loadingName); - - StormDatabase.getInstance().saveStormModel(cityModel); - completableFuture.complete(cityModel); - }); - - return completableFuture; + CityModel cityModel = new CityModel(); + cityModel.setName(cityName); + cityModel.setTitle(title); + cityModel.setTemperature(temperature); + cityModel.setColor(color); + cityModel.setLoadingName(loadingName); + + // Completes only once the row is written, and fails when the save fails. + return StormDatabase.getInstance().saveStormModel(cityModel).thenApply(id -> cityModel); } public CompletableFuture deleteCity(CityModel city) { diff --git a/src/main/java/nl/openminetopia/modules/places/commands/mtcity/subcommands/MTCityCreateCommand.java b/src/main/java/nl/openminetopia/modules/places/commands/mtcity/subcommands/MTCityCreateCommand.java index 5e0c38ef..b4ec8d9d 100644 --- a/src/main/java/nl/openminetopia/modules/places/commands/mtcity/subcommands/MTCityCreateCommand.java +++ b/src/main/java/nl/openminetopia/modules/places/commands/mtcity/subcommands/MTCityCreateCommand.java @@ -12,8 +12,11 @@ import nl.openminetopia.modules.places.models.WorldModel; import nl.openminetopia.utils.ChatUtils; import nl.openminetopia.utils.WorldGuardUtils; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import java.util.concurrent.CompletionException; + @CommandAlias("mtstad|mtcity") public class MTCityCreateCommand extends BaseCommand { @@ -45,19 +48,24 @@ public void create(Player player, String name, String loadingName) { String title = "" + loadingName.toUpperCase(); placesModule.createCity(name, title, "", 21.64, loadingName) - .whenComplete((cityModel, throwable1) -> { - if (throwable != null) { - player.sendMessage(ChatUtils.color("Failed to create city: " + throwable.getMessage())); + .whenComplete((cityModel, saveError) -> Bukkit.getScheduler().runTask(OpenMinetopia.getInstance(), () -> { + if (saveError != null) { + OpenMinetopia.getInstance().getLogger().severe("Failed to save city " + name + ": " + cause(saveError).getMessage()); + player.sendMessage(ChatUtils.color("Failed to create city: " + cause(saveError).getMessage())); return; } placesModule.getCityModels().add(cityModel); - }); - - player.sendMessage(ChatUtils.color("City " + loadingName + " has been created!")); + player.sendMessage(ChatUtils.color("City " + loadingName + " has been created!")); + })); return; } player.sendMessage(ChatUtils.color("Region " + name + " does not exist!")); }); } + + /** The save's own exception rather than the CompletionException wrapping it. */ + private static Throwable cause(Throwable throwable) { + return throwable instanceof CompletionException && throwable.getCause() != null ? throwable.getCause() : throwable; + } } diff --git a/src/main/java/nl/openminetopia/modules/places/commands/mtworld/subcommands/MTWorldCreateCommand.java b/src/main/java/nl/openminetopia/modules/places/commands/mtworld/subcommands/MTWorldCreateCommand.java index 3ca4fc81..a5bc3c50 100644 --- a/src/main/java/nl/openminetopia/modules/places/commands/mtworld/subcommands/MTWorldCreateCommand.java +++ b/src/main/java/nl/openminetopia/modules/places/commands/mtworld/subcommands/MTWorldCreateCommand.java @@ -11,8 +11,12 @@ import nl.openminetopia.modules.places.PlacesModule; import nl.openminetopia.modules.places.models.WorldModel; import nl.openminetopia.utils.ChatUtils; +import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.entity.Player; +import java.util.concurrent.CompletionException; + @CommandAlias("mtwereld|mtworld") public class MTWorldCreateCommand extends BaseCommand { @@ -29,24 +33,33 @@ public void create(Player player, String loadingName) { } } - placesModule.createWorld(player.getWorld().getName(), title, "", 21.64, loadingName) - .whenComplete((worldModel, throwable) -> { + String worldName = player.getWorld().getName(); + placesModule.createWorld(worldName, title, "", 21.64, loadingName) + .whenComplete((worldModel, throwable) -> Bukkit.getScheduler().runTask(OpenMinetopia.getInstance(), () -> { if (throwable != null) { - player.sendMessage(ChatUtils.color("Failed to create world: " + throwable.getMessage())); + OpenMinetopia.getInstance().getLogger().severe("Failed to save world " + worldName + ": " + cause(throwable).getMessage()); + player.sendMessage(ChatUtils.color("Failed to create world: " + cause(throwable).getMessage())); return; } placesModule.getWorldModels().add(worldModel); - }); - player.sendMessage(ChatUtils.color("World " + loadingName + " has been created!")); + player.sendMessage(ChatUtils.color("World " + loadingName + " has been created!")); - for (Player worldPlayer : player.getWorld().getPlayers()) { - MinetopiaPlayer minetopiaPlayer = PlayerManager.getInstance().getOnlineMinetopiaPlayer(worldPlayer); - if (minetopiaPlayer == null) return; + World world = Bukkit.getWorld(worldName); + if (world == null) return; + for (Player worldPlayer : world.getPlayers()) { + MinetopiaPlayer minetopiaPlayer = PlayerManager.getInstance().getOnlineMinetopiaPlayer(worldPlayer); + if (minetopiaPlayer == null) continue; - minetopiaPlayer.getFitness().getFitnessModule().getFitnessRunnable().forceMarkDirty(worldPlayer.getUniqueId()); + minetopiaPlayer.getFitness().getFitnessModule().getFitnessRunnable().forceMarkDirty(worldPlayer.getUniqueId()); - ScoreboardManager.getInstance().addScoreboard(worldPlayer); - } + ScoreboardManager.getInstance().addScoreboard(worldPlayer); + } + })); + } + + /** The save's own exception rather than the CompletionException wrapping it. */ + private static Throwable cause(Throwable throwable) { + return throwable instanceof CompletionException && throwable.getCause() != null ? throwable.getCause() : throwable; } }