Skip to content

Commit

Permalink
Address code review
Browse files Browse the repository at this point in the history
This could also help with further rewriting to aid development for multiple platforms.
  • Loading branch information
DerEchtePilz committed Sep 5, 2024
1 parent e3c96df commit 7a2c6bd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import java.util.List;

record CommentedConfigOption<T>(List<String> comment, String path, T option) {
record CommentedConfigOption<T>(List<String> comment, T option) {
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package dev.jorel.commandapi.config;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

Expand All @@ -19,13 +19,13 @@ private ConfigGenerator() {}
public static YamlConfiguration generateDefaultConfig() throws InvalidConfigurationException {
YamlConfiguration config = new YamlConfiguration();
Set<String> sections = new HashSet<>();
for (CommentedConfigOption<?> commentedConfigOption : DefaultedBukkitConfig.ALL_OPTIONS) {
String path = commentedConfigOption.path();
for (Map.Entry<String, CommentedConfigOption<?>> commentedConfigOption : DefaultedBukkitConfig.ALL_OPTIONS.entrySet()) {
String path = commentedConfigOption.getKey();

tryCreateSection(config, path, sections);

config.set(path, commentedConfigOption.option());
config.setComments(path, commentedConfigOption.comment());
config.set(path, commentedConfigOption.getValue().option());
config.setComments(path, commentedConfigOption.getValue().comment());
}
return process(config.saveToString());
}
Expand All @@ -37,8 +37,8 @@ public static YamlConfiguration generateWithNewValues(YamlConfiguration existing

boolean wasConfigUpdated = false;
Set<String> sections = new HashSet<>();
for (CommentedConfigOption<?> commentedConfigOption : DefaultedBukkitConfig.ALL_OPTIONS) {
String path = commentedConfigOption.path();
for (Map.Entry<String, CommentedConfigOption<?>> commentedConfigOption : DefaultedBukkitConfig.ALL_OPTIONS.entrySet()) {
String path = commentedConfigOption.getKey();

// Update config option
if (existingConfig.contains(path)) {
Expand All @@ -47,7 +47,7 @@ public static YamlConfiguration generateWithNewValues(YamlConfiguration existing
} else {
wasConfigUpdated = true;
tryCreateSection(config, path, sections);
config.set(path, commentedConfigOption.option());
config.set(path, commentedConfigOption.getValue().option());
}

// Update config option comments
Expand All @@ -57,12 +57,12 @@ public static YamlConfiguration generateWithNewValues(YamlConfiguration existing
// As a result, we wrap them in new ArrayLists first to be able to compare them
List<String> existingComment = new ArrayList<>(existingConfig.getComments(path));
existingComment.removeIf(Objects::isNull);
List<String> defaultComment = new ArrayList<>(commentedConfigOption.comment());
List<String> defaultComment = new ArrayList<>(commentedConfigOption.getValue().comment());

if (!existingComment.equals(defaultComment)) {
wasConfigUpdated = true;
}
config.setComments(path, commentedConfigOption.comment());
config.setComments(path, commentedConfigOption.getValue().comment());
}
if (shouldRemoveValues) {
wasConfigUpdated = true;
Expand Down Expand Up @@ -93,11 +93,7 @@ private static void tryCreateSection(YamlConfiguration config, String path, Set<
String sectionName = sectionNames[i];
if (!existingSections.contains(sectionName)) {
config.createSection(sectionName);
for (CommentedConfigOption<?> commentedSection : DefaultedBukkitConfig.ALL_SECTIONS) {
if (commentedSection.path().equals(sectionName)) {
config.setComments(sectionName, commentedSection.comment());
}
}
config.setComments(sectionName, DefaultedBukkitConfig.ALL_SECTIONS.get(sectionName).comment());
}
existingSections.add(sectionName);
}
Expand All @@ -106,27 +102,18 @@ private static void tryCreateSection(YamlConfiguration config, String path, Set<

private static boolean shouldRemoveOptions(YamlConfiguration config) {
Set<String> configOptions = config.getKeys(true);
List<String> sections = new ArrayList<>();
for (String configOption : configOptions) {
ConfigurationSection section = config.getConfigurationSection(configOption);
if (section != null) {
sections.add(configOption);
}
}
for (String sectionName : sections) {
configOptions.remove(sectionName);
}
Set<String> defaultConfigOptions = new HashSet<>();
for (CommentedConfigOption<?> defaultConfigOption : DefaultedBukkitConfig.ALL_OPTIONS) {
defaultConfigOptions.add(defaultConfigOption.path());
}
List<String> optionsToRemove = new ArrayList<>();
configOptions.removeIf(config::isConfigurationSection);

Set<String> defaultConfigOptions = DefaultedBukkitConfig.ALL_OPTIONS.keySet();

boolean shouldRemoveOptions = false;
for (String option : configOptions) {
if (!defaultConfigOptions.contains(option)) {
optionsToRemove.add(option);
shouldRemoveOptions = true;
break;
}
}
return !optionsToRemove.isEmpty();
return shouldRemoveOptions;
}

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package dev.jorel.commandapi.config;

import org.jetbrains.annotations.ApiStatus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* Default config values for the plugin's config.yml file
*/
class DefaultedBukkitConfig {
@SuppressWarnings("ClassEscapesDefinedScope")
@ApiStatus.Internal
public class DefaultedBukkitConfig {

public static final CommentedConfigOption<Boolean> VERBOSE_OUTPUTS = new CommentedConfigOption<>(
List.of(
"Verbose outputs (default: false)",
"If \"true\", outputs command registration and unregistration logs in the console"
),
"verbose-outputs", false
), false
);

public static final CommentedConfigOption<Boolean> SILENT_LOGS = new CommentedConfigOption<>(
List.of(
"Silent logs (default: false)",
"If \"true\", turns off all logging from the CommandAPI, except for errors."
),
"silent-logs", false
), false
);

public static final CommentedConfigOption<String> MISSING_EXECUTOR_IMPLEMENTATION = new CommentedConfigOption<>(
Expand All @@ -31,8 +34,7 @@ class DefaultedBukkitConfig {
"parameters are:",
" %s - the executor class (lowercase)",
" %S - the executor class (normal case)"
),
"messages.missing-executor-implementation", "This command has no implementations for %s"
), "This command has no implementations for %s"
);

public static final CommentedConfigOption<Boolean> CREATE_DISPATCHER_JSON = new CommentedConfigOption<>(
Expand All @@ -41,8 +43,7 @@ class DefaultedBukkitConfig {
"If \"true\", the CommandAPI creates a command_registration.json file showing the",
"mapping of registered commands. This is designed to be used by developers -",
"setting this to \"false\" will improve command registration performance."
),
"create-dispatcher-json", false
), false
);

public static final CommentedConfigOption<Boolean> USE_LATEST_NMS_VERSION = new CommentedConfigOption<>(
Expand All @@ -51,8 +52,7 @@ class DefaultedBukkitConfig {
"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."
),
"use-latest-nms-version", false
), false
);

public static final CommentedConfigOption<Boolean> BE_LENIENT_FOR_MINOR_VERSIONS = new CommentedConfigOption<>(
Expand All @@ -62,8 +62,7 @@ class DefaultedBukkitConfig {
"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."
),
"be-lenient-for-minor-versions", false
), false
);

public static final CommentedConfigOption<Boolean> SHOULD_HOOK_PAPER_RELOAD = new CommentedConfigOption<>(
Expand All @@ -75,8 +74,7 @@ class DefaultedBukkitConfig {
"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."
),
"hook-paper-reload", false
), false
);

public static final CommentedConfigOption<Boolean> SKIP_RELOAD_DATAPACKS = new CommentedConfigOption<>(
Expand All @@ -85,25 +83,22 @@ class DefaultedBukkitConfig {
"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."
),
"skip-initial-datapack-reload", false
), false
);

public static final CommentedConfigOption<List<?>> PLUGINS_TO_CONVERT = new CommentedConfigOption<>(
List.of(
"Plugins to convert (default: [])",
"Controls the list of plugins to process for command conversion."
),
"plugins-to-convert", new ArrayList<>()
), new ArrayList<>()
);

public static final CommentedConfigOption<List<String>> OTHER_COMMANDS_TO_CONVERT = new CommentedConfigOption<>(
List.of(
"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."
),
"other-commands-to-convert", new ArrayList<>()
), new ArrayList<>()
);

public static final CommentedConfigOption<List<String>> SKIP_SENDER_PROXY = new CommentedConfigOption<>(
Expand All @@ -112,34 +107,32 @@ class DefaultedBukkitConfig {
"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."
),
"skip-sender-proxy", new ArrayList<>()
), new ArrayList<>()
);

public static final List<CommentedConfigOption<?>> ALL_OPTIONS = List.of(
VERBOSE_OUTPUTS,
SILENT_LOGS,
MISSING_EXECUTOR_IMPLEMENTATION,
CREATE_DISPATCHER_JSON,
USE_LATEST_NMS_VERSION,
BE_LENIENT_FOR_MINOR_VERSIONS,
SHOULD_HOOK_PAPER_RELOAD,
SKIP_RELOAD_DATAPACKS,
PLUGINS_TO_CONVERT,
OTHER_COMMANDS_TO_CONVERT,
SKIP_SENDER_PROXY
public static final Map<String, CommentedConfigOption<?>> ALL_OPTIONS = Map.ofEntries(
Map.entry("verbose-outputs", VERBOSE_OUTPUTS),
Map.entry("silent-logs", SILENT_LOGS),
Map.entry("messages.missing-executor-implementation", MISSING_EXECUTOR_IMPLEMENTATION),
Map.entry("create-dispatcher-json", CREATE_DISPATCHER_JSON),
Map.entry("use-latest-nms-version", USE_LATEST_NMS_VERSION),
Map.entry("be-lenient-for-minor-versions", BE_LENIENT_FOR_MINOR_VERSIONS),
Map.entry("hook-paper-reload", SHOULD_HOOK_PAPER_RELOAD),
Map.entry("skip-initial-datapack-reload", SKIP_RELOAD_DATAPACKS),
Map.entry("plugins-to-convert", PLUGINS_TO_CONVERT),
Map.entry("other-commands-to-convert", OTHER_COMMANDS_TO_CONVERT),
Map.entry("skip-sender-proxy", SKIP_SENDER_PROXY)
);

public static final CommentedConfigOption<?> SECTION_MESSAGE = new CommentedConfigOption<>(
List.of(
"Messages",
"Controls messages that the CommandAPI displays to players"
),
"messages", null
), null
);

public static final List<CommentedConfigOption<?>> ALL_SECTIONS = List.of(
SECTION_MESSAGE
public static final Map<String, CommentedConfigOption<?>> ALL_SECTIONS = Map.of(
"messages", SECTION_MESSAGE
);

}

0 comments on commit 7a2c6bd

Please sign in to comment.