From dd9e06ea693a31f75685348f7db8333d1d2b893d Mon Sep 17 00:00:00 2001 From: MrGeoTech Date: Tue, 3 Jan 2023 15:39:39 -0600 Subject: [PATCH] Rewriting ResourceLocation and rearranging some methods --- .../data/registries/ResourceLocation.kt | 75 ++++++++++++------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/registries/ResourceLocation.kt b/src/main/java/de/bixilon/minosoft/data/registries/ResourceLocation.kt index 17da03899..386608729 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/ResourceLocation.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/ResourceLocation.kt @@ -16,23 +16,62 @@ import de.bixilon.minosoft.data.language.translate.Translatable import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition import java.util.* -open class ResourceLocation( +/** + * A resource location is a string that identifies a resource. It is composed of a namespace and a path, separated by a colon (:). + * The namespace is optional and defaults to "minecraft". The path is the path to the resource, separated by forward slashes (/). + * + * @param namespace The namespace of the resource location + * @param path The path of the resource location + * + * @throws IllegalArgumentException If the namespace or path does not match the allowed pattern. See [ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN] and [ProtocolDefinition.ALLOWED_PATH_PATTERN] + * + * @see Resource location + */ +open class ResourceLocation private constructor ( val namespace: String = ProtocolDefinition.DEFAULT_NAMESPACE, - val path: String, + val path: String ) : Comparable, Translatable { // compare is for moshi private val hashCode = Objects.hash(namespace, path) - open val full: String = "$namespace:$path" override val translationKey: ResourceLocation get() = this - constructor(full: String) : this(full.namespace, full.path) + init { + if (!ProtocolDefinition.ALLOWED_NAMESPACE_PATTERN.matches(namespace)) + throw IllegalArgumentException("Namespace '$namespace' is not allowed!") + if (!ProtocolDefinition.ALLOWED_PATH_PATTERN.matches(path)) + throw IllegalArgumentException("Path '$path' is not allowed!") + } + /** + * Adds a prefix to the path of this resource location. + * + * @param prefix The prefix to add + */ + fun prefix(prefix: String): ResourceLocation { + return ResourceLocation(namespace, prefix + path) + } + + /** + * @return If the namespace is "minecraft", the path is returned. Otherwise, the full string is returned. + */ + fun toMinifiedString(): String { + return if (namespace == ProtocolDefinition.DEFAULT_NAMESPACE) path + else toString() + } + + override fun toString(): String { + return "$namespace:$path" + } override fun hashCode(): Int { return hashCode } + override fun compareTo(other: ResourceLocation): Int { + return hashCode() - other.hashCode() + } + override fun equals(other: Any?): Boolean { if (other === this) { return true @@ -46,17 +85,6 @@ open class ResourceLocation( return path == other.path && namespace == other.namespace } - override fun toString(): String { - return full - } - - fun toMinifiedString(): String { - if (namespace == ProtocolDefinition.DEFAULT_NAMESPACE) { - return path - } - return toString() - } - companion object { val String.namespace: String get() { @@ -77,23 +105,12 @@ open class ResourceLocation( } fun getResourceLocation(resourceLocation: String): ResourceLocation { - return ResourceLocation(resourceLocation) + return ResourceLocation(resourceLocation.namespace, resourceLocation.path) } - fun getPathResourceLocation(resourceLocation: String): ResourceLocation { - if (resourceLocation.contains(":")) { - return ResourceLocation(resourceLocation) - } - val split = resourceLocation.split("/".toRegex(), 2).toTypedArray() - return ResourceLocation(split[0], split[1]) + fun getResourceLocation(namespace: String, path: String): ResourceLocation { + return ResourceLocation(namespace, path) } } - fun prefix(prefix: String): ResourceLocation { - return ResourceLocation(namespace, prefix + path) - } - - override fun compareTo(other: ResourceLocation): Int { - return hashCode() - other.hashCode() - } }