diff --git a/commandapi-core/pom.xml b/commandapi-core/pom.xml index 379d3f261..25f7e2958 100644 --- a/commandapi-core/pom.xml +++ b/commandapi-core/pom.xml @@ -65,6 +65,12 @@ ${project.version} provided + + org.jetbrains + annotations + 24.1.0 + provided + diff --git a/commandapi-core/src/main/java/dev/jorel/commandapi/config/CommentedSection.java b/commandapi-core/src/main/java/dev/jorel/commandapi/config/CommentedSection.java new file mode 100644 index 000000000..88dae173f --- /dev/null +++ b/commandapi-core/src/main/java/dev/jorel/commandapi/config/CommentedSection.java @@ -0,0 +1,6 @@ +package dev.jorel.commandapi.config; + +import java.util.List; + +record CommentedSection(List comment) { +} diff --git a/commandapi-core/src/main/java/dev/jorel/commandapi/config/DefaultedConfig.java b/commandapi-core/src/main/java/dev/jorel/commandapi/config/DefaultedConfig.java new file mode 100644 index 000000000..a1c110523 --- /dev/null +++ b/commandapi-core/src/main/java/dev/jorel/commandapi/config/DefaultedConfig.java @@ -0,0 +1,49 @@ +package dev.jorel.commandapi.config; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +@SuppressWarnings("ClassEscapesDefinedScope") +@ApiStatus.Internal +public abstract class DefaultedConfig { + + final Map> allOptions = new LinkedHashMap<>(); + final Map allSections = new LinkedHashMap<>(); + + public static final CommentedConfigOption VERBOSE_OUTPUTS = new CommentedConfigOption<>( + List.of( + "Verbose outputs (default: false)", + "If \"true\", outputs command registration and unregistration logs in the console" + ), false + ); + + public static final CommentedConfigOption SILENT_LOGS = new CommentedConfigOption<>( + List.of( + "Silent logs (default: false)", + "If \"true\", turns off all logging from the CommandAPI, except for errors." + ), false + ); + + public static final CommentedConfigOption MISSING_EXECUTOR_IMPLEMENTATION = new CommentedConfigOption<>( + List.of( + "Missing executor implementation (default: \"This command has no implementations for %s\")", + "The message to display to senders when a command has no executor. Available", + "parameters are:", + " %s - the executor class (lowercase)", + " %S - the executor class (normal case)" + ), "This command has no implementations for %s" + ); + + public static final CommentedConfigOption CREATE_DISPATCHER_JSON = new CommentedConfigOption<>( + List.of( + "Create dispatcher JSON (default: false)", + "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." + ), false + ); + +} diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/config/ConfigGenerator.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/config/ConfigGenerator.java index 1843e390e..38e792575 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/config/ConfigGenerator.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/config/ConfigGenerator.java @@ -14,12 +14,28 @@ @ApiStatus.Internal public class ConfigGenerator { - private ConfigGenerator() {} + private final DefaultedBukkitConfig defaultedBukkitConfig; - public static YamlConfiguration generateDefaultConfig() throws InvalidConfigurationException { + private ConfigGenerator() { + this.defaultedBukkitConfig = DefaultedBukkitConfig.createDefault(); + } + + private ConfigGenerator(DefaultedBukkitConfig defaultedBukkitConfig) { + this.defaultedBukkitConfig = defaultedBukkitConfig; + } + + public static ConfigGenerator createNew() { + return new ConfigGenerator(); + } + + public static ConfigGenerator createNew(DefaultedBukkitConfig defaultedBukkitConfig) { + return new ConfigGenerator(defaultedBukkitConfig); + } + + public YamlConfiguration generateDefaultConfig() throws InvalidConfigurationException { YamlConfiguration config = new YamlConfiguration(); Set sections = new HashSet<>(); - for (Map.Entry> commentedConfigOption : DefaultedBukkitConfig.ALL_OPTIONS.entrySet()) { + for (Map.Entry> commentedConfigOption : defaultedBukkitConfig.getAllOptions().entrySet()) { String path = commentedConfigOption.getKey(); tryCreateSection(config, path, sections); @@ -30,14 +46,14 @@ public static YamlConfiguration generateDefaultConfig() throws InvalidConfigurat return process(config.saveToString()); } - public static YamlConfiguration generateWithNewValues(YamlConfiguration existingConfig) throws InvalidConfigurationException { + public YamlConfiguration generateWithNewValues(YamlConfiguration existingConfig) throws InvalidConfigurationException { YamlConfiguration config = new YamlConfiguration(); boolean shouldRemoveValues = shouldRemoveOptions(existingConfig); boolean wasConfigUpdated = false; Set sections = new HashSet<>(); - for (Map.Entry> commentedConfigOption : DefaultedBukkitConfig.ALL_OPTIONS.entrySet()) { + for (Map.Entry> commentedConfigOption : defaultedBukkitConfig.getAllOptions().entrySet()) { String path = commentedConfigOption.getKey(); // Update config option @@ -70,7 +86,7 @@ public static YamlConfiguration generateWithNewValues(YamlConfiguration existing return (wasConfigUpdated) ? process(config.saveToString()) : null; } - private static YamlConfiguration process(String configAsString) throws InvalidConfigurationException { + private YamlConfiguration process(String configAsString) throws InvalidConfigurationException { String[] configStrings = configAsString.split("\n"); StringBuilder configBuilder = new StringBuilder(); for (String configString : configStrings) { @@ -84,7 +100,7 @@ private static YamlConfiguration process(String configAsString) throws InvalidCo return config; } - private static void tryCreateSection(YamlConfiguration config, String path, Set existingSections) { + private void tryCreateSection(YamlConfiguration config, String path, Set existingSections) { if (path.contains(".")) { // We have to create a section, or multiple if applicable, first, if it doesn't exist already String[] sectionNames = path.split("\\."); @@ -93,18 +109,21 @@ private static void tryCreateSection(YamlConfiguration config, String path, Set< String sectionName = sectionNames[i]; if (!existingSections.contains(sectionName)) { config.createSection(sectionName); - config.setComments(sectionName, DefaultedBukkitConfig.ALL_SECTIONS.get(sectionName).comment()); + List comment = defaultedBukkitConfig.getAllSections().get(sectionName).comment(); + if (comment != null) { + config.setComments(sectionName, comment); + } } existingSections.add(sectionName); } } } - private static boolean shouldRemoveOptions(YamlConfiguration config) { + private boolean shouldRemoveOptions(YamlConfiguration config) { Set configOptions = config.getKeys(true); configOptions.removeIf(config::isConfigurationSection); - Set defaultConfigOptions = DefaultedBukkitConfig.ALL_OPTIONS.keySet(); + Set defaultConfigOptions = defaultedBukkitConfig.getAllOptions().keySet(); boolean shouldRemoveOptions = false; for (String option : configOptions) { diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/config/DefaultedBukkitConfig.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/config/DefaultedBukkitConfig.java index e3a413392..847873168 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/config/DefaultedBukkitConfig.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/config/DefaultedBukkitConfig.java @@ -12,40 +12,7 @@ */ @SuppressWarnings("ClassEscapesDefinedScope") @ApiStatus.Internal -public class DefaultedBukkitConfig { - - public static final CommentedConfigOption VERBOSE_OUTPUTS = new CommentedConfigOption<>( - List.of( - "Verbose outputs (default: false)", - "If \"true\", outputs command registration and unregistration logs in the console" - ), false - ); - - public static final CommentedConfigOption SILENT_LOGS = new CommentedConfigOption<>( - List.of( - "Silent logs (default: false)", - "If \"true\", turns off all logging from the CommandAPI, except for errors." - ), false - ); - - public static final CommentedConfigOption MISSING_EXECUTOR_IMPLEMENTATION = new CommentedConfigOption<>( - List.of( - "Missing executor implementation (default: \"This command has no implementations for %s\")", - "The message to display to senders when a command has no executor. Available", - "parameters are:", - " %s - the executor class (lowercase)", - " %S - the executor class (normal case)" - ), "This command has no implementations for %s" - ); - - public static final CommentedConfigOption CREATE_DISPATCHER_JSON = new CommentedConfigOption<>( - List.of( - "Create dispatcher JSON (default: false)", - "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." - ), false - ); +public class DefaultedBukkitConfig extends DefaultedConfig { public static final CommentedConfigOption USE_LATEST_NMS_VERSION = new CommentedConfigOption<>( List.of( @@ -111,30 +78,48 @@ public class DefaultedBukkitConfig { ), new ArrayList<>() ); - public static final Map> ALL_OPTIONS = new LinkedHashMap<>(); - public static final Map> ALL_SECTIONS = new LinkedHashMap<>(); - - public static final CommentedConfigOption SECTION_MESSAGE = new CommentedConfigOption<>( + public static final CommentedSection SECTION_MESSAGE = new CommentedSection( List.of( "Messages", "Controls messages that the CommandAPI displays to players" - ), null + ) ); - static { - ALL_OPTIONS.put("verbose-outputs", VERBOSE_OUTPUTS); - ALL_OPTIONS.put("silent-logs", SILENT_LOGS); - ALL_OPTIONS.put("messages.missing-executor-implementation", MISSING_EXECUTOR_IMPLEMENTATION); - ALL_OPTIONS.put("create-dispatcher-json", CREATE_DISPATCHER_JSON); - ALL_OPTIONS.put("use-latest-nms-version", USE_LATEST_NMS_VERSION); - ALL_OPTIONS.put("be-lenient-for-minor-versions", BE_LENIENT_FOR_MINOR_VERSIONS); - ALL_OPTIONS.put("hook-paper-reload", SHOULD_HOOK_PAPER_RELOAD); - ALL_OPTIONS.put("skip-initial-datapack-reload", SKIP_RELOAD_DATAPACKS); - ALL_OPTIONS.put("plugins-to-convert", PLUGINS_TO_CONVERT); - ALL_OPTIONS.put("other-commands-to-convert", OTHER_COMMANDS_TO_CONVERT); - ALL_OPTIONS.put("skip-sender-proxy", SKIP_SENDER_PROXY); - - ALL_SECTIONS.put("messages", SECTION_MESSAGE); + private DefaultedBukkitConfig() {} + + public static DefaultedBukkitConfig createDefault() { + DefaultedBukkitConfig config = new DefaultedBukkitConfig(); + config.allOptions.put("verbose-outputs", VERBOSE_OUTPUTS); + config.allOptions.put("silent-logs", SILENT_LOGS); + config.allOptions.put("messages.missing-executor-implementation", MISSING_EXECUTOR_IMPLEMENTATION); + config.allOptions.put("create-dispatcher-json", CREATE_DISPATCHER_JSON); + config.allOptions.put("use-latest-nms-version", USE_LATEST_NMS_VERSION); + config.allOptions.put("be-lenient-for-minor-versions", BE_LENIENT_FOR_MINOR_VERSIONS); + config.allOptions.put("hook-paper-reload", SHOULD_HOOK_PAPER_RELOAD); + config.allOptions.put("skip-initial-datapack-reload", SKIP_RELOAD_DATAPACKS); + config.allOptions.put("plugins-to-convert", PLUGINS_TO_CONVERT); + config.allOptions.put("other-commands-to-convert", OTHER_COMMANDS_TO_CONVERT); + config.allOptions.put("skip-sender-proxy", SKIP_SENDER_PROXY); + + config.allSections.put("messages", SECTION_MESSAGE); + + return config; } + public static DefaultedBukkitConfig create(Map> options, Map sections) { + DefaultedBukkitConfig config = new DefaultedBukkitConfig(); + + config.allOptions.putAll(options); + config.allSections.putAll(sections); + + return config; + } + + public Map> getAllOptions() { + return allOptions; + } + + public Map getAllSections() { + return allSections; + } } diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin-mojang-mapped/src/main/java/dev/jorel/commandapi/CommandAPIMain.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin-mojang-mapped/src/main/java/dev/jorel/commandapi/CommandAPIMain.java index 4a4869187..81d28e47b 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin-mojang-mapped/src/main/java/dev/jorel/commandapi/CommandAPIMain.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin-mojang-mapped/src/main/java/dev/jorel/commandapi/CommandAPIMain.java @@ -148,10 +148,11 @@ public void onEnable() { @Override public void saveDefaultConfig() { File configFile = new File(getDataFolder(), "config.yml"); + ConfigGenerator configGenerator = ConfigGenerator.createNew(); if (!getDataFolder().exists()) { getDataFolder().mkdir(); try { - YamlConfiguration defaultConfig = ConfigGenerator.generateDefaultConfig(); + YamlConfiguration defaultConfig = configGenerator.generateDefaultConfig(); defaultConfig.save(configFile); } catch (Exception e) { getLogger().severe("Could not create default config file! This is (probably) a bug."); @@ -166,7 +167,7 @@ public void saveDefaultConfig() { // Update the config if necessary YamlConfiguration existingConfig = YamlConfiguration.loadConfiguration(configFile); try { - YamlConfiguration updatedConfig = ConfigGenerator.generateWithNewValues(existingConfig); + YamlConfiguration updatedConfig = configGenerator.generateWithNewValues(existingConfig); if (updatedConfig == null) { return; } diff --git a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin/src/main/java/dev/jorel/commandapi/CommandAPIMain.java b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin/src/main/java/dev/jorel/commandapi/CommandAPIMain.java index 0f96af1e8..059828922 100644 --- a/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin/src/main/java/dev/jorel/commandapi/CommandAPIMain.java +++ b/commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin/src/main/java/dev/jorel/commandapi/CommandAPIMain.java @@ -147,10 +147,11 @@ public void onEnable() { @Override public void saveDefaultConfig() { File configFile = new File(getDataFolder(), "config.yml"); + ConfigGenerator configGenerator = ConfigGenerator.createNew(); if (!getDataFolder().exists()) { getDataFolder().mkdir(); try { - YamlConfiguration defaultConfig = ConfigGenerator.generateDefaultConfig(); + YamlConfiguration defaultConfig = configGenerator.generateDefaultConfig(); defaultConfig.save(configFile); } catch (Exception e) { getLogger().severe("Could not create default config file! This is (probably) a bug."); @@ -165,7 +166,7 @@ public void saveDefaultConfig() { // Update the config if necessary YamlConfiguration existingConfig = YamlConfiguration.loadConfiguration(configFile); try { - YamlConfiguration updatedConfig = ConfigGenerator.generateWithNewValues(existingConfig); + YamlConfiguration updatedConfig = configGenerator.generateWithNewValues(existingConfig); if (updatedConfig == null) { return; }