Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
api(libs.examination.api)
api(libs.examination.string)
compileOnlyApi(libs.jetbrainsAnnotations)
implementation(projects.adventureProperties)
testImplementation(libs.guava)
annotationProcessor(projects.adventureAnnotationProcessors)
testCompileOnly(libs.autoService.annotations)
Expand Down
1 change: 1 addition & 0 deletions bom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
"extra-kotlin",
"key",
"nbt",
"properties",
"serializer-configurate4",
"text-logger-slf4j",
"text-minimessage",
Expand Down
1 change: 1 addition & 0 deletions key/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
api(libs.examination.api)
api(libs.examination.string)
compileOnlyApi(libs.jetbrainsAnnotations)
implementation(projects.adventureProperties)
testImplementation(libs.guava)
}

Expand Down
22 changes: 20 additions & 2 deletions key/src/main/java/net/kyori/adventure/key/KeyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import java.util.Objects;
import java.util.OptionalInt;
import java.util.stream.Stream;
import net.kyori.adventure.internal.properties.AdventureProperties;
import net.kyori.examination.ExaminableProperty;
import org.intellij.lang.annotations.RegExp;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.util.Objects.requireNonNull;

Expand All @@ -41,14 +43,16 @@ final class KeyImpl implements Key {
static final @RegExp String NAMESPACE_PATTERN = "[a-z0-9_\\-.]+";
static final @RegExp String VALUE_PATTERN = "[a-z0-9_\\-./]+";

static final AdventureProperties.@Nullable KeyInternStrategy INTERN_STRATEGY = AdventureProperties.KEY_INTERN_STRATEGY.value();

private final String namespace;
private final String value;

KeyImpl(final @NotNull String namespace, final @NotNull String value) {
checkError("namespace", namespace, namespace, value, Key.checkNamespace(namespace), NAMESPACE_PATTERN);
checkError("value", value, namespace, value, Key.checkValue(value), VALUE_PATTERN);
this.namespace = requireNonNull(namespace, "namespace");
this.value = requireNonNull(value, "value");
this.namespace = finalizeNamespace(requireNonNull(namespace, "namespace"));
this.value = finalizeValue(requireNonNull(value, "value"));
}

private static void checkError(final String name, final String checkPart, final String namespace, final String value, final OptionalInt index, final String pattern) {
Expand All @@ -66,6 +70,20 @@ private static void checkError(final String name, final String checkPart, final
}
}

private static String finalizeNamespace(final @NotNull String namespace) {
if (AdventureProperties.KeyInternStrategy.ALL.equals(INTERN_STRATEGY) || AdventureProperties.KeyInternStrategy.NAMESPACE.equals(INTERN_STRATEGY)) {
return namespace.intern();
}
return namespace;
}

private static String finalizeValue(final @NotNull String value) {
if (AdventureProperties.KeyInternStrategy.ALL.equals(INTERN_STRATEGY)) {
return value.intern();
}
return value;
}

static boolean allowedInNamespace(final char character) {
return character == '_' || character == '-' || (character >= 'a' && character <= 'z') || (character >= '0' && character <= '9') || character == '.';
}
Expand Down
10 changes: 10 additions & 0 deletions properties/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
plugins {
id("adventure.common-conventions")
}

dependencies {
api(libs.examination.api)
api(libs.examination.string)
}

applyJarMetadata("net.kyori.adventure.internal.properties")
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ public final class AdventureProperties {
*/
public static final Property<Boolean> TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED = property("text.warnWhenLegacyFormattingDetected", Boolean::parseBoolean, Boolean.FALSE);

/**
* Property for specifying what strategy to use for interning strings in keys.
*
* @since 4.22.0
*/
public static final Property<KeyInternStrategy> KEY_INTERN_STRATEGY = property("key.internStrategy", KeyInternStrategy::valueOf, KeyInternStrategy.NAMESPACE);

private AdventureProperties() {
}

Expand Down Expand Up @@ -94,4 +101,16 @@ public interface Property<T> {
*/
@Nullable T value();
}

/**
* The strategy to use for interning strings in keys.
*
* @since 4.22.0
*/
@ApiStatus.Internal
public enum KeyInternStrategy {
NONE,
NAMESPACE,
ALL
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ sequenceOf(
"extra-kotlin",
"key",
"nbt",
"properties",
"serializer-configurate4",
"text-logger-slf4j",
"text-minimessage",
Expand Down