Rewriting ResourceLocation and rearranging some methods

This commit is contained in:
MrGeoTech 2023-01-03 15:39:39 -06:00
parent 4fee62885f
commit dd9e06ea69
No known key found for this signature in database
GPG Key ID: 015B8BF2BAD3DEEA

View File

@ -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 <a href="https://minecraft.fandom.com/wiki/Resource_location">Resource location</a>
*/
open class ResourceLocation private constructor (
val namespace: String = ProtocolDefinition.DEFAULT_NAMESPACE,
val path: String,
val path: String
) : Comparable<ResourceLocation>, 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()
}
}