forked from Happyuky7/FileManagerBukkit1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileManager.java
121 lines (102 loc) · 3.94 KB
/
FileManager.java
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationOptions;
import org.bukkit.configuration.MemoryConfigurationOptions;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
public class FileManager extends YamlConfiguration {
/*
Code by: Happyuky7
Github: https://github.com/Happyuky7
License: MIT
Link: https://github.com/Happyuky7/FileManagerBukkit1
*/
/*
FileManager Link: https://github.com/Happyuky7/FileManagerBukkit1
*/
private final String fileName;
private final JavaPlugin plugin;
private final File folder;
public FileManager(JavaPlugin plugin, String fileName, File folder) {
this(plugin, fileName, ".yml", folder);
}
public FileManager(JavaPlugin plugin, String filename, String fileextension, File folder) {
this.folder = folder;
this.plugin = plugin;
this.fileName = filename + (filename.endsWith(fileextension) ? "" : fileextension);
createFile();
}
public FileManager(JavaPlugin plugin, String fileName) {
this(plugin, fileName, ".yml");
}
public FileManager(JavaPlugin plugin, String fileName, String fileExtension) {
this(plugin, fileName, fileExtension, plugin.getDataFolder());
}
public JavaPlugin getPlugin() {
return this.plugin;
}
public String getColouredString(String path) {
String getted;
try {
getted = getString(path);
} catch (NullPointerException e) {
getted = path;
}
return ChatColor.translateAlternateColorCodes('&', getted);
}
public List<String> getColouredStringList(String path) {
List<String> f = new ArrayList<>();
for (String l : getStringList(path))
f.add(l.replace('&', '&'));
return f;
}
public <T> T get(Class<T> clazz, String path) {
Object obj = get(path);
return clazz.cast(obj);
}
private void createFile() {
try {
File file = new File(this.folder, this.fileName);
if (!file.exists()) {
if (this.plugin.getResource(this.fileName) != null) {
this.plugin.saveResource(this.fileName, false);
} else {
save(file);
}
load(file);
} else {
load(file);
save(file);
}
} catch (Exception ex) {
this.plugin.getLogger().log(Level.SEVERE, "error occurred creating the " + this.fileName + " file");
this.plugin.getLogger().log(Level.SEVERE, "");
this.plugin.getLogger().log(Level.SEVERE, "Error:" + ex.getMessage());
}
}
public void save() {
File folder = this.plugin.getDataFolder();
try {
save(new File(folder, this.fileName));
} catch (Exception ex) {
this.plugin.getLogger().log(Level.SEVERE, "error occurred while saving the " + this.fileName + " file");
this.plugin.getLogger().log(Level.SEVERE, "");
this.plugin.getLogger().log(Level.SEVERE, "Error:" + ex.getMessage());
}
}
public void reload() {
File folder = this.plugin.getDataFolder();
File file = new File(folder, this.fileName);
try {
load(file);
} catch (IOException|org.bukkit.configuration.InvalidConfigurationException e) {
this.plugin.getLogger().log(Level.SEVERE, "error occurred while reloading the " + this.fileName + " file");
this.plugin.getLogger().log(Level.SEVERE, "");
this.plugin.getLogger().log(Level.SEVERE, "Error:" + e.getMessage());
}
}
}