Skip to content

Commit

Permalink
String Array as Config Path (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer authored Jun 20, 2024
1 parent f833eef commit c28aeee
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 480 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public BukkitConfig(Plugin plugin) {
this(plugin, "config.yml");
}

private String toPath(PathString pathString) {
return PathString.toPath(String.valueOf(configuration.options().pathSeparator()), pathString);
private String toPath(String... path) {
return PathString.join(String.valueOf(configuration.options().pathSeparator()), path);
}

private Map<PathString, Object> toPathStringMap(Map<String, Object> map) {
return PathString.toPathStringMap(String.valueOf(configuration.options().pathSeparator()), map);
private Map<String[], Object> toPathStringMap(Map<String, Object> map) {
return PathString.split(String.valueOf(configuration.options().pathSeparator()), map);
}

@Override
Expand All @@ -78,17 +78,17 @@ public YamlConfiguration getOriginal() {
}

@Override
public Object get(PathString path, Object def) {
public Object get(Object def, String... path) {
return this.configuration.get(toPath(path), def);
}

@Override
public void set(PathString path, Object value) {
public void set(Object value, String... path) {
this.configuration.set(toPath(path), value);
}

@Override
public boolean contains(PathString path) {
public boolean contains(String... path) {
return this.configuration.isSet(toPath(path));
}

Expand All @@ -98,8 +98,8 @@ public String getName() {
}

@Override
public Map<PathString, Object> getValues(PathString path, boolean deep) {
if (path.isRoot()) {
public Map<String[], Object> getValues(boolean deep, String... path) {
if (path.length == 0) {
return toPathStringMap(this.configuration.getValues(deep));
} else {
return Optional.ofNullable(this.configuration.getConfigurationSection(toPath(path)))
Expand Down Expand Up @@ -159,8 +159,8 @@ public void reload() {
}

@Override
public List<String> getComment(PathString path, CommentType type) {
if (path.isRoot()) {
public List<String> getComment(CommentType type, String... path) {
if (path.length == 0) {
String header = this.configuration.options().header();
return header.isEmpty() ? Collections.emptyList() : Arrays.asList(header.split("\\r?\\n"));
}
Expand All @@ -182,8 +182,8 @@ public List<String> getComment(PathString path, CommentType type) {
}

@Override
public void setComment(PathString path, List<String> value, CommentType type) {
if (path.isRoot()) {
public void setComment(CommentType type, List<String> value, String... path) {
if (path.length == 0) {
this.configuration.options()
.copyHeader(true)
.header(value == null || value.isEmpty() ? null : String.join(System.lineSeparator(), value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.io.IOException;
import java.util.*;

import static me.hsgamer.hscore.config.PathString.joinDefault;
import static me.hsgamer.hscore.config.PathString.splitDefault;

/**
* The BungeeCord Configuration
*/
Expand All @@ -38,32 +41,24 @@ public BungeeConfig(Plugin plugin, String filename) {
this(new File(plugin.getDataFolder(), filename));
}

private String toPath(PathString pathString) {
return PathString.toPath(".", pathString);
}

private Map<PathString, Object> toPathStringMap(Map<String, Object> map) {
return PathString.toPathStringMap(".", map);
}

@Override
public Configuration getOriginal() {
return this.configuration;
}

@Override
public Object get(PathString path, Object def) {
return this.configuration.get(toPath(path), def);
public Object get(Object def, String... path) {
return this.configuration.get(joinDefault(path), def);
}

@Override
public void set(PathString path, Object value) {
this.configuration.set(toPath(path), value);
public void set(Object value, String... path) {
this.configuration.set(joinDefault(path), value);
}

@Override
public boolean contains(PathString path) {
return this.configuration.contains(toPath(path));
public boolean contains(String... path) {
return this.configuration.contains(joinDefault(path));
}

@Override
Expand All @@ -72,13 +67,13 @@ public String getName() {
}

@Override
public Map<PathString, Object> getValues(PathString path, boolean deep) {
if (path.isRoot()) {
return toPathStringMap(this.getValues(configuration, deep));
public Map<String[], Object> getValues(boolean deep, String... path) {
if (path.length == 0) {
return splitDefault(this.getValues(configuration, deep));
} else {
return Optional.ofNullable(configuration.getSection(toPath(path)))
return Optional.ofNullable(configuration.getSection(joinDefault(path)))
.map(section -> this.getValues(section, deep))
.map(this::toPathStringMap)
.map(PathString::splitDefault)
.orElse(Collections.emptyMap());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import me.hsgamer.hscore.config.Config;
import me.hsgamer.hscore.config.DecorativeConfig;
import me.hsgamer.hscore.config.PathString;
import me.hsgamer.hscore.config.annotation.Comment;
import me.hsgamer.hscore.config.annotation.ConfigPath;
import me.hsgamer.hscore.config.annotation.converter.Converter;
Expand Down Expand Up @@ -42,6 +41,18 @@ public AnnotatedConfig(Config config) {
super(config);
}

private Field getField(String... path) {
return pathFieldMap.get(new PathString(path));
}

private void setField(Field field, String... path) {
pathFieldMap.put(new PathString(path), field);
}

private boolean containsField(String... path) {
return pathFieldMap.containsKey(new PathString(path));
}

@Override
public void setup() {
super.setup();
Expand All @@ -51,7 +62,7 @@ public void setup() {
.sorted(this::compareField)
.forEach(field -> {
ConfigPath configPath = field.getAnnotation(ConfigPath.class);
pathFieldMap.put(new PathString(configPath.value()), field);
setField(field, configPath.value());
validFields.add(field);
});
validFields.forEach(this::setupField);
Expand All @@ -60,10 +71,10 @@ public void setup() {
}

@Override
public void set(PathString path, Object value) {
Field field = pathFieldMap.get(path);
public void set(Object value, String... path) {
Field field = getField(path);
if (field == null) {
super.set(path, value);
super.set(value, path);
return;
}
checkAndSetField(field, value);
Expand All @@ -80,8 +91,8 @@ public void reload() {
}

private void setupClassComment() {
if (this.getClass().isAnnotationPresent(Comment.class) && this.getComment(PathString.ROOT).isEmpty()) {
this.setComment(PathString.ROOT, Arrays.asList(this.getClass().getAnnotation(Comment.class).value()));
if (this.getClass().isAnnotationPresent(Comment.class) && this.getComment().isEmpty()) {
this.setComment(Arrays.asList(this.getClass().getAnnotation(Comment.class).value()));
}
}

Expand Down Expand Up @@ -113,14 +124,14 @@ private int compareField(Field field1, Field field2) {

private void setupField(Field field) {
ConfigPath configPath = field.getAnnotation(ConfigPath.class);
PathString path = new PathString(configPath.value());
String[] path = configPath.value();
Converter converter = DefaultConverterManager.getConverterIfDefault(field.getGenericType(), configPath.converter());
Object defaultValue = this.getValue(field);

if (!contains(path)) {
super.set(path, converter.convertToRaw(defaultValue));
super.set(converter.convertToRaw(defaultValue), path);
if (field.isAnnotationPresent(Comment.class)) {
super.setComment(path, Arrays.asList(field.getAnnotation(Comment.class).value()));
super.setComment(Arrays.asList(field.getAnnotation(Comment.class).value()), path);
}
} else {
this.setValue(field, converter.convert(this.getNormalized(path)));
Expand All @@ -129,9 +140,9 @@ private void setupField(Field field) {

private void checkAndSetField(Field field, Object value) {
ConfigPath configPath = field.getAnnotation(ConfigPath.class);
PathString path = new PathString(configPath.value());
String[] path = configPath.value();
Converter converter = DefaultConverterManager.getConverterIfDefault(field.getGenericType(), configPath.converter());
super.set(path, converter.convertToRaw(value));
super.set(converter.convertToRaw(value), path);
this.setValue(field, value);
}

Expand Down Expand Up @@ -160,4 +171,25 @@ private Object getValue(Field field) {
}
return value;
}

private static class PathString {
private final String[] path;

private PathString(String[] path) {
this.path = path;
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
PathString that = (PathString) object;
return Objects.deepEquals(path, that.path);
}

@Override
public int hashCode() {
return Arrays.hashCode(path);
}
}
}

This file was deleted.

Loading

0 comments on commit c28aeee

Please sign in to comment.