Skip to content

Commit

Permalink
Fix stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
DerEchtePilz committed Sep 22, 2024
1 parent 9e5409d commit 7ddf213
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package dev.jorel.commandapi.config;

import java.util.List;

record CommentedConfigOption<T>(List<String> comment, T option) {
record CommentedConfigOption<T>(String[] comment, T option) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jetbrains.annotations.ApiStatus;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

Expand All @@ -22,7 +23,7 @@ public static ConfigGenerator createNew(DefaultedConfig 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]));
adapter.setComment(commentedConfigOption.getKey(), commentedConfigOption.getValue().comment());
}
}

Expand All @@ -47,23 +48,25 @@ public <T, C extends DefaultedConfig> ConfigurationAdapter<T, C> generateWithNew
}

// Update config option comment
String[] defaultComment = commentedConfigOption.getValue().comment().toArray(new String[0]);
String[] defaultComment = commentedConfigOption.getValue().comment();
String[] configComment = existingConfig.getComment(path);

if (!Arrays.equals(defaultComment, configComment)) {
wasConfigUpdated = true;
}

updatedConfig.setComment(path, commentedConfigOption.getValue().comment().toArray(new String[0]));
updatedConfig.setComment(path, commentedConfigOption.getValue().comment());
}
if (shouldRemoveValues) {
wasConfigUpdated = true;
}
return (wasConfigUpdated) ? updatedConfig : null;
System.out.println(wasConfigUpdated);
return (wasConfigUpdated) ? updatedConfig.complete() : null;
}

private <T, C extends DefaultedConfig> boolean shouldRemoveOptions(ConfigurationAdapter<T, C> config) {
Set<String> configOptions = config.getKeys();
configOptions.forEach(System.out::println);
Set<String> defaultConfigOptions = defaultedConfig.getAllOptions().keySet();

boolean shouldRemoveOptions = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public interface ConfigurationAdapter<Configuration, DefaultConfiguration extend

void tryCreateSection(String key, DefaultConfiguration defaultConfiguration);

ConfigurationAdapter<Configuration, DefaultConfiguration> complete();

Configuration config();

ConfigurationAdapter<Configuration, DefaultConfiguration> createNew();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ public abstract class DefaultedConfig {
protected final Map<String, CommentedSection> allSections = new LinkedHashMap<>();

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

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

public static final CommentedConfigOption<String> MISSING_EXECUTOR_IMPLEMENTATION = new CommentedConfigOption<>(
List.of(
new String[]{
"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"
}, "This command has no implementations for %s"
);

public static final CommentedConfigOption<Boolean> CREATE_DISPATCHER_JSON = new CommentedConfigOption<>(
List.of(
new String[]{
"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
}, false
);

public static final CommentedSection SECTION_MESSAGE = new CommentedSection(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +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.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand All @@ -30,14 +32,16 @@ public Object getValue(String key) {

@Override
public String[] getComment(String key) {
List<String> comments = config.getStringList(key);
List<String> comments = new ArrayList<>(config.getComments(key));
comments.removeIf(Objects::isNull);
return comments.toArray(new String[0]);
}

@Override
public Set<String> getKeys() {
return config.getKeys(false);
Set<String> keys = new HashSet<>(config.getKeys(true));
keys.removeIf(config::isConfigurationSection);
return keys;
}

@Override
Expand All @@ -60,44 +64,53 @@ public void tryCreateSection(String key, DefaultedBukkitConfig defaultedBukkitCo
List<String> sectionCandidates = new ArrayList<>(Arrays.asList(paths).subList(0, paths.length - 1));

// Create new sections
ConfigurationSection root = null;
ConfigurationSection section = 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());

if (keys.contains(sectionCandidate) && section == null) {
section = config.getConfigurationSection(sectionCandidate);
} else if (section == null) {
section = config.createSection(sectionCandidate);
config.setComments(pathSoFar.toString(), 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());
ConfigurationSection currentSection = section.getConfigurationSection(sectionCandidate);
if (currentSection == null) {
section = section.createSection(sectionCandidate);
config.setComments(pathSoFar.toString(), defaultedBukkitConfig.getAllSections().get(pathSoFar.toString()).comment());
} else {
root = section;
section = currentSection;
}
}
}
}

@Override
public ConfigurationAdapter<YamlConfiguration, DefaultedBukkitConfig> createNew() {
return new BukkitConfigurationAdapter(new YamlConfiguration());
public ConfigurationAdapter<YamlConfiguration, DefaultedBukkitConfig> 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 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);
public ConfigurationAdapter<YamlConfiguration, DefaultedBukkitConfig> createNew() {
return new BukkitConfigurationAdapter(new YamlConfiguration());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,90 +15,92 @@
public class DefaultedBukkitConfig extends DefaultedConfig {

public static final CommentedConfigOption<Boolean> USE_LATEST_NMS_VERSION = new CommentedConfigOption<>(
List.of(
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
}, false
);

public static final CommentedConfigOption<Boolean> BE_LENIENT_FOR_MINOR_VERSIONS = new CommentedConfigOption<>(
List.of(
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
}, false
);

public static final CommentedConfigOption<Boolean> SHOULD_HOOK_PAPER_RELOAD = new CommentedConfigOption<>(
List.of(
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."
), false
}, false
);

public static final CommentedConfigOption<Boolean> SKIP_RELOAD_DATAPACKS = new CommentedConfigOption<>(
List.of(
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
}, false
);

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

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

public static final CommentedConfigOption<List<String>> SKIP_SENDER_PROXY = new CommentedConfigOption<>(
List.of(
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<>()
}, new ArrayList<>()
);

private DefaultedBukkitConfig() {
}

public static DefaultedBukkitConfig 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 DefaultedBukkitConfig.create(
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)
),
Map.ofEntries(
Map.entry("messages", SECTION_MESSAGE)
)
options,
sections
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jetbrains.annotations.ApiStatus;

import java.util.LinkedHashMap;
import java.util.Map;

@SuppressWarnings("ClassEscapesDefinedScope")
Expand All @@ -11,16 +12,18 @@ public class DefaultedVelocityConfig extends DefaultedConfig {
private DefaultedVelocityConfig() {}

public static DefaultedVelocityConfig 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);

Map<String, CommentedSection> sections = new LinkedHashMap<>();
sections.put("messages", SECTION_MESSAGE);

return DefaultedVelocityConfig.create(
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.ofEntries(
Map.entry("messages", SECTION_MESSAGE)
)
options,
sections
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public CommentedConfigurationNode config() {
return config;
}

@Override
public ConfigurationAdapter<ConfigurationNode, DefaultedVelocityConfig> complete() {
return this;
}

@Override
public ConfigurationAdapter<ConfigurationNode, DefaultedVelocityConfig> createNew() {
return new VelocityConfigurationAdapter(loader, loader.createNode());
Expand Down

0 comments on commit 7ddf213

Please sign in to comment.