Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 18 additions & 30 deletions src/main/java/nl/openminetopia/modules/places/PlacesModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,15 @@ public CompletableFuture<Collection<WorldModel>> getWorlds() {
}

public CompletableFuture<WorldModel> createWorld(String worldName, String title, String color, double temperature, String loadingName) {
CompletableFuture<WorldModel> 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<Void> deleteWorld(String worldName) {
Expand All @@ -180,21 +174,15 @@ public CompletableFuture<Void> deleteWorld(String worldName) {
}

public CompletableFuture<CityModel> createCity(String cityName, String title, String color, double temperature, String loadingName) {
CompletableFuture<CityModel> 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<Void> deleteCity(CityModel city) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -45,19 +48,24 @@ public void create(Player player, String name, String loadingName) {

String title = "<bold>" + loadingName.toUpperCase();
placesModule.createCity(name, title, "<gold>", 21.64, loadingName)
.whenComplete((cityModel, throwable1) -> {
if (throwable != null) {
player.sendMessage(ChatUtils.color("<red>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("<red>Failed to create city: " + cause(saveError).getMessage()));
return;
}
placesModule.getCityModels().add(cityModel);
});

player.sendMessage(ChatUtils.color("<green>City <white>" + loadingName + " <green>has been created!"));
player.sendMessage(ChatUtils.color("<green>City <white>" + loadingName + " <green>has been created!"));
}));
return;
}

player.sendMessage(ChatUtils.color("<red>Region <white>" + name + " <red>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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -29,24 +33,33 @@ public void create(Player player, String loadingName) {
}
}

placesModule.createWorld(player.getWorld().getName(), title, "<gold>", 21.64, loadingName)
.whenComplete((worldModel, throwable) -> {
String worldName = player.getWorld().getName();
placesModule.createWorld(worldName, title, "<gold>", 21.64, loadingName)
.whenComplete((worldModel, throwable) -> Bukkit.getScheduler().runTask(OpenMinetopia.getInstance(), () -> {
if (throwable != null) {
player.sendMessage(ChatUtils.color("<red>Failed to create world: " + throwable.getMessage()));
OpenMinetopia.getInstance().getLogger().severe("Failed to save world " + worldName + ": " + cause(throwable).getMessage());
player.sendMessage(ChatUtils.color("<red>Failed to create world: " + cause(throwable).getMessage()));
return;
}
placesModule.getWorldModels().add(worldModel);
});

player.sendMessage(ChatUtils.color("<green>World <white>" + loadingName + " <green>has been created!"));
player.sendMessage(ChatUtils.color("<green>World <white>" + loadingName + " <green>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;
}
}
Loading