9
9
10
10
import java .io .File ;
11
11
import java .io .IOException ;
12
+ import java .lang .management .ManagementFactory ;
12
13
import java .nio .file .*;
13
14
import java .nio .file .attribute .BasicFileAttributes ;
14
15
import java .util .Collections ;
19
20
import com .dumptruckman .minecraft .util .Logging ;
20
21
import io .vavr .control .Try ;
21
22
import jakarta .inject .Inject ;
23
+ import org .bukkit .Bukkit ;
22
24
import org .jetbrains .annotations .NotNull ;
23
25
import org .jetbrains .annotations .Nullable ;
24
26
import org .jvnet .hk2 .annotations .Service ;
27
+ import org .mvplugins .multiverse .core .config .CoreConfig ;
25
28
26
29
import static java .nio .file .StandardCopyOption .COPY_ATTRIBUTES ;
27
30
31
34
@ Service
32
35
public final class FileUtils {
33
36
37
+ private final CoreConfig config ;
34
38
private final File serverFolder ;
35
- private final File bukkitYml ;
36
- private final File serverProperties ;
39
+ private File bukkitYml ;
40
+ private File serverProperties ;
37
41
38
42
@ Inject
39
- FileUtils () {
43
+ FileUtils (CoreConfig config ) {
44
+ this .config = config ;
40
45
this .serverFolder = new File (System .getProperty ("user.dir" ));
41
46
Logging .finer ("Server folder: " + this .serverFolder );
42
- this .bukkitYml = findFileFromServerDirectory ("bukkit.yml" );
43
- this .serverProperties = findFileFromServerDirectory ("server.properties" );
44
- }
45
-
46
- private @ Nullable File findFileFromServerDirectory (String fileName ) {
47
- File [] files ;
48
- try {
49
- files = this .serverFolder .listFiles ((file , s ) -> s .equalsIgnoreCase (fileName ));
50
- } catch (Exception e ) {
51
- Logging .severe ("Could not read from server directory. Unable to locate file: %s" , fileName );
52
- Logging .severe (e .getMessage ());
53
- return null ;
54
- }
55
- if (files != null && files .length == 1 ) {
56
- return files [0 ];
57
- }
58
- Logging .warning ("Unable to locate file from server directory: %s" , fileName );
59
- return null ;
60
47
}
61
48
62
49
/**
@@ -74,6 +61,10 @@ public File getServerFolder() {
74
61
* @return The bukkit.yml file if exist, else null.
75
62
*/
76
63
public @ Nullable File getBukkitConfig () {
64
+ if (this .bukkitYml == null ) {
65
+ this .bukkitYml = findFileFromServerDirectory (config .getBukkitYmlPath ());
66
+ Logging .finer ("Bukkit.yml: " + this .bukkitYml );
67
+ }
77
68
return this .bukkitYml ;
78
69
}
79
70
@@ -83,9 +74,26 @@ public File getServerFolder() {
83
74
* @return The server.properties file if exist, else null.
84
75
*/
85
76
public @ Nullable File getServerProperties () {
77
+ if (this .serverProperties == null ) {
78
+ this .serverProperties = findFileFromServerDirectory ("server.properties" );
79
+ Logging .finer ("server.properties: %s" , this .serverProperties );
80
+ }
86
81
return this .serverProperties ;
87
82
}
88
83
84
+ private @ Nullable File findFileFromServerDirectory (String fileName ) {
85
+ if (this .serverFolder == null ) {
86
+ Logging .warning ("Unable to locate server directory." );
87
+ return null ;
88
+ }
89
+ File file = new File (this .serverFolder , fileName );
90
+ if (!file .exists ()) {
91
+ Logging .warning ("Unable to locate file from server directory: %s" , fileName );
92
+ return null ;
93
+ }
94
+ return file ;
95
+ }
96
+
89
97
/**
90
98
* Deletes the given folder completely.
91
99
*
0 commit comments