From 6f4774428eb91ba1bf4ffb6c1ff1083d94c56e2d Mon Sep 17 00:00:00 2001 From: Riley Park Date: Tue, 21 Dec 2021 14:28:19 -0800 Subject: [PATCH] feat(api): Internal configuration --- .../adventure/text/TextComponentImpl.java | 3 +- .../translation/TranslationLocales.java | 3 +- .../net/kyori/adventure/util/Services.java | 3 +- .../util/internal/AdventureConfig.java | 108 ++++++++++++++++++ .../util/internal/AdventureConfigImpl.java | 52 +++++++++ .../adventure/util/internal/package-info.java | 28 +++++ 6 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/net/kyori/adventure/util/internal/AdventureConfig.java create mode 100644 api/src/main/java/net/kyori/adventure/util/internal/AdventureConfigImpl.java create mode 100644 api/src/main/java/net/kyori/adventure/util/internal/package-info.java diff --git a/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java index 235624d988..ee2e19b9a2 100644 --- a/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java @@ -29,6 +29,7 @@ import java.util.stream.Stream; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.util.Nag; +import net.kyori.adventure.util.internal.AdventureConfig; import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -37,7 +38,7 @@ import static java.util.Objects.requireNonNull; final class TextComponentImpl extends AbstractComponent implements TextComponent { - private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = Boolean.getBoolean(String.join(".", "net", "kyori", "adventure", "text", "warnWhenLegacyFormattingDetected")); + private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = AdventureConfig.getBoolean(AdventureConfig.OPTION_TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED, false); @VisibleForTesting static final char SECTION_CHAR = 'ยง'; diff --git a/api/src/main/java/net/kyori/adventure/translation/TranslationLocales.java b/api/src/main/java/net/kyori/adventure/translation/TranslationLocales.java index c074bbdfda..d9c37a1503 100644 --- a/api/src/main/java/net/kyori/adventure/translation/TranslationLocales.java +++ b/api/src/main/java/net/kyori/adventure/translation/TranslationLocales.java @@ -25,12 +25,13 @@ import java.util.Locale; import java.util.function.Supplier; +import net.kyori.adventure.util.internal.AdventureConfig; final class TranslationLocales { private static final Supplier GLOBAL; static { - final String property = System.getProperty("net.kyo".concat("ri.adventure.defaultTranslationLocale")); + final String property = AdventureConfig.getString(AdventureConfig.OPTION_DEFAULT_TRANSLATION_LOCALE); if (property == null || property.isEmpty()) { GLOBAL = () -> Locale.US; } else if (property.equals("system")) { diff --git a/api/src/main/java/net/kyori/adventure/util/Services.java b/api/src/main/java/net/kyori/adventure/util/Services.java index 491b5289a7..2669f8bbd2 100644 --- a/api/src/main/java/net/kyori/adventure/util/Services.java +++ b/api/src/main/java/net/kyori/adventure/util/Services.java @@ -26,6 +26,7 @@ import java.util.Iterator; import java.util.Optional; import java.util.ServiceLoader; +import net.kyori.adventure.util.internal.AdventureConfig; import org.jetbrains.annotations.NotNull; /** @@ -35,7 +36,7 @@ */ public final class Services { // net.kyori.adventure.serviceLoadFailuresAreFatal - private static final boolean SERVICE_LOAD_FAILURES_ARE_FATAL = Boolean.parseBoolean(System.getProperty(String.join(".", "net", "kyori", "adventure", "serviceLoadFailuresAreFatal"), String.valueOf(true))); + private static final boolean SERVICE_LOAD_FAILURES_ARE_FATAL = AdventureConfig.getBoolean(AdventureConfig.OPTION_SERVICE_LOAD_FAILURES_ARE_FATAL, true); private Services() { } diff --git a/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfig.java b/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfig.java new file mode 100644 index 0000000000..6e01587775 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfig.java @@ -0,0 +1,108 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2021 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.util.internal; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.UnknownNullability; + +/** + * Adventure configuration. + * + * @since 4.10.0 + */ +@ApiStatus.Internal +public final class AdventureConfig { + /** + * The directory in which our configuration file lives. + * + * @since 4.10.0 + */ + public static final String FILESYSTEM_DIRECTORY_NAME = "config"; + /** + * The configuration file name. + * + * @since 4.10.0 + */ + public static final String FILESYSTEM_FILE_NAME = "adventure.properties"; + + /** + * Option for specifying the default translation locale. + * + * @since 4.10.0 + */ + public static final String OPTION_DEFAULT_TRANSLATION_LOCALE = "defaultTranslationLocale"; + /** + * Option for specifying whether service load failures are fatal. + * + * @since 4.10.0 + */ + public static final String OPTION_SERVICE_LOAD_FAILURES_ARE_FATAL = "serviceLoadFailuresAreFatal"; + /** + * Option for specifying whether to warn when legacy formatting is detected. + * + * @since 4.10.0 + */ + public static final String OPTION_TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED = "text.warnWhenLegacyFormattingDetected"; + + private AdventureConfig() { + } + + /** + * Gets a boolean value. + * + * @param key the key + * @param defaultValue the default value + * @return the boolean value + * @since 4.10.0 + */ + public static boolean getBoolean(final @NotNull String key, final boolean defaultValue) { + return Boolean.parseBoolean(getString(key, Boolean.toString(defaultValue))); + } + + /** + * Gets a string value, or {@code null}. + * + * @param key the key + * @return the string value, or {@code null} + * @since 4.10.0 + */ + public static @Nullable String getString(final @NotNull String key) { + return getString(key, null); + } + + /** + * Gets a string value. + * + * @param key the key + * @param defaultValue the default value + * @return the string value + * @since 4.10.0 + */ + public static @Nullable String getString(final @NotNull String key, final @Nullable String defaultValue) { + final String property = String.join(".", "net", "kyori", "adventure", key); + return AdventureConfigImpl.PROPERTIES.getProperty(key, System.getProperty(property, defaultValue)); + } +} diff --git a/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfigImpl.java b/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfigImpl.java new file mode 100644 index 0000000000..3f9c6109a8 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/util/internal/AdventureConfigImpl.java @@ -0,0 +1,52 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2021 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.util.internal; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; + +final class AdventureConfigImpl { + static final Properties PROPERTIES = loadProperties(); + + private AdventureConfigImpl() { + } + + private static Properties loadProperties() { + final Properties properties = new Properties(); + final Path path = Paths.get(AdventureConfig.FILESYSTEM_DIRECTORY_NAME, AdventureConfig.FILESYSTEM_FILE_NAME); + if (Files.isRegularFile(path)) { + try (final InputStream is = Files.newInputStream(path)) { + properties.load(is); + } catch (final IOException e) { + // Well, that's awkward. + e.printStackTrace(); + } + } + return properties; + } +} diff --git a/api/src/main/java/net/kyori/adventure/util/internal/package-info.java b/api/src/main/java/net/kyori/adventure/util/internal/package-info.java new file mode 100644 index 0000000000..a82a4cc09e --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/util/internal/package-info.java @@ -0,0 +1,28 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2021 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +/** + * Internal utilities. + */ +@org.jetbrains.annotations.ApiStatus.Internal +package net.kyori.adventure.util.internal;