-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.java
More file actions
103 lines (92 loc) · 3.32 KB
/
FileManager.java
File metadata and controls
103 lines (92 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.github.pinont.singularitylib.api.manager;
import com.github.pinont.singularitylib.plugin.CorePlugin;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.List;
import java.util.Objects;
import static com.github.pinont.singularitylib.plugin.CorePlugin.sendConsoleMessage;
import static com.github.pinont.singularitylib.plugin.CorePlugin.sendDebugMessage;
/**
* Manages file operations for the plugin.
* Provides utilities for file and folder management within the plugin's data directory.
*/
public class FileManager {
/**
* The plugin instance for accessing the data folder.
*/
public final Plugin plugin;
/**
* Default constructor for FileManager.
*/
public FileManager(Plugin plugin) {
this.plugin = plugin;
}
/**
* Gets a file from the plugin's data folder.
*
* @param fileName the name of the file to get
* @return the File object
*/
public File getFile(String fileName) {
// Implement the logic to get the file
return new File(plugin.getDataFolder(), fileName);
}
/**
* Gets a file from a specific subfolder within the plugin's data folder.
*
* @param subfolder the subfolder path
* @param fileName the name of the file to get
* @return the File object
*/
public File getFile(String subfolder, String fileName) {
// Implement the logic to get the file
return new File(plugin.getDataFolder() + "/" + subfolder, fileName);
}
/**
* Gets all files from a specific subfolder.
*
* @param subfolder the subfolder to get files from
* @return a list of files in the subfolder, or empty list if folder doesn't exist or is empty
*/
public List<File> getFileFromFolder(String subfolder) {
File folder = new File(plugin.getDataFolder() + "/" + subfolder);
if (isFolderExists(subfolder) && Objects.requireNonNull(folder.listFiles()).length > 0) {
return List.of(Objects.requireNonNull(folder.listFiles()));
}
return List.of();
}
/**
* Checks if a subfolder exists within the plugin's data directory.
*
* @param subfolder the subfolder path to check
* @return true if the folder exists, false otherwise
*/
public boolean isFolderExists(String subfolder) {
File folder = new File(plugin.getDataFolder() + "/" + subfolder);
sendDebugMessage("Checking if folder exists: " + folder.getAbsolutePath() + " - " + folder.exists());
return folder.exists();
}
/**
* Saves a file to a specific subfolder within the plugin's data directory.
*
* @param subfolder the subfolder to save the file to
* @param file the file to save
*/
public void save(String subfolder, File file) {
if (!file.exists()) {
sendConsoleMessage("File does not exist: " + file.getAbsolutePath());
return;
}
File folder = new File(plugin.getDataFolder() + "/" + subfolder);
if (!isFolderExists(subfolder)) {
folder.mkdirs();
}
file = new File(folder, file.getName());
try {
file.createNewFile();
} catch (Exception e) {
sendConsoleMessage("Error creating file: " + e.getMessage());
}
}
}