Skip to content

Commit

Permalink
Another way, again.
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Dec 22, 2021
1 parent c6648eb commit 4c4a91c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 62 deletions.
Expand Up @@ -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 = AdventureProperties.booleanValueOf(AdventureProperties.TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED, false);
private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = Boolean.TRUE.equals(AdventureProperties.TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED.value());
@VisibleForTesting
static final char SECTION_CHAR = '§';

Expand Down
Expand Up @@ -26,12 +26,13 @@
import java.util.Locale;
import java.util.function.Supplier;
import net.kyori.adventure.util.internal.AdventureProperties;
import org.jetbrains.annotations.Nullable;

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

static {
final String property = AdventureProperties.valueOf(AdventureProperties.DEFAULT_TRANSLATION_LOCALE, null);
final @Nullable String property = AdventureProperties.DEFAULT_TRANSLATION_LOCALE.value();
if (property == null || property.isEmpty()) {
GLOBAL = () -> Locale.US;
} else if (property.equals("system")) {
Expand Down
3 changes: 1 addition & 2 deletions api/src/main/java/net/kyori/adventure/util/Services.java
Expand Up @@ -35,8 +35,7 @@
* @since 4.8.0
*/
public final class Services {
// net.kyori.adventure.serviceLoadFailuresAreFatal
private static final boolean SERVICE_LOAD_FAILURES_ARE_FATAL = AdventureProperties.booleanValueOf(AdventureProperties.SERVICE_LOAD_FAILURES_ARE_FATAL, true);
private static final boolean SERVICE_LOAD_FAILURES_ARE_FATAL = Boolean.TRUE.equals(AdventureProperties.SERVICE_LOAD_FAILURES_ARE_FATAL.value());

private Services() {
}
Expand Down
Expand Up @@ -36,83 +36,56 @@
@ApiStatus.Internal
public final class AdventureProperties {
/**
* Option for specifying the default translation locale.
* Property for specifying the default translation locale.
*
* @since 4.10.0
*/
public static final Option<String> DEFAULT_TRANSLATION_LOCALE = option("defaultTranslationLocale", Function.identity());
public static final Property<String> DEFAULT_TRANSLATION_LOCALE = option("defaultTranslationLocale", Function.identity(), null);
/**
* Option for specifying whether service load failures are fatal.
* Property for specifying whether service load failures are fatal.
*
* @since 4.10.0
*/
public static final Option<Boolean> SERVICE_LOAD_FAILURES_ARE_FATAL = option("serviceLoadFailuresAreFatal", Boolean::parseBoolean);
public static final Property<Boolean> SERVICE_LOAD_FAILURES_ARE_FATAL = option("serviceLoadFailuresAreFatal", Boolean::parseBoolean, Boolean.TRUE);
/**
* Option for specifying whether to warn when legacy formatting is detected.
* Property for specifying whether to warn when legacy formatting is detected.
*
* @since 4.10.0
*/
public static final Option<Boolean> TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED = option("text.warnWhenLegacyFormattingDetected", Boolean::parseBoolean);
public static final Property<Boolean> TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED = option("text.warnWhenLegacyFormattingDetected", Boolean::parseBoolean, Boolean.FALSE);

private AdventureProperties() {
}

/**
* Creates a new option.
* Creates a new property.
*
* @param name the option name
* @param name the property 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 option the option
* @param defaultValue the default value
* @return the boolean value
* @since 4.10.0
*/
public static boolean booleanValueOf(final @NotNull Option<Boolean> option, final boolean defaultValue) {
return Boolean.TRUE.equals(valueOf(option, defaultValue));
}

/**
* Gets a boolean value.
*
* @param option the option
* @param defaultValue the default value
* @return the boolean value
* @param <T> the value type
* @return a property
* @since 4.10.0
*/
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;
public static <T> @NotNull Property<T> option(final @NotNull String name, final @NotNull Function<String, T> parser, final @Nullable T defaultValue) {
return new AdventurePropertiesImpl.PropertyImpl<>(name, parser, defaultValue);
}

/**
* An option.
* A property.
*
* @param <T> the value type
* @since 4.10.0
*/
@ApiStatus.Internal
@ApiStatus.NonExtendable
@SuppressWarnings("unused")
public interface Option<T> {
public interface Property<T> {
/**
* Gets the name.
* Gets the value.
*
* @return the name
* @return the value
* @since 4.10.0
*/
@NotNull String name();
@Nullable T value();
}
}
Expand Up @@ -31,45 +31,51 @@
import java.util.Properties;
import java.util.function.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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 static final Properties PROPERTIES = new Properties();

private AdventurePropertiesImpl() {
}

private static Properties loadProperties() {
final Properties properties = new Properties();
static {
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);
PROPERTIES.load(is);
} catch (final IOException e) {
// Well, that's awkward.
e.printStackTrace();
}
}
return properties;
}

static final class OptionImpl<T> implements AdventureProperties.Option<T> {
private AdventurePropertiesImpl() {
}

static final class PropertyImpl<T> implements AdventureProperties.Property<T> {
private final String name;
final Function<String, T> parser;
private final Function<String, T> parser;
private final @Nullable T defaultValue;

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

@Override
public @NotNull String name() {
return this.name;
public @Nullable T value() {
final String property = String.join(".", "net", "kyori", "adventure", this.name);
final String value = System.getProperty(property, PROPERTIES.getProperty(this.name));
if (value != null) {
return this.parser.apply(value);
}
return this.defaultValue;
}

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

Expand Down

0 comments on commit 4c4a91c

Please sign in to comment.