-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleYml.java
148 lines (126 loc) · 4.69 KB
/
SimpleYml.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
package fi.natroutter.natlibs.config;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import fi.natroutter.natlibs.NATLibs;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
public class SimpleYml extends YamlConfiguration {
private Class clazz;
private File file;
private YamlConfiguration defaults;
private String resourceLocation;
/**
* @param file of the file
*/
public SimpleYml(Class clazz, File file) {
this.clazz = clazz;
this.file = file;
this.resourceLocation = "/"+file.getName();
options().parseComments(true);
options().copyDefaults(true);
options().copyHeader(true);
reload();
save();
}
public SimpleYml(Class clazz, File file, String resourceLocation) {
this.clazz = clazz;
this.file = file;
this.resourceLocation = resourceLocation.startsWith("/") ? resourceLocation : "/" + resourceLocation;
options().parseComments(true);
options().copyDefaults(true);
options().copyHeader(true);
reload();
save();
}
/**
* Gets default value from jars yml file.
*
* @param path for value
* @return value
*/
public <T> T getOrDefault(String path) {
T value = (T) get(path);
T defaultVal = (T) defaults.get(path);
if (value == null && defaultVal == null) {
Bukkit.getLogger().warning("§4["+clazz.getSimpleName()+"] §cMissing entry \""+path+"\" in file " + file.getName());
} else if (value == null) {
Bukkit.getLogger().warning("§4["+clazz.getSimpleName()+"] §cMissing entry \""+path+"\" in file " + file.getName() + " Using default: " + defaultVal);
}
return value == null ? defaultVal : value;
}
/**
* Gets from user set value.
*
* @param path for value
* @return value
*/
public <T> T getOrDefault(String path, T def) {
T value = (T) get(path);
return value == null ? def : value;
}
//* Reloads config from file
public void save() {
Bukkit.getConsoleSender().sendMessage("Saving file: " + file.getName());
try {
save(file);
Bukkit.getConsoleSender().sendMessage("Save: OK");
} catch (IOException e) {
e.printStackTrace();
}
}
public InputStream getResource(String filename) {
try {
URL url = clazz.getResource(filename);
if (url == null) {
return null;
} else {
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
return connection.getInputStream();
}
} catch (IOException var4) {
return null;
}
}
//* Reloads config from file
public void reload() {
//Reloads loads file and sets defaults from source
Bukkit.getConsoleSender().sendMessage("§4TEST: " + clazz.getPackage().getName() + " - " + clazz.getSimpleName());
Bukkit.getConsoleSender().sendMessage("§4TEST1: " + file.getAbsolutePath());
Bukkit.getConsoleSender().sendMessage("§4TEST2: " + file.getName());
Bukkit.getConsoleSender().sendMessage("§4TEST3: " + file.getPath());
Bukkit.getConsoleSender().sendMessage("§4TEST4: " + resourceLocation);
try {
Files.createParentDirs(file);
file.createNewFile();
load(file);
InputStream defaultStream = getResource(resourceLocation);
if (defaultStream == null) {
Bukkit.getConsoleSender().sendMessage("§4DEBUG 1");
} else {
Bukkit.getConsoleSender().sendMessage("§4DEBUG 2");
}
if (defaultStream != null) {
//todo switch to use Files.bufferedreader
defaults = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultStream, Charsets.UTF_8));
//Copies header
options().setHeader(defaults.options().getHeader());
//Copies default values which havent been added yet & comments
setDefaults(defaults);
}
} catch (IOException | InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
}
}
}