Better error handling for finding files in server directory

This commit is contained in:
Ben Woo 2023-09-01 23:33:30 +08:00
parent 55aac81258
commit 867f4a447d
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8

View File

@ -105,30 +105,31 @@ public class FileUtils {
@Nullable
public static File getBukkitConfig() {
// Look in the default position
File[] files = getServer().getWorldContainer().listFiles((file, s) -> s.equalsIgnoreCase("bukkit.yml"));
if (files != null && files.length == 1) {
return files[0];
}
// TODO: Implement binary search to find file, config option or use reflections to get it from configuration on CraftServer
Logging.warning("Could not read bukkit.yml");
return null;
return findFileFromServerDirectory("bukkit.yml");
}
@Nullable
public static File getServerProperties() {
// Look in the default position
File[] files = getServer().getWorldContainer().listFiles((file, s) -> s.equalsIgnoreCase("server.properties"));
return findFileFromServerDirectory("server.properties");
}
if (files != null && files.length == 1) {
return files[0];
@Nullable
private static File findFileFromServerDirectory(String fileName) {
File[] files;
try {
// TODO: getWorldContainer may throw error for MockBukkit during test
files = getServer().getWorldContainer().listFiles((file, s) -> s.equalsIgnoreCase(fileName));
} catch (Exception e) {
Logging.severe("Could not read from server directory. Unable to locate file: %s", fileName);
Logging.severe(e.getMessage());
return null;
}
// TODO: Implement binary search to find file, config option or use reflections to get it from configuration on CraftServer
Logging.warning("Could not read 'server.properties");
if (files != null && files.length == 1) {
return files[0];
}
Logging.warning("Unable to locate file from server directory: %s", fileName);
return null;
}