Skip to content

Commit ffa5379

Browse files
committed
Improve error handling and logging in YAML storage and player/world loading
1 parent bff0258 commit ffa5379

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed

buildsystem-core/src/main/java/de/eintosti/buildsystem/BuildSystemPlugin.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
import org.bukkit.plugin.PluginManager;
108108
import org.bukkit.plugin.ServicePriority;
109109
import org.bukkit.plugin.java.JavaPlugin;
110+
import org.bukkit.scheduler.BukkitTask;
110111

111112
public class BuildSystemPlugin extends JavaPlugin {
112113

@@ -132,6 +133,8 @@ public class BuildSystemPlugin extends JavaPlugin {
132133

133134
private BuildSystemApi api;
134135

136+
private BukkitTask configSaveTask;
137+
135138
@Override
136139
public void onLoad() {
137140
instance = this;
@@ -169,7 +172,7 @@ public void onEnable() {
169172

170173
registerStats();
171174

172-
Bukkit.getScheduler().runTaskTimer(this, this::saveBuildConfig, 6000L, 6000L);
175+
this.configSaveTask = Bukkit.getScheduler().runTaskTimer(this, this::saveBuildConfig, 6000L, 6000L);
173176

174177
Bukkit.getConsoleSender().sendMessage(
175178
"%sBuildSystem » Plugin %senabled%s!".formatted(ChatColor.RESET, ChatColor.GREEN, ChatColor.RESET)
@@ -194,6 +197,10 @@ public void onDisable() {
194197
saveConfig();
195198
saveBuildConfig();
196199

200+
if (this.configSaveTask != null) {
201+
this.configSaveTask.cancel();
202+
}
203+
197204
unregisterExpansions();
198205
this.api.unregister();
199206

buildsystem-core/src/main/java/de/eintosti/buildsystem/storage/PlayerStorageImpl.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import java.util.HashMap;
2828
import java.util.Map;
2929
import java.util.UUID;
30-
import java.util.concurrent.ExecutionException;
3130
import java.util.function.Function;
31+
import java.util.logging.Level;
3232
import java.util.logging.Logger;
3333
import java.util.stream.Collectors;
3434
import org.bukkit.entity.Player;
@@ -51,13 +51,14 @@ public PlayerStorageImpl(BuildSystemPlugin plugin) {
5151
}
5252

5353
public void loadPlayers() {
54-
try {
55-
this.buildPlayers.putAll(
56-
load().get().stream().collect(Collectors.toMap(BuildPlayer::getUniqueId, Function.identity()))
57-
);
58-
} catch (InterruptedException | ExecutionException e) {
59-
logger.severe("Failed to load players from storage: " + e.getMessage());
60-
}
54+
load().thenAccept(players -> {
55+
Map<UUID, BuildPlayer> loadedPlayers = players.stream().collect(Collectors.toMap(BuildPlayer::getUniqueId, Function.identity()));
56+
this.buildPlayers.putAll(loadedPlayers);
57+
logger.info("Loaded " + players.size() + " players from storage");
58+
}).exceptionally(throwable -> {
59+
logger.log(Level.SEVERE, "Failed to load players from storage", throwable);
60+
return null;
61+
});
6162
}
6263

6364
@Override

buildsystem-core/src/main/java/de/eintosti/buildsystem/storage/WorldStorageImpl.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import java.util.Map;
3737
import java.util.Objects;
3838
import java.util.UUID;
39-
import java.util.concurrent.ExecutionException;
39+
import java.util.logging.Level;
4040
import java.util.logging.Logger;
4141
import org.bukkit.Bukkit;
4242
import org.bukkit.World;
@@ -164,32 +164,29 @@ public boolean isCorrectVisibility(boolean privateWorld, Visibility visibility)
164164
}
165165

166166
public void loadWorlds() {
167-
try {
168-
load().get().forEach(this::addBuildWorld);
167+
load().thenAccept(worlds -> {
168+
worlds.forEach(this::addBuildWorld);
169169
assignWorldsToFolders();
170-
} catch (InterruptedException | ExecutionException e) {
171-
logger.severe("Failed to load worlds from storage: " + e.getMessage());
172-
return;
173-
}
174-
175-
boolean loadAllWorlds = !Unload.enabled;
176-
if (loadAllWorlds) {
177-
logger.info("*** All worlds will be loaded now ***");
178-
} else {
179-
logger.info("*** World unloading is enabled ('world.unload.enabled' = true); skipping pre-loading of worlds ***");
180-
}
181170

182-
List<BuildWorld> notLoaded = new ArrayList<>();
183-
getBuildWorlds().forEach(buildWorld -> {
184-
if (loadWorld(buildWorld, loadAllWorlds) == LoadResult.FAILED) {
185-
notLoaded.add(buildWorld);
171+
boolean loadAllWorlds = !Unload.enabled;
172+
if (loadAllWorlds) {
173+
logger.info("*** All worlds will be loaded now ***");
186174
}
187-
});
188-
notLoaded.forEach(this::removeBuildWorld);
189175

190-
if (loadAllWorlds) {
191-
logger.info("*** All worlds have been loaded ***");
192-
}
176+
List<BuildWorld> notLoaded = new ArrayList<>();
177+
getBuildWorlds().forEach(buildWorld -> {
178+
LoadResult loadResult = loadWorld(buildWorld, loadAllWorlds);
179+
if (loadResult == LoadResult.FAILED) {
180+
notLoaded.add(buildWorld);
181+
}
182+
});
183+
notLoaded.forEach(this::removeBuildWorld);
184+
185+
logger.info("Loaded " + worlds.size() + " worlds from storage");
186+
}).exceptionally(throwable -> {
187+
logger.log(Level.SEVERE, "Failed to load worlds from storage", throwable);
188+
return null;
189+
});
193190
}
194191

195192
/**

buildsystem-core/src/main/java/de/eintosti/buildsystem/storage/yaml/AbstractYamlStorage.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import de.eintosti.buildsystem.BuildSystemPlugin;
2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
2325
import org.bukkit.configuration.InvalidConfigurationException;
2426
import org.bukkit.configuration.file.FileConfiguration;
2527
import org.bukkit.configuration.file.YamlConfiguration;
@@ -31,10 +33,12 @@ public abstract class AbstractYamlStorage {
3133

3234
private final File file;
3335
private final FileConfiguration configuration;
36+
private final Logger logger;
3437

3538
public AbstractYamlStorage(BuildSystemPlugin plugin, String fileName) {
3639
this.file = new File(plugin.getDataFolder(), fileName);
3740
this.configuration = YamlConfiguration.loadConfiguration(file);
41+
this.logger = plugin.getLogger();
3842
loadFile();
3943
}
4044

@@ -48,15 +52,15 @@ public void loadFile() {
4852
try {
4953
configuration.load(file);
5054
} catch (IOException | InvalidConfigurationException e) {
51-
e.printStackTrace();
55+
logger.log(Level.SEVERE, "Failed to load configuration file: " + file.getName(), e);
5256
}
5357
}
5458

5559
public void saveFile() {
5660
try {
5761
configuration.save(file);
5862
} catch (IOException e) {
59-
e.printStackTrace();
63+
logger.log(Level.SEVERE, "Failed to save configuration file: " + file.getName(), e);
6064
}
6165
}
6266

buildsystem-core/src/main/java/de/eintosti/buildsystem/util/UUIDFetcher.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.gson.TypeAdapter;
2424
import com.google.gson.stream.JsonReader;
2525
import com.google.gson.stream.JsonWriter;
26+
import de.eintosti.buildsystem.BuildSystemPlugin;
2627
import de.eintosti.buildsystem.util.ServerModeChecker.ServerMode;
2728
import java.io.BufferedReader;
2829
import java.io.FileNotFoundException;
@@ -34,6 +35,7 @@
3435
import java.util.Locale;
3536
import java.util.Map;
3637
import java.util.UUID;
38+
import java.util.logging.Level;
3739
import org.bukkit.Bukkit;
3840
import org.jspecify.annotations.NullMarked;
3941
import org.jspecify.annotations.Nullable;
@@ -83,7 +85,7 @@ public static UUID getUUID(String name) {
8385
cacheUser(uuid, name);
8486
return uuid;
8587
} catch (Exception e) {
86-
e.printStackTrace();
88+
BuildSystemPlugin.get().getLogger().log(Level.SEVERE, "Failed to fetch UUID for player: " + name, e);
8789
}
8890

8991
return null;
@@ -126,7 +128,7 @@ public static String getName(UUID uuid) {
126128
cacheUser(uuid, name);
127129
return name;
128130
} catch (Exception e) {
129-
e.printStackTrace();
131+
BuildSystemPlugin.get().getLogger().log(Level.SEVERE, "Failed to fetch name for UUID: " + uuid, e);
130132
}
131133

132134
return null;

0 commit comments

Comments
 (0)