-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
213 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
api/src/main/java/net/kyori/adventure/util/internal/AdventureProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* 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.util.function.Function; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Adventure properties. | ||
* | ||
* @since 4.10.0 | ||
*/ | ||
@ApiStatus.Internal | ||
public final class AdventureProperties { | ||
/** | ||
* Property for specifying the default translation locale. | ||
* | ||
* @since 4.10.0 | ||
*/ | ||
public static final Property<String> DEFAULT_TRANSLATION_LOCALE = option("defaultTranslationLocale", Function.identity(), null); | ||
/** | ||
* Property for specifying whether service load failures are fatal. | ||
* | ||
* @since 4.10.0 | ||
*/ | ||
public static final Property<Boolean> SERVICE_LOAD_FAILURES_ARE_FATAL = option("serviceLoadFailuresAreFatal", Boolean::parseBoolean, Boolean.TRUE); | ||
/** | ||
* Property for specifying whether to warn when legacy formatting is detected. | ||
* | ||
* @since 4.10.0 | ||
*/ | ||
public static final Property<Boolean> TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED = option("text.warnWhenLegacyFormattingDetected", Boolean::parseBoolean, Boolean.FALSE); | ||
|
||
private AdventureProperties() { | ||
} | ||
|
||
/** | ||
* Creates a new property. | ||
* | ||
* @param name the property name | ||
* @param parser the value parser | ||
* @param defaultValue the default value | ||
* @param <T> the value type | ||
* @return a property | ||
* @since 4.10.0 | ||
*/ | ||
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); | ||
} | ||
|
||
/** | ||
* A property. | ||
* | ||
* @param <T> the value type | ||
* @since 4.10.0 | ||
*/ | ||
@ApiStatus.Internal | ||
@ApiStatus.NonExtendable | ||
public interface Property<T> { | ||
/** | ||
* Gets the value. | ||
* | ||
* @return the value | ||
* @since 4.10.0 | ||
*/ | ||
@Nullable T value(); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
api/src/main/java/net/kyori/adventure/util/internal/AdventurePropertiesImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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; | ||
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"; | ||
private static 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); | ||
} catch (final IOException e) { | ||
// Well, that's awkward. | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private AdventurePropertiesImpl() { | ||
} | ||
|
||
static final class PropertyImpl<T> implements AdventureProperties.Property<T> { | ||
private final String name; | ||
private final Function<String, T> parser; | ||
private final @Nullable T defaultValue; | ||
|
||
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 @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 @Nullable Object that) { | ||
return this == that; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return this.name.hashCode(); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
api/src/main/java/net/kyori/adventure/util/internal/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |