From 94040e42869f47a796fc37648fbbc1be70f0534f Mon Sep 17 00:00:00 2001 From: Fabrizio La Rosa Date: Wed, 29 Jul 2020 00:35:35 +0200 Subject: [PATCH] Optimize visitors management --- .../com/songoda/skyblock/visit/Visit.java | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/songoda/skyblock/visit/Visit.java b/src/main/java/com/songoda/skyblock/visit/Visit.java index 89585dc7..546cf927 100644 --- a/src/main/java/com/songoda/skyblock/visit/Visit.java +++ b/src/main/java/com/songoda/skyblock/visit/Visit.java @@ -8,11 +8,13 @@ import com.songoda.skyblock.island.IslandLevel; import com.songoda.skyblock.island.IslandLocation; import com.songoda.skyblock.island.IslandStatus; import com.songoda.skyblock.island.IslandWorld; +import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import java.io.File; import java.io.IOException; import java.util.*; +import java.util.stream.Collectors; public class Visit { @@ -24,8 +26,9 @@ public class Visit { private int islandSize; private int islandMembers; private int safeLevel; - private double islandBankBalance; + private final double islandBankBalance; private List islandSignature; + private final Set islandVisitors; private IslandStatus status; @@ -41,6 +44,16 @@ public class Visit { this.islandLevel = islandLevel; this.islandSignature = islandSignature; this.status = status; + this.islandVisitors = new HashSet<>(); + + FileConfiguration configLoad = plugin.getFileManager() + .getConfig(new File(new File(plugin.getDataFolder().toString() + "/visit-data"), + islandOwnerUUID.toString() + ".yml")) + .getFileConfiguration(); + + for (String visitor : configLoad.getStringList("Visitors")) { + islandVisitors.add(FastUUID.parseUUID(visitor)); + } } public UUID getOwnerUUID() { @@ -109,46 +122,17 @@ public class Visit { } public Set getVisitors() { - Set islandVisitors = new HashSet<>(); - - for (String islandVisitorList : plugin.getFileManager() - .getConfig(new File(new File(plugin.getDataFolder().toString() + "/visit-data"), - islandOwnerUUID.toString() + ".yml")) - .getFileConfiguration().getStringList("Visitors")) { - islandVisitors.add(FastUUID.parseUUID(islandVisitorList)); - } - return islandVisitors; } public void addVisitor(UUID uuid) { - List islandVisitors = new ArrayList<>(); - FileConfiguration configLoad = plugin.getFileManager() - .getConfig(new File(new File(plugin.getDataFolder().toString() + "/visit-data"), - islandOwnerUUID.toString() + ".yml")) - .getFileConfiguration(); - - for (String islandVisitorList : configLoad.getStringList("Visitors")) { - islandVisitors.add(islandVisitorList); - } - - islandVisitors.add(FastUUID.toString(uuid)); - configLoad.set("Visitors", islandVisitors); + islandVisitors.add(uuid); + Bukkit.getScheduler().runTaskAsynchronously(plugin, this::save); } public void removeVisitor(UUID uuid) { - List islandVisitors = new ArrayList<>(); - FileConfiguration configLoad = plugin.getFileManager() - .getConfig(new File(new File(plugin.getDataFolder().toString() + "/visit-data"), - islandOwnerUUID.toString() + ".yml")) - .getFileConfiguration(); - - for (String islandVisitorList : configLoad.getStringList("Visitors")) { - islandVisitors.add(islandVisitorList); - } - - islandVisitors.remove(FastUUID.toString(uuid)); - configLoad.set("Visitors", islandVisitors); + islandVisitors.remove(uuid); + Bukkit.getScheduler().runTaskAsynchronously(plugin, this::save); } public boolean isVoter(UUID uuid) { @@ -209,10 +193,12 @@ public class Visit { return plugin.getBanManager().getIsland(getOwnerUUID()); } - public void save() { + public synchronized void save() { FileManager.Config config = plugin.getFileManager().getConfig(new File( new File(plugin.getDataFolder().toString() + "/visit-data"), islandOwnerUUID.toString() + ".yml")); - + + config.getFileConfiguration().set("Visitors", new ArrayList<>(islandVisitors.stream().map(UUID::toString).collect(Collectors.toSet()))); + try { config.getFileConfiguration().save(config.getFile()); } catch (IOException e) {