-
-
Notifications
You must be signed in to change notification settings - Fork 72
Rework plugin config system #596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d4bdade
Rework plugin config system
DerEchtePilz 2c698f8
Use a loop instead of a Stream
DerEchtePilz 9ca2efe
We want stacktraces
DerEchtePilz 77029d2
Include generated config.yml in the documentation
DerEchtePilz 6bdc2fd
Address code review
DerEchtePilz 86b3f27
Use a LinkedHashMap for config options
DerEchtePilz 3140b23
First iteration of an extendable and testable config system
DerEchtePilz 78b1f7c
Update config system to be platform agnostic
DerEchtePilz a21e635
Clean up dependencies
DerEchtePilz a0bd0ef
Fix stuff
DerEchtePilz 90fac61
Annotations
DerEchtePilz 5b8886a
Fix Annotations
DerEchtePilz 460a466
Fix more stuff
DerEchtePilz 25b040e
Fix defaults in config.md
DerEchtePilz 7d2ef1e
Simplify a thing
DerEchtePilz beeca43
Fix VelocityConfigurationAdapter#getKeys()
DerEchtePilz 32b7eed
Don't update the Velocity config if not necessary
DerEchtePilz e1c35d7
Clean up Velocity plugin class
DerEchtePilz be6dfa3
Create static method to create a dummy instance
DerEchtePilz 0e0d2d5
Remove Velocity config file
DerEchtePilz 2698520
Introduce common modules to not include config classes in API modules
DerEchtePilz aa0d977
Remove unnecessary dependency
DerEchtePilz bff49cd
Move more common classes into the common module
DerEchtePilz 677d545
Inline some stuff
DerEchtePilz f66e16c
Address code review
DerEchtePilz 392d77f
More code review
DerEchtePilz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update config system to be platform agnostic
- Loading branch information
commit 78b1f7c29bdc6034cd165eacb629dabc17e27e9f
There are no files selected for viewing
This file contains hidden or 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 hidden or 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); | ||
DerEchtePilz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
DerEchtePilz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
8000 | } |
28 changes: 28 additions & 0 deletions
28
commandapi-core/src/main/java/dev/jorel/commandapi/config/ConfigurationAdapter.java
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.