mirror of
https://gitlab.com/SamB440/rpgregions-2.git
synced 2026-01-06 15:41:35 +00:00
Add option to save data async
This commit is contained in:
@@ -4,6 +4,7 @@ import net.islandearth.rpgregions.managers.data.region.ConfiguredRegion;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface IRPGRegionsCache {
|
||||
|
||||
@@ -15,4 +16,6 @@ public interface IRPGRegionsCache {
|
||||
void removeConfiguredRegion(String id);
|
||||
|
||||
Map<String, ConfiguredRegion> getConfiguredRegions();
|
||||
|
||||
CompletableFuture<Boolean> saveAll(boolean async);
|
||||
}
|
||||
|
||||
@@ -233,6 +233,7 @@ public final class RPGRegions extends JavaPlugin implements IRPGRegionsAPI, Lang
|
||||
manager.enableUnstableAPI("help");
|
||||
manager.getCommandCompletions().registerAsyncCompletion("regions", context -> ImmutableList.copyOf(getManagers().getRegionsCache().getConfiguredRegions().keySet()));
|
||||
manager.getCommandCompletions().registerAsyncCompletion("integration-regions", context -> ImmutableList.copyOf(this.getManagers().getIntegrationManager().getAllRegionNames(context.getPlayer().getWorld())));
|
||||
manager.getCommandCompletions().registerAsyncCompletion("async", context -> ImmutableList.of("--async"));
|
||||
manager.getCommandCompletions().registerAsyncCompletion("schematics", context -> {
|
||||
File schematicFolder = new File("plugins/WorldEdit/schematics/");
|
||||
List<String> files = new ArrayList<>();
|
||||
|
||||
@@ -33,9 +33,11 @@ import java.io.Reader;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@CommandAlias("rpgregions")
|
||||
public class RPGRegionsCommand extends BaseCommand {
|
||||
@@ -168,8 +170,10 @@ public class RPGRegionsCommand extends BaseCommand {
|
||||
|
||||
@Subcommand("save")
|
||||
@CommandPermission("rpgregions.save")
|
||||
public void onSave(CommandSender sender) {
|
||||
sender.sendMessage(ChatColor.GREEN + "Saving data...");
|
||||
@CommandCompletion("@async")
|
||||
public void onSave(CommandSender sender, @co.aikar.commands.annotation.Optional String[] args) {
|
||||
boolean async = Arrays.asList(args).contains("--async");
|
||||
sender.sendMessage(ChatColor.GREEN + "Saving data..." + (async ? ChatColor.GOLD + " (async)" : ""));
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
// Save all player data (quit event not called for shutdown)
|
||||
@@ -179,11 +183,15 @@ public class RPGRegionsCommand extends BaseCommand {
|
||||
});
|
||||
|
||||
// Save all region configs
|
||||
plugin.getManagers().getRegionsCache().getConfiguredRegions().forEach((id, region) -> region.save(plugin));
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
long totalTime = endTime - startTime;
|
||||
sender.sendMessage(ChatColor.GREEN + "Done! (" + totalTime + "ms)");
|
||||
long asyncStartTime = System.currentTimeMillis();
|
||||
CompletableFuture<Boolean> saveFuture = plugin.getManagers().getRegionsCache().saveAll(async);
|
||||
long mainEndTime = System.currentTimeMillis();
|
||||
long mainTotalTime = mainEndTime - startTime;
|
||||
saveFuture.thenAccept(saved -> {
|
||||
long asyncTotalTime = System.currentTimeMillis() - asyncStartTime;
|
||||
sender.sendMessage(ChatColor.GREEN + "Done! (" + mainTotalTime + "ms) "
|
||||
+ ChatColor.GOLD + "(async execution took " + asyncTotalTime + "ms)");
|
||||
});
|
||||
}
|
||||
|
||||
@Subcommand("reset")
|
||||
@@ -289,7 +297,6 @@ public class RPGRegionsCommand extends BaseCommand {
|
||||
regenerateConfirm.add(player.getUniqueId());
|
||||
player.sendMessage(ChatColor.YELLOW + "Run /rpgregions addschematic " + schematicName + " again to confirm. Only use if you know what you are doing!");
|
||||
player.sendMessage(ChatColor.RED + "MAKE SURE YOU ARE STANDING WHERE YOU CREATED THE SCHEMATIC ORIGINALLY (//copy, //schematic save), OTHERWISE IT WILL NOT PASTE CORRECTLY.");
|
||||
player.sendMessage(WARNING_MESSAGE);
|
||||
} else {
|
||||
regenerateConfirm.remove(player.getUniqueId());
|
||||
Regenerate regenerate = configuredRegion.getRegenerate();
|
||||
@@ -297,10 +304,10 @@ public class RPGRegionsCommand extends BaseCommand {
|
||||
regenerate.setSchematicName(schematicName);
|
||||
regenerate.setOrigin(player.getLocation());
|
||||
configuredRegion.setRegenerate(regenerate);
|
||||
player.sendMessage(ChatColor.GREEN + "This region has had a regenerate section added, and schematicName set to " + schematicName + " and origin set to " + player.getLocation().toString() + ".");
|
||||
player.sendMessage(ChatColor.GREEN + "This region has had a regenerate section added, and schematicName set to " + schematicName + " and origin set to " + player.getLocation() + ".");
|
||||
player.sendMessage(ChatColor.YELLOW + "Run /rpgregions save and " + ChatColor.BOLD + "CONFIGURE BEFORE RELOADING OR RESTARTING THE SERVER.");
|
||||
player.sendMessage(WARNING_MESSAGE);
|
||||
}
|
||||
player.sendMessage(WARNING_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@ package net.islandearth.rpgregions.managers.data;
|
||||
|
||||
import net.islandearth.rpgregions.RPGRegions;
|
||||
import net.islandearth.rpgregions.managers.data.region.ConfiguredRegion;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class RPGRegionsCache implements IRPGRegionsCache {
|
||||
@@ -36,4 +38,19 @@ public class RPGRegionsCache implements IRPGRegionsCache {
|
||||
public Map<String, ConfiguredRegion> getConfiguredRegions() {
|
||||
return configuredRegions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Boolean> saveAll(boolean async) {
|
||||
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
||||
if (async) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
configuredRegions.forEach((id, region) -> region.save(plugin));
|
||||
future.complete(true);
|
||||
});
|
||||
} else {
|
||||
configuredRegions.forEach((id, region) -> region.save(plugin));
|
||||
future.complete(true);
|
||||
}
|
||||
return future;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user