Skip to content

Commit

Permalink
And another way.
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Dec 22, 2021
1 parent 7fb132c commit c6648eb
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +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.adventure.util.internal.AdventureProperties;
import net.kyori.examination.ExaminableProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -38,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 = AdventureConfig.getBoolean(AdventureConfig.OPTION_TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED, false);
private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = AdventureProperties.booleanValueOf(AdventureProperties.TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED, false);
@VisibleForTesting
static final char SECTION_CHAR = '§';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

import java.util.Locale;
import java.util.function.Supplier;
import net.kyori.adventure.util.internal.AdventureConfig;
import net.kyori.adventure.util.internal.AdventureProperties;

final class TranslationLocales {
private static final Supplier<Locale> GLOBAL;

static {
final String property = AdventureConfig.getString(AdventureConfig.OPTION_DEFAULT_TRANSLATION_LOCALE);
final String property = AdventureProperties.valueOf(AdventureProperties.DEFAULT_TRANSLATION_LOCALE, null);
if (property == null || property.isEmpty()) {
GLOBAL = () -> Locale.US;
} else if (property.equals("system")) {
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/net/kyori/adventure/util/Services.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Iterator;
import java.util.Optional;
import java.util.ServiceLoader;
import net.kyori.adventure.util.internal.AdventureConfig;
import net.kyori.adventure.util.internal.AdventureProperties;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -36,7 +36,7 @@
*/
public final class Services {
// net.kyori.adventure.serviceLoadFailuresAreFatal
private static final boolean SERVICE_LOAD_FAILURES_ARE_FATAL = AdventureConfig.getBoolean(AdventureConfig.OPTION_SERVICE_LOAD_FAILURES_ARE_FATAL, true);
private static final boolean SERVICE_LOAD_FAILURES_ARE_FATAL = AdventureProperties.booleanValueOf(AdventureProperties.SERVICE_LOAD_FAILURES_ARE_FATAL, true);

private Services() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,89 +23,96 @@
*/
package net.kyori.adventure.util.internal;

import java.util.function.Function;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

/**
* Adventure configuration.
* Adventure properties.
*
* @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";

public final class AdventureProperties {
/**
* Option for specifying the default translation locale.
*
* @since 4.10.0
*/
public static final String OPTION_DEFAULT_TRANSLATION_LOCALE = "defaultTranslationLocale";
public static final Option<String> DEFAULT_TRANSLATION_LOCALE = option("defaultTranslationLocale", Function.identity());
/**
* Option for specifying whether service load failures are fatal.
*
* @since 4.10.0
*/
public static final String OPTION_SERVICE_LOAD_FAILURES_ARE_FATAL = "serviceLoadFailuresAreFatal";
public static final Option<Boolean> SERVICE_LOAD_FAILURES_ARE_FATAL = option("serviceLoadFailuresAreFatal", Boolean::parseBoolean);
/**
* 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";
public static final Option<Boolean> TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED = option("text.warnWhenLegacyFormattingDetected", Boolean::parseBoolean);

private AdventureConfig() {
private AdventureProperties() {
}

/**
* Creates a new option.
*
* @param name the option name
* @param parser the value parser
* @param <T> the value type
* @return the option
* @since 4.10.0
*/
public static <T> @NotNull Option<T> option(final @NotNull String name, final @NotNull Function<String, T> parser) {
return new AdventurePropertiesImpl.OptionImpl<>(name, parser);
}

/**
* Gets a boolean value.
*
* @param key the key
* @param option the option
* @param defaultValue the default value
* @return the boolean value
* @since 4.10.0
*/
@SuppressWarnings("checkstyle:MethodName")
public static boolean getBoolean(final @NotNull String key, final boolean defaultValue) {
return Boolean.parseBoolean(getString(key, Boolean.toString(defaultValue)));
public static boolean booleanValueOf(final @NotNull Option<Boolean> option, final boolean defaultValue) {
return Boolean.TRUE.equals(valueOf(option, defaultValue));
}

/**
* Gets a string value, or {@code null}.
* Gets a boolean value.
*
* @param key the key
* @return the string value, or {@code null}
* @param option the option
* @param defaultValue the default value
* @return the boolean value
* @since 4.10.0
*/
@SuppressWarnings("checkstyle:MethodName")
public static @Nullable String getString(final @NotNull String key) {
return getString(key, null);
public static <T> @Nullable T valueOf(final @NotNull Option<T> option, final @Nullable T defaultValue) {
final String key = option.name();
final String property = String.join(".", "net", "kyori", "adventure", key);
final String value = System.getProperty(property, AdventurePropertiesImpl.PROPERTIES.getProperty(key));
return value != null ? ((AdventurePropertiesImpl.OptionImpl<T>) option).parser.apply(value) : defaultValue;
}

/**
* Gets a string value.
* An option.
*
* @param key the key
* @param defaultValue the default value
* @return the string value
* @param <T> the value type
* @since 4.10.0
*/
@SuppressWarnings("checkstyle:MethodName")
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));
@ApiStatus.Internal
@ApiStatus.NonExtendable
@SuppressWarnings("unused")
public interface Option<T> {
/**
* Gets the name.
*
* @return the name
* @since 4.10.0
*/
@NotNull String name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.function.Function;
import org.jetbrains.annotations.NotNull;

final class AdventureConfigImpl {
final class AdventurePropertiesImpl {
private static final String FILESYSTEM_DIRECTORY_NAME = "config";
private static final String FILESYSTEM_FILE_NAME = "adventure.properties";
static final Properties PROPERTIES = loadProperties();

private AdventureConfigImpl() {
private AdventurePropertiesImpl() {
}

private static Properties loadProperties() {
final Properties properties = new Properties();
final Path path = Paths.get(AdventureConfig.FILESYSTEM_DIRECTORY_NAME, AdventureConfig.FILESYSTEM_FILE_NAME);
final Path path = Paths.get(FILESYSTEM_DIRECTORY_NAME, FILESYSTEM_FILE_NAME);
if (Files.isRegularFile(path)) {
try (final InputStream is = Files.newInputStream(path)) {
properties.load(is);
Expand All @@ -49,4 +53,29 @@ private static Properties loadProperties() {
}
return properties;
}

static final class OptionImpl<T> implements AdventureProperties.Option<T> {
private final String name;
final Function<String, T> parser;

OptionImpl(final @NotNull String name, final @NotNull Function<String, T> parser) {
this.name = name;
this.parser = parser;
}

@Override
public @NotNull String name() {
return this.name;
}

@Override
public boolean equals(final Object that) {
return this == that;
}

@Override
public int hashCode() {
return this.name.hashCode();
}
}
}

0 comments on commit c6648eb

Please sign in to comment.