-
-
Notifications
You must be signed in to change notification settings - Fork 71
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 all commits
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
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin-common/pom.xml
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,80 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>dev.jorel</groupId> | ||
<artifactId>commandapi-bukkit</artifactId> | ||
<version>9.6.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>commandapi-bukkit-plugin-common</artifactId> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<compilerArgs> | ||
<arg>-Xlint</arg> | ||
</compilerArgs> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<executions> | ||
<!-- to avoid bugs in some situations --> | ||
<execution> | ||
<id>default-prepare-agent</id> | ||
<goals> | ||
<goal>prepare-agent</goal> | ||
</goals> | ||
</execution> | ||
<!-- create report during maven verify phase --> | ||
<execution> | ||
<id>report</id> | ||
<phase>verify</phase> | ||
<goals> | ||
<goal>report</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
< 8000 /td> | <repositories> | |
<repository> | ||
<id>papermc-repo</id> | ||
<url>https://repo.papermc.io/repository/maven-public/</url> | ||
</repository> | ||
<repository> | ||
<id>sonatype</id> | ||
<url>https://oss.sonatype.org/content/groups/public/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.papermc.paper</groupId> | ||
<artifactId>paper-api</artifactId> | ||
<version>1.21.1-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>dev.jorel</groupId> | ||
<artifactId>commandapi-plugin</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>dev.jorel</groupId> | ||
<artifactId>commandapi-bukkit-core</artifactId> | ||
<version>${project.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
File renamed without changes.
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
153 changes: 153 additions & 0 deletions
153
...t-plugin-common/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,153 @@ | ||
package dev.jorel.commandapi.config; | ||
|
||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.configuration.InvalidConfigurationException; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
|
||
public record BukkitConfigurationAdapter(YamlConfiguration config) implements ConfigurationAdapter<YamlConfiguration> { | ||
|
||
public static BukkitConfigurationAdapter createDummyInstance() { | ||
return new BukkitConfigurationAdapter(null); | ||
} | ||
|
||
@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 = new ArrayList<>(config.getComments(key)); | ||
comments.removeIf(Objects::isNull); | ||
return comments.toArray(new String[0]); | ||
} | ||
|
||
@Override | ||
public Set<String> getKeys() { | ||
Set<String> keys = new HashSet<>(config.getKeys(true)); | ||
keys.removeIf(config::isConfigurationSection); | ||
return keys; | ||
} | ||
|
||
@Override | ||
public boolean contains(String key) { | ||
return config.contains(key); | ||
} | ||
|
||
@Override | ||
public void tryCreateSection(String key) { | ||
if (!key.contains(".")) { | ||
return; | ||
} | ||
|
||
// Collect config keys | ||
Set<String> keys = config.getKeys(true); | ||
keys.removeIf(k -> !config.isConfigurationSection(k)); | ||
|
||
// Collect sections | ||
String[] sectionCandidates = key.split("\\."); | ||
sectionCandidates = Arrays.copyOf(sectionCandidates, sectionCandidates.length - 1); | ||
|
||
// Create new sections | ||
ConfigurationSection section = null; | ||
for (String sectionCandidate : sectionCandidates) { | ||
DerEchtePilz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (keys.contains(sectionCandidate) && section == null) { | ||
section = config.getConfigurationSection(sectionCandidate); | ||
} else if (section == null) { | ||
section = config.createSection(sectionCandidate); | ||
} else { | ||
ConfigurationSection currentSection = section.getConfigurationSection(sectionCandidate); | ||
if (currentSection == null) { | ||
section = section.createSection(sectionCandidate); | ||
} else { | ||
section = currentSection; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ConfigurationAdapter<YamlConfiguration> complete() { | ||
String[] configStrings = config.saveToString().split("\n"); | ||
StringBuilder configBuilder = new StringBuilder(); | ||
for (String configString : configStrings) { | ||
configBuilder.append(configString).append("\n"); | ||
if (!configString.contains("#")) { | ||
configBuilder.append("\n"); | ||
} | ||
} | ||
try { | ||
config.loadFromString(configBuilder.toString()); | ||
} catch (InvalidConfigurationException e) { | ||
e.printStackTrace(System.err); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public ConfigurationAdapter<YamlConfiguration> createNew() { | ||
return new BukkitConfigurationAdapter(new YamlConfiguration()); | ||
} | ||
|
||
@Override | ||
public void saveDefaultConfig(File directory, File configFile, Logger logger) { | ||
DerEchtePilz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ConfigGenerator configGenerator = ConfigGenerator.createNew(DefaultBukkitConfig.createDefault()); | ||
if (!directory.exists()) { | ||
boolean createdDirectory = directory.mkdirs(); | ||
if (!createdDirectory) { | ||
logger.severe("Failed to create directory for the CommandAPI's config.yml file!"); | ||
} | ||
try { | ||
ConfigurationAdapter<YamlConfiguration> bukkitConfigurationAdapter = new BukkitConfigurationAdapter(new YamlConfiguration()); | ||
configGenerator.populateDefaultConfig(bukkitConfigurationAdapter); | ||
bukkitConfigurationAdapter.config().save(configFile); | ||
} catch (IOException e) { | ||
logger.severe("Could not create default config file! This is (probably) a bug."); | ||
logger.severe("Error message: " + e.getMessage()); | ||
logger.severe("Stacktrace:"); | ||
for (StackTraceElement element : e.getStackTrace()) { | ||
< 10000 span class='blob-code-inner blob-code-marker ' data-code-marker="+"> logger.severe(element.toString()); | ||
} | ||
} | ||
return; | ||
} | ||
// Update the config if necessary | ||
try { | ||
YamlConfiguration existingYamlConfig = YamlConfiguration.loadConfiguration(configFile); | ||
ConfigurationAdapter<YamlConfiguration> existingConfig = new BukkitConfigurationAdapter(existingYamlConfig); | ||
ConfigurationAdapter<YamlConfiguration> updatedConfig = configGenerator.generateWithNewValues(existingConfig); | ||
if (updatedConfig == null) { | ||
return; | ||
} | ||
updatedConfig.config().save(configFile); | ||
} catch (IOException e) { | ||
logger.severe("Could not update config! This is (probably) a bug."); | ||
logger.severe("Error message: " + e.getMessage()); | ||
logger.severe("Stacktrace:"); | ||
for (StackTraceElement element : e.getStackTrace()) { | ||
logger.severe(element.toString()); | ||
} | ||
} | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
...i-bukkit-plugin-common/src/main/java/dev/jorel/commandapi/config/DefaultBukkitConfig.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,105 @@ | ||
package dev.jorel.commandapi.config; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DefaultBukkitConfig extends DefaultConfig { | ||
|
||
public static final CommentedConfigOption<Boolean> USE_LATEST_NMS_VERSION = new CommentedConfigOption<>( | ||
new String[] { | ||
"Use latest version (default: false)", | ||
"If \"true\", the CommandAPI will use the latest available NMS implementation", | ||
"when the CommandAPI is used. This avoids all checks to see if the latest NMS", | ||
"implementation is actually compatible with the current Minecraft version." | ||
}, false | ||
); | ||
|
||
public static final CommentedConfigOption<Boolean> BE_LENIENT_FOR_MINOR_VERSIONS = new CommentedConfigOption<>( | ||
new String[] { | ||
"Be lenient with version checks when loading for new minor Minecraft versions (default: false)", | ||
"If \"true\", the CommandAPI loads NMS implementations for potentially unsupported Minecraft versions.", | ||
"For example, this setting may allow updating from 1.21.1 to 1.21.2 as only the minor version is changing", | ||
"but will not allow an update from 1.21.2 to 1.22.", | ||
"Keep in mind that implementations may vary and actually updating the CommandAPI might be necessary." | ||
}, false | ||
); | ||
|
||
public static final CommentedConfigOption<Boolean> SHOULD_HOOK_PAPER_RELOAD = new CommentedConfigOption<>( | ||
new String[] { | ||
"Hook into Paper's ServerResourcesReloadedEvent (default: true)", | ||
"If \"true\", and the CommandAPI detects it is running on a Paper server, it will", | ||
"hook into Paper's ServerResourcesReloadedEvent to detect when /minecraft:reload is run.", | ||
"This allows the CommandAPI to automatically call its custom datapack-reloading", | ||
"function which allows CommandAPI commands to be used in datapacks.", | ||
"If you set this to false, CommandAPI commands may not work inside datapacks after", | ||
"reloading datapacks." | ||
}, true | ||
); | ||
|
||
public static final CommentedConfigOption<Boolean> SKIP_RELOAD_DATAPACKS = new CommentedConfigOption<>( | ||
new String[] { | ||
"Skips the initial datapack reload when the server loads (default: false)", | ||
"If \"true\", the CommandAPI will not reload datapacks when the server has finished", | ||
"loading. Datapacks will still be reloaded if performed manually when \"hook-paper-reload\"", | ||
"is set to \"true\" and /minecraft:reload is run." | ||
}, false | ||
); | ||
|
||
public static final CommentedConfigOption<List<?>> PLUGINS_TO_CONVERT = new CommentedConfigOption<>( | ||
new String[] { | ||
"Plugins to convert (default: [])", | ||
"Controls the list of plugins to process for command conversion." | ||
}, new ArrayList<>() | ||
); | ||
|
||
public static final CommentedConfigOption<List<String>> OTHER_COMMANDS_TO_CONVERT = new CommentedConfigOption<>( | ||
new String[] { | ||
"Other commands to convert (default: [])", | ||
"A list of other commands to convert. This should be used for commands which", | ||
"are not declared in a plugin.yml file." | ||
}, new ArrayList<>() | ||
); | ||
|
||
public static final CommentedConfigOption<List<String>> SKIP_SENDER_PROXY = new CommentedConfigOption<>( | ||
new String[] { | ||
"Skip sender proxy (default: [])", | ||
"Determines whether the proxy sender should be skipped when converting a", | ||
"command. If you are having issues with plugin command conversion, add the", | ||
"plugin to this list." | ||
}, new ArrayList<>() | ||
); | ||
|
||
private DefaultBukkitConfig() { | ||
} | ||
|
||
public static DefaultBukkitConfig createDefault() { | ||
Map<String, CommentedConfigOption<?>> options = new LinkedHashMap<>(); | ||
options.put("verbose-outputs", VERBOSE_OUTPUTS); | ||
options.put("silent-logs", SILENT_LOGS); | ||
options.put("messages.missing-executor-implementation", MISSING_EXECUTOR_IMPLEMENTATION); | ||
options.put("create-dispatcher-json", CREATE_DISPATCHER_JSON); | ||
options.put("use-latest-nms-version", USE_LATEST_NMS_VERSION); | ||
options.put("be-lenient-for-minor-versions", BE_LENIENT_FOR_MINOR_VERSIONS); | ||
options.put("hook-paper-reload", SHOULD_HOOK_PAPER_RELOAD); | ||
options.put("skip-initial-datapack-reload", SKIP_RELOAD_DATAPACKS); | ||
options.put("plugins-to-convert", PLUGINS_TO_CONVERT); | ||
options.put("other-commands-to-convert", OTHER_COMMANDS_TO_CONVERT); | ||
options.put("skip-sender-proxy", SKIP_SENDER_PROXY); | ||
|
||
Map<String, CommentedSection> sections = new LinkedHashMap<>(); | ||
sections.put("messages", SECTION_MESSAGE); | ||
|
||
return DefaultBukkitConfig.create(options, sections); | ||
} | ||
|
||
public static DefaultBukkitConfig create(Map<String, CommentedConfigOption<?>> options, Map<String, CommentedSection> sections) { | ||
DefaultBukkitConfig config = new DefaultBukkitConfig(); | ||
|
||
config.allOptions.putAll(options); | ||
config.allSections.putAll(sections); | ||
|
||
return config; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.
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.