generated from srnyx/plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AnnoyingData.java
74 lines (64 loc) · 2.13 KB
/
AnnoyingData.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
package xyz.srnyx.annoyingapi.file;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import xyz.srnyx.annoyingapi.AnnoyingPlugin;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* Represents a file in the plugin's folder (in {@code /data/})
*/
public class AnnoyingData extends YamlConfiguration implements AnnoyingFile {
@NotNull private final String path;
@NotNull private final File file;
private final boolean canBeEmpty;
/**
* Constructs and loads a new {@link AnnoyingData} from the path
*
* @param plugin the plugin that is creating the file
* @param path the path to the file (relative to {@code data} folder in the plugin's folder)
* @param canBeEmpty whether the file can be empty. If false, the file will be deleted if it's empty when {@link #save()} is used
*/
public AnnoyingData(@NotNull AnnoyingPlugin plugin, @NotNull String path, boolean canBeEmpty) {
this.path = path;
this.file = new File(plugin.getDataFolder(), "data/" + path);
this.canBeEmpty = canBeEmpty;
load();
}
/**
* Constructs and loads a new {@link AnnoyingData} from the path
*
* @param plugin the plugin that is creating the file
* @param path the path to the file (inside {@code /data/})
*/
public AnnoyingData(@NotNull AnnoyingPlugin plugin, @NotNull String path) {
this(plugin, path, true);
}
@Override @NotNull
public YamlConfiguration getYaml() {
return this;
}
@Override @NotNull
public String getPath() {
return path;
}
@Override @NotNull
public File getFile() {
return file;
}
@Override
public boolean canBeEmpty() {
return canBeEmpty;
}
@Override
public void create() {
final Path filePath = file.toPath();
try {
Files.createDirectories(filePath.getParent());
Files.createFile(filePath);
} catch (final IOException e) {
e.printStackTrace();
}
}
}