-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update config system to be platform agnostic
- Loading branch information
1 parent
311f4ba
commit 667a533
Showing
13 changed files
with
457 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
commandapi-core/src/main/java/dev/jorel/commandapi/config/ConfigGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package dev.jorel.commandapi.config; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@ApiStatus.Internal | ||
public class ConfigGenerator { | ||
|
||
private final DefaultedConfig defaultedConfig; | ||
|
||
private ConfigGenerator(DefaultedConfig defaultedConfig) { | ||
this.defaultedConfig = defaultedConfig; | ||
} | ||
|
||
public static ConfigGenerator createNew(DefaultedConfig defaultedConfig) { | ||
return new ConfigGenerator(defaultedConfig); | ||
} | ||
|
||
public <T, C extends DefaultedConfig> void populateDefaultConfig(ConfigurationAdapter<T, C> adapter) { | ||
for (Map.Entry<String, CommentedConfigOption<?>> commentedConfigOption : defaultedConfig.getAllOptions().entrySet()) { | ||
adapter.setValue(commentedConfigOption.getKey(), commentedConfigOption.getValue().option()); | ||
adapter.setComment(commentedConfigOption.getKey(), commentedConfigOption.getValue().comment().toArray(new String[0])); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T, C extends DefaultedConfig> ConfigurationAdapter<T, C> generateWithNewValues(ConfigurationAdapter<T, C> existingConfig) { | ||
ConfigurationAdapter<T, C> updatedConfig = existingConfig.createNew(); | ||
|
||
boolean shouldRemoveValues = shouldRemoveOptions(existingConfig); | ||
|
||
boolean wasConfigUpdated = false; | ||
for (Map.Entry<String, CommentedConfigOption<?>> commentedConfigOption : defaultedConfig.getAllOptions().entrySet()) { | ||
String path = commentedConfigOption.getKey(); | ||
|
||
// Update config option | ||
if (existingConfig.contains(path)) { | ||
updatedConfig.tryCreateSection(path, (C) defaultedConfig); | ||
updatedConfig.setValue(path, existingConfig.getValue(path)); | ||
} else { | ||
wasConfigUpdated = true; | ||
updatedConfig.tryCreateSection(path, (C) defaultedConfig); | ||
updatedConfig.setValue(path, commentedConfigOption.getValue().option()); | ||
} | ||
|
||
// Update config option comment | ||
String[] defaultComment = commentedConfigOption.getValue().comment().toArray(new String[0]); | ||
String[] configComment = existingConfig.getComment(path); | ||
|
||
if (!Arrays.equals(defaultComment, configComment)) { | ||
wasConfigUpdated = true; | ||
} | ||
|
||
updatedConfig.setComment(path, commentedConfigOption.getValue().comment().toArray(new String[0])); | ||
} | ||
if (shouldRemoveValues) { | ||
wasConfigUpdated = true; | ||
} | ||
return (wasConfigUpdated) ? updatedConfig : null; | ||
} | ||
|
||
private <T, C extends DefaultedConfig> boolean shouldRemoveOptions(ConfigurationAdapter<T, C> config) { | ||
Set<String> configOptions = config.getKeys(); | ||
Set<String> defaultConfigOptions = defaultedConfig.getAllOptions().keySet(); | ||
|
||
boolean shouldRemoveOptions = false; | ||
for (String option : configOptions) { | ||
if (!defaultConfigOptions.contains(option)) { | ||
shouldRemoveOptions = true; | ||
break; | ||
} | ||
} | ||
return shouldRemoveOptions; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
commandapi-core/src/main/java/dev/jorel/commandapi/config/ConfigurationAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.jorel.commandapi.config; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.util.Set; | ||
|
||
@ApiStatus.Internal | ||
public interface ConfigurationAdapter<Configuration, DefaultConfiguration extends DefaultedConfig> { | ||
|
||
void setValue(String key, Object value); | ||
|
||
void setComment(String key, String[] comment); | ||
|
||
Object getValue(String key); | ||
|
||
String[] getComment(String key); | ||
|
||
Set<String> getKeys(); | ||
|
||
boolean contains(String key); | ||
|
||
void tryCreateSection(String key, DefaultConfiguration defaultConfiguration); | ||
|
||
Configuration config(); | ||
|
||
ConfigurationAdapter<Configuration, DefaultConfiguration> createNew(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...api-bukkit-core/src/main/java/dev/jorel/commandapi/config/BukkitConfigurationAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package dev.jorel.commandapi.config; | ||
|
||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
@ApiStatus.Internal | ||
public record BukkitConfigurationAdapter(YamlConfiguration config) implements ConfigurationAdapter<YamlConfiguration, DefaultedBukkitConfig> { | ||
|
||
@Override | ||
public void setValue(String key, Object value) { | ||
config.set(key, value); | ||
} | ||
|
||
@Override | ||
public void setComment(String key, String[] comment) { | ||
config.setComments(key, Arrays.asList(comment)); | ||
} | ||
|
||
@Override | ||
public Object getValue(String key) { | ||
return config.get(key); | ||
} | ||
|
||
@Override | ||
public String[] getComment(String key) { | ||
List<String> comments = config.getStringList(key); | ||
comments.removeIf(Objects::isNull); | ||
return comments.toArray(new String[0]); | ||
} | ||
|
||
@Override | ||
public Set<String> getKeys() { | ||
return config.getKeys(false); | ||
} | ||
|
||
@Override | ||
public boolean contains(String key) { | ||
return config.contains(key); | ||
} | ||
|
||
@Override | ||
public void tryCreateSection(String key, DefaultedBukkitConfig defaultedBukkitConfig) { | ||
if (!key.contains(".")) { | ||
return; | ||
} | ||
|
||
// Collect config keys | ||
Set<String> keys = getKeys(); | ||
keys.removeIf(k -> !config.isConfigurationSection(k)); | ||
|
||
// Collect sections | ||
String[] paths = key.split("\\."); | ||
List<String> sectionCandidates = new ArrayList<>(Arrays.asList(paths).subList(0, paths.length - 1)); | ||
|
||
// Create new sections | ||
ConfigurationSection root = null; | ||
StringBuilder pathSoFar = new StringBuilder(); | ||
for (String sectionCandidate : sectionCandidates) { | ||
if (pathSoFar.isEmpty()) { | ||
pathSoFar.append(sectionCandidate); | ||
} else { | ||
pathSoFar.append(".").append(sectionCandidate); | ||
} | ||
if (keys.contains(sectionCandidate) && root == null) { | ||
root = config.getConfigurationSection(sectionCandidate); | ||
} else if (root == null) { | ||
root = config.createSection(sectionCandidate); | ||
root.setComments(sectionCandidate, defaultedBukkitConfig.getAllSections().get(pathSoFar.toString()).comment()); | ||
} else { | ||
ConfigurationSection section = root.getConfigurationSection(sectionCandidate); | ||
if (section == null) { | ||
root = root.createSection(sectionCandidate); | ||
root.setComments(sectionCandidate, defaultedBukkitConfig.getAllSections().get(pathSoFar.toString()).comment()); | ||
} else { | ||
root = section; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ConfigurationAdapter<YamlConfiguration, DefaultedBukkitConfig> createNew() { | ||
return new BukkitConfigurationAdapter(new YamlConfiguration()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
BukkitConfigurationAdapter that = (BukkitConfigurationAdapter) o; | ||
String thisConfigString = config.saveToString(); | ||
String thatConfigString = that.config.saveToString(); | ||
return thisConfigString.equals(thatConfigString); | ||
} | ||
|
||
} |
Oops, something went wrong.