-
Notifications
You must be signed in to change notification settings - Fork 0
/
YmlFile.java
163 lines (150 loc) · 5.19 KB
/
YmlFile.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package net.codersky.mcutils.files.yaml;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.plugin.java.JavaPlugin;
import net.codersky.mcutils.MCPlugin;
import net.codersky.mcutils.files.FileHolder;
/**
* Represents a basic yml file, not necessarily present
* on the plugin's jar file as a resource.
*
* @since MCUtils 1.0.0
*
* @author xDec0de_
*/
public class YmlFile extends CharsetYamlConfiguration implements FileHolder {
private final String path;
final File file;
/**
* Creates an instance, <b>NOT</b> a file, of a {@link YmlFile} for the specified <b>plugin</b> and <b>path</b>.
* Fast access for file creation is provided by {@link MCPlugin#registerFile(String, Class)}.
* <p>
* {@link YmlFile}s aren't required to be on <b>plugin</b>'s jar file as a resource. For this
* exact reason they can't copy any default file from <b>plugin</b>'s source. This files can
* be used, for example, as storage files for players, where you don't need a "template" file to copy.
*
* @param plugin an instance of the plugin creating the file, used to get it's data folder, if null,
* the file will be just in <b>path</b>, not inside a plugin data folder.
* @param path the path of the file to create, the ".yml" extension is automatically added if missing,
* if the path is null, empty or blank, "file" will be used.
* @param charset the charset to use, if null, {@link StandardCharsets#UTF_8} will be used.
*
* @throws IllegalArgumentException if <b>plugin</b> is null.
*
* @since MCUtils 1.0.0
*
* @see MCPlugin#registerFile(YmlFile)
* @see MCPlugin#registerFile(String, Class)
* @see #create()
*/
public YmlFile(@Nullable JavaPlugin plugin, @Nullable String path, @Nullable Charset charset) {
super(charset);
String modifiedPath = path;
if (modifiedPath == null || modifiedPath.isBlank())
modifiedPath = "file.yml";
else if (!modifiedPath.endsWith(".yml"))
modifiedPath += ".yml";
this.file = plugin == null ? new File(modifiedPath) : new File(plugin.getDataFolder(), modifiedPath);
this.path = modifiedPath;
}
/**
* Creates an instance, <b>NOT</b> a file, of a {@link YmlFile} for the specified <b>plugin</b> and <b>path</b>.
* Fast access for file creation is provided by {@link MCPlugin#registerFile(String, Class)}.
* <p>
* {@link YmlFile}s aren't required to be on <b>plugin</b>'s jar file as a resource. For this
* exact reason they can't copy any default file from <b>plugin</b>'s source. This files can
* be used, for example, as storage files for players, where you don't need a "template" file to copy.
* <p>
* This constructor uses {@link StandardCharsets#UTF_8}, to specify use {@link #YmlFile(JavaPlugin, String, Charset)}
*
* @param plugin an instance of the plugin creating the file, used to get it's data folder.
* @param path the path of the file to create, the ".yml" extension is automatically added if missing,
* if the path is null, empty or blank, "file" will be used.
*
* @throws IllegalArgumentException if <b>plugin</b> is null.
*
* @since MCUtils 1.0.0
*
* @see MCPlugin#registerFile(YmlFile)
* @see MCPlugin#registerFile(String, Class)
* @see #create()
*/
public YmlFile(@Nonnull JavaPlugin plugin, @Nullable String path) {
this(plugin, path, StandardCharsets.UTF_8);
}
/**
* Creates this file, the file is not required to
* be present on the plugin's jar. For this reason
* the file created will be empty. If you want to use
* a plugin source file use {@link PluginFile} instead.
* @return
*
* @since MCUtils 1.0.0
*/
public boolean create() {
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return reload();
}
/**
* Gets the path of this yml file, including the .yml extension.
* Files are always inside of the data folder of the plugin
* that created them, which isn't a part of this String.
*
* @return The path of this yml file.
*
* @since MCUtils 1.0.0
*/
@Nonnull
public String getPath() {
return path;
}
public File getFile() {
return file;
}
/**
* Saves this file. If there is any error saving the file,
* the errors will be logged and false will be returned.
*
* @return true if no errors occurred while saving, false otherwise.
*
* @since MCUtils 1.0.0
*/
public boolean save() {
try {
this.save(file);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* Reloads this file. Any non saved value contained within this configuration will be removed
* and the new values will be loaded from the given file. If there is any error reloading the file,
* the errors will be logged and false will be returned.
*
* @return true if no errors occurred while reloading, false otherwise.
*
* @since MCUtils 1.0.0
*/
public boolean reload() {
try {
this.load(file);
return true;
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
return false;
}
}
}