Skip to content

Commit d0313b5

Browse files
committed
Comments improvements
1 parent 5d2194d commit d0313b5

File tree

3 files changed

+34
-61
lines changed

3 files changed

+34
-61
lines changed

src/main/java/pl/mikigal/config/BukkitConfiguration.java

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -27,62 +27,29 @@
2727
*/
2828
public class BukkitConfiguration extends YamlConfiguration {
2929

30-
private static final Field yamlOptionsField;
31-
private static final Field yamlRepresenterField;
32-
private static final Field yamlField;
33-
34-
static {
35-
try {
36-
yamlOptionsField = YamlConfiguration.class.getDeclaredField("yamlOptions");
37-
yamlOptionsField.setAccessible(true);
38-
39-
yamlRepresenterField = YamlConfiguration.class.getDeclaredField("yamlRepresenter");
40-
yamlRepresenterField.setAccessible(true);
41-
42-
yamlField = YamlConfiguration.class.getDeclaredField("yaml");
43-
yamlField.setAccessible(true);
44-
} catch (NoSuchFieldException e) {
45-
throw new InvalidConfigException("Could not find Fields of YamlConfiguration", e);
46-
}
47-
}
48-
4930
/**
5031
* Properties of config
5132
*/
5233
private final File file;
5334
private final NameStyle nameStyle;
5435
private final CommentStyle commentStyle;
5536
private final boolean automaticColorStrings;
37+
private final String configComment;
5638

5739
/**
5840
* Caches
5941
*/
6042
private final Map<String, Object> cache;
6143
private final Map<String, String> comments;
6244

63-
/**
64-
* Internal objects from YamlConfiguration, needed for handling comments
65-
* @see YamlConfiguration
66-
*/
67-
private final DumperOptions yamlOptions;
68-
private final YamlRepresenter yamlRepresenter;
69-
private final Yaml yaml;
70-
71-
public BukkitConfiguration(File file, NameStyle nameStyle, CommentStyle commentStyle, boolean automaticColorStrings) {
45+
public BukkitConfiguration(File file, NameStyle nameStyle, CommentStyle commentStyle, boolean automaticColorStrings, String configComment) {
7246
this.file = file;
7347
this.nameStyle = nameStyle;
7448
this.commentStyle = commentStyle;
7549
this.automaticColorStrings = automaticColorStrings;
7650
this.cache = new HashMap<>();
7751
this.comments = new HashMap<>();
78-
79-
try {
80-
this.yamlOptions = (DumperOptions) yamlOptionsField.get(this);
81-
this.yamlRepresenter = (YamlRepresenter) yamlRepresenterField.get(this);
82-
this.yaml = (Yaml) yamlField.get(this);
83-
} catch (IllegalAccessException e) {
84-
throw new InvalidConfigException("Could not get values of Fields in YamlConfiguration", e);
85-
}
52+
this.configComment = configComment;
8653

8754
this.copyDefaultConfig();
8855
this.load();
@@ -138,40 +105,38 @@ public Object get(String path) {
138105

139106
/**
140107
* Workaround for writing comments to .yml file, Bukkit does to allow to do it
141-
* @return yml content as String
108+
* @return Content of config parsed to YAML
142109
*/
143110
@Override
144111
public String saveToString() {
145-
this.yamlOptions.setIndent(this.options().indent());
146-
this.yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
147-
this.yamlOptions.setAllowUnicode(true);
148-
this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
149-
150-
String header = this.buildHeader();
151-
String dump = yaml.dump(this.getValues(false));
152-
if (dump.equals("{}\n")) {
153-
dump = "";
154-
}
112+
String yaml = super.saveToString();
155113

156114
List<String> lines = new ArrayList<>();
157-
for (String line : (header + dump).split("\n")) {
158-
if (!line.contains(":") || line.startsWith(" ")) {
115+
if (this.configComment != null) {
116+
lines.add(this.configComment);
117+
}
118+
119+
for (String line : yaml.split("\n")) {
120+
// It's not line with new field
121+
if (!line.contains(":") || line.startsWith(" ") || line.startsWith("\t")) {
159122
lines.add(line);
160123
continue;
161124
}
162125

163-
String key = line.split(":")[0];
164-
String comment = this.comments.get(key);
165-
if (comment != null) {
166-
if (this.commentStyle == CommentStyle.ABOVE_CONTENT) {
167-
lines.add("# " + comment);
168-
} else {
169-
lines.add(line + " # " + comment);
170-
}
126+
String configFieldName = line.split(":")[0];
127+
String comment = this.comments.get(configFieldName);
128+
if (comment == null) {
129+
lines.add(line);
130+
continue;
131+
}
171132

133+
if (this.commentStyle == CommentStyle.INLINE) {
134+
lines.add(line + " # " + comment);
172135
continue;
173136
}
174137

138+
// ABOVE_CONTENT
139+
lines.add("# " + comment);
175140
lines.add(line);
176141
}
177142

src/main/java/pl/mikigal/config/ConfigAPI.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pl.mikigal.config;
22

33
import org.bukkit.plugin.java.JavaPlugin;
4+
import pl.mikigal.config.annotation.Comment;
45
import pl.mikigal.config.annotation.ConfigName;
56
import pl.mikigal.config.exception.InvalidConfigException;
67
import pl.mikigal.config.serializer.Serializer;
@@ -47,20 +48,25 @@ public class ConfigAPI {
4748
* @see CommentStyle
4849
* @return Instance of {@param clazz} ready to use methods
4950
*/
50-
public static <T extends Config> T init(Class<T> clazz, NameStyle nameStyle, CommentStyle commentStyle, boolean automaticColorStrings, JavaPlugin plugin) {
51+
public static <T extends Config> T init(Class<T> clazz, NameStyle nameStyle, CommentStyle commentStyle,
52+
boolean automaticColorStrings, JavaPlugin plugin) {
5153
ConfigAPI.plugin = plugin;
5254
ConfigName configName = clazz.getAnnotation(ConfigName.class);
5355
if (configName == null) {
5456
throw new InvalidConfigException("Config must have annotation ConfigName with file's name");
5557
}
5658

59+
Comment configCommentAnnotation = clazz.getAnnotation(Comment.class);
60+
String configComment = configCommentAnnotation == null ? null : configCommentAnnotation.value();
61+
5762
String name = configName.value() + (configName.value().endsWith(".yml") ? "" : ".yml");
5863
File file = new File(plugin.getDataFolder(), name);
5964

60-
BukkitConfiguration rawConfiguration = new BukkitConfiguration(file, nameStyle, commentStyle, automaticColorStrings);
65+
BukkitConfiguration rawConfiguration = new BukkitConfiguration(file, nameStyle, commentStyle, automaticColorStrings, configComment);
6166
rawConfigurations.put(name, rawConfiguration);
6267

63-
T configuration = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new ConfigInvocationHandler(clazz, rawConfiguration, automaticColorStrings));
68+
T configuration = (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz},
69+
new ConfigInvocationHandler(clazz, rawConfiguration, automaticColorStrings));
6470
configurations.put(name, configuration);
6571

6672
return configuration;

src/main/java/pl/mikigal/config/annotation/Comment.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
/**
99
* Allows to create comment for field in config
10+
* New lines can be made with <code>\n</code> if annotation is used for class or CommentStyle is ABOVE_CONTENT
11+
* @see pl.mikigal.config.style.CommentStyle
1012
* @since 1.0
1113
* @author Mikołaj Gałązka
1214
*/
13-
@Target({ElementType.METHOD})
15+
@Target({ElementType.METHOD, ElementType.TYPE})
1416
@Retention(value = RetentionPolicy.RUNTIME)
1517
public @interface Comment {
1618
String value();

0 commit comments

Comments
 (0)